Skip to content

Commit b508424

Browse files
committed
contrib: github-merge improvements
Some minor github-merge improvements I've made over time: User interface: - Print merge details again before signing off, to refresh your memory - usually I'll have done lots of different things in the shell so this will have scrolled out a long time ago. - Require a valid answer on the prompts. One of the requested answers must be typed, if not, the prompt will re-ask. This prevents accidentally rejecting. Efficiency: - Condense "accept merge" and "sign off" prompts. There's no reason to have this as two separate prompts, both are just opportunities to skip out on the merge, no action is performed in between. Merging: - Strip spaces from github title. This avoids redundant spaces surrounding it from getting into the commit message.
1 parent 27faa6c commit b508424

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

contrib/devtools/github-merge.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2016 The Bitcoin Core developers
2+
# Copyright (c) 2016-2017 Bitcoin Core Developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -15,7 +15,7 @@
1515
# In case of a clean merge that is accepted by the user, the local branch with
1616
# name $BRANCH is overwritten with the merged result, and optionally pushed.
1717
from __future__ import division,print_function,unicode_literals
18-
import os
18+
import os,sys
1919
from sys import stdin,stdout,stderr
2020
import argparse
2121
import hashlib
@@ -127,6 +127,9 @@ def tree_sha512sum(commit='HEAD'):
127127
raise IOError('Non-zero return value executing git cat-file')
128128
return overall.hexdigest()
129129

130+
def print_merge_details(pull, title, branch, base_branch, head_branch):
131+
print('%s#%s%s %s %sinto %s%s' % (ATTR_RESET+ATTR_PR,pull,ATTR_RESET,title,ATTR_RESET+ATTR_PR,branch,ATTR_RESET))
132+
subprocess.check_call([GIT,'log','--graph','--topo-order','--pretty=format:'+COMMIT_FORMAT,base_branch+'..'+head_branch])
130133

131134
def parse_arguments():
132135
epilog = '''
@@ -171,7 +174,7 @@ def main():
171174
info = retrieve_pr_info(repo,pull)
172175
if info is None:
173176
exit(1)
174-
title = info['title']
177+
title = info['title'].strip()
175178
# precedence order for destination branch argument:
176179
# - command line argument
177180
# - githubmerge.branch setting
@@ -256,8 +259,7 @@ def main():
256259
printf("ERROR: Cannot update message.",file=stderr)
257260
exit(4)
258261

259-
print('%s#%s%s %s %sinto %s%s' % (ATTR_RESET+ATTR_PR,pull,ATTR_RESET,title,ATTR_RESET+ATTR_PR,branch,ATTR_RESET))
260-
subprocess.check_call([GIT,'log','--graph','--topo-order','--pretty=format:'+COMMIT_FORMAT,base_branch+'..'+head_branch])
262+
print_merge_details(pull, title, branch, base_branch, head_branch)
261263
print()
262264

263265
# Run test command if configured.
@@ -276,12 +278,6 @@ def main():
276278
print("Difference with github ignored.",file=stderr)
277279
else:
278280
exit(6)
279-
reply = ask_prompt("Press 'd' to accept the diff.")
280-
if reply.lower() == 'd':
281-
print("Diff accepted.",file=stderr)
282-
else:
283-
print("ERROR: Diff rejected.",file=stderr)
284-
exit(6)
285281
else:
286282
# Verify the result manually.
287283
print("Dropping you on a shell so you can try building/testing the merged source.",file=stderr)
@@ -290,29 +286,26 @@ def main():
290286
if os.path.isfile('/etc/debian_version'): # Show pull number on Debian default prompt
291287
os.putenv('debian_chroot',pull)
292288
subprocess.call([BASH,'-i'])
293-
reply = ask_prompt("Type 'm' to accept the merge.")
294-
if reply.lower() == 'm':
295-
print("Merge accepted.",file=stderr)
296-
else:
297-
print("ERROR: Merge rejected.",file=stderr)
298-
exit(7)
299289

300290
second_sha512 = tree_sha512sum()
301291
if first_sha512 != second_sha512:
302292
print("ERROR: Tree hash changed unexpectedly",file=stderr)
303293
exit(8)
304294

305295
# Sign the merge commit.
306-
reply = ask_prompt("Type 's' to sign off on the merge.")
307-
if reply == 's':
308-
try:
309-
subprocess.check_call([GIT,'commit','-q','--gpg-sign','--amend','--no-edit'])
310-
except subprocess.CalledProcessError as e:
311-
print("Error signing, exiting.",file=stderr)
296+
print_merge_details(pull, title, branch, base_branch, head_branch)
297+
while True:
298+
reply = ask_prompt("Type 's' to sign off on the above merge, or 'x' to reject and exit.").lower()
299+
if reply == 's':
300+
try:
301+
subprocess.check_call([GIT,'commit','-q','--gpg-sign','--amend','--no-edit'])
302+
break
303+
except subprocess.CalledProcessError as e:
304+
print("Error signing, exiting.",file=stderr)
305+
exit(1)
306+
elif reply == 'x':
307+
print("Not signing off on merge, exiting.",file=stderr)
312308
exit(1)
313-
else:
314-
print("Not signing off on merge, exiting.",file=stderr)
315-
exit(1)
316309

317310
# Put the result in branch.
318311
subprocess.check_call([GIT,'checkout','-q',branch])
@@ -326,9 +319,13 @@ def main():
326319
subprocess.call([GIT,'branch','-q','-D',local_merge_branch],stderr=devnull)
327320

328321
# Push the result.
329-
reply = ask_prompt("Type 'push' to push the result to %s, branch %s." % (host_repo,branch))
330-
if reply.lower() == 'push':
331-
subprocess.check_call([GIT,'push',host_repo,'refs/heads/'+branch])
322+
while True:
323+
reply = ask_prompt("Type 'push' to push the result to %s, branch %s, or 'x' to exit without pushing." % (host_repo,branch)).lower()
324+
if reply == 'push':
325+
subprocess.check_call([GIT,'push',host_repo,'refs/heads/'+branch])
326+
break
327+
elif reply == 'x':
328+
exit(1)
332329

333330
if __name__ == '__main__':
334331
main()

0 commit comments

Comments
 (0)