blob: b0525b4f1d857a537eb294de1bfeb953767096f7 [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 HookResults(object):
486 """Contains the parsed output of the presubmit hooks."""
487 def __init__(self, output_from_hooks=None):
488 self.reviewers = []
489 self.output = None
490 self._ParseOutputFromHooks(output_from_hooks)
491
492 def _ParseOutputFromHooks(self, output_from_hooks):
493 if not output_from_hooks:
494 return
495 lines = []
496 reviewers = []
497 reviewer_regexp = re.compile('ADD: R=(.+)')
498 for l in output_from_hooks.splitlines():
499 m = reviewer_regexp.match(l)
500 if m:
501 reviewers.extend(m.group(1).split(','))
502 else:
503 lines.append(l)
504 self.output = '\n'.join(lines)
505 self.reviewers = ','.join(reviewers)
506
507
508class ChangeDescription(object):
509 """Contains a parsed form of the change description."""
510 def __init__(self, subject, log_desc, reviewers):
511 self.subject = subject
512 self.log_desc = log_desc
513 self.reviewers = reviewers
514 self.description = self.log_desc
515
516 def Update(self):
517 initial_text = """# Enter a description of the change.
518# This will displayed on the codereview site.
519# The first line will also be used as the subject of the review.
520"""
521 initial_text += self.description
522 if 'R=' not in self.description and self.reviewers:
523 initial_text += '\nR=' + self.reviewers
524 if 'BUG=' not in self.description:
525 initial_text += '\nBUG='
526 if 'TEST=' not in self.description:
527 initial_text += '\nTEST='
528 self._ParseDescription(UserEditedLog(initial_text))
529
530 def _ParseDescription(self, description):
531 if not description:
532 self.description = description
533 return
534
535 parsed_lines = []
536 reviewers_regexp = re.compile('\s*R=(.+)')
537 reviewers = ''
538 subject = ''
539 for l in description.splitlines():
540 if not subject:
541 subject = l
542 matched_reviewers = reviewers_regexp.match(l)
543 if matched_reviewers:
544 reviewers = matched_reviewers.group(1)
545 parsed_lines.append(l)
546
547 self.description = '\n'.join(parsed_lines) + '\n'
548 self.subject = subject
549 self.reviewers = reviewers
550
551 def IsEmpty(self):
552 return not self.description
553
554
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000555def FindCodereviewSettingsFile(filename='codereview.settings'):
556 """Finds the given file starting in the cwd and going up.
557
558 Only looks up to the top of the repository unless an
559 'inherit-review-settings-ok' file exists in the root of the repository.
560 """
561 inherit_ok_file = 'inherit-review-settings-ok'
562 cwd = os.getcwd()
563 root = os.path.abspath(RunGit(['rev-parse', '--show-cdup']).strip())
564 if os.path.isfile(os.path.join(root, inherit_ok_file)):
565 root = '/'
566 while True:
567 if filename in os.listdir(cwd):
568 if os.path.isfile(os.path.join(cwd, filename)):
569 return open(os.path.join(cwd, filename))
570 if cwd == root:
571 break
572 cwd = os.path.dirname(cwd)
573
574
575def LoadCodereviewSettingsFromFile(fileobj):
576 """Parse a codereview.settings file and updates hooks."""
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000577 keyvals = {}
578 for line in fileobj.read().splitlines():
579 if not line or line.startswith("#"):
580 continue
581 k, v = line.split(": ", 1)
582 keyvals[k] = v
583
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000584 def SetProperty(name, setting, unset_error_ok=False):
585 fullname = 'rietveld.' + name
586 if setting in keyvals:
587 RunGit(['config', fullname, keyvals[setting]])
588 else:
589 RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok)
590
591 SetProperty('server', 'CODE_REVIEW_SERVER')
592 # Only server setting is required. Other settings can be absent.
593 # In that case, we ignore errors raised during option deletion attempt.
594 SetProperty('cc', 'CC_LIST', unset_error_ok=True)
595 SetProperty('tree-status-url', 'STATUS', unset_error_ok=True)
596 SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True)
597
598 if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals:
599 #should be of the form
600 #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof
601 #ORIGIN_URL_CONFIG: http://src.chromium.org/git
602 RunGit(['config', keyvals['PUSH_URL_CONFIG'],
603 keyvals['ORIGIN_URL_CONFIG']])
604
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000605
606@usage('[repo root containing codereview.settings]')
607def CMDconfig(parser, args):
608 """edit configuration for this tree"""
609
610 (options, args) = parser.parse_args(args)
611 if len(args) == 0:
612 GetCodereviewSettingsInteractively()
613 return 0
614
615 url = args[0]
616 if not url.endswith('codereview.settings'):
617 url = os.path.join(url, 'codereview.settings')
618
619 # Load code review settings and download hooks (if available).
620 LoadCodereviewSettingsFromFile(urllib2.urlopen(url))
621 return 0
622
623
624def CMDstatus(parser, args):
625 """show status of changelists"""
626 parser.add_option('--field',
627 help='print only specific field (desc|id|patch|url)')
628 (options, args) = parser.parse_args(args)
629
630 # TODO: maybe make show_branches a flag if necessary.
631 show_branches = not options.field
632
633 if show_branches:
634 branches = RunGit(['for-each-ref', '--format=%(refname)', 'refs/heads'])
635 if branches:
636 print 'Branches associated with reviews:'
637 for branch in sorted(branches.splitlines()):
638 cl = Changelist(branchref=branch)
639 print " %10s: %s" % (cl.GetBranch(), cl.GetIssue())
640
641 cl = Changelist()
642 if options.field:
643 if options.field.startswith('desc'):
644 print cl.GetDescription()
645 elif options.field == 'id':
646 issueid = cl.GetIssue()
647 if issueid:
648 print issueid
649 elif options.field == 'patch':
650 patchset = cl.GetPatchset()
651 if patchset:
652 print patchset
653 elif options.field == 'url':
654 url = cl.GetIssueURL()
655 if url:
656 print url
657 else:
658 print
659 print 'Current branch:',
660 if not cl.GetIssue():
661 print 'no issue assigned.'
662 return 0
663 print cl.GetBranch()
664 print 'Issue number:', cl.GetIssue(), '(%s)' % cl.GetIssueURL()
665 print 'Issue description:'
666 print cl.GetDescription(pretty=True)
667 return 0
668
669
670@usage('[issue_number]')
671def CMDissue(parser, args):
672 """Set or display the current code review issue number.
673
674 Pass issue number 0 to clear the current issue.
675"""
676 (options, args) = parser.parse_args(args)
677
678 cl = Changelist()
679 if len(args) > 0:
680 try:
681 issue = int(args[0])
682 except ValueError:
683 DieWithError('Pass a number to set the issue or none to list it.\n'
684 'Maybe you want to run git cl status?')
685 cl.SetIssue(issue)
686 print 'Issue number:', cl.GetIssue(), '(%s)' % cl.GetIssueURL()
687 return 0
688
689
690def CreateDescriptionFromLog(args):
691 """Pulls out the commit log to use as a base for the CL description."""
692 log_args = []
693 if len(args) == 1 and not args[0].endswith('.'):
694 log_args = [args[0] + '..']
695 elif len(args) == 1 and args[0].endswith('...'):
696 log_args = [args[0][:-1]]
697 elif len(args) == 2:
698 log_args = [args[0] + '..' + args[1]]
699 else:
700 log_args = args[:] # Hope for the best!
701 return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args)
702
703
704def UserEditedLog(starting_text):
705 """Given some starting text, let the user edit it and return the result."""
706 editor = os.getenv('EDITOR', 'vi')
707
708 (file_handle, filename) = tempfile.mkstemp()
709 fileobj = os.fdopen(file_handle, 'w')
710 fileobj.write(starting_text)
711 fileobj.close()
712
mhm@chromium.orgd161d842011-03-12 18:56:50 +0000713 # Open up the default editor in the system to get the CL description.
thomasvl@chromium.orgca7953a2011-03-14 14:43:04 +0000714 result = None
mhm@chromium.org3d4d7ed2011-03-12 02:19:19 +0000715 try:
thomasvl@chromium.orgca7953a2011-03-14 14:43:04 +0000716 subprocess.check_call(['env', editor, filename], shell=True)
mhm@chromium.org3d4d7ed2011-03-12 02:19:19 +0000717 fileobj = open(filename)
718 result = fileobj.read()
719 fileobj.close()
720 finally:
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000721 os.remove(filename)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000722
mhm@chromium.org3d4d7ed2011-03-12 02:19:19 +0000723 if not result:
724 return
dpranke@chromium.org37248c02011-03-12 04:36:48 +0000725
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000726 stripcomment_re = re.compile(r'^#.*$', re.MULTILINE)
mhm@chromium.org3d4d7ed2011-03-12 02:19:19 +0000727 return stripcomment_re.sub('', result).strip()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000728
729
dpranke@chromium.org23beb9e2011-03-11 23:20:54 +0000730def ConvertToInteger(inputval):
731 """Convert a string to integer, but returns either an int or None."""
732 try:
733 return int(inputval)
734 except (TypeError, ValueError):
735 return None
736
737
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000738def RunHook(committing, upstream_branch, rietveld_server, tbr, may_prompt):
739 """Calls sys.exit() if the hook fails; returns a HookResults otherwise."""
dpranke@chromium.org23beb9e2011-03-11 23:20:54 +0000740 import presubmit_support
741 import scm
742 import watchlists
743
744 root = RunCommand(['git', 'rev-parse', '--show-cdup']).strip()
745 if not root:
746 root = "."
747 absroot = os.path.abspath(root)
748 if not root:
749 raise Exception("Could not get root directory.")
750
751 # We use the sha1 of HEAD as a name of this change.
752 name = RunCommand(['git', 'rev-parse', 'HEAD']).strip()
753 files = scm.GIT.CaptureStatus([root], upstream_branch)
754
755 cl = Changelist()
756 issue = ConvertToInteger(cl.GetIssue())
757 patchset = ConvertToInteger(cl.GetPatchset())
758 if issue:
759 description = cl.GetDescription()
760 else:
761 # If the change was never uploaded, use the log messages of all commits
762 # up to the branch point, as git cl upload will prefill the description
763 # with these log messages.
764 description = RunCommand(['git', 'log', '--pretty=format:%s%n%n%b',
765 '%s...' % (upstream_branch)]).strip()
766 change = presubmit_support.GitChange(name, description, absroot, files,
767 issue, patchset)
768
769 # Apply watchlists on upload.
770 if not committing:
771 watchlist = watchlists.Watchlists(change.RepositoryRoot())
772 files = [f.LocalPath() for f in change.AffectedFiles()]
773 watchers = watchlist.GetWatchersForPaths(files)
774 RunCommand(['git', 'config', '--replace-all',
775 'rietveld.extracc', ','.join(watchers)])
776
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000777 output = StringIO.StringIO()
dpranke@chromium.org5a2fefb2011-03-13 23:54:56 +0000778 should_continue = presubmit_support.DoPresubmitChecks(change, committing,
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000779 verbose=None, output_stream=output, input_stream=sys.stdin,
dpranke@chromium.org37248c02011-03-12 04:36:48 +0000780 default_presubmit=None, may_prompt=False, tbr=tbr,
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000781 host_url=cl.GetRietveldServer())
782 hook_results = HookResults(output.getvalue())
783 if hook_results.output:
784 print hook_results.output
785
786 # TODO(dpranke): We should propagate the error out instead of calling exit().
dpranke@chromium.org5a2fefb2011-03-13 23:54:56 +0000787 if should_continue and hook_results.output and (
788 '** Presubmit ERRORS **\n' in hook_results.output or
789 '** Presubmit WARNINGS **\n' in hook_results.output):
790 should_continue = False
dpranke@chromium.org37248c02011-03-12 04:36:48 +0000791
dpranke@chromium.org5a2fefb2011-03-13 23:54:56 +0000792 if not should_continue:
dpranke@chromium.org37248c02011-03-12 04:36:48 +0000793 if may_prompt:
794 response = raw_input('Are you sure you want to continue? (y/N): ')
795 if not response.lower().startswith('y'):
796 sys.exit(1)
797 else:
798 sys.exit(1)
799
800
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000801 return hook_results
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000802
803
804def CMDpresubmit(parser, args):
805 """run presubmit tests on the current changelist"""
806 parser.add_option('--upload', action='store_true',
807 help='Run upload hook instead of the push/dcommit hook')
808 (options, args) = parser.parse_args(args)
809
810 # Make sure index is up-to-date before running diff-index.
811 RunGit(['update-index', '--refresh', '-q'], error_ok=True)
812 if RunGit(['diff-index', 'HEAD']):
813 # TODO(maruel): Is this really necessary?
814 print 'Cannot presubmit with a dirty tree. You must commit locally first.'
815 return 1
816
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000817 cl = Changelist()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000818 if args:
819 base_branch = args[0]
820 else:
821 # Default to diffing against the "upstream" branch.
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000822 base_branch = cl.GetUpstreamBranch()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000823
824 if options.upload:
825 print '*** Presubmit checks for UPLOAD would report: ***'
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000826 RunHook(committing=False, upstream_branch=base_branch,
827 rietveld_server=cl.GetRietveldServer(), tbr=False,
828 may_prompt=False)
829 return 0
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000830 else:
831 print '*** Presubmit checks for DCOMMIT would report: ***'
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000832 RunHook(committing=True, upstream_branch=base_branch,
833 rietveld_server=cl.GetRietveldServer, tbr=False,
834 may_prompt=False)
835 return 0
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000836
837
838@usage('[args to "git diff"]')
839def CMDupload(parser, args):
840 """upload the current changelist to codereview"""
841 parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
842 help='bypass upload presubmit hook')
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000843 parser.add_option('-f', action='store_true', dest='force',
844 help="force yes to questions (don't prompt)")
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000845 parser.add_option('-m', dest='message', help='message for patch')
846 parser.add_option('-r', '--reviewers',
847 help='reviewer email addresses')
maruel@chromium.orgb2a7c332011-02-25 20:30:37 +0000848 parser.add_option('--cc',
849 help='cc email addresses')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000850 parser.add_option('--send-mail', action='store_true',
851 help='send email to reviewer immediately')
852 parser.add_option("--emulate_svn_auto_props", action="store_true",
853 dest="emulate_svn_auto_props",
854 help="Emulate Subversion's auto properties feature.")
855 parser.add_option("--desc_from_logs", action="store_true",
856 dest="from_logs",
857 help="""Squashes git commit logs into change description and
858 uses message as subject""")
859 (options, args) = parser.parse_args(args)
860
861 # Make sure index is up-to-date before running diff-index.
862 RunGit(['update-index', '--refresh', '-q'], error_ok=True)
863 if RunGit(['diff-index', 'HEAD']):
864 print 'Cannot upload with a dirty tree. You must commit locally first.'
865 return 1
866
867 cl = Changelist()
868 if args:
869 base_branch = args[0]
870 else:
871 # Default to diffing against the "upstream" branch.
872 base_branch = cl.GetUpstreamBranch()
873 args = [base_branch + "..."]
874
875 if not options.bypass_hooks:
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000876 hook_results = RunHook(committing=False, upstream_branch=base_branch,
877 rietveld_server=cl.GetRietveldServer(), tbr=False,
878 may_prompt=(not options.force))
879 else:
880 hook_results = HookResults()
881
882 if not options.reviewers and hook_results.reviewers:
883 options.reviewers = hook_results.reviewers
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000884
885 # --no-ext-diff is broken in some versions of Git, so try to work around
886 # this by overriding the environment (but there is still a problem if the
887 # git config key "diff.external" is used).
888 env = os.environ.copy()
889 if 'GIT_EXTERNAL_DIFF' in env:
890 del env['GIT_EXTERNAL_DIFF']
891 subprocess.call(['git', 'diff', '--no-ext-diff', '--stat', '-M'] + args,
892 env=env)
893
894 upload_args = ['--assume_yes'] # Don't ask about untracked files.
895 upload_args.extend(['--server', cl.GetRietveldServer()])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000896 if options.emulate_svn_auto_props:
897 upload_args.append('--emulate_svn_auto_props')
898 if options.send_mail:
899 if not options.reviewers:
900 DieWithError("Must specify reviewers to send email.")
901 upload_args.append('--send_mail')
902 if options.from_logs and not options.message:
903 print 'Must set message for subject line if using desc_from_logs'
904 return 1
905
906 change_desc = None
907
908 if cl.GetIssue():
909 if options.message:
910 upload_args.extend(['--message', options.message])
911 upload_args.extend(['--issue', cl.GetIssue()])
912 print ("This branch is associated with issue %s. "
913 "Adding patch to that issue." % cl.GetIssue())
914 else:
915 log_desc = CreateDescriptionFromLog(args)
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000916 change_desc = ChangeDescription(options.message, log_desc,
917 options.reviewers)
918 if not options.from_logs:
919 change_desc.Update()
920
921 if change_desc.IsEmpty():
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000922 print "Description is empty; aborting."
923 return 1
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000924
925 upload_args.extend(['--message', change_desc.subject])
926 upload_args.extend(['--description', change_desc.description])
927 if change_desc.reviewers:
928 upload_args.extend(['--reviewers', change_desc.reviewers])
maruel@chromium.orgb2a7c332011-02-25 20:30:37 +0000929 cc = ','.join(filter(None, (settings.GetCCList(), options.cc)))
930 if cc:
931 upload_args.extend(['--cc', cc])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000932
933 # Include the upstream repo's URL in the change -- this is useful for
934 # projects that have their source spread across multiple repos.
935 remote_url = None
936 if settings.GetIsGitSvn():
maruel@chromium.orgb92e4802011-03-03 20:22:00 +0000937 # URL is dependent on the current directory.
938 data = RunGit(['svn', 'info'], cwd=settings.GetRoot())
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000939 if data:
940 keys = dict(line.split(': ', 1) for line in data.splitlines()
941 if ': ' in line)
942 remote_url = keys.get('URL', None)
943 else:
944 if cl.GetRemoteUrl() and '/' in cl.GetUpstreamBranch():
945 remote_url = (cl.GetRemoteUrl() + '@'
946 + cl.GetUpstreamBranch().split('/')[-1])
947 if remote_url:
948 upload_args.extend(['--base_url', remote_url])
949
950 try:
951 issue, patchset = upload.RealMain(['upload'] + upload_args + args)
952 except:
953 # If we got an exception after the user typed a description for their
954 # change, back up the description before re-raising.
955 if change_desc:
956 backup_path = os.path.expanduser(DESCRIPTION_BACKUP_FILE)
957 print '\nGot exception while uploading -- saving description to %s\n' \
958 % backup_path
959 backup_file = open(backup_path, 'w')
dpranke@chromium.org970c5222011-03-12 00:32:24 +0000960 backup_file.write(change_desc.description)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000961 backup_file.close()
962 raise
963
964 if not cl.GetIssue():
965 cl.SetIssue(issue)
966 cl.SetPatchset(patchset)
967 return 0
968
969
970def SendUpstream(parser, args, cmd):
971 """Common code for CmdPush and CmdDCommit
972
973 Squashed commit into a single.
974 Updates changelog with metadata (e.g. pointer to review).
975 Pushes/dcommits the code upstream.
976 Updates review and closes.
977 """
978 parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
979 help='bypass upload presubmit hook')
980 parser.add_option('-m', dest='message',
981 help="override review description")
982 parser.add_option('-f', action='store_true', dest='force',
983 help="force yes to questions (don't prompt)")
984 parser.add_option('-c', dest='contributor',
985 help="external contributor for patch (appended to " +
986 "description and used as author for git). Should be " +
987 "formatted as 'First Last <email@example.com>'")
988 parser.add_option('--tbr', action='store_true', dest='tbr',
989 help="short for 'to be reviewed', commit branch " +
990 "even without uploading for review")
991 (options, args) = parser.parse_args(args)
992 cl = Changelist()
993
994 if not args or cmd == 'push':
995 # Default to merging against our best guess of the upstream branch.
996 args = [cl.GetUpstreamBranch()]
997
998 base_branch = args[0]
999
chase@chromium.orgc76e6752011-01-10 18:17:12 +00001000 # Make sure index is up-to-date before running diff-index.
1001 RunGit(['update-index', '--refresh', '-q'], error_ok=True)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001002 if RunGit(['diff-index', 'HEAD']):
1003 print 'Cannot %s with a dirty tree. You must commit locally first.' % cmd
1004 return 1
1005
1006 # This rev-list syntax means "show all commits not in my branch that
1007 # are in base_branch".
1008 upstream_commits = RunGit(['rev-list', '^' + cl.GetBranchRef(),
1009 base_branch]).splitlines()
1010 if upstream_commits:
1011 print ('Base branch "%s" has %d commits '
1012 'not in this branch.' % (base_branch, len(upstream_commits)))
1013 print 'Run "git merge %s" before attempting to %s.' % (base_branch, cmd)
1014 return 1
1015
1016 if cmd == 'dcommit':
1017 # This is the revision `svn dcommit` will commit on top of.
1018 svn_head = RunGit(['log', '--grep=^git-svn-id:', '-1',
1019 '--pretty=format:%H'])
1020 extra_commits = RunGit(['rev-list', '^' + svn_head, base_branch])
1021 if extra_commits:
1022 print ('This branch has %d additional commits not upstreamed yet.'
1023 % len(extra_commits.splitlines()))
1024 print ('Upstream "%s" or rebase this branch on top of the upstream trunk '
1025 'before attempting to %s.' % (base_branch, cmd))
1026 return 1
1027
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001028 if not options.bypass_hooks:
1029 RunHook(committing=True, upstream_branch=base_branch,
1030 rietveld_server=cl.GetRietveldServer(), tbr=options.tbr,
1031 may_prompt=(not options.force))
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001032
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001033 if not options.force and not options.bypass_hooks:
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001034 if cmd == 'dcommit':
1035 # Check the tree status if the tree status URL is set.
1036 status = GetTreeStatus()
1037 if 'closed' == status:
1038 print ('The tree is closed. Please wait for it to reopen. Use '
1039 '"git cl dcommit -f" to commit on a closed tree.')
1040 return 1
1041 elif 'unknown' == status:
1042 print ('Unable to determine tree status. Please verify manually and '
1043 'use "git cl dcommit -f" to commit on a closed tree.')
1044
1045 description = options.message
1046 if not options.tbr:
1047 # It is important to have these checks early. Not only for user
1048 # convenience, but also because the cl object then caches the correct values
1049 # of these fields even as we're juggling branches for setting up the commit.
1050 if not cl.GetIssue():
1051 print 'Current issue unknown -- has this branch been uploaded?'
1052 print 'Use --tbr to commit without review.'
1053 return 1
1054
1055 if not description:
1056 description = cl.GetDescription()
1057
1058 if not description:
1059 print 'No description set.'
1060 print 'Visit %s/edit to set it.' % (cl.GetIssueURL())
1061 return 1
1062
1063 description += "\n\nReview URL: %s" % cl.GetIssueURL()
1064 else:
1065 if not description:
1066 # Submitting TBR. See if there's already a description in Rietveld, else
1067 # create a template description. Eitherway, give the user a chance to edit
1068 # it to fill in the TBR= field.
1069 if cl.GetIssue():
1070 description = cl.GetDescription()
1071
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001072 # TODO(dpranke): Update to use ChangeDescription object.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001073 if not description:
1074 description = """# Enter a description of the change.
1075# This will be used as the change log for the commit.
1076
1077"""
1078 description += CreateDescriptionFromLog(args)
1079
1080 description = UserEditedLog(description + '\nTBR=')
1081
1082 if not description:
1083 print "Description empty; aborting."
1084 return 1
1085
1086 if options.contributor:
1087 if not re.match('^.*\s<\S+@\S+>$', options.contributor):
1088 print "Please provide contibutor as 'First Last <email@example.com>'"
1089 return 1
1090 description += "\nPatch from %s." % options.contributor
1091 print 'Description:', repr(description)
1092
1093 branches = [base_branch, cl.GetBranchRef()]
1094 if not options.force:
1095 subprocess.call(['git', 'diff', '--stat'] + branches)
1096 raw_input("About to commit; enter to confirm.")
1097
1098 # We want to squash all this branch's commits into one commit with the
1099 # proper description.
bauerb@chromium.orgb4a75c42011-03-08 08:35:38 +00001100 # We do this by doing a "reset --soft" to the base branch (which keeps
1101 # the working copy the same), then dcommitting that.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001102 MERGE_BRANCH = 'git-cl-commit'
1103 # Delete the merge branch if it already exists.
1104 if RunGitWithCode(['show-ref', '--quiet', '--verify',
1105 'refs/heads/' + MERGE_BRANCH])[0] == 0:
1106 RunGit(['branch', '-D', MERGE_BRANCH])
1107
1108 # We might be in a directory that's present in this branch but not in the
1109 # trunk. Move up to the top of the tree so that git commands that expect a
1110 # valid CWD won't fail after we check out the merge branch.
1111 rel_base_path = RunGit(['rev-parse', '--show-cdup']).strip()
1112 if rel_base_path:
1113 os.chdir(rel_base_path)
1114
1115 # Stuff our change into the merge branch.
1116 # We wrap in a try...finally block so if anything goes wrong,
1117 # we clean up the branches.
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00001118 retcode = -1
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001119 try:
bauerb@chromium.orgb4a75c42011-03-08 08:35:38 +00001120 RunGit(['checkout', '-q', '-b', MERGE_BRANCH])
1121 RunGit(['reset', '--soft', base_branch])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001122 if options.contributor:
1123 RunGit(['commit', '--author', options.contributor, '-m', description])
1124 else:
1125 RunGit(['commit', '-m', description])
1126 if cmd == 'push':
1127 # push the merge branch.
1128 remote, branch = cl.FetchUpstreamTuple()
1129 retcode, output = RunGitWithCode(
1130 ['push', '--porcelain', remote, 'HEAD:%s' % branch])
1131 logging.debug(output)
1132 else:
1133 # dcommit the merge branch.
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00001134 retcode, output = RunGitWithCode(['svn', 'dcommit', '--no-rebase'])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001135 finally:
1136 # And then swap back to the original branch and clean up.
1137 RunGit(['checkout', '-q', cl.GetBranch()])
1138 RunGit(['branch', '-D', MERGE_BRANCH])
1139
1140 if cl.GetIssue():
1141 if cmd == 'dcommit' and 'Committed r' in output:
1142 revision = re.match('.*?\nCommitted r(\\d+)', output, re.DOTALL).group(1)
1143 elif cmd == 'push' and retcode == 0:
maruel@chromium.orgdf947ea2011-01-12 20:44:54 +00001144 match = (re.match(r'.*?([a-f0-9]{7})\.\.([a-f0-9]{7})$', l)
1145 for l in output.splitlines(False))
1146 match = filter(None, match)
1147 if len(match) != 1:
1148 DieWithError("Couldn't parse ouput to extract the committed hash:\n%s" %
1149 output)
1150 revision = match[0].group(2)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001151 else:
1152 return 1
1153 viewvc_url = settings.GetViewVCUrl()
1154 if viewvc_url and revision:
1155 cl.description += ('\n\nCommitted: ' + viewvc_url + revision)
1156 print ('Closing issue '
1157 '(you may be prompted for your codereview password)...')
1158 cl.CloseIssue()
1159 cl.SetIssue(0)
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00001160
1161 if retcode == 0:
1162 hook = POSTUPSTREAM_HOOK_PATTERN % cmd
1163 if os.path.isfile(hook):
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001164 RunCommand([hook, base_branch], error_ok=True)
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00001165
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001166 return 0
1167
1168
1169@usage('[upstream branch to apply against]')
1170def CMDdcommit(parser, args):
1171 """commit the current changelist via git-svn"""
1172 if not settings.GetIsGitSvn():
thakis@chromium.orgcde3bb62011-01-20 01:16:14 +00001173 message = """This doesn't appear to be an SVN repository.
1174If your project has a git mirror with an upstream SVN master, you probably need
1175to run 'git svn init', see your project's git mirror documentation.
1176If your project has a true writeable upstream repository, you probably want
1177to run 'git cl push' instead.
1178Choose wisely, if you get this wrong, your commit might appear to succeed but
1179will instead be silently ignored."""
1180 print(message)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001181 raw_input('[Press enter to dcommit or ctrl-C to quit]')
1182 return SendUpstream(parser, args, 'dcommit')
1183
1184
1185@usage('[upstream branch to apply against]')
1186def CMDpush(parser, args):
1187 """commit the current changelist via git"""
1188 if settings.GetIsGitSvn():
1189 print('This appears to be an SVN repository.')
1190 print('Are you sure you didn\'t mean \'git cl dcommit\'?')
1191 raw_input('[Press enter to push or ctrl-C to quit]')
1192 return SendUpstream(parser, args, 'push')
1193
1194
1195@usage('<patch url or issue id>')
1196def CMDpatch(parser, args):
1197 """patch in a code review"""
1198 parser.add_option('-b', dest='newbranch',
1199 help='create a new branch off trunk for the patch')
1200 parser.add_option('-f', action='store_true', dest='force',
1201 help='with -b, clobber any existing branch')
1202 parser.add_option('--reject', action='store_true', dest='reject',
1203 help='allow failed patches and spew .rej files')
1204 parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit',
1205 help="don't commit after patch applies")
1206 (options, args) = parser.parse_args(args)
1207 if len(args) != 1:
1208 parser.print_help()
1209 return 1
1210 input = args[0]
1211
1212 if re.match(r'\d+', input):
1213 # Input is an issue id. Figure out the URL.
1214 issue = input
1215 server = settings.GetDefaultServerUrl()
1216 fetch = urllib2.urlopen('%s/%s' % (server, issue)).read()
1217 m = re.search(r'/download/issue[0-9]+_[0-9]+.diff', fetch)
1218 if not m:
1219 DieWithError('Must pass an issue ID or full URL for '
1220 '\'Download raw patch set\'')
1221 url = '%s%s' % (server, m.group(0).strip())
1222 else:
1223 # Assume it's a URL to the patch. Default to http.
1224 input = FixUrl(input)
1225 match = re.match(r'.*?/issue(\d+)_\d+.diff', input)
1226 if match:
1227 issue = match.group(1)
1228 url = input
1229 else:
1230 DieWithError('Must pass an issue ID or full URL for '
1231 '\'Download raw patch set\'')
1232
1233 if options.newbranch:
1234 if options.force:
1235 RunGit(['branch', '-D', options.newbranch],
1236 swallow_stderr=True, error_ok=True)
1237 RunGit(['checkout', '-b', options.newbranch,
1238 Changelist().GetUpstreamBranch()])
1239
1240 # Switch up to the top-level directory, if necessary, in preparation for
1241 # applying the patch.
1242 top = RunGit(['rev-parse', '--show-cdup']).strip()
1243 if top:
1244 os.chdir(top)
1245
1246 patch_data = urllib2.urlopen(url).read()
1247 # Git patches have a/ at the beginning of source paths. We strip that out
1248 # with a sed script rather than the -p flag to patch so we can feed either
1249 # Git or svn-style patches into the same apply command.
1250 # re.sub() should be used but flags=re.MULTILINE is only in python 2.7.
1251 sed_proc = Popen(['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'],
1252 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
1253 patch_data = sed_proc.communicate(patch_data)[0]
1254 if sed_proc.returncode:
1255 DieWithError('Git patch mungling failed.')
1256 logging.info(patch_data)
1257 # We use "git apply" to apply the patch instead of "patch" so that we can
1258 # pick up file adds.
1259 # The --index flag means: also insert into the index (so we catch adds).
1260 cmd = ['git', 'apply', '--index', '-p0']
1261 if options.reject:
1262 cmd.append('--reject')
1263 patch_proc = Popen(cmd, stdin=subprocess.PIPE)
1264 patch_proc.communicate(patch_data)
1265 if patch_proc.returncode:
1266 DieWithError('Failed to apply the patch')
1267
1268 # If we had an issue, commit the current state and register the issue.
1269 if not options.nocommit:
1270 RunGit(['commit', '-m', 'patch from issue %s' % issue])
1271 cl = Changelist()
1272 cl.SetIssue(issue)
1273 print "Committed patch."
1274 else:
1275 print "Patch applied to index."
1276 return 0
1277
1278
1279def CMDrebase(parser, args):
1280 """rebase current branch on top of svn repo"""
1281 # Provide a wrapper for git svn rebase to help avoid accidental
1282 # git svn dcommit.
1283 # It's the only command that doesn't use parser at all since we just defer
1284 # execution to git-svn.
1285 RunGit(['svn', 'rebase'] + args, redirect_stdout=False)
1286 return 0
1287
1288
1289def GetTreeStatus():
1290 """Fetches the tree status and returns either 'open', 'closed',
1291 'unknown' or 'unset'."""
1292 url = settings.GetTreeStatusUrl(error_ok=True)
1293 if url:
1294 status = urllib2.urlopen(url).read().lower()
1295 if status.find('closed') != -1 or status == '0':
1296 return 'closed'
1297 elif status.find('open') != -1 or status == '1':
1298 return 'open'
1299 return 'unknown'
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001300 return 'unset'
1301
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001302
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001303def GetTreeStatusReason():
1304 """Fetches the tree status from a json url and returns the message
1305 with the reason for the tree to be opened or closed."""
1306 # Don't import it at file level since simplejson is not installed by default
1307 # on python 2.5 and it is only used for git-cl tree which isn't often used,
1308 # forcing everyone to install simplejson isn't efficient.
1309 try:
1310 import simplejson as json
1311 except ImportError:
1312 try:
1313 import json
1314 # Some versions of python2.5 have an incomplete json module. Check to make
1315 # sure loads exists.
1316 json.loads
1317 except (ImportError, AttributeError):
1318 print >> sys.stderr, 'Please install simplejson'
1319 sys.exit(1)
1320
msb@chromium.orgbf1a7ba2011-02-01 16:21:46 +00001321 url = settings.GetTreeStatusUrl()
1322 json_url = urlparse.urljoin(url, '/current?format=json')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001323 connection = urllib2.urlopen(json_url)
1324 status = json.loads(connection.read())
1325 connection.close()
1326 return status['message']
1327
dpranke@chromium.org970c5222011-03-12 00:32:24 +00001328
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001329def CMDtree(parser, args):
1330 """show the status of the tree"""
1331 (options, args) = parser.parse_args(args)
1332 status = GetTreeStatus()
1333 if 'unset' == status:
1334 print 'You must configure your tree status URL by running "git cl config".'
1335 return 2
1336
1337 print "The tree is %s" % status
1338 print
1339 print GetTreeStatusReason()
1340 if status != 'open':
1341 return 1
1342 return 0
1343
1344
1345def CMDupstream(parser, args):
1346 """print the name of the upstream branch, if any"""
1347 (options, args) = parser.parse_args(args)
1348 cl = Changelist()
1349 print cl.GetUpstreamBranch()
1350 return 0
1351
1352
1353def Command(name):
1354 return getattr(sys.modules[__name__], 'CMD' + name, None)
1355
1356
1357def CMDhelp(parser, args):
1358 """print list of commands or help for a specific command"""
1359 (options, args) = parser.parse_args(args)
1360 if len(args) == 1:
1361 return main(args + ['--help'])
1362 parser.print_help()
1363 return 0
1364
1365
1366def GenUsage(parser, command):
1367 """Modify an OptParse object with the function's documentation."""
1368 obj = Command(command)
1369 more = getattr(obj, 'usage_more', '')
1370 if command == 'help':
1371 command = '<command>'
1372 else:
1373 # OptParser.description prefer nicely non-formatted strings.
1374 parser.description = re.sub('[\r\n ]{2,}', ' ', obj.__doc__)
1375 parser.set_usage('usage: %%prog %s [options] %s' % (command, more))
1376
1377
1378def main(argv):
1379 """Doesn't parse the arguments here, just find the right subcommand to
1380 execute."""
1381 # Do it late so all commands are listed.
1382 CMDhelp.usage_more = ('\n\nCommands are:\n' + '\n'.join([
1383 ' %-10s %s' % (fn[3:], Command(fn[3:]).__doc__.split('\n')[0].strip())
1384 for fn in dir(sys.modules[__name__]) if fn.startswith('CMD')]))
1385
1386 # Create the option parse and add --verbose support.
1387 parser = optparse.OptionParser()
1388 parser.add_option('-v', '--verbose', action='store_true')
1389 old_parser_args = parser.parse_args
1390 def Parse(args):
1391 options, args = old_parser_args(args)
1392 if options.verbose:
1393 logging.basicConfig(level=logging.DEBUG)
1394 else:
1395 logging.basicConfig(level=logging.WARNING)
1396 return options, args
1397 parser.parse_args = Parse
1398
1399 if argv:
1400 command = Command(argv[0])
1401 if command:
1402 # "fix" the usage and the description now that we know the subcommand.
1403 GenUsage(parser, argv[0])
1404 try:
1405 return command(parser, argv[1:])
1406 except urllib2.HTTPError, e:
1407 if e.code != 500:
1408 raise
1409 DieWithError(
1410 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith '
1411 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e)))
1412
1413 # Not a known command. Default to help.
1414 GenUsage(parser, 'help')
1415 return CMDhelp(parser, argv)
1416
1417
1418if __name__ == '__main__':
1419 sys.exit(main(sys.argv[1:]))