|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +import os |
| 6 | +import re |
| 7 | +import git |
| 8 | + |
| 9 | +def log_commits_between_branches(repo_path, from_branch, to_branch): |
| 10 | + repo = git.Repo(repo_path) |
| 11 | + |
| 12 | + # Get the common ancestor of the two branches |
| 13 | + common_ancestor = repo.merge_base(from_branch, to_branch) |
| 14 | + |
| 15 | + print(f"Common ancestor is {common_ancestor}") |
| 16 | + |
| 17 | + # Get the commits in 'from_branch' that are not in 'to_branch' |
| 18 | + commits = list(repo.iter_commits(f"{to_branch}..{from_branch}")) |
| 19 | + |
| 20 | + for commit in commits: |
| 21 | + print(commit.hexsha, commit.message.strip()) |
| 22 | + |
| 23 | +def file_prepend(file, str): |
| 24 | + with open(file, 'r') as fd: |
| 25 | + contents = fd.read() |
| 26 | + new_contents = str + contents |
| 27 | + |
| 28 | + # Overwrite file but now with prepended string on it |
| 29 | + with open(file, 'w') as fd: |
| 30 | + fd.write(new_contents) |
| 31 | + |
| 32 | +def process_git_request(fname, target_branch, source_branch, prj_dir): |
| 33 | + retcode = 0 # presume success |
| 34 | + file = open(fname, "w") |
| 35 | + working_dir = prj_dir |
| 36 | + os.chdir(working_dir) |
| 37 | + |
| 38 | + git_cmd = f"git log -1 --format=%H origin/{target_branch}" |
| 39 | + gitbr_out, gitbr_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() |
| 40 | + if gitbr_err: |
| 41 | + print(f"git log -1 returned error {gitbr_err}") |
| 42 | + else: |
| 43 | + gitbr_lines = gitbr_out.splitlines() |
| 44 | + for x in gitbr_lines: |
| 45 | + print(f"git log -1 output line {x}") |
| 46 | + local_target_branch = x |
| 47 | + |
| 48 | + git_cmd = f"git log -1 --format=%H origin/{source_branch}" |
| 49 | + gitbr_out, gitbr_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() |
| 50 | + if gitbr_err: |
| 51 | + print(f"git log -1 returned error {gitbr_err}") |
| 52 | + else: |
| 53 | + gitbr_lines = gitbr_out.splitlines() |
| 54 | + for x in gitbr_lines: |
| 55 | + print(f"git log -1 output line {x}") |
| 56 | + local_source_branch = x |
| 57 | + |
| 58 | +# git_cmd = f"git log --oneline --no-abbrev-commit " + local_target_branch + ".." + local_source_branch + "-- ." |
| 59 | +# print(f"git command is {git_cmd}")a |
| 60 | + log_commits_between_branches(prj_dir, source_branch, target_branch) |
| 61 | + return 0 |
| 62 | + loglines_to_check = 13 |
| 63 | + try: |
| 64 | + out = subprocess.run(git_cmd, shell=True, capture_output=True, text=True, encoding='latin-1') |
| 65 | + if out.returncode: |
| 66 | + print(f"Command error output is {out}") |
| 67 | + file.write(f"Command error output is {out}") |
| 68 | + file.close() |
| 69 | + return 1 |
| 70 | + else: |
| 71 | + print(f"Git log line executed") |
| 72 | + |
| 73 | + line_count = len(str(out.stdout).splitlines()) |
| 74 | + print(f"Got {line_count} lines of git log output") |
| 75 | + if line_count > 1000: |
| 76 | + print(f"Huge Line count {line_count}") |
| 77 | + return 0 |
| 78 | + |
| 79 | + output_lines = out.stdout |
| 80 | + print(f"{output_lines}") |
| 81 | + return 0 |
| 82 | + commit_sha = "" |
| 83 | + # we just want the commit sha IDs |
| 84 | + for x in output_lines: |
| 85 | +# print(f"This is output_lines {x}") |
| 86 | + if not bool(re.search(r'[^\x30-\x39a-fA-F]', x)): # equivalent to Ruby's !x[/\H/] |
| 87 | + continue |
| 88 | + else: |
| 89 | + y = x.split() |
| 90 | + commit_sha = str(y[0]) |
| 91 | + print(f"Found a commit in line ", commit_sha) |
| 92 | + |
| 93 | + git_cmd = "git show " + commit_sha |
| 94 | + gitlog_out, gitlog_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() |
| 95 | + |
| 96 | + loglines = gitlog_out.splitlines() |
| 97 | + lines_counted = 0 |
| 98 | + local_diffdiff_sha = commit_sha |
| 99 | + upstream_diffdiff_sha = "" |
| 100 | + upstream_diff = False |
| 101 | + |
| 102 | + for logline in loglines: |
| 103 | +# print(f"Processing logline {commit_sha}") |
| 104 | + lines_counted += 1 |
| 105 | + if lines_counted == 1: |
| 106 | + file.write("Merge Request sha: " + local_diffdiff_sha) |
| 107 | + file.write("\n") |
| 108 | + if lines_counted == 2: # email address |
| 109 | + if "ciq.com" not in logline.lower(): |
| 110 | + # Bad Author |
| 111 | + s = f"error:\nBad {logline}\n" |
| 112 | + print(s) |
| 113 | + file.write(s) |
| 114 | + file.close() |
| 115 | + return retcode |
| 116 | + if lines_counted > 1: |
| 117 | + if "jira" in logline.lower(): |
| 118 | + file.write("\t" + logline + "\n") |
| 119 | + |
| 120 | + if "upstream-diff" in logline.lower(): |
| 121 | + upstream_diff = True |
| 122 | + |
| 123 | + if "commit" in logline.lower(): |
| 124 | + commit_sha = re.search(r'[0-9a-f]{40}', logline) |
| 125 | + upstream_diffdiff_sha = str(commit_sha.group(0)) if commit_sha else "" |
| 126 | + print(f"Upstream : " + upstream_diffdiff_sha) |
| 127 | + if upstream_diffdiff_sha: |
| 128 | + file.write("\tUpstream sha: " + upstream_diffdiff_sha) |
| 129 | + file.write("\n") |
| 130 | + |
| 131 | + if lines_counted > loglines_to_check: # Everything we need should be in the first loglines_to_check lines |
| 132 | + # print(f"Breaking after {loglines_to_check} lines of commit checking") |
| 133 | + break |
| 134 | + |
| 135 | + if local_diffdiff_sha and upstream_diffdiff_sha: |
| 136 | + diff_cmd = os.path.join(os.getcwd(), ".github/workflows/diffdiff.py") + " --colour --commit " + local_diffdiff_sha |
| 137 | + # print("diffdiff: " + diff_cmd) |
| 138 | + process = subprocess.run(diff_cmd, shell=True, capture_output=True, text=True) |
| 139 | + diff_out = process.stdout |
| 140 | + diff_err = process.stderr |
| 141 | + diff_status = process.returncode |
| 142 | + |
| 143 | + if diff_status != 0 and not upstream_diff: |
| 144 | + print(f"diffdiff out: " + diff_out) |
| 145 | + print(f"diffdiff err: " + diff_err) |
| 146 | + retcode = 1 |
| 147 | + file.write("error:\nCommit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n") |
| 148 | + except Exception as error: |
| 149 | + print(f"Exception in git log command error {error}") |
| 150 | + |
| 151 | + finally: |
| 152 | + file.close() |
| 153 | + |
| 154 | + return retcode |
| 155 | + |
| 156 | +first_arg, *argv_in = sys.argv[1:] # Skip script name in sys.argv |
| 157 | + |
| 158 | +if len(argv_in) < 5: |
| 159 | + print("Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor") |
| 160 | + sys.exit() |
| 161 | + |
| 162 | +fname = str(first_arg) |
| 163 | +fname = "tmp-" + fname |
| 164 | +# print("filename is " + fname) |
| 165 | +target_branch = str(argv_in[0]) |
| 166 | +# print("target branch is " + target_branch) |
| 167 | +source_branch = str(argv_in[1]) |
| 168 | +# print("source branch is " + source_branch) |
| 169 | +prj_dir = str(argv_in[2]) |
| 170 | +# print("project dir is " + prj_dir) |
| 171 | +pullreq = str(argv_in[3]) |
| 172 | +# print("pull request is " + pullreq) |
| 173 | +requestor = str(argv_in[4]) |
| 174 | + |
| 175 | +retcode = process_git_request(fname, target_branch, pullreq, prj_dir) |
| 176 | + |
| 177 | +if retcode != 0: |
| 178 | + with open(fname, 'r') as fd: |
| 179 | + contents = fd.read() |
| 180 | + print(contents) |
| 181 | + sys.exit(1) |
| 182 | +else: |
| 183 | + print("Done") |
| 184 | + |
| 185 | +sys.exit(0) |
| 186 | + |
0 commit comments