|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | import sys
|
| 3 | +import re |
3 | 4 | import platform
|
4 | 5 | import subprocess
|
5 | 6 |
|
6 | 7 | clear_screen_command = 'cls' if platform.system() == 'Windows' else 'clear'
|
7 |
| -subprocess.call(clear_screen_command) |
| 8 | +# subprocess.call(clear_screen_command) |
8 | 9 |
|
9 |
| -# Checking for not staged files, if there's files pending, exit the script |
10 |
| -git_command = ['git', '--no-pager', 'status', '-s'] |
11 |
| -execute = subprocess.run(git_command, stdout=subprocess.PIPE) |
12 |
| -modified_files = execute.stdout \ |
13 |
| - .decode('utf-8') \ |
14 |
| - .split('\n') |
15 |
| -modified_files = [ file[3:] for file in modified_files if file ] |
| 10 | +# Receive the filename with the temporary git commit msg. |
| 11 | +msg_file = sys.argv[1] |
16 | 12 |
|
17 |
| -print(modified_files) |
| 13 | +msg_filehandle = open(msg_file) |
| 14 | +with msg_filehandle as f: |
| 15 | + commit_msg_parts = f.readlines() |
| 16 | + commit_msg = ''.join(commit_msg_parts) |
18 | 17 |
|
19 |
| -if len(modified_files): |
20 |
| - print('Files have been found in the work area, please stash or commit them first and run this command again.\n') |
| 18 | +if not commit_msg: |
21 | 19 | sys.exit(1)
|
| 20 | +else: |
| 21 | + execute = subprocess.run(['git', 'branch'], stdout=subprocess.PIPE) |
| 22 | + branch_list = execute.stdout \ |
| 23 | + .decode('utf-8') \ |
| 24 | + |
| 25 | + # Extract issue part from active branch for branch list |
| 26 | + # * bugfix/APM-123456-test-branch-for-commit-msg |
| 27 | + # ^^^^^^^^^^^^^^^^^ |
| 28 | + # master |
| 29 | + regex = re.compile(r'(?<=\*\s).+APM-[0-9]*', re.MULTILINE) |
| 30 | + issue_id = ''.join(re.findall(regex, branch_list)) |
| 31 | + |
| 32 | + msg_filehandle = open(msg_file, 'w') |
| 33 | + with msg_filehandle as f: |
| 34 | + complete_commit_msg = '{} {}'.format(issue_id, commit_msg).strip() |
| 35 | + f.write(complete_commit_msg) |
| 36 | + |
| 37 | +sys.exit(0) |
0 commit comments