forked from commitizen-tools/commitizen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.py
More file actions
85 lines (67 loc) · 2.4 KB
/
commit.py
File metadata and controls
85 lines (67 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import contextlib
import os
import tempfile
import questionary
from commitizen import factory, git, out
from commitizen.cz.exceptions import CzException
NO_ANSWERS = 5
COMMIT_ERROR = 6
NO_COMMIT_BACKUP = 7
NOTHING_TO_COMMIT = 8
CUSTOM_ERROR = 9
class Commit:
"""Show prompt for the user to create a guided commit."""
def __init__(self, config: dict, arguments: dict):
self.config: dict = config
self.cz = factory.commiter_factory(self.config)
self.arguments = arguments
self.temp_file: str = os.path.join(tempfile.gettempdir(), "cz.commit.backup")
def read_backup_message(self) -> str:
# Check the commit backup file exists
if not os.path.isfile(self.temp_file):
out.error("No commit backup found")
raise SystemExit(NO_COMMIT_BACKUP)
# Read commit message from backup
with open(self.temp_file, "r") as f:
return f.read().strip()
def prompt_commit_questions(self) -> str:
# Prompt user for the commit message
cz = self.cz
questions = cz.questions()
try:
answers = questionary.prompt(questions, style=cz.style)
except ValueError as err:
root_err = err.__context__
if isinstance(root_err, CzException):
out.error(root_err.__str__())
raise SystemExit(CUSTOM_ERROR)
raise err
if not answers:
raise SystemExit(NO_ANSWERS)
return cz.message(answers)
def __call__(self):
if git.is_staging_clean():
out.write("No files added to staging!")
raise SystemExit(NOTHING_TO_COMMIT)
retry: bool = self.arguments.get("retry")
if retry:
m = self.read_backup_message()
else:
m = self.prompt_commit_questions()
out.info(f"\n{m}\n")
c = git.commit(m)
if c.err:
out.error(c.err)
# Create commit backup
with open(self.temp_file, "w") as f:
f.write(m)
raise SystemExit(COMMIT_ERROR)
if "nothing added" in c.out or "no changes added to commit" in c.out:
out.error(c.out)
elif c.err:
out.error(c.err)
else:
with contextlib.suppress(FileNotFoundError):
os.remove(self.temp_file)
out.write(c.out)
out.success("Commit successful!")