Teach 'repo upload --replace' how to add replacement patch sets

Users are prompted with the list of known changes we are about
to upload, and they can fill out the current change numbers for
any changes which already exist in the data store.  For each of
those changes the change number and commit id is sent as part of
the upload request, so Gerrit can insert the new commit as a new
patch set of the existing change, rather than make a new change.

This facility permits developers to replace a patch so they can
address comments made on a prior version of the same change.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce 2008-11-11 17:12:43 -08:00
parent ec18b4bac4
commit c99883fee9
3 changed files with 83 additions and 4 deletions

View File

@ -75,6 +75,7 @@ def UploadBundle(project,
dest_branch, dest_branch,
src_branch, src_branch,
bases, bases,
replace_changes = None,
save_cookies=True): save_cookies=True):
srv = _GetRpcServer(email, server, save_cookies) srv = _GetRpcServer(email, server, save_cookies)
@ -113,6 +114,10 @@ def UploadBundle(project,
req.dest_branch = str(dest_branch) req.dest_branch = str(dest_branch)
for c in revlist: for c in revlist:
req.contained_object.append(c) req.contained_object.append(c)
for change_id,commit_id in replace_changes.iteritems():
r = req.replace.add()
r.change_id = change_id
r.object_id = commit_id
else: else:
req = UploadBundleContinue() req = UploadBundleContinue()
req.bundle_id = bundle_id req.bundle_id = bundle_id
@ -148,6 +153,10 @@ def UploadBundle(project,
elif rsp.status_code == UploadBundleResponse.UNAUTHORIZED_USER: elif rsp.status_code == UploadBundleResponse.UNAUTHORIZED_USER:
reason = ('Unauthorized user. Visit http://%s/hello to sign up.' reason = ('Unauthorized user. Visit http://%s/hello to sign up.'
% server) % server)
elif rsp.status_code == UploadBundleResponse.UNKNOWN_CHANGE:
reason = 'invalid change id'
elif rsp.status_code == UploadBundleResponse.CHANGE_CLOSED:
reason = 'one or more changes are closed'
else: else:
reason = 'unknown error ' + str(rsp.status_code) reason = 'unknown error ' + str(rsp.status_code)
raise UploadError(reason) raise UploadError(reason)

View File

@ -104,6 +104,7 @@ class ReviewableBranch(object):
self.project = project self.project = project
self.branch = branch self.branch = branch
self.base = base self.base = base
self.replace_changes = None
@property @property
def name(self): def name(self):
@ -123,6 +124,16 @@ class ReviewableBranch(object):
'--') '--')
return self._commit_cache return self._commit_cache
@property
def unabbrev_commits(self):
r = dict()
for commit in self.project.bare_git.rev_list(
not_rev(self.base),
R_HEADS + self.name,
'--'):
r[commit[0:8]] = commit
return r
@property @property
def date(self): def date(self):
return self.project.bare_git.log( return self.project.bare_git.log(
@ -132,7 +143,8 @@ class ReviewableBranch(object):
'--') '--')
def UploadForReview(self): def UploadForReview(self):
self.project.UploadForReview(self.name) self.project.UploadForReview(self.name,
self.replace_changes)
@property @property
def tip_url(self): def tip_url(self):
@ -444,7 +456,7 @@ class Project(object):
return rb return rb
return None return None
def UploadForReview(self, branch=None): def UploadForReview(self, branch=None, replace_changes=None):
"""Uploads the named branch for code review. """Uploads the named branch for code review.
""" """
if branch is None: if branch is None:
@ -482,7 +494,8 @@ class Project(object):
dest_project = branch.remote.projectname, dest_project = branch.remote.projectname,
dest_branch = dest_branch, dest_branch = dest_branch,
src_branch = R_HEADS + branch.name, src_branch = R_HEADS + branch.name,
bases = base_list) bases = base_list,
replace_changes = replace_changes)
except proto_client.ClientLoginError: except proto_client.ClientLoginError:
raise UploadError('Login failure') raise UploadError('Login failure')
except urllib2.HTTPError, e: except urllib2.HTTPError, e:

View File

@ -29,7 +29,7 @@ class Upload(InteractiveCommand):
common = True common = True
helpSummary = "Upload changes for code review" helpSummary = "Upload changes for code review"
helpUsage=""" helpUsage="""
%prog [<project>]... %prog {[<project>]... | --replace <project>}
""" """
helpDescription = """ helpDescription = """
The '%prog' command is used to send changes to the Gerrit code The '%prog' command is used to send changes to the Gerrit code
@ -46,6 +46,11 @@ no projects are specified, '%prog' will search for uploadable
changes in all projects listed in the manifest. changes in all projects listed in the manifest.
""" """
def _Options(self, p):
p.add_option('--replace',
dest='replace', action='store_true',
help='Upload replacement patchesets from this branch')
def _SingleBranch(self, branch): def _SingleBranch(self, branch):
project = branch.project project = branch.project
name = branch.name name = branch.name
@ -129,6 +134,50 @@ changes in all projects listed in the manifest.
_die("nothing uncommented for upload") _die("nothing uncommented for upload")
self._UploadAndReport(todo) self._UploadAndReport(todo)
def _ReplaceBranch(self, project):
branch = project.CurrentBranch
if not branch:
print >>sys.stdout, "no branches ready for upload"
return
branch = project.GetUploadableBranch(branch)
if not branch:
print >>sys.stdout, "no branches ready for upload"
return
script = []
script.append('# Replacing from branch %s' % branch.name)
for commit in branch.commits:
script.append('[ ] %s' % commit)
script.append('')
script.append('# Insert change numbers in the brackets to add a new patch set.')
script.append('# To create a new change record, leave the brackets empty.')
script = Editor.EditString("\n".join(script)).split("\n")
change_re = re.compile(r'^\[\s*(\d{1,})\s*\]\s*([0-9a-f]{1,}) .*$')
to_replace = dict()
full_hashes = branch.unabbrev_commits
for line in script:
m = change_re.match(line)
if m:
f = m.group(2)
try:
f = full_hashes[f]
except KeyError:
print 'fh = %s' % full_hashes
print >>sys.stderr, "error: commit %s not found" % f
sys.exit(1)
to_replace[m.group(1)] = f
if not to_replace:
print >>sys.stderr, "error: no replacements specified"
print >>sys.stderr, " use 'repo upload' without --replace"
sys.exit(1)
branch.replace_changes = to_replace
self._UploadAndReport([branch])
def _UploadAndReport(self, todo): def _UploadAndReport(self, todo):
have_errors = False have_errors = False
for branch in todo: for branch in todo:
@ -168,6 +217,14 @@ changes in all projects listed in the manifest.
project_list = self.GetProjects(args) project_list = self.GetProjects(args)
pending = [] pending = []
if opt.replace:
if len(project_list) != 1:
print >>sys.stderr, \
'error: --replace requires exactly one project'
sys.exit(1)
self._ReplaceBranch(project_list[0])
return
for project in project_list: for project in project_list:
avail = project.GetUploadableBranches() avail = project.GetUploadableBranches()
if avail: if avail: