-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.py
More file actions
294 lines (237 loc) · 10.9 KB
/
commit.py
File metadata and controls
294 lines (237 loc) · 10.9 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python3
# become a better commiter
# author doneskandari@gmail.com
import os
import sys
import subprocess
import git
from termcolor import colored
from art import text2art
def git_add():
try:
repo = git.Repo('.') # Replace '.' with the path to your repository
# Get untracked files
untracked_files = repo.untracked_files
# Get not staged files
not_staged_files = [diff.a_path for diff in repo.index.diff(None)]
# Merge untracked and not staged files
changed_files = []
changed_files.extend(untracked_files)
changed_files.extend(not_staged_files)
# Get the current Git branch
branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
# Print the branch name
print("\tCurrent Branch\t\t\t\t", colored(branch, 'white'))
if len(changed_files) == 0:
print("\n\tNo Changes.")
exit()
# Print the list of changed files with colors
print(f"\n\tChanges (Untracked, Not Staged)")
for i, file in enumerate(changed_files):
colored_num = colored(i+1, "green")
colored_filename = colored(file, "green")
print(f"\t\t\t\t\t\t {colored_num}. {colored_filename}")
print("\n\t----------------------------------------------------")
# Prompt the user to select files to add to the staging area
while True:
selection = input("\tEnter Number(s) (e.g. 1,2) Or 'all' Or 'exit': ")
# Handle special commands
if selection == "exit":
exit()
elif selection == "all":
selected_files = set(range(1, len(changed_files) + 1))
break
# Parse the user's selection
selected_files = set()
for sel in selection.split(","):
try:
file_number = int(sel)
if file_number < 1 or file_number > len(changed_files):
raise ValueError
selected_files.add(file_number)
except ValueError:
selected_files = set()
break
# Add the selected files to the staging area
if selected_files:
for i in selected_files:
filename = changed_files[i-1]
subprocess.run(["git", "add", f"{filename}"])
print("\n\t----------------------------------------------------")
print("\n\tEntered Stage Area",colored(f"\t\t\t {filename}", "green"))
break
else:
print(colored("\tInvalid Selection. Please Try Again.", "red"))
except Exception as e:
print("An Error Occurred:", e)
def git_commit():
try:
# Open the Git repository in the current working directory
repo = git.Repo(".")
# Get a list of files in the staging area
staged_files = [item.a_path for item in repo.index.diff('HEAD')]
# Get the current Git branch
branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
# Print the branch name
print("\tCurrent Branch\t\t\t\t", colored(branch, 'white'))
if len(staged_files) == 0:
print("\n\tNo Changes.")
exit()
else:
print(f"\n\tStaged Changes")
for i ,file in enumerate(staged_files):
print(colored(f"\t\t\t\t\t\t {i+1}. {file}", 'green'))
header_message = ""
# Define valid tags and their explanations
valid_tags = {
"1": {"tag": "feature", "explanation": "a new feature"},
"2": {"tag": "fix", "explanation": "bug fix"},
"3": {"tag": "refactor", "explanation": "code restructuring"},
"4": {"tag": "test", "explanation": "test-related changes"},
"5": {"tag": "doc", "explanation": "documentation updates"},
"6": {"tag": "style", "explanation": "code formatting"},
"7": {"tag": "perf", "explanation": "improve performance"},
"8": {"tag": "config", "explanation": "change the configuration file"},
"9": {"tag": "security", "explanation": "improve securiy"},
"10": {"tag": "revert", "explanation": "undo or revert previous changes"}
}
print(f"\n\tValid Tags")
for key, value in valid_tags.items():
print(colored(f"\t\t\t\t\t\t {key}. {value['tag']}: {value['explanation']}", "green"))
print("\n\t----------------------------------------------------")
# Prompt user for input and validate against valid tags
while True:
commit_tag_num = input("\tEnter Commit Tag: ")
if commit_tag_num in valid_tags:
commit_tag = valid_tags[commit_tag_num]["tag"]
break
else:
print("\tInvalid Tag. Please Try Again.")
header_message += commit_tag
header_message += ": "
# Prompt user for commit message header
while True:
commit_message = input("\tEnter Commit Message: ")
if commit_message:
break
else:
print(colored("\tCommit Message Cannot Be Empty. Please Try Again.", 'red'))
header_message += commit_message
# Prompt user for commit smessage body
commit_body = input("\tEnter Commit Body: ")
# Prompt user for commit metadata
while True:
commit_metadata = input("\tEnter Commit Metadata: ")
if commit_metadata:
break
else:
print(colored("\tCommit Metadata Cannot Be Empty. Please Try Again.", 'red'))
print("\n\tyour commit message looks like:\n")
if commit_body != "":
print(colored(f"\t{header_message}\n\n\t{commit_body}\n\n\t{commit_metadata}", 'white'))
else:
print(colored(f"\t{header_message}\n\n\t{commit_metadata}", 'white'))
while True:
confirm = input("\n\tConfirm? Y/N: ")
if confirm.lower() == "y":
print("\n\t----------------------------------------------------")
# Commit changes with tag and message
if commit_body != "":
subprocess.run(["git", "commit", "-m", f"{header_message}\n\n", "-m", f"{commit_body}\n\n", "-m", f"{commit_metadata}\n\n"])
print("Changes Has Been Committed.")
else:
subprocess.run(["git", "commit", "-m", f"{header_message}\n\n", "-m", f"{commit_metadata}\n\n"])
print("Changes Has Been Committed.")
break
elif confirm.lower() == "n":
print(colored("\tRun The Commit Action Again.", 'red'))
exit()
else:
print(colored("\tInvalid Input. Please Try Again.", 'red'))
except Exception as e:
print("An error occurred:", e)
def git_push():
repo = git.Repo('.')
# Get the current Git branch
branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
# Print the branch name
print("\tCurrent Branch\t\t\t\t", colored(branch, 'white'))
os.system(f"git push")
# Run the `git log` command with the `-n 1` option to get the latest commit
output = subprocess.check_output(['git', 'log', '-n', '1'])
print("\n----------------------------------------------------")
# Print the output to the console
print(f"{output.decode()}")
def git_branch():
repo = git.Repo('.')
# Get the current Git branch
branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
# Print the branch name
print("\tCurrent Branch\t\t\t\t", colored(branch, 'white'))
print("\n\tNote: Use Descriptive, Consistent, Short Name.")
print("\n\tNote: Use Only Alphanumeric Characters && Dashes (-)")
header_message = ""
# Define valid tags and their explanations
valid_tags = {
"1": {"tag": "feature", "explanation": "a new feature"},
"2": {"tag": "fix", "explanation": "bug fix"},
"3": {"tag": "refactor", "explanation": "code restructuring"},
"4": {"tag": "test", "explanation": "test-related changes"},
"5": {"tag": "doc", "explanation": "documentation updates"},
"6": {"tag": "style", "explanation": "code formatting"},
"7": {"tag": "perf", "explanation": "improve performance"},
"8": {"tag": "config", "explanation": "change the configuration file"},
"9": {"tag": "security", "explanation": "improve securiy"},
"10": {"tag": "revert", "explanation": "undo or revert previous changes"},
"11": {"tag": "service", "explanation": "a new service"}
}
print(f"\n\tValid Tags")
for key, value in valid_tags.items():
print(colored(f"\t\t\t\t\t\t {key}. {value['tag']}: {value['explanation']}", "green"))
print("\n\t----------------------------------------------------")
# Prompt user for input and validate against valid tags
while True:
branch_tag_num = input("\tEnter Branch Tag: ")
if branch_tag_num in valid_tags:
branch_tag = valid_tags[branch_tag_num]["tag"]
break
else:
print("\tInvalid Tag. Please Try Again.")
# get the new branch name from user input
branch_name = input("\tEnter The Name For The New Branch: ")
branch_name = f"krz/{branch_tag}/{branch_name}"
repo.git.checkout('-b', branch_name)
print(f"\n\tBranch Created\t\t\t\t", colored(f"{branch_name}", 'green'))
print(f"\n\tCheckout To\t\t\t\t", colored(f"{branch_name}", 'green'))
def generate_logo(action):
logo = text2art(action.upper(), font='rd')
# Split the string into a list of lines
logo_lines = logo.splitlines()
# Add a tab character to the beginning of each line
logo_lines_with_tabs = [f"\t{logo_line}" for logo_line in logo_lines]
# Join the modified lines back into a single string
final_logo = "\n".join(logo_lines_with_tabs)
print()
print(f"{final_logo}")
print()
# Get the action to take from the command line argument
if len(sys.argv) > 1:
action = sys.argv[1]
else:
print("\tPlease Specify Action (e.g. 'add', 'commit', 'push').")
exit()
if action == "add":
generate_logo(action)
git_add()
elif action == "commit":
generate_logo(action)
git_commit()
elif action == "push":
generate_logo(action)
git_push()
elif action == "branch":
generate_logo(action)
git_branch()
else:
print("\tAction Not Supported.")
exit()