|
| 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 | + repo = git.Repo(".") |
| 39 | + commits = repo.iter_commits(f"{target_branch}..{source_branch}") |
| 40 | + loglines_to_check = 13 |
| 41 | + for commit in commits: |
| 42 | + print(f"{commit.hexsha} {commit.message.splitlines()[0]}") |
| 43 | + |
| 44 | + commit_sha = commit.hexsha |
| 45 | + |
| 46 | + git_cmd = "git show " + commit_sha |
| 47 | + gitlog_out, gitlog_err = subprocess.Popen(git_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).communicate() |
| 48 | + |
| 49 | + loglines = gitlog_out.splitlines() |
| 50 | + lines_counted = 0 |
| 51 | + local_diffdiff_sha = commit_sha |
| 52 | + upstream_diffdiff_sha = "" |
| 53 | + upstream_diff = False |
| 54 | + |
| 55 | + for logline in loglines: |
| 56 | +# print(f"Processing logline {commit_sha}") |
| 57 | + lines_counted += 1 |
| 58 | + if lines_counted == 1: |
| 59 | + file.write("Merge Request sha: " + local_diffdiff_sha) |
| 60 | + file.write("\n") |
| 61 | + if lines_counted == 2: # email address |
| 62 | + if "ciq.com" not in logline.lower(): |
| 63 | + # Bad Author |
| 64 | + s = f"error:\nBad {logline}\n" |
| 65 | + print(s) |
| 66 | + file.write(s) |
| 67 | + file.close() |
| 68 | + return retcode |
| 69 | + if lines_counted > 1: |
| 70 | + if "jira" in logline.lower(): |
| 71 | + file.write("\t" + logline + "\n") |
| 72 | + |
| 73 | + if "upstream-diff" in logline.lower(): |
| 74 | + upstream_diff = True |
| 75 | + |
| 76 | + if "commit" in logline.lower(): |
| 77 | + local_commit_sha = re.search(r'[0-9a-f]{40}', logline) |
| 78 | + upstream_diffdiff_sha = str(local_commit_sha.group(0)) if local_commit_sha else "" |
| 79 | + if upstream_diffdiff_sha: |
| 80 | + print(f"Upstream : " + upstream_diffdiff_sha) |
| 81 | + file.write("\tUpstream sha: " + upstream_diffdiff_sha) |
| 82 | + file.write("\n") |
| 83 | + |
| 84 | + if lines_counted > loglines_to_check: # Everything we need should be in the first loglines_to_check lines |
| 85 | +# print(f"Breaking after {loglines_to_check} lines of commit checking") |
| 86 | + break |
| 87 | + |
| 88 | + if local_diffdiff_sha and upstream_diffdiff_sha: |
| 89 | + diff_cmd = os.path.join(os.getcwd(), ".github/workflows/diffdiff.py") + " --colour --commit " + local_diffdiff_sha |
| 90 | +# print("diffdiff: " + diff_cmd) |
| 91 | + process = subprocess.run(diff_cmd, shell=True, capture_output=True, text=True) |
| 92 | + diff_out = process.stdout |
| 93 | + diff_err = process.stderr |
| 94 | + diff_status = process.returncode |
| 95 | + |
| 96 | + if diff_status != 0 and not upstream_diff: |
| 97 | + print(f"diffdiff out: " + diff_out) |
| 98 | + print(f"diffdiff err: " + diff_err) |
| 99 | + retcode = 1 |
| 100 | + file.write("error:\nCommit: " + local_diffdiff_sha + " differs with no upstream tag in commit message\n") |
| 101 | + |
| 102 | + return retcode |
| 103 | + |
| 104 | +first_arg, *argv_in = sys.argv[1:] # Skip script name in sys.argv |
| 105 | + |
| 106 | +if len(argv_in) < 5: |
| 107 | + print("Not enough arguments: fname, target_branch, source_branch, prj_dir, pull_request, requestor") |
| 108 | + sys.exit() |
| 109 | + |
| 110 | +fname = str(first_arg) |
| 111 | +fname = "tmp-" + fname |
| 112 | +# print("filename is " + fname) |
| 113 | +target_branch = str(argv_in[0]) |
| 114 | +# print("target branch is " + target_branch) |
| 115 | +source_branch = str(argv_in[1]) |
| 116 | +# print("source branch is " + source_branch) |
| 117 | +prj_dir = str(argv_in[2]) |
| 118 | +# print("project dir is " + prj_dir) |
| 119 | +pullreq = str(argv_in[3]) |
| 120 | +# print("pull request is " + pullreq) |
| 121 | +requestor = str(argv_in[4]) |
| 122 | + |
| 123 | +retcode = process_git_request(fname, target_branch, source_branch, prj_dir) |
| 124 | + |
| 125 | +if retcode != 0: |
| 126 | + with open(fname, 'r') as fd: |
| 127 | + contents = fd.read() |
| 128 | + print(contents) |
| 129 | + sys.exit(1) |
| 130 | +else: |
| 131 | + print("Done") |
| 132 | + |
| 133 | +sys.exit(0) |
| 134 | + |
0 commit comments