-
Notifications
You must be signed in to change notification settings - Fork 1
/
gc
executable file
·433 lines (399 loc) · 18.3 KB
/
gc
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env python3
# Zac the Wise on 24 May
# Little script that adds emojis and labels for git commits
# More info: https://github.com/TechWiz-3/git-commit-emojis
from sys import argv # import cli arguement function
from sys import exit # import exit function
from os import system
BOLD = "\u001b[1m"
RESET = "\033[0m"
def get_opts():
"""Get cli options/arguements"""
for arg in argv[1:]: # gets arguements except the first one and assigns them
if arg == "-m": # message option
commit_message = ""
# get the argument for the option
get_arg_index = argv.index('-m') # find the index number for the option
get_arg_index=get_arg_index+1
try:
args_after_m = argv[get_arg_index:]
for msg_word in args_after_m:
commit_message += f"{msg_word} "
commit_message = commit_message[:-1] # remove extra space from end
except IndexError:
return "error", "You didn't include the arguement for the commit message like so: -m \"my commit message\""
else:
if commit_message == "": # if the commit message is empty
return "error", "Commit message cannot be empty"
return "message only", commit_message
elif arg == "-sh":
get_arg_index = argv.index('-sh') # find the index number for the option
try:
shortcut = argv[get_arg_index+1] # get the arguement after the option and assign it
except IndexError:
from help_cmds import Help
Help(cmd="shortcuts") # displays help message from help_cmds.py
else:
if shortcut == "ty" or shortcut == "typo":
commit_message = "✏️ FIX TYPO"
return "shortcut", commit_message
elif shortcut == "cl" or shortcut == "clean":
commit_message = "🧹 CLEAN UP"
return "shortcut", commit_message
elif shortcut == "in" or shortcut == "init" or shortcut == "initial":
commit_message = "🎉 INITIAL COMMIT"
return "shortcut", commit_message
elif shortcut in ("ln", "lint", "linter"):
commit_message = "🚨 FIX LINT WARNINGS"
return "shortcut", commit_message
else:
return "error", "Unrecognized shortcut usage. Use commit -sh for a list of available shortcuts"
elif arg in ("-s", "--strict"): # strict select menu option
commit_message = "" # label + user input message
message = "" # raw user input message
get_arg_index = argv.index(arg)
try:
args_after_s = argv[get_arg_index+1:]
if args_after_s[0] == "-m":
try:
message = args_after_s[1]
except IndexError:
return "error", "You didn't include the arguement for the commit message like so: -m \"my commit message\""
else:
if message == "":
return "error", "Commit message cannot be empty"
else:
message = args_after_s[1:]
for word in message:
commit_message += f"{word} "
return "strict", commit_message
elif args_after_s[0] == "":
return "error", "After the -s or --strict, add the commit message directly or with the -m switch"
else:
message = args_after_s[0:]
for word in message:
commit_message += f"{word} "
return "strict", commit_message
except IndexError:
return "error", "You need to include your commit message after the -s or --strict option, you can do this directly after the option or use the -m <commit message> option"
elif arg in ("-e", "--extra", "--extra-menu"):
commit_message = ""
message = ""
get_arg_index = argv.index(arg)
try:
args_after_e = argv[get_arg_index+1:]
if args_after_e[0] == "-m":
try:
message = args_after_e[1]
except IndexError:
return "error", "You didn't include the arguement for the commit message like so: -m \"my commit message\""
else:
if not message: # message is empty
return "error", "Commit message cannot be empty"
else:
message = args_after_e[1:]
for word in message:
commit_message += f"{word} "
commit_message = commit_message[:-1]
return "extra", commit_message
elif not args_after_e: # if empty
return "error", "After the -s or --strict, add the commit message directly or with the -m switch"
else:
message = args_after_e
for word in message:
commit_message += f"{word} "
return "extra", commit_message
except IndexError:
return "error", "You need to include your commit message after the -s or --strict option, you can do this directly after the option or use the -m <commit message> option"
elif arg in ("-sa", "--show-all", "--showall"):
show_all()
return exit()
elif arg == "-h" or arg == "--help": # help option
from help_cmds import Help
Help(cmd="regular") # displays help message from help_cmds.py
return exit() # stop the rest of the program from running
def get_special_opts():
"""Get special options which come after the commit message"""
label_controller = None
# special options are:
# --push, --add. --label --add-push
args = argv[1:]
good_lbl_cntrllrs = ["--improve", "--new", "--doc", "--fix", "--release"]
num_lbl_cntrllrs = ["-1", "-2", "-3", "-4", "-5"]
pre_commit_commands = []
post_commit_commands = []
recognised_commands = ["--add", "--push"]
for arg in args:
if arg in ("-m", "-e", "--extra", "-s", "--strict"): # args that have commit messages after them
break
if arg in ("-l", "--label", "--get-label"):
# break since push and add are not available with this option
pre_commit_commands.append("--label")
break
if arg in ("-ap", "--add-push"):
# for convenience, this command will be passed
# in with the pre and post commit commands
pre_commit_commands.append("--add-push")
post_commit_commands.append("--add-push")
if "--push" == arg:
post_commit_commands.append("--push")
if "--add" == arg:
pre_commit_commands.append("--add")
if arg in good_lbl_cntrllrs:
from label_controllers import Label
label_controller = Label.get_msg(arg)
if arg in num_lbl_cntrllrs:
from label_controllers import Label
label_controller = Label.get_msg_from_num(arg)
return pre_commit_commands, post_commit_commands, label_controller
def commands_execution(label, message, special_opts: tuple = None, shortcut: bool = False):
precommit, postcommit, label_controller = special_opts # returns a list for each, if no options then they're empty
if precommit: # if precommit isn't empty
if "--label" in precommit:
# copy to clipboard
from utils import Clipboard
c = Clipboard()
c.copy(f"{label}{message}")
# return with the commit label and exit
print("")
print(f"{label}{message}")
print("")
print("Exiting, have a happy commit...")
print("")
exit()
if "--add-push" in precommit:
try:
get_arg_index = argv.index("--add-push")
except ValueError:
get_arg_index = argv.index("-ap")
try:
args_after_add = argv[get_arg_index+1:]
except IndexError:
print("Please add the files you would like to add after the --add option. For more info see the --help special command")
exit()
else:
from utils import CommitUtils
CommitUtils.add_files(args_after_add)
if "--add" in precommit:
get_arg_index = argv.index("--add")
try:
args_after_add = argv[get_arg_index+1:]
except IndexError:
print("Please add the files you would like to add after the --add option. For more info see the --help special command")
exit()
else:
from utils import CommitUtils
CommitUtils.add_files(args_after_add)
if shortcut:
# do shortcut stuff
system(f"git commit -m \"{message}\"")
else:
# do message stuff
system(f"git commit -m \"{label}{message}\"")
# check that the command was excuted successfully
if postcommit:
if "--push" in postcommit or "--add-push" in postcommit:
system("git push")
def select_menu():
"""Displays the select menu and returns the value to enter into the commit"""
options = ["👌 Improvement", "📦 Addition", "📖 Documentation", "🐛 Bug-fix", "🔖 Version-tag", "🚪 Exit"]
commit_label = ""
select_label = None
for option in options:
opt_no = options.index(option)
print(f"{opt_no+1}) {option}")
select_label = input("Enter the number corresponding to which type of change you made: ")
try: # attempt to convert input to an integer
select_label = int(select_label)
except ValueError: # if a number isn't entered
# attempt to remove any accidentally mistyped special characters
from re import sub
remove = "[m'/\]\\\\]" # characters that can be easily mistyped with an enter press
# the reason there are 4 backslashes is because
# one backslash is the user input, another is
# added automatically, an extra backslash is
# added to each to show that we're using
# backslashes instead of escape characters
# yeah that was fun lmao
select_label = str(select_label)
select_label = sub(remove, '', select_label)
# after removing characters try converting back to integer
try:
select_label = int(select_label)
except ValueError: # if the conversion doesn't work
print("🧙 This ain't fair, I'm a CLI tool not a wizard (yet). Please enter numbers not characters lol.")
exit()
select_menu_labels = { # list each select menu number and it's corresponding commit label
1 : "👌 IMPROVE: ",
2 : "📦 NEW: ",
3 : "📖 DOC: ",
4 : "🐛 FIX: ",
5 : "🔖 "
}
# loop through the menu number and label
for menu_number, label in select_menu_labels.items():
if select_label == menu_number:
commit_label = label
return commit_label
# if none of the label options were selected
if select_label == 6: # exit option
print("Exiting have a nice day...")
exit()
else:
print("Unrecognised menu option, exiting...")
exit()
def second_menu():
"""Displays another select menu with more options that are used less often"""
options = [
"🔧 Configuration files",
"🚚 Move files",
"🙈 Ignore files",
"❌ Remove files",
"🚪 Exit"
]
for option in options:
opt_no = options.index(option)
print(f"{opt_no + 1}) {option}")
select_label = input("Enter the number corresponding to which type of change you made: ")
try: # attempt to convert input to an integer
select_label = int(select_label)
except ValueError: # if a number isn't entered
# attempt to remove any accidentally mistyped special characters
from re import sub
remove = "[m'/\]\\\\]" # characters that can be easily mistyped with an enter press
# the reason there are 4 backslashes is because
# one backslash is the user input, another is
# added automatically, an extra backslash is
# added to each to show that we're using
# backslashes instead of escape characters
# yeah that was fun lmao
select_label = str(select_label)
select_label = sub(remove, '', select_label)
# after removing characters try converting back to integer
try:
select_label = int(select_label)
except ValueError: # if the conversion doesn't work
print("🧙 This ain't fair, I'm a CLI tool not a wizard (yet). Please enter numbers not characters lol.")
exit()
select_menu_labels = {
1 : "🔧 CONFIG: ",
2 : "🚚 MOVE: ",
3 : "🙈 IGNORE: ",
4 : "❌ REMOVE: "
}
for menu_number, label in select_menu_labels.items():
if select_label == menu_number:
commit_label = label
return commit_label
# if this is running then the option used hasn't been recognised
if select_label == 5:
print("Exiting, have a nice day...")
exit()
else:
print("Unrecognised menu option, exiting...")
exit()
def strict_menu():
"""Displays a select menu which strictly follows the labels specified by
the emoji log project by ahmadawais"""
options = ["👌 Improvement", "📦 Addition", "📖 Documentation", "🐛 Bug-fix", "🚀 Release", "🤖 Test", "‼️ Breaking", "🚪 Exit"]
for option in options:
opt_no = options.index(option)
print(f"{opt_no+1}) {option}")
select_label = input("Enter the number corresponding to which type of change you made: ")
try:
select_label = int(select_label)
except ValueError:
# attempt to remove any accidentally mistyped special characters
from re import sub
remove = "[;m'/\]\\\\]" # characters that can be easily mistyped with an enter press
# the reason there are 4 backslashes is because
# one backslash is the user input, another is
# added automatically, an extra backslash is
# added to each to show that we're using
# backslashes instead of escape characters
# yeah that was fun lmao
select_label = str(select_label)
select_label = sub(remove, '', select_label)
# after removing characters try converting back to integer
try:
select_label = int(select_label)
except ValueError: # if the conversion doesn't work
print("🧙 This ain't fair, I'm a CLI tool not a wizard (yet). Please enter numbers not characters lol.")
exit()
select_menu_labels = {
1 : "👌 IMPROVE: ",
2 : "📦 NEW: ",
3 : "📖 DOC: ",
4 : "🐛 FIX: ",
5 : "🚀 RELEASE: ",
6 : "🤖 TEST: ",
7 : "‼️ BREAKING: "
}
for menu_number, label in select_menu_labels.items():
# if entered value corresponds to dict item number
if select_label == menu_number:
# assign the label to be the label
# corresponding to the related number
commit_label = label
return commit_label
# if this is running then the option used hasn't been recognised
if select_label == 8:
print("Exiting, have a nice day...")
exit()
else:
print("Unrecognised menu option, exiting...")
exit()
def show_all():
"""Displays a list of all select menu options and their message results"""
print("")
print(f"{BOLD}Standard select menu:{RESET}")
print("")
print("1) 👌 Improvement 👌 IMPROVE: <message>")
print("2) 📦 Addition 📦 NEW: <message>")
print("3) 📖 Documentation 📖 DOC: <message>")
print("4) 🐛 Bug-fix 🐛 FIX <message>")
print("5) 🔖 Version-tag 🔖 <message>")
print("")
print("")
print(f"{BOLD}Extra select menu{RESET}")
print("")
print("1) 🔧 Config files 🔧 CONFIG: <message>")
print("2) 🚚 Move files 🚚 MOVE: <message>")
print("3) 🙈 Ignore files 🙈 IGNORE: <message>")
print("4) ❌ Remove files ❌ REMOVE: <message>")
print("")
print("")
print(f"{BOLD}Strict select menu (emoji-log){RESET}")
print("")
print("1) 👌 Improvement 👌 IMPROVE: <message>")
print("2) 📦 Addition 📦 NEW: <message>")
print("3) 📖 Documentation 📖 DOC: <message>")
print("4) 🐛 Bug-fix 🐛 FIX: <message>")
print("5) 🚀 Release 🚀 RELEASE: <message>")
print("6) 🤖 Test 🤖 TEST: <message>")
print("7) ‼️ Breaking ‼️ BREAKING: <message>")
print("")
if __name__ == "__main__":
options = get_opts()
if options == None:
print("Incorrect usage. Refer to commit --help")
print("")
else:
special_opts = get_special_opts()
typ, msg = options # assign the results of get_opts
if special_opts[2]:
commands_execution(special_opts[2], msg, special_opts=special_opts)
exit() # exit
if typ == "message only":
label = select_menu()
commands_execution(label, msg, special_opts=special_opts)
elif typ == "strict":
label = strict_menu()
commands_execution(label, msg, special_opts=special_opts)
elif typ == "extra":
label = second_menu()
commands_execution(label, msg, special_opts=special_opts)
elif typ == "shortcut":
commands_execution(None, msg, shortcut=True, special_opts=special_opts)
elif typ == "error":
print(f"Error occured: {msg}")