Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 629 lines (538 sloc) 22.7 KB
#!/usr/bin/python
# git-cl -- a git-command for integrating reviews on Rietveld
# Copyright (C) 2008 Evan Martin <martine@danga.com>
import getpass
import optparse
import os
import re
import subprocess
import sys
import tempfile
import textwrap
import upload
import urllib2
import projecthosting_upload
import cl_settings
# mimetype exceptions: if you can't upload to rietveld, add the
# relevant extension to this list. The only important part is the
# "text/x-script." bit; the stuff after the dot doesn't matter
import mimetypes
mimetypes.add_type("text/x-script.scheme", ".scm")
mimetypes.add_type("application/xml", ".xml")
mimetypes.add_type("text/x-script.postscript", ".ps")
mimetypes.add_type("text/x-script.perl", ".pl")
mimetypes.add_type("text/x-script.tex", ".latex")
mimetypes.add_type("text/x-script.texinfo", ".texi")
mimetypes.add_type("text/x-script.shell", ".sh")
try:
import readline
except ImportError:
pass
DEFAULT_SERVER = 'codereview.appspot.com'
PREDCOMMIT_HOOK = '.git/hooks/pre-cl-dcommit'
PREUPLOAD_HOOK = '.git/hooks/pre-cl-upload'
settings = cl_settings.Settings()
def TrackerURL(issue):
"""Return the Tracker URL for a particular issue."""
# make the server/project customizable?
return 'http://code.google.com/p/lilypond/issues/detail?id=%s' % issue
def 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:
cl_settings.RunGit(['config', 'rietveld.server', newserver])
tracker_server = settings.GetTrackerServer(error_ok=True)
prompt = 'Allura server'
prompt += ' [%s]' % tracker_server
newtracker = raw_input(prompt + ': ')
while not tracker_server and not newtracker:
prompt = 'You must provide the address of the Allura tracker server: '
newtracker = raw_input(prompt)
if newtracker and newtracker != tracker_server:
cl_settings.RunGit(['config', 'allura.tracker', newtracker])
token = settings.GetToken(error_ok=True)
prompt = 'Allura bearer token (see https://sourceforge.net/auth/oauth/)'
prompt += ' [%s]' % token
newtoken = raw_input(prompt + ': ')
while not token and not newtoken:
prompt = 'You must provide a bearer token to authenticate: '
newtoken = raw_input(prompt)
if newtoken and newtoken != token:
cl_settings.RunGit(['config', 'allura.token', newtoken])
def SetProperty(initial, caption, name):
prompt = caption
if initial:
prompt += ' ("x" to clear) [%s]' % initial
new_val = raw_input(prompt + ': ')
if new_val == 'x':
cl_settings.RunGit(['config', '--unset-all', 'rietveld.' + name], error_ok=True)
elif new_val and new_val != initial:
cl_settings.RunGit(['config', 'rietveld.' + name, new_val])
SetProperty(settings.GetCCList(), 'CC list', 'cc')
def LoadCodereviewSettingsFromFile(file):
"""Parse a codereview.settings file."""
settings = {}
for line in file.read().splitlines():
if not line or line.startswith("#"):
continue
k, v = line.split(": ", 1)
settings[k] = v
def GetProperty(name):
return settings.get(name)
def SetProperty(name, setting, unset_error_ok=False):
fullname = 'rietveld.' + name
if setting in settings:
cl_settings.RunGit(['config', fullname, settings[setting]])
else:
cl_settings.RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok)
SetProperty('server', 'CODE_REVIEW_SERVER')
# Only server setting is required. Other settings can be absent.
# In that case, we ignore errors raised during option deletion attempt.
SetProperty('cc', 'CC_LIST', unset_error_ok=True)
SetProperty('tree-status-url', 'STATUS', unset_error_ok=True)
SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True)
hooks = {}
if GetProperty('GITCL_PREUPLOAD'):
hooks['preupload'] = GetProperty('GITCL_PREUPLOAD')
if GetProperty('GITCL_PREDCOMMIT'):
hooks['predcommit'] = GetProperty('GITCL_PREDCOMMIT')
return hooks
def CmdConfig(args):
def DownloadToFile(url, filename):
filename = os.path.join(settings.GetRoot(), filename)
if os.path.exists(filename):
print '%s exists, skipping' % filename
return False
contents = urllib2.urlopen(url).read()
file = open(filename, 'w')
file.write(contents)
file.close()
os.chmod(filename, 0755)
return True
parser = optparse.OptionParser(
usage='git cl config [repo root containing codereview.settings]')
(options, args) = parser.parse_args(args)
if len(args) == 0:
GetCodereviewSettingsInteractively()
return
url = args[0]
if not url.endswith('codereview.settings'):
url = os.path.join(url, 'codereview.settings')
# Load Codereview settings and download hooks (if available).
hooks = LoadCodereviewSettingsFromFile(urllib2.urlopen(url))
for key, filename in (('predcommit', PREDCOMMIT_HOOK),
('preupload', PREUPLOAD_HOOK)):
if key in hooks:
DownloadToFile(hooks[key], filename)
def CmdStatus(args):
parser = optparse.OptionParser(usage='git cl status [options]')
parser.add_option('--field', help='print only specific field (desc|id|url)')
(options, args) = parser.parse_args(args)
# TODO: maybe make show_branches a flag if necessary.
show_branches = not options.field
if show_branches:
branches = cl_settings.RunGit(['for-each-ref', '--format=%(refname)', 'refs/heads'])
if branches:
print 'Branches associated with reviews:'
for branch in sorted(branches.splitlines()):
cl = cl_settings.Changelist(branchref=branch)
print " %20s: %s" % (cl.GetBranch(), cl.GetRietveldIssue())
cl = cl_settings.Changelist()
if options.field:
if options.field.startswith('desc'):
print cl.GetDescription()
elif options.field == 'id':
print cl.GetRietveldIssue()
elif options.field == 'url':
print cl.GetRietveldURL()
else:
print
print 'Current branch:',
if not cl.GetRietveldIssue():
print 'no issue assigned.'
return 0
print cl.GetBranch()
if cl.GetTrackerIssue():
print 'Tracker issue:', cl.GetTrackerIssue(), '(%s)' % cl.GetTrackerURL()
else:
print 'Tracker issue: None'
print 'Rietveld issue:', cl.GetRietveldIssue(), '(%s)' % cl.GetRietveldURL()
print 'Issue description:'
print cl.GetDescription(pretty=True)
def CmdIssue(args):
parser = optparse.OptionParser(usage='git cl issue [issue_number]')
parser.description = ('Set or display the current Rietveld issue. ' +
'Pass issue number 0 to clear the current issue.')
(options, args) = parser.parse_args(args)
cl = cl_settings.Changelist()
if len(args) > 0:
cl.SetRietveldIssue(int(args[0]))
print 'Rietveld issue:', cl.GetRietveldIssue(), '(%s)' % cl.GetRietveldURL()
def UserEditedLog(starting_text):
"""Given some starting text, let the user edit it and return the result."""
editor = os.getenv('EDITOR', 'vi')
(file_handle, filename) = tempfile.mkstemp()
file = os.fdopen(file_handle, 'w')
file.write(starting_text)
file.close()
ret = subprocess.call(editor + ' ' + filename, shell=True)
if ret != 0:
os.remove(filename)
return
file = open(filename)
text = file.read()
file.close()
os.remove(filename)
stripcomment_re = re.compile(r'^#.*$', re.MULTILINE)
return stripcomment_re.sub('', text).strip()
def RunHook(hook, upstream_branch='origin', error_ok=False):
"""Run a given hook if it exists. By default, we fail on errors."""
hook = '%s/%s' % (settings.GetRoot(), hook)
if not os.path.exists(hook):
return
output = cl_settings.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 cl_settings.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 issue tracker",
action="store_true", dest="no_code_issue")
(options, args) = parser.parse_args(args)
if cl_settings.RunGit(['diff-index', 'HEAD']):
print 'Cannot upload with a dirty tree. You must commit locally first.'
return 1
cl = cl_settings.Changelist()
if args:
base_branch = args[0]
else:
# Default to diffing against the "upstream" branch.
base_branch = cl.GetUpstreamBranch()
args = [base_branch + "..."]
if not options.bypass_hooks:
RunHook(PREUPLOAD_HOOK, upstream_branch=base_branch, error_ok=False)
# --no-ext-diff is broken in some versions of Git, so try to work around
# this by overriding the environment (but there is still a problem if the
# git config key "diff.external" is used).
env = os.environ.copy()
if 'GIT_EXTERNAL_DIFF' in env: del env['GIT_EXTERNAL_DIFF']
subprocess.call(['git', 'diff', '--no-ext-diff', '--stat', '-M'] + args,
env=env)
upload_args = ['--assume_yes'] # Don't ask about untracked files.
upload_args.extend(['--server', settings.GetServer()])
if options.reviewers:
upload_args.extend(['--reviewers', options.reviewers])
upload_args.extend(['--cc', settings.GetCCList()])
if options.message:
upload_args.extend(['--message', options.message])
if options.send_mail:
if not options.reviewers:
settings.DieWithError("Must specify reviewers to send email.")
upload_args.append('--send_mail')
if cl.GetRietveldIssue():
upload_args.extend(['--issue', cl.GetRietveldIssue()])
print ("This branch is associated with Rietveld issue %s. "
"Adding patch to that issue." % cl.GetRietveldIssue())
prompt = "Message describing this patch set: "
desc = options.message or raw_input(prompt).strip()
else:
# Construct a description for this change from the log.
# We need to convert diff options to log options.
log_args = []
if len(args) == 1 and not args[0].endswith('.'):
log_args = [args[0] + '..']
elif len(args) == 2:
log_args = [args[0] + '..' + args[1]]
else:
log_args = args[:] # Hope for the best!
desc = cl_settings.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
# TODO: dies when desc is empty
subject = desc.splitlines()[0]
upload_args.extend(['--title', subject])
upload_args.extend(['--message', desc])
upload_args.extend(['--oauth2'])
issue, patchset = upload.RealMain(['upload'] + upload_args + args)
if not cl.GetRietveldIssue():
cl.SetRietveldIssue(issue)
cl.SetPatchset(patchset)
if not options.no_code_issue:
issueId = cl.GetTrackerIssue()
issueId = projecthosting_upload.upload(issue, patchset, subject, desc, issueId)
cl.SetTrackerIssue(issueId)
def CmdDCommit(args):
parser = optparse.OptionParser(
usage='git cl dcommit [options] [git-svn branch to apply against]')
parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
help='bypass upload presubmit hook')
parser.add_option('-m', dest='message',
help="override review description")
parser.add_option('-f', action='store_true', dest='force',
help="force yes to questions (don't prompt)")
parser.add_option('-c', dest='contributor',
help="external contributor for patch (appended to " +
"description)")
parser.add_option('--tbr', action='store_true', dest='tbr',
help="short for 'to be reviewed', commit branch " +
"even without uploading for review")
(options, args) = parser.parse_args(args)
cl = cl_settings.Changelist()
if not args:
# Default to merging against our best guess of the upstream branch.
args = [cl.GetUpstreamBranch()]
base_branch = args[0]
if cl_settings.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 = cl_settings.RunGit(['rev-list', '^' + cl.GetBranchRef(),
base_branch]).splitlines()
if upstream_commits:
print ('Base branch "%s" has %d commits '
'not in this branch.' % (base_branch, len(upstream_commits)))
print 'Run "git merge %s" before attempting to dcommit.' % base_branch
return 1
if not options.force and not options.bypass_hooks:
RunHook(PREDCOMMIT_HOOK, upstream_branch=base_branch, error_ok=False)
# Check the tree status if the tree status URL is set.
status = GetTreeStatus()
if 'closed' == status:
print ('The tree is closed. Please wait for it to reopen. Use '
'"git cl dcommit -f" to commit on a closed tree.')
return 1
elif 'unknown' == status:
print ('Unable to determine tree status. Please verify manually and '
'use "git cl dcommit -f" to commit on a closed tree.')
description = options.message
if not options.tbr:
# It is important to have these checks early. Not only for user
# convenience, but also because the cl object then caches the correct values
# of these fields even as we're juggling branches for setting up the commit.
if not cl.GetRietveldIssue():
print 'Current issue unknown -- has this branch been uploaded?'
print 'Use --tbr to commit without review.'
return 1
if not description:
description = cl.GetDescription()
if not description:
print 'No description set.'
print 'Visit %s/edit to set it.' % (cl.GetRietveldURL())
return 1
description += "\n\nReview URL: %s" % cl.GetRietveldURL()
else:
# Submitting TBR. Get a description now.
if not description:
description = UserEditedLog('TBR: ')
if not description:
print "Description empty; aborting."
return 1
if options.contributor:
description += "\nPatch from %s." % options.contributor
print 'Description:', repr(description)
branches = [base_branch, cl.GetBranchRef()]
if not options.force:
subprocess.call(['git', 'diff', '--stat'] + branches)
raw_input("About to commit; enter to confirm.")
# We want to squash all this branch's commits into one commit with the
# proper description.
# We do this by doing a "merge --squash" into a new commit branch, then
# dcommitting that.
MERGE_BRANCH = 'git-cl-commit'
# Delete the merge branch if it already exists.
if cl_settings.RunGit(['show-ref', '--quiet', '--verify', 'refs/heads/' + MERGE_BRANCH],
exit_code=True) == 0:
cl_settings.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 = cl_settings.RunGit(['rev-parse', '--show-cdup']).strip()
if rel_base_path:
os.chdir(rel_base_path)
# Stuff our change into the merge branch.
# We wrap in a try...finally block so if anything goes wrong,
# we clean up the branches.
try:
cl_settings.RunGit(['checkout', '-q', '-b', MERGE_BRANCH, base_branch])
cl_settings.RunGit(['merge', '--squash', cl.GetBranchRef()])
cl_settings.RunGit(['commit', '-m', description])
# dcommit the merge branch.
output = cl_settings.RunGit(['svn', 'dcommit', '--no-rebase'])
finally:
# And then swap back to the original branch and clean up.
cl_settings.RunGit(['checkout', '-q', cl.GetBranch()])
cl_settings.RunGit(['branch', '-D', MERGE_BRANCH])
if cl.has_RietveldIssue and output.find("Committed r") != -1:
print "Closing issue (you may be prompted for your codereview password)..."
viewvc_url = settings.GetViewVCUrl()
if viewvc_url:
revision = re.compile(".*?\nCommitted r(\d+)",
re.DOTALL).match(output).group(1)
cl.description = (cl.description +
"\n\nCommitted: " + viewvc_url + revision)
cl.CloseRietveldIssue()
cl.SetRietveldIssue(0)
def CmdPatch(args):
parser = optparse.OptionParser(usage=('git cl patch [options] '
'<patch url or Rietveld issue ID>'))
parser.add_option('-b', dest='newbranch',
help='create a new branch off trunk for the patch')
parser.add_option('-f', action='store_true', dest='force',
help='with -b, clobber any existing branch')
parser.add_option('--reject', action='store_true', dest='reject',
help='allow failed patches and spew .rej files')
parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit',
help="don't commit after patch applies")
(options, args) = parser.parse_args(args)
if len(args) != 1:
return parser.print_help()
input = args[0]
if re.match(r'\d+', input):
# Input is an issue id. Figure out the URL.
issue = input
fetch = "curl --silent https://%s/%s" % (settings.GetServer(), issue)
grep = "grep -E -o '/download/issue[0-9]+_[0-9]+.diff'"
pipe = subprocess.Popen("%s | %s" % (fetch, grep), shell=True,
stdout=subprocess.PIPE)
path = pipe.stdout.read().strip()
url = 'https://%s%s' % (settings.GetServer(), path)
if len(path) == 0:
# There is no patch to download (patch may be too large, see
# http://code.google.com/p/rietveld/issues/detail?id=196).
# Try to download individual patches for each file instead,
# and concatenate them to obtain the complete patch.
grep = "grep -E -o '/download/issue[0-9]+_[0-9]+_[0-9]+.diff'"
pipe = subprocess.Popen("%s | %s" % (fetch, grep), shell=True,
stdout=subprocess.PIPE)
paths = pipe.stdout.read().strip().split("\n")
url = 'https://%s{%s}' % (settings.GetServer(), ",".join(paths))
else:
# Assume it's a URL to the patch.
match = re.match(r'https?://.*?/issue(\d+)_\d+.diff', input)
if match:
issue = match.group(1)
url = input.replace("http:", "https:")
else:
print "Must pass an Rietveld issue ID or full URL for 'Download raw patch set'"
return 1
if options.newbranch:
if options.force:
cl_settings.RunGit(['branch', '-D', options.newbranch], error_ok=True)
cl_settings.RunGit(['checkout', '-b', options.newbranch])
# Switch up to the top-level directory, if necessary, in preparation for
# applying the patch.
top = cl_settings.RunGit(['rev-parse', '--show-cdup']).strip()
if top:
os.chdir(top)
# Construct a pipeline to feed the patch into "git apply".
# We use "git apply" to apply the patch instead of "patch" so that we can
# pick up file adds.
# 1) Fetch the patch.
fetch = "curl --silent %s" % url
# 2) Munge the patch.
# Git patches have a/ at the beginning of source paths. We strip that out
# with a sed script rather than the -p flag to patch so we can feed either
# Git or svn-style patches into the same apply command.
gitsed = "sed -e 's|^--- a/|--- |; s|^+++ b/|+++ |'"
# 3) Apply the patch.
# The --index flag means: also insert into the index (so we catch adds).
apply = "git apply --index -p0"
if options.reject:
apply += " --reject"
subprocess.check_call(' | '.join([fetch, gitsed, apply]), shell=True)
# If we had an issue, commit the current state and register the issue.
if not options.nocommit:
cl_settings.RunGit(['commit', '-m', 'patch from issue %s' % issue])
cl = cl_settings.Changelist()
cl.SetRietveldIssue(issue)
print "Committed patch."
else:
print "Patch applied to index."
def CmdRebase(args):
# Provide a wrapper for git svn rebase to help avoid accidental
# git svn dcommit.
cl_settings.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 = cl_settings.Changelist()
print cl.GetUpstreamBranch()
COMMANDS = [
('config', 'edit configuration for this tree', CmdConfig),
('dcommit', 'commit the current changelist via git-svn', CmdDCommit),
('issue', 'show/set current branch\'s Rietveld issue', CmdIssue),
('patch', 'patch in a code review', CmdPatch),
('presubmit', 'run presubmit tests on the current changelist', CmdPresubmit),
('rebase', 'rebase current branch on top of svn repo', CmdRebase),
('status', 'show status of changelists', CmdStatus),
('tree', 'show the status of the tree', CmdTreeStatus),
('upload', 'upload the current changelist to codereview', CmdUpload),
('upstream', 'print the name of the upstream branch, if any', CmdUpstream),
]
def Usage(name):
print 'usage: %s <command>' % name
print 'commands are:'
for name, desc, _ in COMMANDS:
print ' %-10s %s' % (name, desc)
sys.exit(1)
def main(argv):
if len(argv) < 2:
Usage(argv[0])
command = argv[1]
for name, _, func in COMMANDS:
if name == command:
return func(argv[2:])
print 'unknown command: %s' % command
Usage(argv[0])
if __name__ == '__main__':
sys.exit(main(sys.argv))
Something went wrong with that request. Please try again.