You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

github-merge.py 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2016 Bitcoin Core Developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. # This script will locally construct a merge commit for a pull request on a
  6. # github repository, inspect it, sign it and optionally push it.
  7. # The following temporary branches are created/overwritten and deleted:
  8. # * pull/$PULL/base (the current master we're merging onto)
  9. # * pull/$PULL/head (the current state of the remote pull request)
  10. # * pull/$PULL/merge (github's merge)
  11. # * pull/$PULL/local-merge (our merge)
  12. # In case of a clean merge that is accepted by the user, the local branch with
  13. # name $BRANCH is overwritten with the merged result, and optionally pushed.
  14. from __future__ import division,print_function,unicode_literals
  15. import os,sys
  16. from sys import stdin,stdout,stderr
  17. import argparse
  18. import subprocess
  19. # External tools (can be overridden using environment)
  20. GIT = os.getenv('GIT','git')
  21. BASH = os.getenv('BASH','bash')
  22. def git_config_get(option, default=None):
  23. '''
  24. Get named configuration option from git repository.
  25. '''
  26. try:
  27. return subprocess.check_output([GIT,'config','--get',option]).rstrip()
  28. except subprocess.CalledProcessError as e:
  29. return default
  30. def retrieve_pr_title(repo,pull):
  31. '''
  32. Retrieve pull request title from github.
  33. Return None if no title can be found, or an error happens.
  34. '''
  35. import urllib2,json
  36. try:
  37. req = urllib2.Request("https://api.github.com/repos/"+repo+"/pulls/"+pull)
  38. result = urllib2.urlopen(req)
  39. result = json.load(result)
  40. return result['title']
  41. except Exception as e:
  42. print('Warning: unable to retrieve pull title from github: %s' % e)
  43. return None
  44. def ask_prompt(text):
  45. print(text,end=" ",file=stderr)
  46. reply = stdin.readline().rstrip()
  47. print("",file=stderr)
  48. return reply
  49. def parse_arguments(branch):
  50. epilog = '''
  51. In addition, you can set the following git configuration variables:
  52. githubmerge.repository (mandatory),
  53. user.signingkey (mandatory),
  54. githubmerge.host (default: git@github.com),
  55. githubmerge.branch (default: master),
  56. githubmerge.testcmd (default: none).
  57. '''
  58. parser = argparse.ArgumentParser(description='Utility to merge, sign and push github pull requests',
  59. epilog=epilog)
  60. parser.add_argument('pull', metavar='PULL', type=int, nargs=1,
  61. help='Pull request ID to merge')
  62. parser.add_argument('branch', metavar='BRANCH', type=str, nargs='?',
  63. default=branch, help='Branch to merge against (default: '+branch+')')
  64. return parser.parse_args()
  65. def main():
  66. # Extract settings from git repo
  67. repo = git_config_get('githubmerge.repository')
  68. host = git_config_get('githubmerge.host','git@github.com')
  69. branch = git_config_get('githubmerge.branch','master')
  70. testcmd = git_config_get('githubmerge.testcmd')
  71. signingkey = git_config_get('user.signingkey')
  72. if repo is None:
  73. print("ERROR: No repository configured. Use this command to set:", file=stderr)
  74. print("git config githubmerge.repository <owner>/<repo>", file=stderr)
  75. exit(1)
  76. if signingkey is None:
  77. print("ERROR: No GPG signing key set. Set one using:",file=stderr)
  78. print("git config --global user.signingkey <key>",file=stderr)
  79. exit(1)
  80. host_repo = host+":"+repo # shortcut for push/pull target
  81. # Extract settings from command line
  82. args = parse_arguments(branch)
  83. pull = str(args.pull[0])
  84. branch = args.branch
  85. # Initialize source branches
  86. head_branch = 'pull/'+pull+'/head'
  87. base_branch = 'pull/'+pull+'/base'
  88. merge_branch = 'pull/'+pull+'/merge'
  89. local_merge_branch = 'pull/'+pull+'/local-merge'
  90. devnull = open(os.devnull,'w')
  91. try:
  92. subprocess.check_call([GIT,'checkout','-q',branch])
  93. except subprocess.CalledProcessError as e:
  94. print("ERROR: Cannot check out branch %s." % (branch), file=stderr)
  95. exit(3)
  96. try:
  97. subprocess.check_call([GIT,'fetch','-q',host_repo,'+refs/pull/'+pull+'/*:refs/heads/pull/'+pull+'/*'])
  98. except subprocess.CalledProcessError as e:
  99. print("ERROR: Cannot find pull request #%s on %s." % (pull,host_repo), file=stderr)
  100. exit(3)
  101. try:
  102. subprocess.check_call([GIT,'log','-q','-1','refs/heads/'+head_branch], stdout=devnull, stderr=stdout)
  103. except subprocess.CalledProcessError as e:
  104. print("ERROR: Cannot find head of pull request #%s on %s." % (pull,host_repo), file=stderr)
  105. exit(3)
  106. try:
  107. subprocess.check_call([GIT,'log','-q','-1','refs/heads/'+merge_branch], stdout=devnull, stderr=stdout)
  108. except subprocess.CalledProcessError as e:
  109. print("ERROR: Cannot find merge of pull request #%s on %s." % (pull,host_repo), file=stderr)
  110. exit(3)
  111. try:
  112. subprocess.check_call([GIT,'fetch','-q',host_repo,'+refs/heads/'+branch+':refs/heads/'+base_branch])
  113. except subprocess.CalledProcessError as e:
  114. print("ERROR: Cannot find branch %s on %s." % (branch,host_repo), file=stderr)
  115. exit(3)
  116. subprocess.check_call([GIT,'checkout','-q',base_branch])
  117. subprocess.call([GIT,'branch','-q','-D',local_merge_branch], stderr=devnull)
  118. subprocess.check_call([GIT,'checkout','-q','-b',local_merge_branch])
  119. try:
  120. # Create unsigned merge commit.
  121. title = retrieve_pr_title(repo,pull)
  122. if title:
  123. firstline = 'Merge #%s: %s' % (pull,title)
  124. else:
  125. firstline = 'Merge #%s' % (pull,)
  126. message = firstline + '\n\n'
  127. message += subprocess.check_output([GIT,'log','--no-merges','--topo-order','--pretty=format:%h %s (%an)',base_branch+'..'+head_branch])
  128. try:
  129. subprocess.check_call([GIT,'merge','-q','--commit','--no-edit','--no-ff','-m',message,head_branch])
  130. except subprocess.CalledProcessError as e:
  131. print("ERROR: Cannot be merged cleanly.",file=stderr)
  132. subprocess.check_call([GIT,'merge','--abort'])
  133. exit(4)
  134. logmsg = subprocess.check_output([GIT,'log','--pretty=format:%s','-n','1'])
  135. if logmsg.rstrip() != firstline.rstrip():
  136. print("ERROR: Creating merge failed (already merged?).",file=stderr)
  137. exit(4)
  138. # Run test command if configured.
  139. if testcmd:
  140. # Go up to the repository's root.
  141. toplevel = subprocess.check_output([GIT,'rev-parse','--show-toplevel'])
  142. os.chdir(toplevel)
  143. if subprocess.call(testcmd,shell=True):
  144. print("ERROR: Running %s failed." % testcmd,file=stderr)
  145. exit(5)
  146. # Show the created merge.
  147. diff = subprocess.check_output([GIT,'diff',merge_branch+'..'+local_merge_branch])
  148. subprocess.check_call([GIT,'diff',base_branch+'..'+local_merge_branch])
  149. if diff:
  150. print("WARNING: merge differs from github!",file=stderr)
  151. reply = ask_prompt("Type 'ignore' to continue.")
  152. if reply.lower() == 'ignore':
  153. print("Difference with github ignored.",file=stderr)
  154. else:
  155. exit(6)
  156. reply = ask_prompt("Press 'd' to accept the diff.")
  157. if reply.lower() == 'd':
  158. print("Diff accepted.",file=stderr)
  159. else:
  160. print("ERROR: Diff rejected.",file=stderr)
  161. exit(6)
  162. else:
  163. # Verify the result manually.
  164. print("Dropping you on a shell so you can try building/testing the merged source.",file=stderr)
  165. print("Run 'git diff HEAD~' to show the changes being merged.",file=stderr)
  166. print("Type 'exit' when done.",file=stderr)
  167. if os.path.isfile('/etc/debian_version'): # Show pull number on Debian default prompt
  168. os.putenv('debian_chroot',pull)
  169. subprocess.call([BASH,'-i'])
  170. reply = ask_prompt("Type 'm' to accept the merge.")
  171. if reply.lower() == 'm':
  172. print("Merge accepted.",file=stderr)
  173. else:
  174. print("ERROR: Merge rejected.",file=stderr)
  175. exit(7)
  176. # Sign the merge commit.
  177. reply = ask_prompt("Type 's' to sign off on the merge.")
  178. if reply == 's':
  179. try:
  180. subprocess.check_call([GIT,'commit','-q','--gpg-sign','--amend','--no-edit'])
  181. except subprocess.CalledProcessError as e:
  182. print("Error signing, exiting.",file=stderr)
  183. exit(1)
  184. else:
  185. print("Not signing off on merge, exiting.",file=stderr)
  186. exit(1)
  187. # Put the result in branch.
  188. subprocess.check_call([GIT,'checkout','-q',branch])
  189. subprocess.check_call([GIT,'reset','-q','--hard',local_merge_branch])
  190. finally:
  191. # Clean up temporary branches.
  192. subprocess.call([GIT,'checkout','-q',branch])
  193. subprocess.call([GIT,'branch','-q','-D',head_branch],stderr=devnull)
  194. subprocess.call([GIT,'branch','-q','-D',base_branch],stderr=devnull)
  195. subprocess.call([GIT,'branch','-q','-D',merge_branch],stderr=devnull)
  196. subprocess.call([GIT,'branch','-q','-D',local_merge_branch],stderr=devnull)
  197. # Push the result.
  198. reply = ask_prompt("Type 'push' to push the result to %s, branch %s." % (host_repo,branch))
  199. if reply.lower() == 'push':
  200. subprocess.check_call([GIT,'push',host_repo,'refs/heads/'+branch])
  201. if __name__ == '__main__':
  202. main()