Commit 5fb22249 authored by Edward Lemur's avatar Edward Lemur Committed by LUCI CQ

git-cl: Set the right Change-Id footer automatically.

If the Change-Id in the description is different from the one fetched
from Gerrit, automatically set the right one instead of asking the
user to do so.

Change-Id: I0f681d9e7d98f3ed50adc4b6e4929981530e901c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2101368
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: 's avatarAnthony Polito <apolito@google.com>
parent 595eb191
......@@ -2304,41 +2304,20 @@ class Changelist(object):
message = change_desc.description
change_id = self._GetChangeDetail()['change_id']
while True:
footer_change_ids = git_footers.get_footer_change_id(message)
if footer_change_ids == [change_id]:
break
if not footer_change_ids:
message = git_footers.add_footer_change_id(message, change_id)
print('WARNING: appended missing Change-Id to change description.')
continue
# There is already a valid footer but with different or several ids.
# Doing this automatically is non-trivial as we don't want to lose
# existing other footers, yet we want to append just 1 desired
# Change-Id. Thus, just create a new footer, but let user verify the
# new description.
message = '%s\n\nChange-Id: %s' % (message, change_id)
change_desc = ChangeDescription(message, bug=bug, fixed=fixed)
if not options.force:
print(
'WARNING: change %s has Change-Id footer(s):\n'
' %s\n'
'but change has Change-Id %s, according to Gerrit.\n'
'Please, check the proposed correction to the description, '
'and edit it if necessary but keep the "Change-Id: %s" footer\n'
% (self.GetIssue(), '\n '.join(footer_change_ids), change_id,
change_id))
confirm_or_exit(action='edit')
change_desc.prompt()
message = change_desc.description
if not message:
DieWithError("Description is empty. Aborting...")
# Continue the while loop.
# Sanity check of this code - we should end up with proper message
# footer.
assert [change_id] == git_footers.get_footer_change_id(message)
# Make sure that the Change-Id in the description matches the one
# fetched from Gerrit.
footer_change_ids = git_footers.get_footer_change_id(message)
if footer_change_ids != [change_id]:
if footer_change_ids:
# Remove any existing Change-Id footers since they don't match the
# expected change_id footer.
message = git_footers.remove_footer(message, 'Change-Id')
# Add the expected Change-Id footer.
message = git_footers.add_footer_change_id(message, change_id)
print('WARNING: Change-Id has been set to Change-Id fetched from '
'Gerrit. Use `git cl issue 0` if you want to clear it and '
'set a new one.')
change_desc = ChangeDescription(message, bug=bug, fixed=fixed)
else: # if not self.GetIssue()
change_desc = ChangeDescription(message, bug=bug, fixed=fixed)
......
......@@ -887,19 +887,16 @@ class TestGitCl(unittest.TestCase):
(('DownloadGerritHook', False), ''),
]
if squash:
if force or not issue:
if not force:
calls += [
((['RunEditor'],), description),
]
if not force and not issue:
calls += [
((['RunEditor'],), description),
]
# user wants to edit description
if edit_description:
calls += [
((['RunEditor'],), edit_description),
]
ref_to_push = 'abcdef0123456789'
calls += [
]
if custom_cl_base is None:
calls += [
......@@ -1322,18 +1319,18 @@ class TestGitCl(unittest.TestCase):
original_title='Initial upload',
change_id='Ixxx')
def test_gerrit_upload_force_sets_bug_if_wrong_changeid(self):
def test_gerrit_upload_corrects_wrong_change_id(self):
self._run_gerrit_upload_test(
['-b', '10000', '-f', '-m', 'Title'],
u'desc=\n\nChange-Id: Ixxxx\n\nChange-Id: Izzzz\nBug: 10000',
['-b', '10000', '-m', 'Title', '--edit-description'],
u'desc=\n\nBug: 10000\nChange-Id: Ixxxx',
[],
force=True,
issue='123456',
expected_upstream_ref='origin/master',
edit_description='desc=\n\nBug: 10000\nChange-Id: Izzzz',
fetched_description='desc=\n\nChange-Id: Ixxxx',
original_title='Title',
title='Title',
change_id='Izzzz')
change_id='Ixxxx')
def test_gerrit_upload_force_sets_fixed(self):
self._run_gerrit_upload_test(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment