upload -t: Automatically include local branch name

If the -t flag is given to upload, the local branch name is
automatically sent to Gerrit Code Review as the topic branch name
for the change(s).  This requires the server to be Gerrit Code
Review v2.1.3-53-gd50c94e or later, which isn't widely deployed
right now, so the default is opt-out.

Change-Id: I034fcacb405b7cb909147152db427fe69dd7bcbf
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce 2010-07-15 16:52:42 -07:00
parent cc50bac8c7
commit a5ece0e050
2 changed files with 25 additions and 13 deletions

View File

@ -149,10 +149,11 @@ class ReviewableBranch(object):
R_HEADS + self.name, R_HEADS + self.name,
'--') '--')
def UploadForReview(self, people): def UploadForReview(self, people, auto_topic=False):
self.project.UploadForReview(self.name, self.project.UploadForReview(self.name,
self.replace_changes, self.replace_changes,
people) people,
auto_topic=auto_topic)
def GetPublishedRefs(self): def GetPublishedRefs(self):
refs = {} refs = {}
@ -555,7 +556,10 @@ class Project(object):
return rb return rb
return None return None
def UploadForReview(self, branch=None, replace_changes=None, people=([],[])): def UploadForReview(self, branch=None,
replace_changes=None,
people=([],[]),
auto_topic=False):
"""Uploads the named branch for code review. """Uploads the named branch for code review.
""" """
if branch is None: if branch is None:
@ -587,10 +591,15 @@ class Project(object):
for e in people[1]: for e in people[1]:
rp.append('--cc=%s' % sq(e)) rp.append('--cc=%s' % sq(e))
ref_spec = '%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch)
if auto_topic:
ref_spec = ref_spec + '/' + branch.name
cmd = ['push'] cmd = ['push']
cmd.append('--receive-pack=%s' % " ".join(rp)) cmd.append('--receive-pack=%s' % " ".join(rp))
cmd.append(branch.remote.SshReviewUrl(self.UserEmail)) cmd.append(branch.remote.SshReviewUrl(self.UserEmail))
cmd.append('%s:refs/for/%s' % (R_HEADS + branch.name, dest_branch)) cmd.append(ref_spec)
if replace_changes: if replace_changes:
for change_id,commit_id in replace_changes.iteritems(): for change_id,commit_id in replace_changes.iteritems():
cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id)) cmd.append('%s:refs/changes/%s/new' % (commit_id, change_id))

View File

@ -111,6 +111,9 @@ Gerrit Code Review: http://code.google.com/p/gerrit/
""" """
def _Options(self, p): def _Options(self, p):
p.add_option('-t',
dest='auto_topic', action='store_true',
help='Send local branch name to Gerrit Code Review')
p.add_option('--replace', p.add_option('--replace',
dest='replace', action='store_true', dest='replace', action='store_true',
help='Upload replacement patchesets from this branch') help='Upload replacement patchesets from this branch')
@ -121,7 +124,7 @@ Gerrit Code Review: http://code.google.com/p/gerrit/
type='string', action='append', dest='cc', type='string', action='append', dest='cc',
help='Also send email to these email addresses.') help='Also send email to these email addresses.')
def _SingleBranch(self, branch, people): def _SingleBranch(self, opt, branch, people):
project = branch.project project = branch.project
name = branch.name name = branch.name
remote = project.GetBranch(name).remote remote = project.GetBranch(name).remote
@ -154,11 +157,11 @@ Gerrit Code Review: http://code.google.com/p/gerrit/
answer = _ConfirmManyUploads() answer = _ConfirmManyUploads()
if answer: if answer:
self._UploadAndReport([branch], people) self._UploadAndReport(opt, [branch], people)
else: else:
_die("upload aborted by user") _die("upload aborted by user")
def _MultipleBranches(self, pending, people): def _MultipleBranches(self, opt, pending, people):
projects = {} projects = {}
branches = {} branches = {}
@ -227,7 +230,7 @@ Gerrit Code Review: http://code.google.com/p/gerrit/
if not _ConfirmManyUploads(multiple_branches=True): if not _ConfirmManyUploads(multiple_branches=True):
_die("upload aborted by user") _die("upload aborted by user")
self._UploadAndReport(todo, people) self._UploadAndReport(opt, todo, people)
def _AppendAutoCcList(self, branch, people): def _AppendAutoCcList(self, branch, people):
""" """
@ -311,9 +314,9 @@ Gerrit Code Review: http://code.google.com/p/gerrit/
_die("upload aborted by user") _die("upload aborted by user")
branch.replace_changes = to_replace branch.replace_changes = to_replace
self._UploadAndReport([branch], people) self._UploadAndReport(opt, [branch], people)
def _UploadAndReport(self, todo, original_people): def _UploadAndReport(self, opt, todo, original_people):
have_errors = False have_errors = False
for branch in todo: for branch in todo:
try: try:
@ -335,7 +338,7 @@ Gerrit Code Review: http://code.google.com/p/gerrit/
branch.error = 'User aborted' branch.error = 'User aborted'
continue continue
branch.UploadForReview(people) branch.UploadForReview(people, auto_topic=opt.auto_topic)
branch.uploaded = True branch.uploaded = True
except UploadError, e: except UploadError, e:
branch.error = e branch.error = e
@ -391,6 +394,6 @@ Gerrit Code Review: http://code.google.com/p/gerrit/
if not pending: if not pending:
print >>sys.stdout, "no branches ready for upload" print >>sys.stdout, "no branches ready for upload"
elif len(pending) == 1 and len(pending[0][1]) == 1: elif len(pending) == 1 and len(pending[0][1]) == 1:
self._SingleBranch(pending[0][1][0], people) self._SingleBranch(opt, pending[0][1][0], people)
else: else:
self._MultipleBranches(pending, people) self._MultipleBranches(opt, pending, people)