forked from jhansche/git-review
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-review
executable file
·924 lines (736 loc) · 28.8 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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
#!/usr/bin/env python
from __future__ import print_function
COPYRIGHT = """\
Copyright (C) 2011-2012 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 datetime
import json
import os
import re
import shlex
import subprocess
import sys
import time
if sys.version < '3':
from urllib import urlopen
from urlparse import urlparse
import ConfigParser
do_input = raw_input
else:
from urllib.request import urlopen
from urllib.parse import urlparse
import configparser as ConfigParser
do_input = input
from distutils.version import StrictVersion
version = "1.19"
VERBOSE = False
UPDATE = False
CONFIGDIR = os.path.expanduser("~/.config/git-review")
GLOBAL_CONFIG = "/etc/git-review/git-review.conf"
PYPI_URL = "http://pypi.python.org/pypi/git-review/json"
PYPI_CACHE_TIME = 60 * 60 * 24 # 24 hours
_branch_name = None
_has_color = None
class colors:
yellow = '\033[33m'
green = '\033[92m'
reset = '\033[0m'
class GitReviewException(Exception):
pass
class CommandFailed(GitReviewException):
def __init__(self, *args):
Exception.__init__(self, *args)
(rc, output, argv, envp) = args
self.quickmsg = dict([
("argv", " ".join(argv)),
("rc", rc),
("output", output)])
def __str__(self):
return self.__doc__ + """
The following command failed with exit code %(rc)d
"%(argv)s"
-----------------------
%(output)s
-----------------------""" % self.quickmsg
class ChangeSetException(GitReviewException):
def __init__(self, e):
GitReviewException.__init__(self)
self.e = str(e)
def __str__(self):
return self.__doc__ % self.e
def run_command_status(*argv, **env):
if VERBOSE:
print(datetime.datetime.now(), "Running:", " ".join(argv))
if len(argv) == 1:
argv = shlex.split(str(argv[0]))
newenv = os.environ
newenv.update(env)
p = subprocess.Popen(argv, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, env=newenv)
(out, nothing) = p.communicate()
out = out.decode('utf-8')
return (p.returncode, out.strip())
def run_command(*argv, **env):
(rc, output) = run_command_status(*argv, **env)
return output
def run_command_exc(klazz, *argv, **env):
"""Run command *argv, on failure raise 'klazz
klass should be derived from CommandFailed"""
(rc, output) = run_command_status(*argv, **env)
if rc != 0:
raise klazz(rc, output, argv, env)
return output
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(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. """
# Skip version check if distro package turns it off
if os.path.exists(GLOBAL_CONFIG):
config = dict(check=False)
configParser = ConfigParser.ConfigParser(config)
configParser.read(GLOBAL_CONFIG)
if not configParser.getboolean("updates", "check"):
return False
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 get_hooks_target_file():
top_dir = run_command('git rev-parse --git-dir')
hooks_dir = os.path.join(top_dir, "hooks")
return os.path.join(hooks_dir, "commit-msg")
class CannotInstallHook(CommandFailed):
"Problems encountered installing commit-msg hook"
EXIT_CODE = 2
def set_hooks_commit_msg(remote, target_file):
""" Install the commit message hook if needed. """
# Create the hooks directory if it's not there already
hooks_dir = os.path.dirname(target_file)
if not os.path.isdir(hooks_dir):
os.mkdir(hooks_dir)
(hostname, 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))
if port is not None:
port = "-P %s" % port
else:
port = ""
if username is None:
userhost = hostname
else:
userhost = "%s@%s" % (username, hostname)
run_command_exc(
CannotInstallHook,
"scp", port,
userhost + ":hooks/commit-msg",
target_file)
if not os.access(target_file, os.X_OK):
os.chmod(target_file, os.path.stat.S_IREAD | os.path.stat.S_IEXEC)
def test_remote(username, hostname, port, project):
""" Tests that a possible gerrit remote works """
if port is not None:
port = "-p %s" % port
else:
port = ""
if username is None:
userhost = hostname
else:
userhost = "%s@%s" % (username, hostname)
(status, ssh_output) = run_command_status(
"ssh", "-x", port, userhost,
"gerrit", "ls-projects")
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(hostname, port, project, remote):
""" Adds a gerrit remote. """
asked_for_username = False
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 = do_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 \"%s\" that maps to:" % remote)
print("\t%s" % remote_url)
cmd = "git remote add -f %s %s" % (remote, 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 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 = line.split()[2]
parsed_url = urlparse(fetch_url)
project_name = parsed_url.path.lstrip("/")
if project_name.endswith(".git"):
project_name = project_name[:-4]
hostname = parsed_url.netloc
username = None
port = parsed_url.port
if VERBOSE:
print("Found origin %s URL:" % verb, fetch_url)
# Workaround bug in urlparse on OSX
if parsed_url.scheme == "ssh" and parsed_url.path[:2] == "//":
hostname = parsed_url.path[2:].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" and port is None:
port = 22
return (hostname, username, str(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 get_config():
"""Get the configuration options set in the .gitremote file, if present."""
"""Returns a hashmap with hostname, port, project and defaultbranch."""
"""If there is no .gitremote file, default values will be used."""
config = dict(hostname=False, port='29418', project=False,
defaultbranch='master', defaultremote="gerrit",
defaultrebase="1")
top_dir = run_command('git rev-parse --show-toplevel')
target_file = os.path.join(top_dir, ".gitreview")
if os.path.exists(target_file):
configParser = ConfigParser.ConfigParser(config)
configParser.read(target_file)
config['hostname'] = configParser.get("gerrit", "host")
config['port'] = configParser.get("gerrit", "port")
config['project'] = configParser.get("gerrit", "project")
config['defaultbranch'] = configParser.get("gerrit", "defaultbranch")
if configParser.has_option("gerrit", "defaultremote"):
config['defaultremote'] = configParser.get("gerrit",
"defaultremote")
if configParser.has_option("gerrit", "defaultrebase"):
config['defaultrebase'] = configParser.getboolean("gerrit",
"defaultrebase")
return config
def update_remote(remote):
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
return True
def check_remote(branch, remote, hostname, port, project):
"""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/%s" % (remote, branch)
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")
update_remote(remote)
return
if hostname is False or port is False or project is False:
# This means there was no .gitreview file
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(hostname, port, project, remote)
except:
print(sys.exc_info()[2])
print("We don't know where your gerrit is. Please manually create ")
print("a remote named \"%s\" and try again." % remote)
raise
def rebase_changes(branch, remote):
remote_branch = "remotes/%s/%s" % (remote, branch)
if not update_remote(remote):
return False
cmd = "git rebase -i %s" % remote_branch
(status, output) = run_command_status(cmd, GIT_EDITOR='true')
if status != 0:
print("Errors running %s" % cmd)
print(output)
return False
return True
def undo_rebase():
cmd = "git reset --hard ORIG_HEAD"
(status, output) = run_command_status(cmd)
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, have_hook):
update_remote(remote)
branch_name = get_branch_name(branch)
has_color = check_color_support()
if has_color:
color = git_config_get_value("color", "ui").lower()
if(color == "" or color == "true"):
color = "auto"
elif color == "false":
color = "never"
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 == 1 and not have_hook:
print("Your change was committed before the commit hook was installed")
print("Amending the commit to add a gerrit change id")
run_command("git commit --amend", GIT_EDITOR='true')
elif output_lines == 0:
print("No changes between HEAD and %s/%s." % (remote, branch))
print("Submitting for review would be pointless.")
sys.exit(1)
elif 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 = do_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 log HEAD^1..HEAD")
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
class CannotQueryOpenChangesets(CommandFailed):
"Cannot fetch review information from gerrit"
EXIT_CODE = 32
class CannotParseOpenChangesets(ChangeSetException):
"Cannot parse JSON review information from gerrit"
EXIT_CODE = 33
def list_reviews(remote):
(hostname, username, port, project_name) = \
parse_git_show(remote, "Push")
if port is not None:
port = "-p %s" % port
else:
port = ""
if username is None:
userhost = hostname
else:
userhost = "%s@%s" % (username, hostname)
review_info = None
output = run_command_exc(
CannotQueryOpenChangesets,
"ssh", "-x", port, userhost,
"gerrit", "query",
"--format=JSON project:%s status:open" % project_name)
review_list = []
review_field_width = {}
REVIEW_FIELDS = ('number', 'branch', 'subject')
FIELDS = range(0, len(REVIEW_FIELDS))
if check_color_support():
review_field_color = (colors.yellow, colors.green, "")
color_reset = colors.reset
else:
review_field_color = ("", "", "")
color_reset = ""
review_field_width = [0, 0, 0]
review_field_format = ["%*s", "%*s", "%*s"]
review_field_justify = [+1, +1, -1] # -1 is justify to right
for line in output.split("\n"):
# Warnings from ssh wind up in this output
if line[0] != "{":
print(line)
continue
try:
review_info = json.loads(line)
except:
if VERBOSE:
print(output)
raise(CannotParseOpenChangesets, sys.exc_info()[1])
if 'type' in review_info:
break
review_list.append([review_info[f] for f in REVIEW_FIELDS])
for i in FIELDS:
review_field_width[i] = max(
review_field_width[i],
len(review_info[REVIEW_FIELDS[i]])
)
review_field_format = " ".join([
review_field_color[i] +
review_field_format[i] +
color_reset
for i in FIELDS])
review_field_width = [
review_field_width[i] * review_field_justify[i]
for i in FIELDS]
for review_value in review_list:
# At this point we have review_field_format
# like "%*s %*s %*s" and we need to supply
# (width1, value1, width2, value2, ...) tuple to print
# It's easy to zip() widths with actual values,
# but we need to flatten the resulting
# ((width1, value1), (width2, value2), ...) map.
formatted_fields = []
for (width, value) in zip(review_field_width, review_value):
formatted_fields.extend([width, value])
print(review_field_format % tuple(formatted_fields))
print("Found %d items for review" % review_info['rowCount'])
return 0
class CannotQueryPatchSet(CommandFailed):
"Cannot query patchset information"
EXIT_CODE = 34
class PatchSetInformationNotFound(ChangeSetException):
"Could not fetch review information for change %s"
EXIT_CODE = 35
class PatchSetNotFound(ChangeSetException):
"Gerrit review %s not found"
EXIT_CODE = 36
class PatchSetGitFetchFailed(CommandFailed):
"""Cannot fetch patchset contents
Does specified change number belong to this project?"""
EXIT_CODE = 37
class CheckoutNewBranchFailed(CommandFailed):
"Cannot checkout to new branch"
EXIT_CODE = 64
class CheckoutExistingBranchFailed(CommandFailed):
"Cannot checkout existing branch"
EXIT_CODE = 65
class ResetHardFailed(CommandFailed):
"Failed to hard reset downloaded branch"
EXIT_CODE = 66
def download_review(review, masterbranch, remote):
(hostname, username, port, project_name) = \
parse_git_show(remote, "Push")
if port is not None:
port = "-p %s" % port
else:
port = ""
if username is None:
userhost = hostname
else:
userhost = "%s@%s" % (username, hostname)
review_info = None
output = run_command_exc(
CannotQueryPatchSet,
"ssh", "-x", port, userhost,
"gerrit", "query",
"--format=JSON --current-patch-set change:%s" % review)
review_jsons = output.split("\n")
found_review = False
for review_json in review_jsons:
try:
review_info = json.loads(review_json)
found_review = True
except:
pass
if found_review:
break
if not found_review:
if VERBOSE:
print(output)
raise PatchSetInformationNotFound(review)
try:
revision = review_info['currentPatchSet']['revision']
refspec = review_info['currentPatchSet']['ref']
except KeyError:
raise PatchSetNotFound(review)
try:
topic = review_info['topic']
if topic == masterbranch:
topic = review
except KeyError:
topic = review
if review_info.get('owner'):
author = re.sub('\W+', '_', review_info['owner']['name']).lower()
else:
author = 'unknown'
branch_name = "review/%s/%s" % (author, topic)
print("Downloading %s from gerrit into %s" % (refspec, branch_name))
run_command_exc(PatchSetGitFetchFailed,
"git", "fetch", remote, refspec)
try:
output = run_command_exc(CheckoutNewBranchFailed,
"git", "checkout", "-b",
branch_name, "FETCH_HEAD")
except CheckoutNewBranchFailed, e:
if re.search("already exists\.?", output):
print("Branch already exists - reusing")
run_command_exc(CheckoutExistingBranchFailed,
"git", "checkout", branch_name)
run_command_exc(ResetHardFailed,
"git", "reset", "--hard", "FETCH_HEAD")
else:
raise
print("Switched to branch '%s'" % branch_name)
class CheckoutBackExistingBranchFailed(CommandFailed):
"Cannot switch back to existing branch"
EXIT_CODE = 67
class DeleteBranchFailed(CommandFailed):
"Failed to delete branch"
EXIT_CODE = 68
def finish_branch(target_branch):
local_branch = get_branch_name(target_branch)
if VERBOSE:
print("Switching back to '%s' and deleting '%s'" % (target_branch,
local_branch))
run_command_exc(CheckoutBackExistingBranchFailed,
"git", "checkout", target_branch)
print("Switched to branch '%s'" % target_branch)
run_command_exc(DeleteBranchFailed,
"git", "branch", "-D", local_branch)
print("Deleted branch '%s'" % local_branch)
def print_exit_message(status, needs_update):
if needs_update:
print("""
***********************************************************
A new version of git-review is available on PyPI. Please
update your copy with:
pip install -U git-review
to ensure proper behavior with gerrit. Thanks!
***********************************************************
""")
sys.exit(status)
def main():
config = get_config()
usage = "git review [OPTIONS] ... [BRANCH]"
import argparse
parser = argparse.ArgumentParser(usage=usage, description=COPYRIGHT)
parser.add_argument("-t", "--topic", dest="topic",
help="Topic to submit branch to")
parser.add_argument("-D", "--draft", dest="draft", action="store_true",
help="Submit review as a draft")
parser.add_argument("-c", "--compatible", dest="compatible",
action="store_true",
help="Push change to refs/for/* for compatibility "
"with Gerrit versions < 2.3. Ignored if "
"-D/--draft is used.")
parser.add_argument("-n", "--dry-run", dest="dry", action="store_true",
help="Don't actually submit the branch for review")
parser.add_argument("-i", "--new-changeid", dest="regenerate",
action="store_true",
help="Regenerate Change-id before submitting")
parser.add_argument("-r", "--remote", dest="remote",
help="git remote to use for gerrit")
parser.add_argument("-R", "--no-rebase", dest="rebase",
action="store_false",
help="Don't rebase changes before submitting.")
parser.add_argument("-F", "--force-rebase", dest="force_rebase",
action="store_true",
help="Force rebase even when not needed.")
parser.add_argument("-d", "--download", dest="download",
help="Download the contents of an existing gerrit "
"review into a branch")
parser.add_argument("-u", "--update", dest="update", action="store_true",
help="Force updates from remote locations")
parser.add_argument("-s", "--setup", dest="setup", action="store_true",
help="Just run the repo setup commands but don't "
"submit anything")
parser.add_argument("-f", "--finish", dest="finish", action="store_true",
help="Close down this branch and switch back to "
"master on successful submission")
parser.add_argument("-l", "--list", dest="list", action="store_true",
help="List available reviews for the current project")
parser.add_argument("-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_argument("-v", "--verbose", dest="verbose", action="store_true",
help="Output more information about what's going on")
parser.add_argument("--license", dest="license", action="store_true",
help="Print the license and exit")
parser.add_argument("--version", action="version",
version='%s version %s' %
(os.path.split(sys.argv[0])[-1], version))
parser.add_argument("branch", nargs="?", default=config['defaultbranch'])
parser.set_defaults(dry=False,
draft=False,
rebase=config['defaultrebase'],
verbose=False,
update=False,
setup=False,
list=False,
yes=False,
remote=config['defaultremote'])
options = parser.parse_args()
if options.license:
print(COPYRIGHT)
sys.exit(0)
branch = options.branch
global VERBOSE
global UPDATE
VERBOSE = options.verbose
UPDATE = options.update
remote = options.remote
yes = options.yes
status = 0
needs_update = latest_is_newer()
check_remote(branch, remote,
config['hostname'], config['port'], config['project'])
if options.download is not None:
download_review(options.download, branch, remote)
return
elif options.list:
list_reviews(remote)
return
topic = options.topic
if topic is None:
topic = get_topic(branch)
if VERBOSE:
print("Found topic '%s' from parsing changes." % topic)
hook_file = get_hooks_target_file()
have_hook = os.path.exists(hook_file) and os.access(hook_file, os.X_OK)
if not have_hook:
set_hooks_commit_msg(remote, hook_file)
if options.setup:
if options.finish and not options.dry:
finish_branch(branch)
return
if options.rebase:
if not rebase_changes(branch, remote):
print_exit_message(1, needs_update)
if not options.force_rebase and not undo_rebase():
print_exit_message(1, needs_update)
assert_one_change(remote, branch, yes, have_hook)
ref = "publish"
if options.draft:
ref = "drafts"
elif options.compatible:
ref = "for"
cmd = "git push %s HEAD:refs/%s/%s/%s" % (remote, ref, branch,
topic)
if options.regenerate:
print("Amending the commit to regenerate the change id\n")
regenerate_cmd = "git commit --amend"
if options.dry:
print("\tGIT_EDITOR=\"sed -i -e '/^Change-Id:/d'\" %s\n" %
regenerate_cmd)
else:
run_command(regenerate_cmd,
GIT_EDITOR="sed -i -e "
"'/^Change-Id:/d'")
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)
if options.finish and not options.dry and status == 0:
finish_branch(branch)
return
print_exit_message(status, needs_update)
if __name__ == "__main__":
try:
main()
except GitReviewException, e:
print(e)
sys.exit(e.EXIT_CODE)