diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2017-03-01 10:03:33 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2017-03-01 10:03:44 +0100 |
commit | be8ba2cfa44a8459ebd92e3da7c1907c2a49fd5c (patch) | |
tree | 3906850fdeeb2dab87c05b68479f2671b02ee9e6 /contrib/devtools/github-merge.py | |
parent | 11049f4fe62606d1b0380a9ef800ac130f0fbadf (diff) | |
parent | fa89670d34ac7839e7e2fe6f59fdfd6cc76c4483 (diff) |
Merge #9871: Add a tree sha512 hash to merge commits
fa89670 Add SHA512 tree hash to merge commits (Pieter Wuille)
Tree-SHA512: 72321597336d3c4957719c8b907f258814b01499a82d2bc1e8c678b8825461d95f23b42ff6868a25725f4bfc3da24f7b12c058b45cbc7a7dfbf668888b68274e
Diffstat (limited to 'contrib/devtools/github-merge.py')
-rwxr-xr-x | contrib/devtools/github-merge.py | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/contrib/devtools/github-merge.py b/contrib/devtools/github-merge.py index 0cee0921b1..bb6ffb0253 100755 --- a/contrib/devtools/github-merge.py +++ b/contrib/devtools/github-merge.py @@ -18,6 +18,7 @@ from __future__ import division,print_function,unicode_literals import os from sys import stdin,stdout,stderr import argparse +import hashlib import subprocess import json,codecs try: @@ -69,6 +70,27 @@ def ask_prompt(text): print("",file=stderr) return reply +def tree_sha512sum(): + files = sorted(subprocess.check_output([GIT, 'ls-tree', '--full-tree', '-r', '--name-only', 'HEAD']).splitlines()) + overall = hashlib.sha512() + for f in files: + intern = hashlib.sha512() + fi = open(f, 'rb') + while True: + piece = fi.read(65536) + if piece: + intern.update(piece) + else: + break + fi.close() + dig = intern.hexdigest() + overall.update(dig.encode("utf-8")) + overall.update(" ".encode("utf-8")) + overall.update(f) + overall.update("\n".encode("utf-8")) + return overall.hexdigest() + + def parse_arguments(): epilog = ''' In addition, you can set the following git configuration variables: @@ -157,6 +179,9 @@ def main(): subprocess.check_call([GIT,'checkout','-q','-b',local_merge_branch]) try: + # Go up to the repository's root. + toplevel = subprocess.check_output([GIT,'rev-parse','--show-toplevel']).strip() + os.chdir(toplevel) # Create unsigned merge commit. if title: firstline = 'Merge #%s: %s' % (pull,title) @@ -175,14 +200,29 @@ def main(): print("ERROR: Creating merge failed (already merged?).",file=stderr) exit(4) + # Put tree SHA512 into the message + try: + first_sha512 = tree_sha512sum() + message += '\n\nTree-SHA512: ' + first_sha512 + except subprocess.CalledProcessError as e: + printf("ERROR: Unable to compute tree hash") + exit(4) + try: + subprocess.check_call([GIT,'commit','--amend','-m',message.encode('utf-8')]) + except subprocess.CalledProcessError as e: + printf("ERROR: Cannot update message.",file=stderr) + exit(4) + second_sha512 = tree_sha512sum() + if first_sha512 != second_sha512: + print("ERROR: Tree hash changed unexpectedly",file=stderr) + exit(4) + print('%s#%s%s %s %sinto %s%s' % (ATTR_RESET+ATTR_PR,pull,ATTR_RESET,title,ATTR_RESET+ATTR_PR,branch,ATTR_RESET)) subprocess.check_call([GIT,'log','--graph','--topo-order','--pretty=format:'+COMMIT_FORMAT,base_branch+'..'+head_branch]) print() + # Run test command if configured. if testcmd: - # Go up to the repository's root. - toplevel = subprocess.check_output([GIT,'rev-parse','--show-toplevel']).strip() - os.chdir(toplevel) if subprocess.call(testcmd,shell=True): print("ERROR: Running %s failed." % testcmd,file=stderr) exit(5) |