blob: b43f7c83524ebc4752115798c1687d285012b9f6 [file] [log] [blame]
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001#!/usr/bin/python
2# git-cl -- a git-command for integrating reviews on Rietveld
3# Copyright (C) 2008 Evan Martin <martine@danga.com>
4
5import errno
6import logging
7import optparse
8import os
9import re
dpranke@chromium.org970c5222011-03-12 00:32:24 +000010import StringIO
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000011import subprocess
12import sys
13import tempfile
14import textwrap
15import upload
msb@chromium.orgbf1a7ba2011-02-01 16:21:46 +000016import urlparse
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000017import urllib2
18
19try:
20 import readline
21except ImportError:
22 pass
23
24try:
dpranke@chromium.org23beb9e2011-03-11 23:20:54 +000025 # TODO(dpranke): We wrap this in a try block for a limited form of
26 # backwards-compatibility with older versions of git-cl that weren't
27 # dependent on depot_tools. This version should still work outside of
28 # depot_tools as long as --bypass-hooks is used. We should remove this
29 # once this has baked for a while and things seem safe.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000030 depot_tools_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
31 sys.path.append(depot_tools_path)
32 import breakpad
33except ImportError:
34 pass
35
36DEFAULT_SERVER = 'http://codereview.appspot.com'
maruel@chromium.org0ba7f962011-01-11 22:13:58 +000037POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s'
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000038DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup'
39
40def DieWithError(message):
dpranke@chromium.org970c5222011-03-12 00:32:24 +000041 print >> sys.stderr, message
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000042 sys.exit(1)
43
44
45def Popen(cmd, **kwargs):
46 """Wrapper for subprocess.Popen() that logs and watch for cygwin issues"""
47 logging.info('Popen: ' + ' '.join(cmd))
48 try:
49 return subprocess.Popen(cmd, **kwargs)
50 except OSError, e:
51 if e.errno == errno.EAGAIN and sys.platform == 'cygwin':
52 DieWithError(
53 'Visit '
54 'http://code.google.com/p/chromium/wiki/CygwinDllRemappingFailure to '
55 'learn how to fix this error; you need to rebase your cygwin dlls')
56 raise
57
58
59def RunCommand(cmd, error_ok=False, error_message=None,
maruel@chromium.orgb92e4802011-03-03 20:22:00 +000060 redirect_stdout=True, swallow_stderr=False, **kwargs):
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000061 if redirect_stdout:
62 stdout = subprocess.PIPE
63 else:
64 stdout = None
65 if swallow_stderr:
66 stderr = subprocess.PIPE
67 else:
68 stderr = None
maruel@chromium.orgb92e4802011-03-03 20:22:00 +000069 proc = Popen(cmd, stdout=stdout, stderr=stderr, **kwargs)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000070 output = proc.communicate()[0]
71 if not error_ok and proc.returncode != 0:
72 DieWithError('Command "%s" failed.\n' % (' '.join(cmd)) +
73 (error_message or output or ''))
74 return output
75
76
77def RunGit(args, **kwargs):
78 cmd = ['git'] + args
79 return RunCommand(cmd, **kwargs)
80
81
82def RunGitWithCode(args):
83 proc = Popen(['git'] + args, stdout=subprocess.PIPE)
84 output = proc.communicate()[0]
85 return proc.returncode, output
86
87
88def usage(more):
89 def hook(fn):
90 fn.usage_more = more
91 return fn
92 return hook
93
94
95def FixUrl(server):
96 """Fix a server url to defaults protocol to http:// if none is specified."""
97 if not server:
98 return server
99 if not re.match(r'[a-z]+\://.*', server):
100 return 'http://' + server
101 return server
102
103
104class Settings(object):
105 def __init__(self):
106 self.default_server = None
107 self.cc = None
108 self.root = None
109 self.is_git_svn = None
110 self.svn_branch = None
111 self.tree_status_url = None
112 self.viewvc_url = None
113 self.updated = False
114
115 def LazyUpdateIfNeeded(self):
116 """Updates the settings from a codereview.settings file, if available."""
117 if not self.updated:
118 cr_settings_file = FindCodereviewSettingsFile()
119 if cr_settings_file:
120 LoadCodereviewSettingsFromFile(cr_settings_file)
121 self.updated = True
122
123 def GetDefaultServerUrl(self, error_ok=False):
124 if not self.default_server:
125 self.LazyUpdateIfNeeded()
126 self.default_server = FixUrl(self._GetConfig('rietveld.server',
127 error_ok=True))
128 if error_ok:
129 return self.default_server
130 if not self.default_server:
131 error_message = ('Could not find settings file. You must configure '
132 'your review setup by running "git cl config".')
133 self.default_server = FixUrl(self._GetConfig(
134 'rietveld.server', error_message=error_message))
135 return self.default_server
136
137 def GetCCList(self):
138 """Return the users cc'd on this CL.
139
140 Return is a string suitable for passing to gcl with the --cc flag.
141 """
142 if self.cc is None:
maruel@chromium.orgb2a7c332011-02-25 20:30:37 +0000143 base_cc = self._GetConfig('rietveld.cc', error_ok=True)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000144 more_cc = self._GetConfig('rietveld.extracc', error_ok=True)
maruel@chromium.orgb2a7c332011-02-25 20:30:37 +0000145 self.cc = ','.join(filter(None, (base_cc, more_cc))) or ''
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000146 return self.cc
147
148 def GetRoot(self):
149 if not self.root:
150 self.root = os.path.abspath(RunGit(['rev-parse', '--show-cdup']).strip())
151 return self.root
152
153 def GetIsGitSvn(self):
154 """Return true if this repo looks like it's using git-svn."""
155 if self.is_git_svn is None:
156 # If you have any "svn-remote.*" config keys, we think you're using svn.
157 self.is_git_svn = RunGitWithCode(
158 ['config', '--get-regexp', r'^svn-remote\.'])[0] == 0
159 return self.is_git_svn
160
161 def GetSVNBranch(self):
162 if self.svn_branch is None:
163 if not self.GetIsGitSvn():
164 DieWithError('Repo doesn\'t appear to be a git-svn repo.')
165
166 # Try to figure out which remote branch we're based on.
167 # Strategy:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000168 # 1) iterate through our branch history and find the svn URL.
169 # 2) find the svn-remote that fetches from the URL.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000170
171 # regexp matching the git-svn line that contains the URL.
172 git_svn_re = re.compile(r'^\s*git-svn-id: (\S+)@', re.MULTILINE)
173
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000174 # We don't want to go through all of history, so read a line from the
175 # pipe at a time.
176 # The -100 is an arbitrary limit so we don't search forever.
177 cmd = ['git', 'log', '-100', '--pretty=medium']
178 proc = Popen(cmd, stdout=subprocess.PIPE)
179 for line in proc.stdout:
180 match = git_svn_re.match(line)
181 if match:
182 url = match.group(1)
183 proc.stdout.close() # Cut pipe.
184 break
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000185
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000186 if url:
187 svn_remote_re = re.compile(r'^svn-remote\.([^.]+)\.url (.*)$')
188 remotes = RunGit(['config', '--get-regexp',
189 r'^svn-remote\..*\.url']).splitlines()
190 for remote in remotes:
191 match = svn_remote_re.match(remote)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000192 if match:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000193 remote = match.group(1)
194 base_url = match.group(2)
195 fetch_spec = RunGit(
196 ['config', 'svn-remote.'+remote+'.fetch']).strip().split(':')
197 if fetch_spec[0]:
198 full_url = base_url + '/' + fetch_spec[0]
199 else:
200 full_url = base_url
201 if full_url == url:
202 self.svn_branch = fetch_spec[1]
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000203 break
204
205 if not self.svn_branch:
206 DieWithError('Can\'t guess svn branch -- try specifying it on the '
207 'command line')
208
209 return self.svn_branch
210
211 def GetTreeStatusUrl(self, error_ok=False):
212 if not self.tree_status_url:
213 error_message = ('You must configure your tree status URL by running '
214 '"git cl config".')
215 self.tree_status_url = self._GetConfig('rietveld.tree-status-url',
216 error_ok=error_ok,
217 error_message=error_message)
218 return self.tree_status_url
219
220 def GetViewVCUrl(self):
221 if not self.viewvc_url:
222 self.viewvc_url = self._GetConfig('rietveld.viewvc-url', error_ok=True)
223 return self.viewvc_url
224
225 def _GetConfig(self, param, **kwargs):
226 self.LazyUpdateIfNeeded()
227 return RunGit(['config', param], **kwargs).strip()
228
229
230settings = Settings()
231
232
233did_migrate_check = False
234def CheckForMigration():
235 """Migrate from the old issue format, if found.
236
237 We used to store the branch<->issue mapping in a file in .git, but it's
238 better to store it in the .git/config, since deleting a branch deletes that
239 branch's entry there.
240 """
241
242 # Don't run more than once.
243 global did_migrate_check
244 if did_migrate_check:
245 return
246
247 gitdir = RunGit(['rev-parse', '--git-dir']).strip()
248 storepath = os.path.join(gitdir, 'cl-mapping')
249 if os.path.exists(storepath):
250 print "old-style git-cl mapping file (%s) found; migrating." % storepath
251 store = open(storepath, 'r')
252 for line in store:
253 branch, issue = line.strip().split()
254 RunGit(['config', 'branch.%s.rietveldissue' % ShortBranchName(branch),
255 issue])
256 store.close()
257 os.remove(storepath)
258 did_migrate_check = True
259
260
261def ShortBranchName(branch):
262 """Convert a name like 'refs/heads/foo' to just 'foo'."""
263 return branch.replace('refs/heads/', '')
264
265
266class Changelist(object):
267 def __init__(self, branchref=None):
268 # Poke settings so we get the "configure your server" message if necessary.
269 settings.GetDefaultServerUrl()
270 self.branchref = branchref
271 if self.branchref:
272 self.branch = ShortBranchName(self.branchref)
273 else:
274 self.branch = None
275 self.rietveld_server = None
276 self.upstream_branch = None
277 self.has_issue = False
278 self.issue = None
279 self.has_description = False
280 self.description = None
281 self.has_patchset = False
282 self.patchset = None
283
284 def GetBranch(self):
285 """Returns the short branch name, e.g. 'master'."""
286 if not self.branch:
287 self.branchref = RunGit(['symbolic-ref', 'HEAD']).strip()
288 self.branch = ShortBranchName(self.branchref)
289 return self.branch
290
291 def GetBranchRef(self):
292 """Returns the full branch name, e.g. 'refs/heads/master'."""
293 self.GetBranch() # Poke the lazy loader.
294 return self.branchref
295
296 def FetchUpstreamTuple(self):
297 """Returns a tuple containg remote and remote ref,
298 e.g. 'origin', 'refs/heads/master'
299 """
300 remote = '.'
301 branch = self.GetBranch()
302 upstream_branch = RunGit(['config', 'branch.%s.merge' % branch],
303 error_ok=True).strip()
304 if upstream_branch:
305 remote = RunGit(['config', 'branch.%s.remote' % branch]).strip()
306 else:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000307 upstream_branch = RunGit(['config', 'rietveld.upstream-branch'],
308 error_ok=True).strip()
309 if upstream_branch:
310 remote = RunGit(['config', 'rietveld.upstream-remote']).strip()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000311 else:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000312 # Fall back on trying a git-svn upstream branch.
313 if settings.GetIsGitSvn():
314 upstream_branch = settings.GetSVNBranch()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000315 else:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000316 # Else, try to guess the origin remote.
317 remote_branches = RunGit(['branch', '-r']).split()
318 if 'origin/master' in remote_branches:
319 # Fall back on origin/master if it exits.
320 remote = 'origin'
321 upstream_branch = 'refs/heads/master'
322 elif 'origin/trunk' in remote_branches:
323 # Fall back on origin/trunk if it exists. Generally a shared
324 # git-svn clone
325 remote = 'origin'
326 upstream_branch = 'refs/heads/trunk'
327 else:
328 DieWithError("""Unable to determine default branch to diff against.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000329Either pass complete "git diff"-style arguments, like
330 git cl upload origin/master
331or verify this branch is set up to track another (via the --track argument to
332"git checkout -b ...").""")
333
334 return remote, upstream_branch
335
336 def GetUpstreamBranch(self):
337 if self.upstream_branch is None:
338 remote, upstream_branch = self.FetchUpstreamTuple()
339 if remote is not '.':
340 upstream_branch = upstream_branch.replace('heads', 'remotes/' + remote)
341 self.upstream_branch = upstream_branch
342 return self.upstream_branch
343
344 def GetRemoteUrl(self):
345 """Return the configured remote URL, e.g. 'git://example.org/foo.git/'.
346
347 Returns None if there is no remote.
348 """
349 remote = self.FetchUpstreamTuple()[0]
350 if remote == '.':
351 return None
352 return RunGit(['config', 'remote.%s.url' % remote], error_ok=True).strip()
353
354 def GetIssue(self):
355 if not self.has_issue:
356 CheckForMigration()
357 issue = RunGit(['config', self._IssueSetting()], error_ok=True).strip()
358 if issue:
359 self.issue = issue
360 self.rietveld_server = FixUrl(RunGit(
361 ['config', self._RietveldServer()], error_ok=True).strip())
362 else:
363 self.issue = None
364 if not self.rietveld_server:
365 self.rietveld_server = settings.GetDefaultServerUrl()
366 self.has_issue = True
367 return self.issue
368
369 def GetRietveldServer(self):
370 self.GetIssue()
371 return self.rietveld_server
372
373 def GetIssueURL(self):
374 """Get the URL for a particular issue."""
375 return '%s/%s' % (self.GetRietveldServer(), self.GetIssue())
376
377 def GetDescription(self, pretty=False):
378 if not self.has_description:
379 if self.GetIssue():
380 path = '/' + self.GetIssue() + '/description'
381 rpc_server = self._RpcServer()
382 self.description = rpc_server.Send(path).strip()
383 self.has_description = True
384 if pretty:
385 wrapper = textwrap.TextWrapper()
386 wrapper.initial_indent = wrapper.subsequent_indent = ' '
387 return wrapper.fill(self.description)
388 return self.description
389
390 def GetPatchset(self):
391 if not self.has_patchset:
392 patchset = RunGit(['config', self._PatchsetSetting()],
393 error_ok=True).strip()
394 if patchset:
395 self.patchset = patchset
396 else:
397 self.patchset = None
398 self.has_patchset = True
399 return self.patchset
400
401 def SetPatchset(self, patchset):
402 """Set this branch's patchset. If patchset=0, clears the patchset."""
403 if patchset:
404 RunGit(['config', self._PatchsetSetting(), str(patchset)])
405 else:
406 RunGit(['config', '--unset', self._PatchsetSetting()],
407 swallow_stderr=True, error_ok=True)
408 self.has_patchset = False
409
410 def SetIssue(self, issue):
411 """Set this branch's issue. If issue=0, clears the issue."""
412 if issue:
413 RunGit(['config', self._IssueSetting(), str(issue)])
414 if self.rietveld_server:
415 RunGit(['config', self._RietveldServer(), self.rietveld_server])
416 else:
417 RunGit(['config', '--unset', self._IssueSetting()])
418 self.SetPatchset(0)
419 self.has_issue = False
420
421 def CloseIssue(self):
422 rpc_server = self._RpcServer()
423 # Newer versions of Rietveld require us to pass an XSRF token to POST, so
424 # we fetch it from the server. (The version used by Chromium has been
425 # modified so the token isn't required when closing an issue.)
426 xsrf_token = rpc_server.Send('/xsrf_token',
427 extra_headers={'X-Requesting-XSRF-Token': '1'})
428
429 # You cannot close an issue with a GET.
430 # We pass an empty string for the data so it is a POST rather than a GET.
431 data = [("description", self.description),
432 ("xsrf_token", xsrf_token)]
433 ctype, body = upload.EncodeMultipartFormData(data, [])
434 rpc_server.Send('/' + self.GetIssue() + '/close', body, ctype)
435
436 def _RpcServer(self):
437 """Returns an upload.RpcServer() to access this review's rietveld instance.
438 """
439 server = self.GetRietveldServer()
440 return upload.GetRpcServer(server, save_cookies=True)
441
442 def _IssueSetting(self):
443 """Return the git setting that stores this change's issue."""
444 return 'branch.%s.rietveldissue' % self.GetBranch()
445
446 def _PatchsetSetting(self):
447 """Return the git setting that stores this change's most recent patchset."""
448 return 'branch.%s.rietveldpatchset' % self.GetBranch()
449
450 def _RietveldServer(self):
451 """Returns the git setting that stores this change's rietveld server."""
452 return 'branch.%s.rietveldserver' % self.GetBranch()
453
454
455def GetCodereviewSettingsInteractively():
456 """Prompt the user for settings."""
457 server = settings.GetDefaultServerUrl(error_ok=True)
458 prompt = 'Rietveld server (host[:port])'
459 prompt += ' [%s]' % (server or DEFAULT_SERVER)
460 newserver = raw_input(prompt + ': ')
461 if not server and not newserver:
462 newserver = DEFAULT_SERVER
463 if newserver and newserver != server:
464 RunGit(['config', 'rietveld.server', newserver])
465
466 def SetProperty(initial, caption, name):
467 prompt = caption
468 if initial:
469 prompt += ' ("x" to clear) [%s]' % initial
470 new_val = raw_input(prompt + ': ')
471 if new_val == 'x':
472 RunGit(['config', '--unset-all', 'rietveld.' + name], error_ok=True)
473 elif new_val and new_val != initial:
474 RunGit(['config', 'rietveld.' + name, new_val])
475
476 SetProperty(settings.GetCCList(), 'CC list', 'cc')
477 SetProperty(settings.GetTreeStatusUrl(error_ok=True), 'Tree status URL',
478 'tree-status-url')
479 SetProperty(settings.GetViewVCUrl(), 'ViewVC URL', 'viewvc-url')
480
481 # TODO: configure a default branch to diff against, rather than this
482 # svn-based hackery.
483
484
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000485class ChangeDescription(object):
486 """Contains a parsed form of the change description."""
487 def __init__(self, subject, log_desc, reviewers):
488 self.subject = subject
489 self.log_desc = log_desc
490 self.reviewers = reviewers
491 self.description = self.log_desc
492
493 def Update(self):
494 initial_text = """# Enter a description of the change.
495# This will displayed on the codereview site.
496# The first line will also be used as the subject of the review.
497"""
498 initial_text += self.description
499 if 'R=' not in self.description and self.reviewers:
500 initial_text += '\nR=' + self.reviewers
501 if 'BUG=' not in self.description:
502 initial_text += '\nBUG='
503 if 'TEST=' not in self.description:
504 initial_text += '\nTEST='
505 self._ParseDescription(UserEditedLog(initial_text))
506
507 def _ParseDescription(self, description):
508 if not description:
509 self.description = description
510 return
511
512 parsed_lines = []
513 reviewers_regexp = re.compile('\s*R=(.+)')
514 reviewers = ''
515 subject = ''
516 for l in description.splitlines():
517 if not subject:
518 subject = l
519 matched_reviewers = reviewers_regexp.match(l)
520 if matched_reviewers:
521 reviewers = matched_reviewers.group(1)
522 parsed_lines.append(l)
523
524 self.description = '\n'.join(parsed_lines) + '\n'
525 self.subject = subject
526 self.reviewers = reviewers
527
528 def IsEmpty(self):
529 return not self.description
530
531
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000532def FindCodereviewSettingsFile(filename='codereview.settings'):
533 """Finds the given file starting in the cwd and going up.
534
535 Only looks up to the top of the repository unless an
536 'inherit-review-settings-ok' file exists in the root of the repository.
537 """
538 inherit_ok_file = 'inherit-review-settings-ok'
539 cwd = os.getcwd()
540 root = os.path.abspath(RunGit(['rev-parse', '--show-cdup']).strip())
541 if os.path.isfile(os.path.join(root, inherit_ok_file)):
542 root = '/'
543 while True:
544 if filename in os.listdir(cwd):
545 if os.path.isfile(os.path.join(cwd, filename)):
546 return open(os.path.join(cwd, filename))
547 if cwd == root:
548 break
549 cwd = os.path.dirname(cwd)
550
551
552def LoadCodereviewSettingsFromFile(fileobj):
553 """Parse a codereview.settings file and updates hooks."""
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000554 keyvals = {}
555 for line in fileobj.read().splitlines():
556 if not line or line.startswith("#"):
557 continue
558 k, v = line.split(": ", 1)
559 keyvals[k] = v
560
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000561 def SetProperty(name, setting, unset_error_ok=False):
562 fullname = 'rietveld.' + name
563 if setting in keyvals:
564 RunGit(['config', fullname, keyvals[setting]])
565 else:
566 RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok)
567
568 SetProperty('server', 'CODE_REVIEW_SERVER')
569 # Only server setting is required. Other settings can be absent.
570 # In that case, we ignore errors raised during option deletion attempt.
571 SetProperty('cc', 'CC_LIST', unset_error_ok=True)
572 SetProperty('tree-status-url', 'STATUS', unset_error_ok=True)
573 SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True)
574
575 if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals:
576 #should be of the form
577 #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof
578 #ORIGIN_URL_CONFIG: http://src.chromium.org/git
579 RunGit(['config', keyvals['PUSH_URL_CONFIG'],
580 keyvals['ORIGIN_URL_CONFIG']])
581
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000582
583@usage('[repo root containing codereview.settings]')
584def CMDconfig(parser, args):
585 """edit configuration for this tree"""
586
587 (options, args) = parser.parse_args(args)
588 if len(args) == 0:
589 GetCodereviewSettingsInteractively()
590 return 0
591
592 url = args[0]
593 if not url.endswith('codereview.settings'):
594 url = os.path.join(url, 'codereview.settings')
595
596 # Load code review settings and download hooks (if available).
597 LoadCodereviewSettingsFromFile(urllib2.urlopen(url))
598 return 0
599
600
601def CMDstatus(parser, args):
602 """show status of changelists"""
603 parser.add_option('--field',
604 help='print only specific field (desc|id|patch|url)')
605 (options, args) = parser.parse_args(args)
606
607 # TODO: maybe make show_branches a flag if necessary.
608 show_branches = not options.field
609
610 if show_branches:
611 branches = RunGit(['for-each-ref', '--format=%(refname)', 'refs/heads'])
612 if branches:
613 print 'Branches associated with reviews:'
614 for branch in sorted(branches.splitlines()):
615 cl = Changelist(branchref=branch)
616 print " %10s: %s" % (cl.GetBranch(), cl.GetIssue())
617
618 cl = Changelist()
619 if options.field:
620 if options.field.startswith('desc'):
621 print cl.GetDescription()
622 elif options.field == 'id':
623 issueid = cl.GetIssue()
624 if issueid:
625 print issueid
626 elif options.field == 'patch':
627 patchset = cl.GetPatchset()
628 if patchset:
629 print patchset
630 elif options.field == 'url':
631 url = cl.GetIssueURL()
632 if url:
633 print url
634 else:
635 print
636 print 'Current branch:',
637 if not cl.GetIssue():
638 print 'no issue assigned.'
639 return 0
640 print cl.GetBranch()
641 print 'Issue number:', cl.GetIssue(), '(%s)' % cl.GetIssueURL()
642 print 'Issue description:'
643 print cl.GetDescription(pretty=True)
644 return 0
645
646
647@usage('[issue_number]')
648def CMDissue(parser, args):
649 """Set or display the current code review issue number.
650
651 Pass issue number 0 to clear the current issue.
652"""
653 (options, args) = parser.parse_args(args)
654
655 cl = Changelist()
656 if len(args) > 0:
657 try:
658 issue = int(args[0])
659 except ValueError:
660 DieWithError('Pass a number to set the issue or none to list it.\n'
661 'Maybe you want to run git cl status?')
662 cl.SetIssue(issue)
663 print 'Issue number:', cl.GetIssue(), '(%s)' % cl.GetIssueURL()
664 return 0
665
666
667def CreateDescriptionFromLog(args):
668 """Pulls out the commit log to use as a base for the CL description."""
669 log_args = []
670 if len(args) == 1 and not args[0].endswith('.'):
671 log_args = [args[0] + '..']
672 elif len(args) == 1 and args[0].endswith('...'):
673 log_args = [args[0][:-1]]
674 elif len(args) == 2:
675 log_args = [args[0] + '..' + args[1]]
676 else:
677 log_args = args[:] # Hope for the best!
678 return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args)
679
680
681def UserEditedLog(starting_text):
682 """Given some starting text, let the user edit it and return the result."""
683 editor = os.getenv('EDITOR', 'vi')
684
685 (file_handle, filename) = tempfile.mkstemp()
686 fileobj = os.fdopen(file_handle, 'w')
687 fileobj.write(starting_text)
688 fileobj.close()
689
mhm@chromium.orgd161d842011-03-12 18:56:50 +0000690 # Open up the default editor in the system to get the CL description.
thomasvl@chromium.org68f27462011-03-14 14:54:27 +0000691 ret = subprocess.call(editor + ' ' + filename, shell=True)
692 if ret != 0:
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000693 os.remove(filename)
mhm@chromium.org3d4d7ed2011-03-12 02:19:19 +0000694 return
thomasvl@chromium.org68f27462011-03-14 14:54:27 +0000695 fileobj = open(filename)
696 text = fileobj.read()
697 fileobj.close()
698
699 os.remove(filename)
dpranke@chromium.org37248c02011-03-12 04:36:48 +0000700
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000701 stripcomment_re = re.compile(r'^#.*$', re.MULTILINE)
thomasvl@chromium.org68f27462011-03-14 14:54:27 +0000702 return stripcomment_re.sub('', text).strip()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000703
704
dpranke@chromium.org23beb9e2011-03-11 23:20:54 +0000705def ConvertToInteger(inputval):
706 """Convert a string to integer, but returns either an int or None."""
707 try:
708 return int(inputval)
709 except (TypeError, ValueError):
710 return None
711
712
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000713def RunHook(committing, upstream_branch, rietveld_server, tbr, may_prompt):
714 """Calls sys.exit() if the hook fails; returns a HookResults otherwise."""
dpranke@chromium.org23beb9e2011-03-11 23:20:54 +0000715 import presubmit_support
716 import scm
717 import watchlists
718
719 root = RunCommand(['git', 'rev-parse', '--show-cdup']).strip()
720 if not root:
dpranke@chromium.orgdc276cb2011-03-14 21:30:10 +0000721 root = '.'
dpranke@chromium.org23beb9e2011-03-11 23:20:54 +0000722 absroot = os.path.abspath(root)
723 if not root:
dpranke@chromium.orgdc276cb2011-03-14 21:30:10 +0000724 raise Exception('Could not get root directory.')
dpranke@chromium.org23beb9e2011-03-11 23:20:54 +0000725
726 # We use the sha1 of HEAD as a name of this change.
727 name = RunCommand(['git', 'rev-parse', 'HEAD']).strip()
728 files = scm.GIT.CaptureStatus([root], upstream_branch)
729
730 cl = Changelist()
731 issue = ConvertToInteger(cl.GetIssue())
732 patchset = ConvertToInteger(cl.GetPatchset())
733 if issue:
734 description = cl.GetDescription()
735 else:
736 # If the change was never uploaded, use the log messages of all commits
737 # up to the branch point, as git cl upload will prefill the description
738 # with these log messages.
739 description = RunCommand(['git', 'log', '--pretty=format:%s%n%n%b',
dpranke@chromium.orgdc276cb2011-03-14 21:30:10 +0000740 '%s...' % (upstream_branch)]).strip()
dpranke@chromium.org23beb9e2011-03-11 23:20:54 +0000741 change = presubmit_support.GitChange(name, description, absroot, files,
742 issue, patchset)
743
744 # Apply watchlists on upload.
745 if not committing:
746 watchlist = watchlists.Watchlists(change.RepositoryRoot())
747 files = [f.LocalPath() for f in change.AffectedFiles()]
748 watchers = watchlist.GetWatchersForPaths(files)
749 RunCommand(['git', 'config', '--replace-all',
dpranke@chromium.orgdc276cb2011-03-14 21:30:10 +0000750 'rietveld.extracc', ','.join(watchers)])
dpranke@chromium.org23beb9e2011-03-11 23:20:54 +0000751
dpranke@chromium.org5ac21012011-03-16 02:58:25 +0000752 output = presubmit_support.DoPresubmitChecks(change, committing,
753 verbose=False, output_stream=sys.stdout, input_stream=sys.stdin,
754 default_presubmit=None, may_prompt=may_prompt, tbr=tbr,
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000755 host_url=cl.GetRietveldServer())
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000756
757 # TODO(dpranke): We should propagate the error out instead of calling exit().
dpranke@chromium.org5ac21012011-03-16 02:58:25 +0000758 if not output.should_continue():
759 sys.exit(1)
dpranke@chromium.org37248c02011-03-12 04:36:48 +0000760
dpranke@chromium.org5ac21012011-03-16 02:58:25 +0000761 return output
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000762
763
764def CMDpresubmit(parser, args):
765 """run presubmit tests on the current changelist"""
766 parser.add_option('--upload', action='store_true',
767 help='Run upload hook instead of the push/dcommit hook')
768 (options, args) = parser.parse_args(args)
769
770 # Make sure index is up-to-date before running diff-index.
771 RunGit(['update-index', '--refresh', '-q'], error_ok=True)
772 if RunGit(['diff-index', 'HEAD']):
773 # TODO(maruel): Is this really necessary?
774 print 'Cannot presubmit with a dirty tree. You must commit locally first.'
775 return 1
776
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000777 cl = Changelist()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000778 if args:
779 base_branch = args[0]
780 else:
781 # Default to diffing against the "upstream" branch.
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000782 base_branch = cl.GetUpstreamBranch()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000783
784 if options.upload:
785 print '*** Presubmit checks for UPLOAD would report: ***'
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000786 RunHook(committing=False, upstream_branch=base_branch,
787 rietveld_server=cl.GetRietveldServer(), tbr=False,
788 may_prompt=False)
789 return 0
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000790 else:
791 print '*** Presubmit checks for DCOMMIT would report: ***'
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000792 RunHook(committing=True, upstream_branch=base_branch,
793 rietveld_server=cl.GetRietveldServer, tbr=False,
794 may_prompt=False)
795 return 0
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000796
797
798@usage('[args to "git diff"]')
799def CMDupload(parser, args):
800 """upload the current changelist to codereview"""
801 parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
802 help='bypass upload presubmit hook')
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000803 parser.add_option('-f', action='store_true', dest='force',
804 help="force yes to questions (don't prompt)")
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000805 parser.add_option('-m', dest='message', help='message for patch')
806 parser.add_option('-r', '--reviewers',
807 help='reviewer email addresses')
maruel@chromium.orgb2a7c332011-02-25 20:30:37 +0000808 parser.add_option('--cc',
809 help='cc email addresses')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000810 parser.add_option('--send-mail', action='store_true',
811 help='send email to reviewer immediately')
812 parser.add_option("--emulate_svn_auto_props", action="store_true",
813 dest="emulate_svn_auto_props",
814 help="Emulate Subversion's auto properties feature.")
815 parser.add_option("--desc_from_logs", action="store_true",
816 dest="from_logs",
817 help="""Squashes git commit logs into change description and
818 uses message as subject""")
819 (options, args) = parser.parse_args(args)
820
821 # Make sure index is up-to-date before running diff-index.
822 RunGit(['update-index', '--refresh', '-q'], error_ok=True)
823 if RunGit(['diff-index', 'HEAD']):
824 print 'Cannot upload with a dirty tree. You must commit locally first.'
825 return 1
826
827 cl = Changelist()
828 if args:
829 base_branch = args[0]
830 else:
831 # Default to diffing against the "upstream" branch.
832 base_branch = cl.GetUpstreamBranch()
833 args = [base_branch + "..."]
834
dpranke@chromium.org5ac21012011-03-16 02:58:25 +0000835 if not options.bypass_hooks and not options.force:
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000836 hook_results = RunHook(committing=False, upstream_branch=base_branch,
837 rietveld_server=cl.GetRietveldServer(), tbr=False,
dpranke@chromium.org5ac21012011-03-16 02:58:25 +0000838 may_prompt=True)
839 if not options.reviewers and hook_results.reviewers:
840 options.reviewers = hook_results.reviewers
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000841
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000842
843 # --no-ext-diff is broken in some versions of Git, so try to work around
844 # this by overriding the environment (but there is still a problem if the
845 # git config key "diff.external" is used).
846 env = os.environ.copy()
847 if 'GIT_EXTERNAL_DIFF' in env:
848 del env['GIT_EXTERNAL_DIFF']
849 subprocess.call(['git', 'diff', '--no-ext-diff', '--stat', '-M'] + args,
850 env=env)
851
852 upload_args = ['--assume_yes'] # Don't ask about untracked files.
853 upload_args.extend(['--server', cl.GetRietveldServer()])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000854 if options.emulate_svn_auto_props:
855 upload_args.append('--emulate_svn_auto_props')
856 if options.send_mail:
857 if not options.reviewers:
858 DieWithError("Must specify reviewers to send email.")
859 upload_args.append('--send_mail')
860 if options.from_logs and not options.message:
861 print 'Must set message for subject line if using desc_from_logs'
862 return 1
863
864 change_desc = None
865
866 if cl.GetIssue():
867 if options.message:
868 upload_args.extend(['--message', options.message])
869 upload_args.extend(['--issue', cl.GetIssue()])
870 print ("This branch is associated with issue %s. "
871 "Adding patch to that issue." % cl.GetIssue())
872 else:
873 log_desc = CreateDescriptionFromLog(args)
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000874 change_desc = ChangeDescription(options.message, log_desc,
875 options.reviewers)
876 if not options.from_logs:
877 change_desc.Update()
878
879 if change_desc.IsEmpty():
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000880 print "Description is empty; aborting."
881 return 1
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000882
883 upload_args.extend(['--message', change_desc.subject])
884 upload_args.extend(['--description', change_desc.description])
885 if change_desc.reviewers:
886 upload_args.extend(['--reviewers', change_desc.reviewers])
maruel@chromium.orgb2a7c332011-02-25 20:30:37 +0000887 cc = ','.join(filter(None, (settings.GetCCList(), options.cc)))
888 if cc:
889 upload_args.extend(['--cc', cc])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000890
891 # Include the upstream repo's URL in the change -- this is useful for
892 # projects that have their source spread across multiple repos.
893 remote_url = None
894 if settings.GetIsGitSvn():
maruel@chromium.orgb92e4802011-03-03 20:22:00 +0000895 # URL is dependent on the current directory.
896 data = RunGit(['svn', 'info'], cwd=settings.GetRoot())
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000897 if data:
898 keys = dict(line.split(': ', 1) for line in data.splitlines()
899 if ': ' in line)
900 remote_url = keys.get('URL', None)
901 else:
902 if cl.GetRemoteUrl() and '/' in cl.GetUpstreamBranch():
903 remote_url = (cl.GetRemoteUrl() + '@'
904 + cl.GetUpstreamBranch().split('/')[-1])
905 if remote_url:
906 upload_args.extend(['--base_url', remote_url])
907
908 try:
909 issue, patchset = upload.RealMain(['upload'] + upload_args + args)
910 except:
911 # If we got an exception after the user typed a description for their
912 # change, back up the description before re-raising.
913 if change_desc:
914 backup_path = os.path.expanduser(DESCRIPTION_BACKUP_FILE)
915 print '\nGot exception while uploading -- saving description to %s\n' \
916 % backup_path
917 backup_file = open(backup_path, 'w')
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000918 backup_file.write(change_desc.description)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000919 backup_file.close()
920 raise
921
922 if not cl.GetIssue():
923 cl.SetIssue(issue)
924 cl.SetPatchset(patchset)
925 return 0
926
927
928def SendUpstream(parser, args, cmd):
929 """Common code for CmdPush and CmdDCommit
930
931 Squashed commit into a single.
932 Updates changelog with metadata (e.g. pointer to review).
933 Pushes/dcommits the code upstream.
934 Updates review and closes.
935 """
936 parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
937 help='bypass upload presubmit hook')
938 parser.add_option('-m', dest='message',
939 help="override review description")
940 parser.add_option('-f', action='store_true', dest='force',
941 help="force yes to questions (don't prompt)")
942 parser.add_option('-c', dest='contributor',
943 help="external contributor for patch (appended to " +
944 "description and used as author for git). Should be " +
945 "formatted as 'First Last <email@example.com>'")
946 parser.add_option('--tbr', action='store_true', dest='tbr',
947 help="short for 'to be reviewed', commit branch " +
948 "even without uploading for review")
949 (options, args) = parser.parse_args(args)
950 cl = Changelist()
951
952 if not args or cmd == 'push':
953 # Default to merging against our best guess of the upstream branch.
954 args = [cl.GetUpstreamBranch()]
955
956 base_branch = args[0]
957
chase@chromium.orgc76e6752011-01-10 18:17:12 +0000958 # Make sure index is up-to-date before running diff-index.
959 RunGit(['update-index', '--refresh', '-q'], error_ok=True)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000960 if RunGit(['diff-index', 'HEAD']):
961 print 'Cannot %s with a dirty tree. You must commit locally first.' % cmd
962 return 1
963
964 # This rev-list syntax means "show all commits not in my branch that
965 # are in base_branch".
966 upstream_commits = RunGit(['rev-list', '^' + cl.GetBranchRef(),
967 base_branch]).splitlines()
968 if upstream_commits:
969 print ('Base branch "%s" has %d commits '
970 'not in this branch.' % (base_branch, len(upstream_commits)))
971 print 'Run "git merge %s" before attempting to %s.' % (base_branch, cmd)
972 return 1
973
974 if cmd == 'dcommit':
975 # This is the revision `svn dcommit` will commit on top of.
976 svn_head = RunGit(['log', '--grep=^git-svn-id:', '-1',
977 '--pretty=format:%H'])
978 extra_commits = RunGit(['rev-list', '^' + svn_head, base_branch])
979 if extra_commits:
980 print ('This branch has %d additional commits not upstreamed yet.'
981 % len(extra_commits.splitlines()))
982 print ('Upstream "%s" or rebase this branch on top of the upstream trunk '
983 'before attempting to %s.' % (base_branch, cmd))
984 return 1
985
dpranke@chromium.org5ac21012011-03-16 02:58:25 +0000986 if not options.bypass_hooks and not options.force:
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000987 RunHook(committing=True, upstream_branch=base_branch,
988 rietveld_server=cl.GetRietveldServer(), tbr=options.tbr,
dpranke@chromium.org5ac21012011-03-16 02:58:25 +0000989 may_prompt=True)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000990
991 if cmd == 'dcommit':
992 # Check the tree status if the tree status URL is set.
993 status = GetTreeStatus()
994 if 'closed' == status:
995 print ('The tree is closed. Please wait for it to reopen. Use '
996 '"git cl dcommit -f" to commit on a closed tree.')
997 return 1
998 elif 'unknown' == status:
999 print ('Unable to determine tree status. Please verify manually and '
1000 'use "git cl dcommit -f" to commit on a closed tree.')
1001
1002 description = options.message
1003 if not options.tbr:
1004 # It is important to have these checks early. Not only for user
1005 # convenience, but also because the cl object then caches the correct values
1006 # of these fields even as we're juggling branches for setting up the commit.
1007 if not cl.GetIssue():
1008 print 'Current issue unknown -- has this branch been uploaded?'
1009 print 'Use --tbr to commit without review.'
1010 return 1
1011
1012 if not description:
1013 description = cl.GetDescription()
1014
1015 if not description:
1016 print 'No description set.'
1017 print 'Visit %s/edit to set it.' % (cl.GetIssueURL())
1018 return 1
1019
1020 description += "\n\nReview URL: %s" % cl.GetIssueURL()
1021 else:
1022 if not description:
1023 # Submitting TBR. See if there's already a description in Rietveld, else
1024 # create a template description. Eitherway, give the user a chance to edit
1025 # it to fill in the TBR= field.
1026 if cl.GetIssue():
1027 description = cl.GetDescription()
1028
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001029 # TODO(dpranke): Update to use ChangeDescription object.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001030 if not description:
1031 description = """# Enter a description of the change.
1032# This will be used as the change log for the commit.
1033
1034"""
1035 description += CreateDescriptionFromLog(args)
1036
1037 description = UserEditedLog(description + '\nTBR=')
1038
1039 if not description:
1040 print "Description empty; aborting."
1041 return 1
1042
1043 if options.contributor:
1044 if not re.match('^.*\s<\S+@\S+>$', options.contributor):
1045 print "Please provide contibutor as 'First Last <email@example.com>'"
1046 return 1
1047 description += "\nPatch from %s." % options.contributor
1048 print 'Description:', repr(description)
1049
1050 branches = [base_branch, cl.GetBranchRef()]
1051 if not options.force:
1052 subprocess.call(['git', 'diff', '--stat'] + branches)
1053 raw_input("About to commit; enter to confirm.")
1054
1055 # We want to squash all this branch's commits into one commit with the
1056 # proper description.
bauerb@chromium.orgb4a75c42011-03-08 08:35:38 +00001057 # We do this by doing a "reset --soft" to the base branch (which keeps
1058 # the working copy the same), then dcommitting that.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001059 MERGE_BRANCH = 'git-cl-commit'
1060 # Delete the merge branch if it already exists.
1061 if RunGitWithCode(['show-ref', '--quiet', '--verify',
1062 'refs/heads/' + MERGE_BRANCH])[0] == 0:
1063 RunGit(['branch', '-D', MERGE_BRANCH])
1064
1065 # We might be in a directory that's present in this branch but not in the
1066 # trunk. Move up to the top of the tree so that git commands that expect a
1067 # valid CWD won't fail after we check out the merge branch.
1068 rel_base_path = RunGit(['rev-parse', '--show-cdup']).strip()
1069 if rel_base_path:
1070 os.chdir(rel_base_path)
1071
1072 # Stuff our change into the merge branch.
1073 # We wrap in a try...finally block so if anything goes wrong,
1074 # we clean up the branches.
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00001075 retcode = -1
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001076 try:
bauerb@chromium.orgb4a75c42011-03-08 08:35:38 +00001077 RunGit(['checkout', '-q', '-b', MERGE_BRANCH])
1078 RunGit(['reset', '--soft', base_branch])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001079 if options.contributor:
1080 RunGit(['commit', '--author', options.contributor, '-m', description])
1081 else:
1082 RunGit(['commit', '-m', description])
1083 if cmd == 'push':
1084 # push the merge branch.
1085 remote, branch = cl.FetchUpstreamTuple()
1086 retcode, output = RunGitWithCode(
1087 ['push', '--porcelain', remote, 'HEAD:%s' % branch])
1088 logging.debug(output)
1089 else:
1090 # dcommit the merge branch.
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00001091 retcode, output = RunGitWithCode(['svn', 'dcommit', '--no-rebase'])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001092 finally:
1093 # And then swap back to the original branch and clean up.
1094 RunGit(['checkout', '-q', cl.GetBranch()])
1095 RunGit(['branch', '-D', MERGE_BRANCH])
1096
1097 if cl.GetIssue():
1098 if cmd == 'dcommit' and 'Committed r' in output:
1099 revision = re.match('.*?\nCommitted r(\\d+)', output, re.DOTALL).group(1)
1100 elif cmd == 'push' and retcode == 0:
maruel@chromium.orgdf947ea2011-01-12 20:44:54 +00001101 match = (re.match(r'.*?([a-f0-9]{7})\.\.([a-f0-9]{7})$', l)
1102 for l in output.splitlines(False))
1103 match = filter(None, match)
1104 if len(match) != 1:
1105 DieWithError("Couldn't parse ouput to extract the committed hash:\n%s" %
1106 output)
1107 revision = match[0].group(2)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001108 else:
1109 return 1
1110 viewvc_url = settings.GetViewVCUrl()
1111 if viewvc_url and revision:
1112 cl.description += ('\n\nCommitted: ' + viewvc_url + revision)
1113 print ('Closing issue '
1114 '(you may be prompted for your codereview password)...')
1115 cl.CloseIssue()
1116 cl.SetIssue(0)
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00001117
1118 if retcode == 0:
1119 hook = POSTUPSTREAM_HOOK_PATTERN % cmd
1120 if os.path.isfile(hook):
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001121 RunCommand([hook, base_branch], error_ok=True)
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00001122
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001123 return 0
1124
1125
1126@usage('[upstream branch to apply against]')
1127def CMDdcommit(parser, args):
1128 """commit the current changelist via git-svn"""
1129 if not settings.GetIsGitSvn():
thakis@chromium.orgcde3bb62011-01-20 01:16:14 +00001130 message = """This doesn't appear to be an SVN repository.
1131If your project has a git mirror with an upstream SVN master, you probably need
1132to run 'git svn init', see your project's git mirror documentation.
1133If your project has a true writeable upstream repository, you probably want
1134to run 'git cl push' instead.
1135Choose wisely, if you get this wrong, your commit might appear to succeed but
1136will instead be silently ignored."""
1137 print(message)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001138 raw_input('[Press enter to dcommit or ctrl-C to quit]')
1139 return SendUpstream(parser, args, 'dcommit')
1140
1141
1142@usage('[upstream branch to apply against]')
1143def CMDpush(parser, args):
1144 """commit the current changelist via git"""
1145 if settings.GetIsGitSvn():
1146 print('This appears to be an SVN repository.')
1147 print('Are you sure you didn\'t mean \'git cl dcommit\'?')
1148 raw_input('[Press enter to push or ctrl-C to quit]')
1149 return SendUpstream(parser, args, 'push')
1150
1151
1152@usage('<patch url or issue id>')
1153def CMDpatch(parser, args):
1154 """patch in a code review"""
1155 parser.add_option('-b', dest='newbranch',
1156 help='create a new branch off trunk for the patch')
1157 parser.add_option('-f', action='store_true', dest='force',
1158 help='with -b, clobber any existing branch')
1159 parser.add_option('--reject', action='store_true', dest='reject',
1160 help='allow failed patches and spew .rej files')
1161 parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit',
1162 help="don't commit after patch applies")
1163 (options, args) = parser.parse_args(args)
1164 if len(args) != 1:
1165 parser.print_help()
1166 return 1
1167 input = args[0]
1168
1169 if re.match(r'\d+', input):
1170 # Input is an issue id. Figure out the URL.
1171 issue = input
1172 server = settings.GetDefaultServerUrl()
1173 fetch = urllib2.urlopen('%s/%s' % (server, issue)).read()
1174 m = re.search(r'/download/issue[0-9]+_[0-9]+.diff', fetch)
1175 if not m:
1176 DieWithError('Must pass an issue ID or full URL for '
1177 '\'Download raw patch set\'')
1178 url = '%s%s' % (server, m.group(0).strip())
1179 else:
1180 # Assume it's a URL to the patch. Default to http.
1181 input = FixUrl(input)
1182 match = re.match(r'.*?/issue(\d+)_\d+.diff', input)
1183 if match:
1184 issue = match.group(1)
1185 url = input
1186 else:
1187 DieWithError('Must pass an issue ID or full URL for '
1188 '\'Download raw patch set\'')
1189
1190 if options.newbranch:
1191 if options.force:
1192 RunGit(['branch', '-D', options.newbranch],
1193 swallow_stderr=True, error_ok=True)
1194 RunGit(['checkout', '-b', options.newbranch,
1195 Changelist().GetUpstreamBranch()])
1196
1197 # Switch up to the top-level directory, if necessary, in preparation for
1198 # applying the patch.
1199 top = RunGit(['rev-parse', '--show-cdup']).strip()
1200 if top:
1201 os.chdir(top)
1202
1203 patch_data = urllib2.urlopen(url).read()
1204 # Git patches have a/ at the beginning of source paths. We strip that out
1205 # with a sed script rather than the -p flag to patch so we can feed either
1206 # Git or svn-style patches into the same apply command.
1207 # re.sub() should be used but flags=re.MULTILINE is only in python 2.7.
1208 sed_proc = Popen(['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],
1209 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
1210 patch_data = sed_proc.communicate(patch_data)[0]
1211 if sed_proc.returncode:
1212 DieWithError('Git patch mungling failed.')
1213 logging.info(patch_data)
1214 # We use "git apply" to apply the patch instead of "patch" so that we can
1215 # pick up file adds.
1216 # The --index flag means: also insert into the index (so we catch adds).
1217 cmd = ['git', 'apply', '--index', '-p0']
1218 if options.reject:
1219 cmd.append('--reject')
1220 patch_proc = Popen(cmd, stdin=subprocess.PIPE)
1221 patch_proc.communicate(patch_data)
1222 if patch_proc.returncode:
1223 DieWithError('Failed to apply the patch')
1224
1225 # If we had an issue, commit the current state and register the issue.
1226 if not options.nocommit:
1227 RunGit(['commit', '-m', 'patch from issue %s' % issue])
1228 cl = Changelist()
1229 cl.SetIssue(issue)
1230 print "Committed patch."
1231 else:
1232 print "Patch applied to index."
1233 return 0
1234
1235
1236def CMDrebase(parser, args):
1237 """rebase current branch on top of svn repo"""
1238 # Provide a wrapper for git svn rebase to help avoid accidental
1239 # git svn dcommit.
1240 # It's the only command that doesn't use parser at all since we just defer
1241 # execution to git-svn.
1242 RunGit(['svn', 'rebase'] + args, redirect_stdout=False)
1243 return 0
1244
1245
1246def GetTreeStatus():
1247 """Fetches the tree status and returns either 'open', 'closed',
1248 'unknown' or 'unset'."""
1249 url = settings.GetTreeStatusUrl(error_ok=True)
1250 if url:
1251 status = urllib2.urlopen(url).read().lower()
1252 if status.find('closed') != -1 or status == '0':
1253 return 'closed'
1254 elif status.find('open') != -1 or status == '1':
1255 return 'open'
1256 return 'unknown'
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001257 return 'unset'
1258
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001259
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001260def GetTreeStatusReason():
1261 """Fetches the tree status from a json url and returns the message
1262 with the reason for the tree to be opened or closed."""
1263 # Don't import it at file level since simplejson is not installed by default
1264 # on python 2.5 and it is only used for git-cl tree which isn't often used,
1265 # forcing everyone to install simplejson isn't efficient.
1266 try:
1267 import simplejson as json
1268 except ImportError:
1269 try:
1270 import json
1271 # Some versions of python2.5 have an incomplete json module. Check to make
1272 # sure loads exists.
1273 json.loads
1274 except (ImportError, AttributeError):
1275 print >> sys.stderr, 'Please install simplejson'
1276 sys.exit(1)
1277
msb@chromium.orgbf1a7ba2011-02-01 16:21:46 +00001278 url = settings.GetTreeStatusUrl()
1279 json_url = urlparse.urljoin(url, '/current?format=json')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001280 connection = urllib2.urlopen(json_url)
1281 status = json.loads(connection.read())
1282 connection.close()
1283 return status['message']
1284
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001285
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001286def CMDtree(parser, args):
1287 """show the status of the tree"""
1288 (options, args) = parser.parse_args(args)
1289 status = GetTreeStatus()
1290 if 'unset' == status:
1291 print 'You must configure your tree status URL by running "git cl config".'
1292 return 2
1293
1294 print "The tree is %s" % status
1295 print
1296 print GetTreeStatusReason()
1297 if status != 'open':
1298 return 1
1299 return 0
1300
1301
1302def CMDupstream(parser, args):
1303 """print the name of the upstream branch, if any"""
1304 (options, args) = parser.parse_args(args)
1305 cl = Changelist()
1306 print cl.GetUpstreamBranch()
1307 return 0
1308
1309
1310def Command(name):
1311 return getattr(sys.modules[__name__], 'CMD' + name, None)
1312
1313
1314def CMDhelp(parser, args):
1315 """print list of commands or help for a specific command"""
1316 (options, args) = parser.parse_args(args)
1317 if len(args) == 1:
1318 return main(args + ['--help'])
1319 parser.print_help()
1320 return 0
1321
1322
1323def GenUsage(parser, command):
1324 """Modify an OptParse object with the function's documentation."""
1325 obj = Command(command)
1326 more = getattr(obj, 'usage_more', '')
1327 if command == 'help':
1328 command = '<command>'
1329 else:
1330 # OptParser.description prefer nicely non-formatted strings.
1331 parser.description = re.sub('[\r\n ]{2,}', ' ', obj.__doc__)
1332 parser.set_usage('usage: %%prog %s [options] %s' % (command, more))
1333
1334
1335def main(argv):
1336 """Doesn't parse the arguments here, just find the right subcommand to
1337 execute."""
1338 # Do it late so all commands are listed.
1339 CMDhelp.usage_more = ('\n\nCommands are:\n' + '\n'.join([
1340 ' %-10s %s' % (fn[3:], Command(fn[3:]).__doc__.split('\n')[0].strip())
1341 for fn in dir(sys.modules[__name__]) if fn.startswith('CMD')]))
1342
1343 # Create the option parse and add --verbose support.
1344 parser = optparse.OptionParser()
1345 parser.add_option('-v', '--verbose', action='store_true')
1346 old_parser_args = parser.parse_args
1347 def Parse(args):
1348 options, args = old_parser_args(args)
1349 if options.verbose:
1350 logging.basicConfig(level=logging.DEBUG)
1351 else:
1352 logging.basicConfig(level=logging.WARNING)
1353 return options, args
1354 parser.parse_args = Parse
1355
1356 if argv:
1357 command = Command(argv[0])
1358 if command:
1359 # "fix" the usage and the description now that we know the subcommand.
1360 GenUsage(parser, argv[0])
1361 try:
1362 return command(parser, argv[1:])
1363 except urllib2.HTTPError, e:
1364 if e.code != 500:
1365 raise
1366 DieWithError(
1367 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith '
1368 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e)))
1369
1370 # Not a known command. Default to help.
1371 GenUsage(parser, 'help')
1372 return CMDhelp(parser, argv)
1373
1374
1375if __name__ == '__main__':
1376 sys.exit(main(sys.argv[1:]))