- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-push.py
More file actions
Latest commit
63 lines (52 loc) · 2.57 KB
/
Copy pathpre-push.py
File metadata and controls
63 lines (52 loc) · 2.57 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
#!/usr/bin/env python3
importsys
importplatform
importsubprocess
clear_screen_command='cls'ifplatform.system() =='Windows'else'clear'
subprocess.call(clear_screen_command)
# Checking for not staged files, if there's files pending, exit the script
git_command= ['git', '--no-pager', 'status', '-s']
execute=subprocess.run(git_command, stdout=subprocess.PIPE)
modified_files=execute.stdout \
.decode('utf-8') \
.split('\n')
modified_files= [ file[3:] forfileinmodified_filesiffile ]
iflen(modified_files):
print('Files have been found in the work area, please stash or commit them first and run this command again.\n')
sys.exit(1)
forlineinsys.stdin:
git_params=line.replace('\n','') \
.split(' ')
# This will be 40 zeroes when we're trying to push in a new branch
empty_sha='0'*40
local_sha=git_params[1]
remote_sha=Noneifgit_params[3] ==empty_shaelsegit_params[3]
git_command= ['git', '--no-pager', 'diff', '--name-only', local_sha, remote_sha]
execute=subprocess.run(git_command, stdout=subprocess.PIPE)
modified_files=execute.stdout \
.decode('utf-8') \
.split('\n')
# Gather all the unique extensions for modified files in order to execute the configured commands
modified_extensions=list(set(
[ file.split('.')[-1] forfileinmodified_filesiflen(file.split('.')) >1 ]
))
forextensioninmodified_extensions:
extension_config='pre-push.{}.command'.format(extension)
git_command= ['git', 'config', extension_config]
execute=subprocess.run(git_command, stdout=subprocess.PIPE)
pre_push_pipeline=execute.stdout \
.decode('utf-8') \
.replace('\n', '') \
.split(';')
pre_push_pipeline= [pipeline_command.strip() forpipeline_commandinpre_push_pipeline]
forpipeline_commandinpre_push_pipeline:
ifpipeline_commandisnot'':
print(' [{ext}] "{cmd}" '.format(ext=extension, cmd=pipeline_command).center(80, '='))
git_command=pipeline_command.split(' ')
execute=subprocess.call(git_command)
ifexecute:
print('\n'*2)
print('Aborting push because [{cmd}] is returning a non-zero value'.format(cmd=pipeline_command))
sys.exit(execute)
print('\n'*2)
sys.exit(0)