-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleCommit.py
164 lines (142 loc) · 5.75 KB
/
handleCommit.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import subprocess
import sys
from typing import Dict, Tuple
COLORS = {
"red": "\033[91m",
"green": "\033[92m",
"yellow": "\033[93m",
"blue": "\033[94m",
"reset": "\033[0m",
}
COMMIT_TYPES: Dict[int, Tuple[str, str]] = {
1: ('feat', '✨ New feature - A new feature was added to the application'),
2: ('fix', '🐛 Bug fix - A bug was fixed in the codebase'),
3: ('docs', '📚 Documentation - Documentation was added or updated'),
4: ('test', '🧪 Tests - Tests were added or modified'),
5: ('build', '➕ Build system - Changes to build process or dependencies'),
6: ('perf', '⚡ Performance - Performance optimization was implemented'),
7: ('style', '🎨 Code style - Code formatting or style improvements (no logic changes)'),
8: ('refactor', '♻️ Refactoring - Code restructuring without changing functionality'),
9: ('chore', '🔧 Chores - Routine tasks/maintenance (non-functional changes)'),
10: ('ci', '🧱 CI/CD - Changes to CI/CD configuration or scripts'),
11: ('revert', '⏪ Revert - Reverting previous changes'),
12: ('security', '🔒 Security - Security-related improvements or fixes'),
13: ('wip', '🚧 WIP - Work in progress (temporary commit)'),
14: ('raw', '🗃️ Raw data - Updates to raw datasets or data files'),
15: ('cleanup', '🧹 Cleanup - Code cleanup or dead code removal'),
16: ('remove', '🗑️ Removal - Files or code were removed'),
17: ('locale', '🌐 Localization - Translation or localization updates'),
18: ('access', '♿ Accessibility - Accessibility improvements'),
19: ('ux', '💄 UI/UX - User interface/user experience changes'),
20: ('break', '💥 Breaking change - Changes that break backward compatibility'),
}
def print_color(text: str, color: str = "reset") -> None:
print(f"{COLORS.get(color, '')}{text}{COLORS['reset']}")
def get_valid_input(prompt: str, validation_func, optional: bool = False) -> str:
while True:
try:
value = input(prompt).strip()
if optional and not value:
return ""
if validation_func(value):
return value
print_color("Invalid input, please try again.", "yellow")
except KeyboardInterrupt:
print_color("\nOperation cancelled by user.", "yellow")
sys.exit(1)
def is_git_repository() -> bool:
try:
subprocess.run(
["git", "rev-parse", "--is-inside-work-tree"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
return True
except subprocess.CalledProcessError:
return False
def has_staged_changes() -> bool:
try:
subprocess.run(
["git", "diff", "--cached", "--quiet"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
return False
except subprocess.CalledProcessError as e:
if e.returncode == 1:
return True
raise
def format_commit_message(type_str: str, emoji: str, title: str, description: str) -> str:
commit_message = f"{type_str}{emoji} {title}"
if description:
formatted_description = description.replace('/br', '\n')
commit_message += f"\n\n{formatted_description}"
return commit_message
def main():
if not is_git_repository():
print_color("Error: Not a git repository.", "red")
sys.exit(1)
if len(sys.argv) < 2:
print_color("Error: No files specified. Usage: cf <file1> <file2> ...", "red")
sys.exit(1)
files_to_add = sys.argv[1:]
try:
print_color(f"Staging files: {', '.join(files_to_add)}", "blue")
subprocess.run(
["git", "add", *files_to_add],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
text=True
)
except subprocess.CalledProcessError as e:
print_color(f"Error staging files: {e.stderr}", "red")
sys.exit(1)
if not has_staged_changes():
print_color("Error: No changes staged after adding files. Check if files have modifications.", "red")
sys.exit(1)
print("\nSelect the commit type:")
for num, (_, desc) in COMMIT_TYPES.items():
print(f"{num}. {desc}")
commit_type = int(get_valid_input(
"\nEnter the number corresponding to the commit type: ",
lambda x: x.isdigit() and int(x) in COMMIT_TYPES
))
type_str, emoji_desc = COMMIT_TYPES[commit_type]
emoji = emoji_desc.split()[0]
title = get_valid_input(
"Enter the commit title (required, max 72 chars): ",
lambda x: 0 < len(x) <= 72
)
description = get_valid_input(
"Enter the commit description (optional, use '/br' for new lines, press 'Enter' to skip): ",
lambda x: True,
optional=True
)
full_message = format_commit_message(f"[{type_str}]", f" {emoji}", title, description)
print_color("\nCommit message preview:", "blue")
print(full_message)
confirm = input("\nConfirm commit? [Y/n] ").strip().lower()
if confirm not in ('', 'y', 'yes'):
print_color("Commit cancelled.", "yellow")
sys.exit(0)
try:
result = subprocess.run(
["git", "commit", "-m", full_message],
check=True,
text=True,
capture_output=True
)
print_color("\n✓ Commit created successfully!", "green")
print(result.stdout)
except subprocess.CalledProcessError as e:
print_color("\n✗ Error creating commit:", "red")
print_color(e.stderr, "red")
sys.exit(1)
except KeyboardInterrupt:
print_color("\nOperation cancelled by user.", "yellow")
sys.exit(1)
if __name__ == "__main__":
main()