Commit 3c3c034d authored by tandrii@chromium.org's avatar tandrii@chromium.org

Add Gerrit's Change-Id handling to git_footers.

This should insert the message according to Gerrit's own commit-msg
hook implementation.

BUG=579183

Review URL: https://codereview.chromium.org/1758943002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@299093 0039d316-1c4b-4281-b951-d872f2087c98
parent 6593d933
......@@ -62,6 +62,44 @@ def get_footer_svn_id(branch=None):
return svn_id
def get_footer_change_id(message):
"""Returns a list of Gerrit's ChangeId from given commit message."""
return parse_footers(message).get(normalize_name('Change-Id'), [])
def add_footer_change_id(message, change_id):
"""Returns message with Change-ID footer in it.
Assumes that Change-Id is not yet in footers, which is then
inserted after any of these footers: Bug|Issue|Test|Feature.
"""
assert 0 == len(get_footer_change_id(message))
change_id_line = 'Change-Id: %s' % change_id
# This code does the same as parse_footers, but keeps track of line
# numbers so that ChangeId is inserted in the right place.
lines = message.splitlines()
footer_lines = []
for line in reversed(lines):
if line == '' or line.isspace():
break
footer_lines.append(line)
# footers order is from end to start of the message.
footers = map(parse_footer, footer_lines)
if not all(footers):
lines.append('')
lines.append(change_id_line)
else:
after = set(map(normalize_name, ['Bug', 'Issue', 'Test', 'Feature']))
for i, (key, _) in enumerate(footers):
if normalize_name(key) in after:
insert_at = len(lines) - i
break
else:
insert_at = len(lines) - len(footers)
lines.insert(insert_at, change_id_line)
return '\n'.join(lines)
def get_unique(footers, key):
key = normalize_name(key)
values = footers[key]
......
......@@ -66,5 +66,35 @@ My commit message is my best friend. It is my life. I must master it.
git_footers.get_position(footers),
('refs/branch-heads/blabble', None))
def testGetFooterChangeId(self):
msg = '\n'.join(['whatever',
'',
'Change-Id: ignored',
'', # Above is ignored because of this empty line.
'Change-Id: Ideadbeaf'])
self.assertEqual(['Ideadbeaf'], git_footers.get_footer_change_id(msg))
def testAddFooterChangeId(self):
self.assertEqual(
git_footers.add_footer_change_id('header-only', 'Ixxx'),
'header-only\n\nChange-Id: Ixxx')
self.assertEqual(
git_footers.add_footer_change_id('header\n\nsome: footter', 'Ixxx'),
'header\n\nChange-Id: Ixxx\nsome: footter')
self.assertEqual(
git_footers.add_footer_change_id('header\n\nBUG: yy', 'Ixxx'),
'header\n\nBUG: yy\nChange-Id: Ixxx')
self.assertEqual(
git_footers.add_footer_change_id('header\n\nBUG: yy\nPos: 1', 'Ixxx'),
'header\n\nBUG: yy\nChange-Id: Ixxx\nPos: 1')
self.assertEqual(
git_footers.add_footer_change_id('header\n\nBUG: yy\n\nPos: 1', 'Ixxx'),
'header\n\nBUG: yy\n\nChange-Id: Ixxx\nPos: 1')
if __name__ == '__main__':
unittest.main()
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