-
Notifications
You must be signed in to change notification settings - Fork 15
/
git-review
executable file
·600 lines (483 loc) · 19 KB
/
git-review
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#!/usr/bin/env python
COPYRIGHT = """\
Copyright (C) 2011 OpenStack LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
See the License for the specific language governing permissions and
limitations under the License."""
import optparse
import urllib
import json
from distutils.version import StrictVersion
from urlparse import urlparse
import ConfigParser
import os
import sys
import time
import re
import shlex
import subprocess
version = "1.7"
VERBOSE = False
UPDATE = False
CONFIGDIR = os.path.expanduser("~/.config/git-review")
PYPI_URL = "http://pypi.python.org/pypi/git-review/json"
PYPI_CACHE_TIME = 60 * 60 * 24 # 24 hours
_branch_name = None
_has_color = None
def run_command(cmd, status=False, env={}):
if VERBOSE:
print "Running:", cmd
cmd_list = shlex.split(cmd)
newenv = os.environ
newenv.update(env)
p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, env=newenv)
(out, nothing) = p.communicate()
if status:
return (p.returncode, out.strip())
return out.strip()
def run_command_status(cmd, env={}):
return run_command(cmd, True, env)
def update_latest_version(version_file_path):
""" Cache the latest version of git-review for the upgrade check. """
if not os.path.exists(CONFIGDIR):
os.makedirs(CONFIGDIR)
if os.path.exists(version_file_path) and not UPDATE:
if (time.time() - os.path.getmtime(version_file_path)) < 28800:
return
latest_version = version
try:
latest_version = json.load(urllib.urlopen(PYPI_URL))['info']['version']
except:
pass
with open(version_file_path, "w") as version_file:
version_file.write(latest_version)
def latest_is_newer():
""" Check if there is a new version of git-review. """
version_file_path = os.path.join(CONFIGDIR, "latest-version")
update_latest_version(version_file_path)
latest_version = None
with open(version_file_path, "r") as version_file:
latest_version = StrictVersion(version_file.read())
if latest_version > StrictVersion(version):
return True
return False
def set_hooks_commit_msg(remote):
""" Install the commit message hook if needed. """
top_dir = run_command('git rev-parse --show-toplevel')
hooks_dir = os.path.join(top_dir, ".git/hooks")
target_file = os.path.join(hooks_dir, "commit-msg")
if os.path.exists(target_file) and os.access(target_file, os.X_OK):
return True
# Create the hooks directory if it's not there already
if not os.path.isdir(hooks_dir):
os.mkdir(hooks_dir)
(hostname, team, username, port, project_name) = \
parse_git_show(remote, "Push")
if not os.path.exists(target_file) or UPDATE:
if VERBOSE:
print "Fetching commit hook from: scp://%s:%s" % (hostname, port)
scp_cmd = "scp "
if port is not None:
scp_cmd += "-P%s " % port
if username is None:
scp_cmd += hostname
else:
scp_cmd += "%s@%s" % (username, hostname)
scp_cmd += ":hooks/commit-msg %s" % target_file
(status, scp_output) = run_command_status(scp_cmd)
if status != 0:
print "Problems encountered installing commit-msg hook"
print scp_output
return False
if not os.access(target_file, os.X_OK):
os.chmod(target_file, os.path.stat.S_IREAD | os.path.stat.S_IEXEC)
run_command("git commit --amend", env=dict(GIT_EDITOR='true'))
return True
def test_remote(username, hostname, port, project):
""" Tests that a possible gerrit remote works """
if username is None:
ssh_cmd = "ssh -p%s %s gerrit ls-projects"
cmd = ssh_cmd % (port, hostname)
else:
ssh_cmd = "ssh -p%s %s@%s gerrit ls-projects"
cmd = ssh_cmd % (port, username, hostname)
(status, ssh_output) = run_command_status(cmd)
if status == 0:
if VERBOSE:
print "%s@%s:%s worked." % (username, hostname, port)
return True
else:
if VERBOSE:
print "%s@%s:%s did not work." % (username, hostname, port)
return False
def make_remote_url(username, hostname, port, project):
""" Builds a gerrit remote URL """
if username is None:
return "ssh://%s:%s/%s" % (hostname, port, project)
else:
return "ssh://%s@%s:%s/%s" % (username, hostname, port, project)
def add_remote(username, hostname, port, project):
""" Adds a gerrit remote. """
asked_for_username = False
if not username:
username = os.getenv("USERNAME")
if not username:
username = run_command('git config --get gitreview.username')
if not username:
username = os.getenv("USER")
if port is None:
port = 29418
remote_url = make_remote_url(username, hostname, port, project)
if VERBOSE:
print "No remote set, testing %s" % remote_url
if not test_remote(username, hostname, port, project):
print "Could not connect to gerrit."
username = raw_input("Enter your gerrit username: ")
remote_url = make_remote_url(username, hostname, port, project)
print "Trying again with %s" % remote_url
if not test_remote(username, hostname, port, project):
raise Exception("Could not connect to gerrit at %s" % remote_url)
asked_for_username = True
print "Creating a git remote called gerrit that maps to:"
print "\t%s" % remote_url
cmd = "git remote add -f gerrit %s" % remote_url
(status, remote_output) = run_command_status(cmd)
if status != 0:
raise Exception("Error running %s" % cmd)
if asked_for_username:
print
print "This repository is now set up for use with git-review."
print "You can set the default username for future repositories with:"
print ' git config --global --add gitreview.username "%s"' % username
print
def split_hostname(fetch_url):
parsed_url = urlparse(fetch_url)
username = None
hostname = parsed_url.netloc
port = 22
# Workaround bug in urlparse on OSX
if parsed_url.scheme == "ssh" and hostname[:2] == "//":
hostname = hostname[2:]
hostname = hostname.split("/")[0]
if "@" in hostname:
(username, hostname) = hostname.split("@")
if ":" in hostname:
(hostname, port) = hostname.split(":")
# Is origin an ssh location? Let's pull more info
if parsed_url.scheme == "ssh":
return (username, hostname, port)
else:
return (None, hostname, None)
def parse_git_show(remote, verb):
fetch_url = ""
for line in run_command("git remote show -n %s" % remote).split("\n"):
if line.strip().startswith("%s" % verb):
fetch_url = ":".join(line.split(":")[1:]).strip()
project_name = fetch_url.split("/")[-1]
if project_name.endswith(".git"):
project_name = project_name[:-4]
hostname = None
team = None
username = None
port = None
if VERBOSE:
print "Found origin %s URL:" % verb, fetch_url
# Special-case git@github urls - the rest can be parsed with urlparse
if fetch_url.startswith("git@github.com"):
hostname = "github.com"
else:
(username, hostname, port) = split_hostname(fetch_url)
if hostname == "github.com":
team = fetch_url.split("/")[-2]
if team.startswith("git@github.com"):
team = team.split(':')[1]
return (hostname, team, username, port, project_name)
def git_config_get_value(section, option):
cmd = "git config --get %s.%s" % (section, option)
return run_command(cmd).strip()
def check_color_support():
global _has_color
if _has_color is None:
test_command = "git log --color=never --oneline HEAD^1..HEAD"
(status, output) = run_command_status(test_command)
if status == 0:
_has_color = True
else:
_has_color = False
return _has_color
def check_remote(remote):
"""Check that a Gerrit Git remote repo exists, if not, set one."""
has_color = check_color_support()
if has_color:
color_never = "--color=never"
else:
color_never = ""
if remote in run_command("git remote").split("\n"):
remotes = run_command("git branch -a %s" % color_never).split("\n")
for current_remote in remotes:
if current_remote.strip() == "remotes/%s/master" % (remote) \
and not UPDATE:
return
# We have the remote, but aren't set up to fetch. Fix it
if VERBOSE:
print "Setting up gerrit branch tracking for better rebasing"
output = run_command("git remote update %s" % remote)
if VERBOSE:
print output
return
# Check for a .gitreview at the top of the repo with the gerrit location
top_dir = run_command('git rev-parse --show-toplevel')
target_file = os.path.join(top_dir, ".gitreview")
if os.path.exists(target_file):
config = ConfigParser.ConfigParser(dict(port='29418'))
config.read(target_file)
hostname = config.get("gerrit", "host")
port = config.get("gerrit", "port")
project = config.get("gerrit", "project")
username = None
else:
print "No '.gitreview' file found in this repository."
print "We don't know where your gerrit is. Please manually create "
print "a remote named gerrit and try again."
sys.exit(1)
# Gerrit remote not present, try to add it
try:
add_remote(username, hostname, port, project)
except:
print sys.exc_info()[2]
print "We don't know where your gerrit is. Please manually create "
print "a remote named gerrit and try again."
raise
def rebase_changes(branch, remote):
remote_branch = "remotes/%s/%s" % (remote, branch)
cmd = "git remote update %s" % remote
(status, output) = run_command_status(cmd)
if VERBOSE:
print output
if status != 0:
print "Problem running '%s'" % cmd
if not VERBOSE:
print output
return False
cmd = "git rebase -i %s" % remote_branch
(status, output) = run_command_status(cmd, env=dict(GIT_EDITOR='true'))
if status != 0:
print "Errors running %s" % cmd
print output
return False
return True
def get_branch_name(target_branch):
global _branch_name
if _branch_name is not None:
return _branch_name
_branch_name = None
has_color = check_color_support()
if has_color:
color_never = "--color=never"
else:
color_never = ""
for branch in run_command("git branch %s" % color_never).split("\n"):
if branch.startswith('*'):
_branch_name = branch.split()[1].strip()
if _branch_name == "(no":
_branch_name = target_branch
return _branch_name
def assert_one_change(remote, branch, yes):
branch_name = get_branch_name(branch)
has_color = check_color_support()
if has_color:
color = git_config_get_value("color", "ui")
if color == "":
color = "auto"
elif color == "auto":
# Python is not a tty, we have to force colors
color = "always"
use_color = "--color=%s" % color
else:
use_color = ""
cmd = "git log %s --decorate --oneline %s --not remotes/%s/%s" % \
(use_color, branch_name, remote, branch)
(status, output) = run_command_status(cmd)
if status != 0:
print "Had trouble running %s" % cmd
print output
sys.exit(1)
output_lines = len(output.split("\n"))
if output_lines == 0:
print "No changes between HEAD and %s/%s." % (remote, branch)
print "Submitting for review would be pointless."
sys.exit(1)
if output_lines > 1:
if not yes:
print "You have more than one commit that you are about to submit."
print "The outstanding commits are:\n\n%s\n" % output
print "Is this really what you meant to do?"
yes_no = raw_input("Type 'yes' to confirm: ")
if yes_no.lower().strip() != "yes":
print "Aborting."
print "Please rebase/squash your changes and try again"
sys.exit(1)
def get_topic(target_branch):
branch_name = get_branch_name(target_branch)
branch_parts = branch_name.split("/")
if len(branch_parts) >= 3 and branch_parts[0] == "review":
return "/".join(branch_parts[2:])
log_output = run_command("git show --format='%s %b'")
bug_re = r'\b([Bb]ug|[Ll][Pp])\s*[#:]?\s*(\d+)'
match = re.search(bug_re, log_output)
if match is not None:
return "bug/%s" % match.group(2)
bp_re = r'\b([Bb]lue[Pp]rint|[Bb][Pp])\s*[#:]?\s*([0-9a-zA-Z-_]+)'
match = re.search(bp_re, log_output)
if match is not None:
return "bp/%s" % match.group(2)
return branch_name
def download_review(review):
(hostname, team, username, port, project_name) = \
parse_git_show("gerrit", "Push")
ssh_cmds = ["ssh"]
if port is not None:
ssh_cmds.extend(["-p", port])
if username is not None:
ssh_cmds.extend(["-l", username])
ssh_cmd = " ".join(ssh_cmds)
query_string = "--format=JSON --current-patch-set change:%s" % review
review_info = None
(status, output) = run_command_status("%s %s gerrit query %s"
% (ssh_cmd, hostname, query_string))
if status != 0:
print "Could not fetch review information from gerrit"
print output
return status
try:
review_info = json.loads(output.split("\n")[0])
except:
if VERBOSE:
print output
print "Could not find a gerrit review with id: %s" % review
return 1
try:
topic = review_info['topic']
if topic == "master":
topic = review
except KeyError:
topic = review
author = re.sub('\W+', '_', review_info['owner']['name']).lower()
branch_name = "review/%s/%s" % (author, topic)
revision = review_info['currentPatchSet']['revision']
refspec = review_info['currentPatchSet']['ref']
print "Downloading %s from gerrit into %s" % (refspec, branch_name)
(status, output) = run_command_status("git fetch gerrit %s" % refspec)
if status != 0:
print output
return status
checkout_cmd = "git checkout -b %s FETCH_HEAD" % branch_name
(status, output) = run_command_status(checkout_cmd)
if status != 0:
if output.endswith("already exists"):
print "Branch already exists - reusing"
checkout_cmd = "git checkout %s" % branch_name
(status, output) = run_command_status(checkout_cmd)
if status != 0:
print output
return status
reset_cmd = "git reset --hard FETCH_HEAD"
(status, output) = run_command_status(reset_cmd)
if status != 0:
print output
return status
else:
print output
return status
return 0
def print_exit_message(status, needs_update):
if needs_update:
print """
***********************************************************
A new version of git-review is availble on PyPI. Please
update your copy with:
pip install -U git-review
to ensure proper behavior with gerrit. Thanks!
***********************************************************
"""
sys.exit(status)
def main():
usage = "git review [OPTIONS] ... [BRANCH]"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-t", "--topic", dest="topic",
help="Topic to submit branch to")
parser.add_option("-n", "--dry-run", dest="dry", action="store_true",
help="Don't actually submit the branch for review")
parser.add_option("-r", "--remote", dest="remote",
help="git remote to use for gerrit")
parser.add_option("-R", "--no-rebase", dest="rebase",
action="store_false",
help="Don't rebase changes before submitting.")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
help="Output more information about what's going on")
parser.add_option("-d", "--download", dest="download",
help="Download the contents of an existing gerrit "
"review into a branch")
parser.add_option("-u", "--update", dest="update", action="store_true",
help="Force updates from remote locations")
parser.add_option("-s", "--setup", dest="setup", action="store_true",
help="Just run the repo setup commands but don't "
"submit anything")
parser.add_option("-y", "--yes", dest="yes", action="store_true",
help="Indicate that you do, in fact, understand if you "
"are submitting more than one patch")
parser.add_option("--version", dest="version", action="store_true",
help="Print version number and exit")
parser.set_defaults(dry=False, rebase=True, verbose=False, update=False,
setup=False, version=False, yes=False, remote="gerrit")
branch = "master"
(options, args) = parser.parse_args()
if len(args) > 0:
branch = args[0]
global VERBOSE
global UPDATE
VERBOSE = options.verbose
UPDATE = options.update
remote = options.remote
yes = options.yes
status = 0
if options.version:
print '%s, version %s' % (os.path.split(sys.argv[0])[-1], version)
print COPYRIGHT
sys.exit(0)
needs_update = latest_is_newer()
check_remote(remote)
if options.download is not None:
print_exit_message(download_review(options.download), needs_update)
else:
topic = options.topic
if topic is None:
topic = get_topic(branch)
if VERBOSE:
print "Found topic '%s' from parsing changes." % topic
if not set_hooks_commit_msg(remote):
print_exit_message(1, needs_update)
if not options.setup:
if options.rebase:
if not rebase_changes(branch, remote):
print_exit_message(1, needs_update)
assert_one_change(remote, branch, yes)
cmd = "git push %s HEAD:refs/for/%s/%s" % (remote, branch, topic)
if options.dry:
print "Please use the following command " \
"to send your commits to review:\n"
print "\t%s\n" % cmd
else:
(status, output) = run_command_status(cmd)
print output
print_exit_message(status, needs_update)
if __name__ == "__main__":
main()