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 10KB

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