18
18
import os
19
19
from sys import stdin ,stdout ,stderr
20
20
import argparse
21
+ import hashlib
21
22
import subprocess
22
23
import json ,codecs
23
24
try :
@@ -69,6 +70,27 @@ def ask_prompt(text):
69
70
print ("" ,file = stderr )
70
71
return reply
71
72
73
+ def tree_sha512sum ():
74
+ files = sorted (subprocess .check_output ([GIT , 'ls-tree' , '--full-tree' , '-r' , '--name-only' , 'HEAD' ]).splitlines ())
75
+ overall = hashlib .sha512 ()
76
+ for f in files :
77
+ intern = hashlib .sha512 ()
78
+ fi = open (f , 'rb' )
79
+ while True :
80
+ piece = fi .read (65536 )
81
+ if piece :
82
+ intern .update (piece )
83
+ else :
84
+ break
85
+ fi .close ()
86
+ dig = intern .hexdigest ()
87
+ overall .update (dig .encode ("utf-8" ))
88
+ overall .update (" " .encode ("utf-8" ))
89
+ overall .update (f )
90
+ overall .update ("\n " .encode ("utf-8" ))
91
+ return overall .hexdigest ()
92
+
93
+
72
94
def parse_arguments ():
73
95
epilog = '''
74
96
In addition, you can set the following git configuration variables:
@@ -157,6 +179,9 @@ def main():
157
179
subprocess .check_call ([GIT ,'checkout' ,'-q' ,'-b' ,local_merge_branch ])
158
180
159
181
try :
182
+ # Go up to the repository's root.
183
+ toplevel = subprocess .check_output ([GIT ,'rev-parse' ,'--show-toplevel' ]).strip ()
184
+ os .chdir (toplevel )
160
185
# Create unsigned merge commit.
161
186
if title :
162
187
firstline = 'Merge #%s: %s' % (pull ,title )
@@ -175,14 +200,29 @@ def main():
175
200
print ("ERROR: Creating merge failed (already merged?)." ,file = stderr )
176
201
exit (4 )
177
202
203
+ # Put tree SHA512 into the message
204
+ try :
205
+ first_sha512 = tree_sha512sum ()
206
+ message += '\n \n Tree-SHA512: ' + first_sha512
207
+ except subprocess .CalledProcessError as e :
208
+ printf ("ERROR: Unable to compute tree hash" )
209
+ exit (4 )
210
+ try :
211
+ subprocess .check_call ([GIT ,'commit' ,'--amend' ,'-m' ,message .encode ('utf-8' )])
212
+ except subprocess .CalledProcessError as e :
213
+ printf ("ERROR: Cannot update message." ,file = stderr )
214
+ exit (4 )
215
+ second_sha512 = tree_sha512sum ()
216
+ if first_sha512 != second_sha512 :
217
+ print ("ERROR: Tree hash changed unexpectedly" ,file = stderr )
218
+ exit (4 )
219
+
178
220
print ('%s#%s%s %s %sinto %s%s' % (ATTR_RESET + ATTR_PR ,pull ,ATTR_RESET ,title ,ATTR_RESET + ATTR_PR ,branch ,ATTR_RESET ))
179
221
subprocess .check_call ([GIT ,'log' ,'--graph' ,'--topo-order' ,'--pretty=format:' + COMMIT_FORMAT ,base_branch + '..' + head_branch ])
180
222
print ()
223
+
181
224
# Run test command if configured.
182
225
if testcmd :
183
- # Go up to the repository's root.
184
- toplevel = subprocess .check_output ([GIT ,'rev-parse' ,'--show-toplevel' ]).strip ()
185
- os .chdir (toplevel )
186
226
if subprocess .call (testcmd ,shell = True ):
187
227
print ("ERROR: Running %s failed." % testcmd ,file = stderr )
188
228
exit (5 )
0 commit comments