Skip to content


Subversion checkout URL

You can clone with
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

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