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
diff --git a/git_cl.py b/git_cl.py
index 9995032..62f339d 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -39,6 +39,7 @@
 POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s'
 DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup'
 GIT_INSTRUCTIONS_URL = 'http://code.google.com/p/chromium/wiki/UsingNewGit'
+CHANGE_ID = 'Change-Id:'
 
 
 # Initialized in main()
@@ -1017,6 +1018,20 @@
   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):
   """upload the current branch to gerrit."""
   # We assume the remote called "origin" is the one we want.
@@ -1027,6 +1042,8 @@
     branch = options.target_branch
 
   log_desc = options.message or CreateDescriptionFromLog(args)
+  if CHANGE_ID not in log_desc:
+    AddChangeIdToCommitMessage(options, args)
   if options.reviewers:
     log_desc += '\nR=' + options.reviewers
   change_desc = ChangeDescription(log_desc, options.reviewers)