-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitreq.py
More file actions
executable file
·62 lines (50 loc) · 1.82 KB
/
gitreq.py
File metadata and controls
executable file
·62 lines (50 loc) · 1.82 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
#!/usr/bin/env python3
'''gitreq.py: a script that allows you to define fields and '''
__author__ = "Jack Fox"
__email__ = "jfox13@nd.edu"
import sys
import subprocess
import os
import re
TEMPFILE = 'tempfile.tmp'
EDITOR = os.environ.get('EDITOR','vim')
PREREQFILE = 'gitreq.txt'
def prepare_commit_file(req_list: list) -> None:
with open(TEMPFILE, 'w') as message_file:
with open(PREREQFILE, 'r') as prereq_file:
prereq_content = prereq_file.readlines()
for line in prereq_content:
message_file.write("####################\n{}:\n\n####################\n".format(line))
req_list.append(line)
def handle_commit_no_arg(lines: list) -> None:
''' handles commit message '''
req_list = list()
prepare_commit_file(req_list)
subprocess.call([EDITOR,TEMPFILE])
with open(TEMPFILE, 'r') as message_file:
message_content = message_file.read()
if not check_prereq(req_list,message_content):
subprocess.call(['rm',TEMPFILE])
return
comment_index = lines.index('commit') + 1
lines.insert(comment_index,TEMPFILE)
lines.insert(comment_index,'-F')
subprocess.call(lines)
subprocess.call(['rm',TEMPFILE])
def check_prereq(req_list: list, message_content: str) -> None:
''' checks all prereqs are in message '''
for req in req_list:
if not re.search("{}:(?!####################)*####################\n".format(req), message_content).group(1).strip():
print("Error, did not include prereq: {}".format(req))
return False
return True
if __name__ == '__main__':
args = sys.argv
args[0] = 'git'
if '-m' in args and 'commit' in args:
# add into temp file
args.remove('-m')
if 'commit' in args:
handle_commit_no_arg(args)
else:
subprocess.call(args)