Add gerrit Change-Id to commit messages if they do not have one.


BUG=156729


Review URL: https://chromiumcodereview.appspot.com/11236013

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@163366 0039d316-1c4b-4281-b951-d872f2087c98
parent 426557ad
...@@ -39,6 +39,7 @@ DEFAULT_SERVER = 'https://codereview.appspot.com' ...@@ -39,6 +39,7 @@ DEFAULT_SERVER = 'https://codereview.appspot.com'
POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s' POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s'
DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup' DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup'
GIT_INSTRUCTIONS_URL = 'http://code.google.com/p/chromium/wiki/UsingNewGit' GIT_INSTRUCTIONS_URL = 'http://code.google.com/p/chromium/wiki/UsingNewGit'
CHANGE_ID = 'Change-Id:'
# Initialized in main() # Initialized in main()
...@@ -1017,6 +1018,20 @@ def CMDpresubmit(parser, args): ...@@ -1017,6 +1018,20 @@ def CMDpresubmit(parser, args):
return 0 return 0
def AddChangeIdToCommitMessage(options, args):
"""Re-commits using the current message, assumes the commit hook is in
place.
"""
log_desc = options.message or CreateDescriptionFromLog(args)
git_command = ['commit', '--amend', '-m', log_desc]
RunGit(git_command)
new_log_desc = CreateDescriptionFromLog(args)
if CHANGE_ID in new_log_desc:
print 'git-cl: Added Change-Id to commit message.'
else:
print >> sys.stderr, 'ERROR: Gerrit commit-msg hook not available.'
def GerritUpload(options, args, cl): def GerritUpload(options, args, cl):
"""upload the current branch to gerrit.""" """upload the current branch to gerrit."""
# We assume the remote called "origin" is the one we want. # We assume the remote called "origin" is the one we want.
...@@ -1027,6 +1042,8 @@ def GerritUpload(options, args, cl): ...@@ -1027,6 +1042,8 @@ def GerritUpload(options, args, cl):
branch = options.target_branch branch = options.target_branch
log_desc = options.message or CreateDescriptionFromLog(args) log_desc = options.message or CreateDescriptionFromLog(args)
if CHANGE_ID not in log_desc:
AddChangeIdToCommitMessage(options, args)
if options.reviewers: if options.reviewers:
log_desc += '\nR=' + options.reviewers log_desc += '\nR=' + options.reviewers
change_desc = ChangeDescription(log_desc, options.reviewers) change_desc = ChangeDescription(log_desc, options.reviewers)
......
...@@ -436,7 +436,18 @@ class TestGitCl(TestCase): ...@@ -436,7 +436,18 @@ class TestGitCl(TestCase):
def _gerrit_upload_calls(description, reviewers): def _gerrit_upload_calls(description, reviewers):
calls = [ calls = [
((['git', 'log', '--pretty=format:%s\n\n%b', 'master..'],), ((['git', 'log', '--pretty=format:%s\n\n%b', 'master..'],),
description), description)
]
if git_cl.CHANGE_ID not in description:
calls += [
((['git', 'log', '--pretty=format:%s\n\n%b', 'master..'],),
description),
((['git', 'commit', '--amend', '-m', description],),
''),
((['git', 'log', '--pretty=format:%s\n\n%b', 'master..'],),
description)
]
calls += [
((['git', 'config', 'rietveld.cc'],), '') ((['git', 'config', 'rietveld.cc'],), '')
] ]
receive_pack = '--receive-pack=git receive-pack ' receive_pack = '--receive-pack=git receive-pack '
...@@ -451,32 +462,39 @@ class TestGitCl(TestCase): ...@@ -451,32 +462,39 @@ class TestGitCl(TestCase):
] ]
return calls return calls
def _run_gerrit_reviewer_test( def _run_gerrit_upload_test(
self, self,
upload_args, upload_args,
description, description,
reviewers): reviewers):
"""Generic gerrit reviewer test framework.""" """Generic gerrit upload test framework."""
self.calls = self._gerrit_base_calls() self.calls = self._gerrit_base_calls()
self.calls += self._gerrit_upload_calls(description, reviewers) self.calls += self._gerrit_upload_calls(description, reviewers)
git_cl.main(['upload'] + upload_args) git_cl.main(['upload'] + upload_args)
def test_gerrit_no_reviewer(self): def test_gerrit_upload_without_change_id(self):
self._run_gerrit_reviewer_test( self._run_gerrit_upload_test(
[], [],
'desc\n\nBUG=\n', 'desc\n\nBUG=\n',
[]) [])
def test_gerrit_no_reviewer(self):
self._run_gerrit_upload_test(
[],
'desc\n\nBUG=\nChange-Id:123456789\n',
[])
def test_gerrit_reviewers_cmd_line(self): def test_gerrit_reviewers_cmd_line(self):
self._run_gerrit_reviewer_test( self._run_gerrit_upload_test(
['-r', 'foo@example.com'], ['-r', 'foo@example.com'],
'desc\n\nBUG=\n', 'desc\n\nBUG=\nChange-Id:123456789',
['foo@example.com']) ['foo@example.com'])
def test_gerrit_reviewer_multiple(self): def test_gerrit_reviewer_multiple(self):
self._run_gerrit_reviewer_test( self._run_gerrit_upload_test(
[], [],
'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n', 'desc\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n'
'Change-Id:123456789\n',
['reviewer@example.com', 'another@example.com']) ['reviewer@example.com', 'another@example.com'])
......
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