2 # Copyright (c) 2016-2017 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.
6 # This script will locally construct a merge commit for a pull request on a
7 # github repository, inspect it, sign it and optionally push it.
9 # The following temporary branches are created/overwritten and deleted:
10 # * pull/$PULL/base (the current master we're merging onto)
11 # * pull/$PULL/head (the current state of the remote pull request)
12 # * pull/$PULL/merge (github's merge)
13 # * pull/$PULL/local-merge (our merge)
15 # In case of a clean merge that is accepted by the user, the local branch with
16 # name $BRANCH is overwritten with the merged result, and optionally pushed.
17 from __future__
import division
,print_function
,unicode_literals
19 from sys
import stdin
,stdout
,stderr
26 from urllib
.request
import Request
,urlopen
28 from urllib2
import Request
,urlopen
30 # External tools (can be overridden using environment)
31 GIT
= os
.getenv('GIT','git')
32 BASH
= os
.getenv('BASH','bash')
34 # OS specific configuration for terminal attributes
37 COMMIT_FORMAT
= '%h %s (%an)%d'
38 if os
.name
== 'posix': # if posix, assume we can use basic terminal escapes
39 ATTR_RESET
= '\033[0m'
40 ATTR_PR
= '\033[1;36m'
41 COMMIT_FORMAT
= '%C(bold blue)%h%Creset %s %C(cyan)(%an)%Creset%C(green)%d%Creset'
43 def git_config_get(option
, default
=None):
45 Get named configuration option from git repository.
48 return subprocess
.check_output([GIT
,'config','--get',option
]).rstrip().decode('utf-8')
49 except subprocess
.CalledProcessError
as e
:
52 def retrieve_pr_info(repo
,pull
):
54 Retrieve pull request information from github.
55 Return None if no title can be found, or an error happens.
58 req
= Request("https://api.github.com/repos/"+repo
+"/pulls/"+pull
)
60 reader
= codecs
.getreader('utf-8')
61 obj
= json
.load(reader(result
))
63 except Exception as e
:
64 print('Warning: unable to retrieve pull information from github: %s' % e
)
68 print(text
,end
=" ",file=stderr
)
70 reply
= stdin
.readline().rstrip()
74 def get_symlink_files():
75 files
= sorted(subprocess
.check_output([GIT
, 'ls-tree', '--full-tree', '-r', 'HEAD']).splitlines())
78 if (int(f
.decode('utf-8').split(" ")[0], 8) & 0o170000) == 0o120000:
79 ret
.append(f
.decode('utf-8').split("\t")[1])
82 def tree_sha512sum(commit
='HEAD'):
83 # request metadata for entire tree, recursively
86 for line
in subprocess
.check_output([GIT
, 'ls-tree', '--full-tree', '-r', commit
]).splitlines():
87 name_sep
= line
.index(b
'\t')
88 metadata
= line
[:name_sep
].split() # perms, 'blob', blobid
89 assert(metadata
[1] == b
'blob')
90 name
= line
[name_sep
+1:]
92 blob_by_name
[name
] = metadata
[2]
95 # open connection to git-cat-file in batch mode to request data for all blobs
96 # this is much faster than launching it per file
97 p
= subprocess
.Popen([GIT
, 'cat-file', '--batch'], stdout
=subprocess
.PIPE
, stdin
=subprocess
.PIPE
)
98 overall
= hashlib
.sha512()
100 blob
= blob_by_name
[f
]
102 p
.stdin
.write(blob
+ b
'\n')
104 # read header: blob, "blob", size
105 reply
= p
.stdout
.readline().split()
106 assert(reply
[0] == blob
and reply
[1] == b
'blob')
109 intern = hashlib
.sha512()
112 bs
= min(65536, size
- ptr
)
113 piece
= p
.stdout
.read(bs
)
117 raise IOError('Premature EOF reading git cat-file output')
119 dig
= intern.hexdigest()
120 assert(p
.stdout
.read(1) == b
'\n') # ignore LF that follows blob data
121 # update overall hash with file hash
122 overall
.update(dig
.encode("utf-8"))
123 overall
.update(" ".encode("utf-8"))
125 overall
.update("\n".encode("utf-8"))
128 raise IOError('Non-zero return value executing git cat-file')
129 return overall
.hexdigest()
131 def print_merge_details(pull
, title
, branch
, base_branch
, head_branch
):
132 print('%s#%s%s %s %sinto %s%s' % (ATTR_RESET
+ATTR_PR
,pull
,ATTR_RESET
,title
,ATTR_RESET
+ATTR_PR
,branch
,ATTR_RESET
))
133 subprocess
.check_call([GIT
,'log','--graph','--topo-order','--pretty=format:'+COMMIT_FORMAT
,base_branch
+'..'+head_branch
])
135 def parse_arguments():
137 In addition, you can set the following git configuration variables:
138 githubmerge.repository (mandatory),
139 user.signingkey (mandatory),
140 githubmerge.host (default: git@github.com),
141 githubmerge.branch (no default),
142 githubmerge.testcmd (default: none).
144 parser
= argparse
.ArgumentParser(description
='Utility to merge, sign and push github pull requests',
146 parser
.add_argument('pull', metavar
='PULL', type=int, nargs
=1,
147 help='Pull request ID to merge')
148 parser
.add_argument('branch', metavar
='BRANCH', type=str, nargs
='?',
149 default
=None, help='Branch to merge against (default: githubmerge.branch setting, or base branch for pull, or \'master\')')
150 return parser
.parse_args()
153 # Extract settings from git repo
154 repo
= git_config_get('githubmerge.repository')
155 host
= git_config_get('githubmerge.host','git@github.com')
156 opt_branch
= git_config_get('githubmerge.branch',None)
157 testcmd
= git_config_get('githubmerge.testcmd')
158 signingkey
= git_config_get('user.signingkey')
160 print("ERROR: No repository configured. Use this command to set:", file=stderr
)
161 print("git config githubmerge.repository <owner>/<repo>", file=stderr
)
163 if signingkey
is None:
164 print("ERROR: No GPG signing key set. Set one using:",file=stderr
)
165 print("git config --global user.signingkey <key>",file=stderr
)
168 host_repo
= host
+":"+repo
# shortcut for push/pull target
170 # Extract settings from command line
171 args
= parse_arguments()
172 pull
= str(args
.pull
[0])
174 # Receive pull information from github
175 info
= retrieve_pr_info(repo
,pull
)
178 title
= info
['title'].strip()
179 body
= info
['body'].strip()
180 # precedence order for destination branch argument:
181 # - command line argument
182 # - githubmerge.branch setting
183 # - base branch for pull (as retrieved from github)
185 branch
= args
.branch
or opt_branch
or info
['base']['ref'] or 'master'
187 # Initialize source branches
188 head_branch
= 'pull/'+pull
+'/head'
189 base_branch
= 'pull/'+pull
+'/base'
190 merge_branch
= 'pull/'+pull
+'/merge'
191 local_merge_branch
= 'pull/'+pull
+'/local-merge'
193 devnull
= open(os
.devnull
,'w')
195 subprocess
.check_call([GIT
,'checkout','-q',branch
])
196 except subprocess
.CalledProcessError
as e
:
197 print("ERROR: Cannot check out branch %s." % (branch
), file=stderr
)
200 subprocess
.check_call([GIT
,'fetch','-q',host_repo
,'+refs/pull/'+pull
+'/*:refs/heads/pull/'+pull
+'/*',
201 '+refs/heads/'+branch
+':refs/heads/'+base_branch
])
202 except subprocess
.CalledProcessError
as e
:
203 print("ERROR: Cannot find pull request #%s or branch %s on %s." % (pull
,branch
,host_repo
), file=stderr
)
206 subprocess
.check_call([GIT
,'log','-q','-1','refs/heads/'+head_branch
], stdout
=devnull
, stderr
=stdout
)
207 except subprocess
.CalledProcessError
as e
:
208 print("ERROR: Cannot find head of pull request #%s on %s." % (pull
,host_repo
), file=stderr
)
211 subprocess
.check_call([GIT
,'log','-q','-1','refs/heads/'+merge_branch
], stdout
=devnull
, stderr
=stdout
)
212 except subprocess
.CalledProcessError
as e
:
213 print("ERROR: Cannot find merge of pull request #%s on %s." % (pull
,host_repo
), file=stderr
)
215 subprocess
.check_call([GIT
,'checkout','-q',base_branch
])
216 subprocess
.call([GIT
,'branch','-q','-D',local_merge_branch
], stderr
=devnull
)
217 subprocess
.check_call([GIT
,'checkout','-q','-b',local_merge_branch
])
220 # Go up to the repository's root.
221 toplevel
= subprocess
.check_output([GIT
,'rev-parse','--show-toplevel']).strip()
223 # Create unsigned merge commit.
225 firstline
= 'Merge #%s: %s' % (pull
,title
)
227 firstline
= 'Merge #%s' % (pull
,)
228 message
= firstline
+ '\n\n'
229 message
+= subprocess
.check_output([GIT
,'log','--no-merges','--topo-order','--pretty=format:%h %s (%an)',base_branch
+'..'+head_branch
]).decode('utf-8')
230 message
+= '\n\nPull request description:\n\n ' + body
.replace('\n', '\n ') + '\n'
232 subprocess
.check_call([GIT
,'merge','-q','--commit','--no-edit','--no-ff','-m',message
.encode('utf-8'),head_branch
])
233 except subprocess
.CalledProcessError
as e
:
234 print("ERROR: Cannot be merged cleanly.",file=stderr
)
235 subprocess
.check_call([GIT
,'merge','--abort'])
237 logmsg
= subprocess
.check_output([GIT
,'log','--pretty=format:%s','-n','1']).decode('utf-8')
238 if logmsg
.rstrip() != firstline
.rstrip():
239 print("ERROR: Creating merge failed (already merged?).",file=stderr
)
242 symlink_files
= get_symlink_files()
243 for f
in symlink_files
:
244 print("ERROR: File %s was a symlink" % f
)
245 if len(symlink_files
) > 0:
248 # Put tree SHA512 into the message
250 first_sha512
= tree_sha512sum()
251 message
+= '\n\nTree-SHA512: ' + first_sha512
252 except subprocess
.CalledProcessError
as e
:
253 print("ERROR: Unable to compute tree hash")
256 subprocess
.check_call([GIT
,'commit','--amend','-m',message
.encode('utf-8')])
257 except subprocess
.CalledProcessError
as e
:
258 print("ERROR: Cannot update message.", file=stderr
)
261 print_merge_details(pull
, title
, branch
, base_branch
, head_branch
)
264 # Run test command if configured.
266 if subprocess
.call(testcmd
,shell
=True):
267 print("ERROR: Running %s failed." % testcmd
,file=stderr
)
270 # Show the created merge.
271 diff
= subprocess
.check_output([GIT
,'diff',merge_branch
+'..'+local_merge_branch
])
272 subprocess
.check_call([GIT
,'diff',base_branch
+'..'+local_merge_branch
])
274 print("WARNING: merge differs from github!",file=stderr
)
275 reply
= ask_prompt("Type 'ignore' to continue.")
276 if reply
.lower() == 'ignore':
277 print("Difference with github ignored.",file=stderr
)
281 # Verify the result manually.
282 print("Dropping you on a shell so you can try building/testing the merged source.",file=stderr
)
283 print("Run 'git diff HEAD~' to show the changes being merged.",file=stderr
)
284 print("Type 'exit' when done.",file=stderr
)
285 if os
.path
.isfile('/etc/debian_version'): # Show pull number on Debian default prompt
286 os
.putenv('debian_chroot',pull
)
287 subprocess
.call([BASH
,'-i'])
289 second_sha512
= tree_sha512sum()
290 if first_sha512
!= second_sha512
:
291 print("ERROR: Tree hash changed unexpectedly",file=stderr
)
294 # Sign the merge commit.
295 print_merge_details(pull
, title
, branch
, base_branch
, head_branch
)
297 reply
= ask_prompt("Type 's' to sign off on the above merge, or 'x' to reject and exit.").lower()
300 subprocess
.check_call([GIT
,'commit','-q','--gpg-sign','--amend','--no-edit'])
302 except subprocess
.CalledProcessError
as e
:
303 print("Error while signing, asking again.",file=stderr
)
305 print("Not signing off on merge, exiting.",file=stderr
)
308 # Put the result in branch.
309 subprocess
.check_call([GIT
,'checkout','-q',branch
])
310 subprocess
.check_call([GIT
,'reset','-q','--hard',local_merge_branch
])
312 # Clean up temporary branches.
313 subprocess
.call([GIT
,'checkout','-q',branch
])
314 subprocess
.call([GIT
,'branch','-q','-D',head_branch
],stderr
=devnull
)
315 subprocess
.call([GIT
,'branch','-q','-D',base_branch
],stderr
=devnull
)
316 subprocess
.call([GIT
,'branch','-q','-D',merge_branch
],stderr
=devnull
)
317 subprocess
.call([GIT
,'branch','-q','-D',local_merge_branch
],stderr
=devnull
)
321 reply
= ask_prompt("Type 'push' to push the result to %s, branch %s, or 'x' to exit without pushing." % (host_repo
,branch
)).lower()
323 subprocess
.check_call([GIT
,'push',host_repo
,'refs/heads/'+branch
])
328 if __name__
== '__main__':