44import sys
55import subprocess
66import os
7+ import re
78
89def file_prepend (file , str ):
910 with open (file , 'r' ) as fd :
@@ -16,14 +17,15 @@ def file_prepend(file, str):
1617
1718def process_git_request (fname , target_branch , source_branch , prj_dir ):
1819 retcode = 0 # presume success
19- print (f"Opening file { fname } " )
20+ # print(f"Opening file {fname}")
2021 file = open (fname , "w" )
2122 working_dir = prj_dir
22- print (f"Working Dir : { working_dir } " )
23+ # print(f"Working Dir : {working_dir}")
2324 os .chdir (working_dir )
24- print (f"pwd : { os .getcwd ()} " )
25+ # print(f"pwd : {os.getcwd()}")
2526 git_cmd = f"git log --oneline --no-abbrev-commit origin/{ target_branch } ..origin/{ source_branch } "
26- print (git_cmd )
27+ # print(git_cmd)
28+ loglines_to_check = 13
2729 try :
2830 out , err = subprocess .Popen (git_cmd , shell = True , stdout = subprocess .PIPE ,
2931 stderr = subprocess .PIPE , text = True ).communicate ()
@@ -33,18 +35,79 @@ def process_git_request(fname, target_branch, source_branch, prj_dir):
3335 file .close ()
3436 return 1
3537
36- output_lines = out .split ()
38+ output_lines = out .splitlines ()
39+ commit_sha = ""
40+ upstream_diff = False
3741 # we just want the commit sha IDs
3842 for x in output_lines :
39- print (f"This is output_lines { x } " )
43+ # print(f"This is output_lines {x}")
44+ if not bool (re .search (r'[^\x30-\x39a-fA-F]' , x )): # equivalent to Ruby's !x[/\H/]
45+ continue
46+ else :
47+ y = x .split ()
48+ # print(f"This is y {y}")
49+ commit_sha = str (y [0 ])
50+ # print("Found a commit in line ", commit_sha)
4051
52+ git_cmd = "git show " + commit_sha
53+ gitlog_out , gitlog_err = subprocess .Popen (git_cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE , universal_newlines = True ).communicate ()
54+
55+ loglines = gitlog_out .splitlines ()
56+ lines_counted = 0
57+ local_diffdiff_sha = commit_sha
58+ upstream_diffdiff_sha = ""
59+
60+ for logline in loglines :
61+ print (f"Processing logline { logline } " )
62+ lines_counted += 1
63+ if lines_counted == 1 :
64+ file .write ("Merge Request sha: " + local_diffdiff_sha )
65+ file .write ("\n " )
66+ if lines_counted == 2 : # email address
67+ if "ciq.com" not in logline .lower ():
68+ # Bad Author
69+ s = f"error:\n Bad { logline } \n "
70+ print (s )
71+ file .write (s )
72+ file .close ()
73+ return retcode
74+ if lines_counted > 1 :
75+ if "jira" in logline .lower ():
76+ file .write ("\t " + logline + "\n " )
77+
78+ if "upstream-diff" in logline .lower ():
79+ upstream_diff = True
80+
81+ if "commit" in logline .lower ():
82+ commit_sha = re .search (r'[0-9a-f]{40}' , logline )
83+ upstream_diffdiff_sha = str (commit_sha .group (0 )) if commit_sha else ""
84+ print ("Upstream : " + upstream_diffdiff_sha )
85+ if upstream_diffdiff_sha :
86+ file .write ("\t Upstream sha: " + upstream_diffdiff_sha )
87+ file .write ("\n " )
88+
89+ if lines_counted > loglines_to_check : # Everything we need should be in the first loglines_to_check lines
90+ print (f"Breaking after { loglines_to_check } lines of commit checking" )
91+ break
92+
93+ if local_diffdiff_sha and upstream_diffdiff_sha :
94+ diff_cmd = os .path .join (os .getcwd (), ".github/workflows/diffdiff.py" ) + " --colour --commit " + local_diffdiff_sha
95+ print ("diffdiff: " + diff_cmd )
96+ process = subprocess .run (diff_cmd , shell = True , capture_output = True , text = True )
97+ diff_out = process .stdout
98+ diff_err = process .stderr
99+ diff_status = process .returncode
100+
101+ if diff_status != 0 and not upstream_diff :
102+ print ("diffdiff out: " + diff_out )
103+ print ("diffdiff err: " + diff_err )
104+ retcode = 1
105+ file .write ("error:\n Commit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n " )
106+
107+ finally :
41108 file .close ()
42- return retcode
43- except Exception as e :
44- print (f"Error executing git command: { str (e )} " )
45- file .close ()
46- return 1
47- return 0
109+
110+ return retcode
48111
49112first_arg , * argv_in = sys .argv [1 :] # Skip script name in sys.argv
50113
0 commit comments