forked from evmar/git-cl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-cl
executable file
·944 lines (801 loc) · 33 KB
/
git-cl
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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
#!/usr/bin/python
# git-cl -- a git-command for integrating reviews on Rietveld
# Copyright (C) 2008 Evan Martin <[email protected]>
import getpass
import optparse
import os
import re
import subprocess
import sys
import tempfile
import textwrap
import upload
import urllib2
import projecthosting_upload
# mimetype exceptions: if you can't upload to rietveld, add the
# relevant extension to this list. The only important part is the
# "text/x-script." bit; the stuff after the dot doesn't matter
import mimetypes
mimetypes.add_type("text/x-script.scheme", ".scm")
mimetypes.add_type("application/xml", ".xml")
mimetypes.add_type("text/x-script.postscript", ".ps")
mimetypes.add_type("text/x-script.perl", ".pl")
mimetypes.add_type("text/x-script.tex", ".latex")
mimetypes.add_type("text/x-script.texinfo", ".texi")
mimetypes.add_type("text/x-script.shell", ".sh")
try:
import readline
except ImportError:
pass
DEFAULT_SERVER = 'codereview.appspot.com'
PREDCOMMIT_HOOK = '.git/hooks/pre-cl-dcommit'
PREUPLOAD_HOOK = '.git/hooks/pre-cl-upload'
def DieWithError(message):
print >>sys.stderr, message
sys.exit(1)
def RunCommand(cmd, error_ok=False, error_message=None, exit_code=False,
redirect_stdout=True):
# Useful for debugging:
# print >>sys.stderr, ' '.join(cmd)
if redirect_stdout:
stdout = subprocess.PIPE
else:
stdout = None
proc = subprocess.Popen(cmd, stdout=stdout)
output = proc.communicate()[0]
if exit_code:
return proc.returncode
if not error_ok and proc.returncode != 0:
DieWithError('Command "%s" failed.\n' % (' '.join(cmd)) +
(error_message or output))
return output
def RunGit(args, **kwargs):
cmd = ['git'] + args
return RunCommand(cmd, **kwargs)
class Settings:
def __init__(self):
self.server = None
self.cc = None
self.root = None
self.is_git_svn = None
self.svn_branch = None
self.tree_status_url = None
self.viewvc_url = None
def GetServer(self, error_ok=False):
if not self.server:
if not error_ok:
error_message = ('You must configure your review setup by running '
'"git cl config".')
self.server = self._GetConfig('rietveld.server',
error_message=error_message)
else:
self.server = self._GetConfig('rietveld.server', error_ok=True)
return self.server
def GetCCList(self):
if self.cc is None:
self.cc = self._GetConfig('rietveld.cc', error_ok=True)
return self.cc
def GetRoot(self):
if not self.root:
self.root = os.path.abspath(RunGit(['rev-parse', '--show-cdup']).strip())
return self.root
def GetIsGitSvn(self):
"""Return true if this repo looks like it's using git-svn."""
if self.is_git_svn is None:
# If you have any "svn-remote.*" config keys, we think you're using svn.
self.is_git_svn = RunGit(['config', '--get-regexp', r'^svn-remote\.'],
exit_code=True) == 0
return self.is_git_svn
def GetSVNBranch(self):
if self.svn_branch is None:
if not self.GetIsGitSvn():
raise "Repo doesn't appear to be a git-svn repo."
# Try to figure out which remote branch we're based on.
# Strategy:
# 1) find all git-svn branches and note their svn URLs.
# 2) iterate through our branch history and match up the URLs.
# regexp matching the git-svn line that contains the URL.
git_svn_re = re.compile(r'^\s*git-svn-id: (\S+)@', re.MULTILINE)
# Get the refname and svn url for all refs/remotes/*.
remotes = RunGit(['for-each-ref', '--format=%(refname)',
'refs/remotes']).splitlines()
svn_refs = {}
for ref in remotes:
match = git_svn_re.search(RunGit(['cat-file', '-p', ref]))
if match:
svn_refs[match.group(1)] = ref
if len(svn_refs) == 1:
# Only one svn branch exists -- seems like a good candidate.
self.svn_branch = svn_refs.values()[0]
elif len(svn_refs) > 1:
# We have more than one remote branch available. We don't
# want to go through all of history, so read a line from the
# pipe at a time.
# The -100 is an arbitrary limit so we don't search forever.
cmd = ['git', 'log', '-100', '--pretty=medium']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in proc.stdout:
match = git_svn_re.match(line)
if match:
url = match.group(1)
if url in svn_refs:
self.svn_branch = svn_refs[url]
proc.stdout.close() # Cut pipe.
break
if not self.svn_branch:
raise "Can't guess svn branch -- try specifying it on the command line"
return self.svn_branch
def GetTreeStatusUrl(self, error_ok=False):
if not self.tree_status_url:
error_message = ('You must configure your tree status URL by running '
'"git cl config".')
self.tree_status_url = self._GetConfig('rietveld.tree-status-url',
error_ok=error_ok,
error_message=error_message)
return self.tree_status_url
def GetViewVCUrl(self):
if not self.viewvc_url:
self.viewvc_url = self._GetConfig('rietveld.viewvc-url', error_ok=True)
return self.viewvc_url
def _GetConfig(self, param, **kwargs):
return RunGit(['config', param], **kwargs).strip()
settings = Settings()
did_migrate_check = False
def CheckForMigration():
"""Migrate from the old issue format, if found.
We used to store the branch<->issue mapping in a file in .git, but it's
better to store it in the .git/config, since deleting a branch deletes that
branch's entry there.
"""
# Don't run more than once.
global did_migrate_check
if did_migrate_check:
return
gitdir = RunGit(['rev-parse', '--git-dir']).strip()
storepath = os.path.join(gitdir, 'cl-mapping')
if os.path.exists(storepath):
print "old-style git-cl mapping file (%s) found; migrating." % storepath
store = open(storepath, 'r')
for line in store:
branch, issue = line.strip().split()
RunGit(['config', 'branch.%s.rietveldissue' % ShortBranchName(branch),
issue])
store.close()
os.remove(storepath)
did_migrate_check = True
def RietveldURL(issue):
"""Returns the Rietveld URL for a particular issue."""
return 'https://%s/%s' % (settings.GetServer(), issue)
def TrackerURL(issue):
"""Return the Tracker URL for a particular issue."""
# make the server/project customizable?
return 'http://code.google.com/p/lilypond/issues/detail?id=%s' % issue
def ShortBranchName(branch):
"""Convert a name like 'refs/heads/foo' to just 'foo'."""
return branch.replace('refs/heads/', '')
class Changelist:
def __init__(self, branchref=None):
# Poke settings so we get the "configure your server" message if necessary.
settings.GetServer()
self.branchref = branchref
if self.branchref:
self.branch = ShortBranchName(self.branchref)
else:
self.branch = None
self.upstream_branch = None
self.has_RietveldIssue = False
self.has_TrackerIssue = False
self.has_description = False
self.RietveldIssue = None
self.TrackerIssue = None
self.description = None
def GetBranch(self):
"""Returns the short branch name, e.g. 'master'."""
if not self.branch:
self.branchref = RunGit(['symbolic-ref', 'HEAD']).strip()
self.branch = ShortBranchName(self.branchref)
return self.branch
def GetBranchRef(self):
"""Returns the full branch name, e.g. 'refs/heads/master'."""
self.GetBranch() # Poke the lazy loader.
return self.branchref
def GetUpstreamBranch(self):
if self.upstream_branch is None:
branch = self.GetBranch()
upstream_branch = RunGit(['config', 'branch.%s.merge' % branch],
error_ok=True).strip()
if upstream_branch:
remote = RunGit(['config', 'branch.%s.remote' % branch]).strip()
# We have remote=origin and branch=refs/heads/foobar; convert to
# refs/remotes/origin/foobar.
self.upstream_branch = upstream_branch.replace('heads',
'remotes/' + remote)
if not self.upstream_branch:
# Fall back on trying a git-svn upstream branch.
if settings.GetIsGitSvn():
self.upstream_branch = settings.GetSVNBranch()
if not self.upstream_branch:
DieWithError("""Unable to determine default branch to diff against.
Either pass complete "git diff"-style arguments, like
git cl upload origin/master
or verify this branch is set up to track another (via the --track argument to
"git checkout -b ...").""")
return self.upstream_branch
def GetTrackerIssue(self):
"""Returns the Tracker issue associated with this branch."""
if not self.has_TrackerIssue:
CheckForMigration()
issue = RunGit(['config', self._TrackerIssueSetting()], error_ok=True).strip()
if issue:
self.TrackerIssue = issue
else:
self.TrackerIssue = None
self.has_TrackerIssue = True
return self.TrackerIssue
def GetRietveldIssue(self):
if not self.has_RietveldIssue:
CheckForMigration()
issue = RunGit(['config', self._RietveldIssueSetting()], error_ok=True).strip()
if issue:
self.RietveldIssue = issue
else:
self.RietveldIssue = None
self.has_RietveldIssue = True
return self.RietveldIssue
def GetRietveldURL(self):
return RietveldURL(self.GetRietveldIssue())
def GetTrackerURL(self):
return TrackerURL(self.GetTrackerIssue())
def GetDescription(self, pretty=False):
if not self.has_description:
if self.GetRietveldIssue():
url = self.GetRietveldURL() + '/description'
self.description = urllib2.urlopen(url).read().strip()
self.has_description = True
if pretty:
wrapper = textwrap.TextWrapper()
wrapper.initial_indent = wrapper.subsequent_indent = ' '
return wrapper.fill(self.description)
return self.description
def GetPatchset(self):
if not self.has_patchset:
patchset = RunGit(['config', self._PatchsetSetting()],
error_ok=True).strip()
if patchset:
self.patchset = patchset
else:
self.patchset = None
self.has_patchset = True
return self.patchset
def SetPatchset(self, patchset):
"""Set this branch's patchset. If patchset=0, clears the patchset."""
if patchset:
RunGit(['config', self._PatchsetSetting(), str(patchset)])
else:
RunGit(['config', '--unset', self._PatchsetSetting()])
self.has_patchset = False
def SetTrackerIssue(self, issue):
"""Set this branch's Tracker issue. If issue=0, clears the issue."""
if issue:
RunGit(['config', self._TrackerIssueSetting(), str(issue)])
elif self.GetTrackerIssue():
RunGit(['config', '--unset', self._TrackerIssueSetting()])
self.has_TrackerIssue = False
def SetRietveldIssue(self, issue):
"""Set this branch's Rietveld issue. If issue=0, clears the issue."""
if issue:
RunGit(['config', self._RietveldIssueSetting(), str(issue)])
else:
RunGit(['config', '--unset', self._RietveldIssueSetting()])
self.SetTrackerIssue(0)
self.SetPatchset(0)
self.has_RietveldIssue = False
def CloseRietveldIssue(self):
"""Close the Rietveld issue associated with this branch."""
def GetUserCredentials():
email = raw_input('Email: ').strip()
password = getpass.getpass('Password for %s: ' % email)
return email, password
rpc_server = upload.HttpRpcServer(settings.GetServer(),
GetUserCredentials,
host_override=settings.GetServer(),
save_cookies=True)
# You cannot close an issue with a GET.
# We pass an empty string for the data so it is a POST rather than a GET.
data = [("description", self.description),]
ctype, body = upload.EncodeMultipartFormData(data, [])
rpc_server.Send('/' + self.GetRietveldIssue() + '/close', body, ctype)
def _TrackerIssueSetting(self):
"""Returns the git setting that stores the Tracker issue."""
return 'branch.%s.googlecodeissue' % self.GetBranch()
def _RietveldIssueSetting(self):
"""Returns the git setting that stores the Rietveld issue."""
return 'branch.%s.rietveldissue' % self.GetBranch()
def _PatchsetSetting(self):
"""Returns the git setting that stores the most recent Rietveld patchset."""
return 'branch.%s.rietveldpatchset' % self.GetBranch()
def GetCodereviewSettingsInteractively():
"""Prompt the user for settings."""
server = settings.GetServer(error_ok=True)
prompt = 'Rietveld server (host[:port])'
prompt += ' [%s]' % (server or DEFAULT_SERVER)
newserver = raw_input(prompt + ': ')
if not server and not newserver:
newserver = DEFAULT_SERVER
if newserver and newserver != server:
RunGit(['config', 'rietveld.server', newserver])
def SetProperty(initial, caption, name):
prompt = caption
if initial:
prompt += ' ("x" to clear) [%s]' % initial
new_val = raw_input(prompt + ': ')
if new_val == 'x':
RunGit(['config', '--unset-all', 'rietveld.' + name], error_ok=True)
elif new_val and new_val != initial:
RunGit(['config', 'rietveld.' + name, new_val])
SetProperty(settings.GetCCList(), 'CC list', 'cc')
SetProperty(settings.GetTreeStatusUrl(error_ok=True), 'Tree status URL',
'tree-status-url')
SetProperty(settings.GetViewVCUrl(), 'ViewVC URL', 'viewvc-url')
# TODO: configure a default branch to diff against, rather than this
# svn-based hackery.
def LoadCodereviewSettingsFromFile(file):
"""Parse a codereview.settings file."""
settings = {}
for line in file.read().splitlines():
if not line or line.startswith("#"):
continue
k, v = line.split(": ", 1)
settings[k] = v
def GetProperty(name):
return settings.get(name)
def SetProperty(name, setting, unset_error_ok=False):
fullname = 'rietveld.' + name
if setting in settings:
RunGit(['config', fullname, settings[setting]])
else:
RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok)
SetProperty('server', 'CODE_REVIEW_SERVER')
# Only server setting is required. Other settings can be absent.
# In that case, we ignore errors raised during option deletion attempt.
SetProperty('cc', 'CC_LIST', unset_error_ok=True)
SetProperty('tree-status-url', 'STATUS', unset_error_ok=True)
SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True)
hooks = {}
if GetProperty('GITCL_PREUPLOAD'):
hooks['preupload'] = GetProperty('GITCL_PREUPLOAD')
if GetProperty('GITCL_PREDCOMMIT'):
hooks['predcommit'] = GetProperty('GITCL_PREDCOMMIT')
return hooks
def CmdConfig(args):
def DownloadToFile(url, filename):
filename = os.path.join(settings.GetRoot(), filename)
if os.path.exists(filename):
print '%s exists, skipping' % filename
return False
contents = urllib2.urlopen(url).read()
file = open(filename, 'w')
file.write(contents)
file.close()
os.chmod(filename, 0755)
return True
parser = optparse.OptionParser(
usage='git cl config [repo root containing codereview.settings]')
(options, args) = parser.parse_args(args)
if len(args) == 0:
GetCodereviewSettingsInteractively()
return
url = args[0]
if not url.endswith('codereview.settings'):
url = os.path.join(url, 'codereview.settings')
# Load Codereview settings and download hooks (if available).
hooks = LoadCodereviewSettingsFromFile(urllib2.urlopen(url))
for key, filename in (('predcommit', PREDCOMMIT_HOOK),
('preupload', PREUPLOAD_HOOK)):
if key in hooks:
DownloadToFile(hooks[key], filename)
def CmdStatus(args):
parser = optparse.OptionParser(usage='git cl status [options]')
parser.add_option('--field', help='print only specific field (desc|id|url)')
(options, args) = parser.parse_args(args)
# TODO: maybe make show_branches a flag if necessary.
show_branches = not options.field
if show_branches:
branches = RunGit(['for-each-ref', '--format=%(refname)', 'refs/heads'])
if branches:
print 'Branches associated with reviews:'
for branch in sorted(branches.splitlines()):
cl = Changelist(branchref=branch)
print " %10s: %s" % (cl.GetBranch(), cl.GetRietveldIssue())
cl = Changelist()
if options.field:
if options.field.startswith('desc'):
print cl.GetDescription()
elif options.field == 'id':
print cl.GetRietveldIssue()
elif options.field == 'url':
print cl.GetRietveldURL()
else:
print
print 'Current branch:',
if not cl.GetRietveldIssue():
print 'no issue assigned.'
return 0
print cl.GetBranch()
if cl.GetTrackerIssue():
print 'Tracker issue:', cl.GetTrackerIssue(), '(%s)' % cl.GetTrackerURL()
else:
print 'Tracker issue: None'
print 'Rietveld issue:', cl.GetRietveldIssue(), '(%s)' % cl.GetRietveldURL()
print 'Issue description:'
print cl.GetDescription(pretty=True)
def CmdIssue(args):
parser = optparse.OptionParser(usage='git cl issue [issue_number]')
parser.description = ('Set or display the current Rietveld issue. ' +
'Pass issue number 0 to clear the current issue.')
(options, args) = parser.parse_args(args)
cl = Changelist()
if len(args) > 0:
cl.SetRietveldIssue(int(args[0]))
print 'Rietveld issue:', cl.GetRietveldIssue(), '(%s)' % cl.GetRietveldURL()
def UserEditedLog(starting_text):
"""Given some starting text, let the user edit it and return the result."""
editor = os.getenv('EDITOR', 'vi')
(file_handle, filename) = tempfile.mkstemp()
file = os.fdopen(file_handle, 'w')
file.write(starting_text)
file.close()
ret = subprocess.call(editor + ' ' + filename, shell=True)
if ret != 0:
os.remove(filename)
return
file = open(filename)
text = file.read()
file.close()
os.remove(filename)
stripcomment_re = re.compile(r'^#.*$', re.MULTILINE)
return stripcomment_re.sub('', text).strip()
def RunHook(hook, upstream_branch='origin', error_ok=False):
"""Run a given hook if it exists. By default, we fail on errors."""
hook = '%s/%s' % (settings.GetRoot(), hook)
if not os.path.exists(hook):
return
output = RunCommand([hook, upstream_branch], error_ok).strip()
if output != '':
print output
def CmdPresubmit(args):
"""Reports what presubmit checks on the change would report."""
parser = optparse.OptionParser(
usage='git cl presubmit [options]')
(options, args) = parser.parse_args(args)
if RunGit(['diff-index', 'HEAD']):
print 'Cannot presubmit with a dirty tree. You must commit locally first.'
return 1
print '*** Presubmit checks for UPLOAD would report: ***'
RunHook(PREUPLOAD_HOOK, error_ok=True)
print '*** Presubmit checks for DCOMMIT would report: ***'
RunHook(PREDCOMMIT_HOOK, error_ok=True)
def CmdUpload(args):
parser = optparse.OptionParser(
usage='git cl upload [options] [args to "git diff"]')
parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
help='bypass upload presubmit hook')
parser.add_option('-m', dest='message', help='message for patch')
parser.add_option('-r', '--reviewers',
help='reviewer email addresses')
parser.add_option('--send-mail', action='store_true',
help='send email to reviewer immediately')
parser.add_option("-n", "--no-code-issue",
help="do not upload to code.google.com",
action="store_true", dest="no_code_issue")
(options, args) = parser.parse_args(args)
if RunGit(['diff-index', 'HEAD']):
print 'Cannot upload with a dirty tree. You must commit locally first.'
return 1
cl = Changelist()
if args:
base_branch = args[0]
else:
# Default to diffing against the "upstream" branch.
base_branch = cl.GetUpstreamBranch()
args = [base_branch + "..."]
if not options.bypass_hooks:
RunHook(PREUPLOAD_HOOK, upstream_branch=base_branch, error_ok=False)
# --no-ext-diff is broken in some versions of Git, so try to work around
# this by overriding the environment (but there is still a problem if the
# git config key "diff.external" is used).
env = os.environ.copy()
if 'GIT_EXTERNAL_DIFF' in env: del env['GIT_EXTERNAL_DIFF']
subprocess.call(['git', 'diff', '--no-ext-diff', '--stat', '-M'] + args,
env=env)
upload_args = ['--assume_yes'] # Don't ask about untracked files.
upload_args.extend(['--server', settings.GetServer()])
if options.reviewers:
upload_args.extend(['--reviewers', options.reviewers])
upload_args.extend(['--cc', settings.GetCCList()])
if options.message:
upload_args.extend(['--message', options.message])
if options.send_mail:
if not options.reviewers:
DieWithError("Must specify reviewers to send email.")
upload_args.append('--send_mail')
if cl.GetRietveldIssue():
upload_args.extend(['--issue', cl.GetRietveldIssue()])
print ("This branch is associated with issue %s. "
"Adding patch to that issue." % cl.GetRietveldIssue())
prompt = "Message describing this patch set: "
desc = options.message or raw_input(prompt).strip()
else:
# Construct a description for this change from the log.
# We need to convert diff options to log options.
log_args = []
if len(args) == 1 and not args[0].endswith('.'):
log_args = [args[0] + '..']
elif len(args) == 2:
log_args = [args[0] + '..' + args[1]]
else:
log_args = args[:] # Hope for the best!
desc = RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args)
initial_text = """# Enter a description of the change.
# This will be displayed on the codereview site.
# The first line will also be used as the subject of the review."""
desc = UserEditedLog(initial_text + '\n' + desc)
if not desc:
print "Description empty; aborting."
return 1
subject = desc.splitlines()[0]
upload_args.extend(['--title', subject])
upload_args.extend(['--message', desc])
issue, patchset = upload.RealMain(['upload'] + upload_args + args)
if not cl.GetRietveldIssue():
cl.SetRietveldIssue(issue)
cl.SetPatchset(patchset)
if not options.no_code_issue:
issueId = cl.GetTrackerIssue()
issueId = projecthosting_upload.upload(issue, patchset, subject, desc, issueId)
cl.SetTrackerIssue(issueId)
def CmdDCommit(args):
parser = optparse.OptionParser(
usage='git cl dcommit [options] [git-svn branch to apply against]')
parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
help='bypass upload presubmit hook')
parser.add_option('-m', dest='message',
help="override review description")
parser.add_option('-f', action='store_true', dest='force',
help="force yes to questions (don't prompt)")
parser.add_option('-c', dest='contributor',
help="external contributor for patch (appended to " +
"description)")
parser.add_option('--tbr', action='store_true', dest='tbr',
help="short for 'to be reviewed', commit branch " +
"even without uploading for review")
(options, args) = parser.parse_args(args)
cl = Changelist()
if not args:
# Default to merging against our best guess of the upstream branch.
args = [cl.GetUpstreamBranch()]
base_branch = args[0]
if RunGit(['diff-index', 'HEAD']):
print 'Cannot dcommit with a dirty tree. You must commit locally first.'
return 1
# This rev-list syntax means "show all commits not in my branch that
# are in base_branch".
upstream_commits = RunGit(['rev-list', '^' + cl.GetBranchRef(),
base_branch]).splitlines()
if upstream_commits:
print ('Base branch "%s" has %d commits '
'not in this branch.' % (base_branch, len(upstream_commits)))
print 'Run "git merge %s" before attempting to dcommit.' % base_branch
return 1
if not options.force and not options.bypass_hooks:
RunHook(PREDCOMMIT_HOOK, upstream_branch=base_branch, error_ok=False)
# Check the tree status if the tree status URL is set.
status = GetTreeStatus()
if 'closed' == status:
print ('The tree is closed. Please wait for it to reopen. Use '
'"git cl dcommit -f" to commit on a closed tree.')
return 1
elif 'unknown' == status:
print ('Unable to determine tree status. Please verify manually and '
'use "git cl dcommit -f" to commit on a closed tree.')
description = options.message
if not options.tbr:
# It is important to have these checks early. Not only for user
# convenience, but also because the cl object then caches the correct values
# of these fields even as we're juggling branches for setting up the commit.
if not cl.GetRietveldIssue():
print 'Current issue unknown -- has this branch been uploaded?'
print 'Use --tbr to commit without review.'
return 1
if not description:
description = cl.GetDescription()
if not description:
print 'No description set.'
print 'Visit %s/edit to set it.' % (cl.GetRietveldURL())
return 1
description += "\n\nReview URL: %s" % cl.GetRietveldURL()
else:
# Submitting TBR. Get a description now.
if not description:
description = UserEditedLog('TBR: ')
if not description:
print "Description empty; aborting."
return 1
if options.contributor:
description += "\nPatch from %s." % options.contributor
print 'Description:', repr(description)
branches = [base_branch, cl.GetBranchRef()]
if not options.force:
subprocess.call(['git', 'diff', '--stat'] + branches)
raw_input("About to commit; enter to confirm.")
# We want to squash all this branch's commits into one commit with the
# proper description.
# We do this by doing a "merge --squash" into a new commit branch, then
# dcommitting that.
MERGE_BRANCH = 'git-cl-commit'
# Delete the merge branch if it already exists.
if RunGit(['show-ref', '--quiet', '--verify', 'refs/heads/' + MERGE_BRANCH],
exit_code=True) == 0:
RunGit(['branch', '-D', MERGE_BRANCH])
# We might be in a directory that's present in this branch but not in the
# trunk. Move up to the top of the tree so that git commands that expect a
# valid CWD won't fail after we check out the merge branch.
rel_base_path = RunGit(['rev-parse', '--show-cdup']).strip()
if rel_base_path:
os.chdir(rel_base_path)
# Stuff our change into the merge branch.
# We wrap in a try...finally block so if anything goes wrong,
# we clean up the branches.
try:
RunGit(['checkout', '-q', '-b', MERGE_BRANCH, base_branch])
RunGit(['merge', '--squash', cl.GetBranchRef()])
RunGit(['commit', '-m', description])
# dcommit the merge branch.
output = RunGit(['svn', 'dcommit', '--no-rebase'])
finally:
# And then swap back to the original branch and clean up.
RunGit(['checkout', '-q', cl.GetBranch()])
RunGit(['branch', '-D', MERGE_BRANCH])
if cl.has_RietveldIssue and output.find("Committed r") != -1:
print "Closing issue (you may be prompted for your codereview password)..."
viewvc_url = settings.GetViewVCUrl()
if viewvc_url:
revision = re.compile(".*?\nCommitted r(\d+)",
re.DOTALL).match(output).group(1)
cl.description = (cl.description +
"\n\nCommitted: " + viewvc_url + revision)
cl.CloseRietveldIssue()
cl.SetRietveldIssue(0)
def CmdPatch(args):
parser = optparse.OptionParser(usage=('git cl patch [options] '
'<patch url or Rietveld issue ID>'))
parser.add_option('-b', dest='newbranch',
help='create a new branch off trunk for the patch')
parser.add_option('-f', action='store_true', dest='force',
help='with -b, clobber any existing branch')
parser.add_option('--reject', action='store_true', dest='reject',
help='allow failed patches and spew .rej files')
parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit',
help="don't commit after patch applies")
(options, args) = parser.parse_args(args)
if len(args) != 1:
return parser.print_help()
input = args[0]
if re.match(r'\d+', input):
# Input is an issue id. Figure out the URL.
issue = input
fetch = "curl --silent https://%s/%s" % (settings.GetServer(), issue)
grep = "grep -E -o '/download/issue[0-9]+_[0-9]+.diff'"
pipe = subprocess.Popen("%s | %s" % (fetch, grep), shell=True,
stdout=subprocess.PIPE)
path = pipe.stdout.read().strip()
url = 'https://%s%s' % (settings.GetServer(), path)
if len(path) == 0:
# There is no patch to download (patch may be too large, see
# http://code.google.com/p/rietveld/issues/detail?id=196).
# Try to download individual patches for each file instead,
# and concatenate them to obtain the complete patch.
grep = "grep -E -o '/download/issue[0-9]+_[0-9]+_[0-9]+.diff'"
pipe = subprocess.Popen("%s | %s" % (fetch, grep), shell=True,
stdout=subprocess.PIPE)
paths = pipe.stdout.read().strip().split("\n")
url = 'https://%s{%s}' % (settings.GetServer(), ",".join(paths))
else:
# Assume it's a URL to the patch.
match = re.match(r'https?://.*?/issue(\d+)_\d+.diff', input)
if match:
issue = match.group(1)
url = input.replace("http:", "https:")
else:
print "Must pass an Rietveld issue ID or full URL for 'Download raw patch set'"
return 1
if options.newbranch:
if options.force:
RunGit(['branch', '-D', options.newbranch], error_ok=True)
RunGit(['checkout', '-b', options.newbranch])
# Switch up to the top-level directory, if necessary, in preparation for
# applying the patch.
top = RunGit(['rev-parse', '--show-cdup']).strip()
if top:
os.chdir(top)
# Construct a pipeline to feed the patch into "git apply".
# We use "git apply" to apply the patch instead of "patch" so that we can
# pick up file adds.
# 1) Fetch the patch.
fetch = "curl --silent %s" % url
# 2) Munge the patch.
# Git patches have a/ at the beginning of source paths. We strip that out
# with a sed script rather than the -p flag to patch so we can feed either
# Git or svn-style patches into the same apply command.
gitsed = "sed -e 's|^--- a/|--- |; s|^+++ b/|+++ |'"
# 3) Apply the patch.
# The --index flag means: also insert into the index (so we catch adds).
apply = "git apply --index -p0"
if options.reject:
apply += " --reject"
subprocess.check_call(' | '.join([fetch, gitsed, apply]), shell=True)
# If we had an issue, commit the current state and register the issue.
if not options.nocommit:
RunGit(['commit', '-m', 'patch from issue %s' % issue])
cl = Changelist()
cl.SetRietveldIssue(issue)
print "Committed patch."
else:
print "Patch applied to index."
def CmdRebase(args):
# Provide a wrapper for git svn rebase to help avoid accidental
# git svn dcommit.
RunGit(['svn', 'rebase'], redirect_stdout=False)
def GetTreeStatus():
"""Fetches the tree status and returns either 'open', 'closed',
'unknown' or 'unset'."""
url = settings.GetTreeStatusUrl(error_ok=True)
if url:
status = urllib2.urlopen(url).read().lower()
if status.find('closed') != -1 or status == '0':
return 'closed'
elif status.find('open') != -1 or status == '1':
return 'open'
return 'unknown'
return 'unset'
def CmdTreeStatus(args):
status = GetTreeStatus()
if 'unset' == status:
print 'You must configure your tree status URL by running "git cl config".'
return 2
print "The tree is %s" % status
if status != 'open':
return 1
return 0
def CmdUpstream(args):
cl = Changelist()
print cl.GetUpstreamBranch()
COMMANDS = [
('config', 'edit configuration for this tree', CmdConfig),
('dcommit', 'commit the current changelist via git-svn', CmdDCommit),
('issue', 'show/set current branch\'s Rietveld issue', CmdIssue),
('patch', 'patch in a code review', CmdPatch),
('presubmit', 'run presubmit tests on the current changelist', CmdPresubmit),
('rebase', 'rebase current branch on top of svn repo', CmdRebase),
('status', 'show status of changelists', CmdStatus),
('tree', 'show the status of the tree', CmdTreeStatus),
('upload', 'upload the current changelist to codereview', CmdUpload),
('upstream', 'print the name of the upstream branch, if any', CmdUpstream),
]
def Usage(name):
print 'usage: %s <command>' % name
print 'commands are:'
for name, desc, _ in COMMANDS:
print ' %-10s %s' % (name, desc)
sys.exit(1)
def main(argv):
if len(argv) < 2:
Usage(argv[0])
command = argv[1]
for name, _, func in COMMANDS:
if name == command:
return func(argv[2:])
print 'unknown command: %s' % command
Usage(argv[0])
if __name__ == '__main__':
sys.exit(main(sys.argv))