git_interface: Force-move Change-Id to end of commit message

Sometimes the git commit hook inserts the Change-Id into the middle of
the commit message and not to its last paragraph. This seems to happen
if '---' is part of the commit message. Subsequently gerrit refuses to
accept the commit.

Solve the problem by force-moving the Change-Id to the end of the commit
message.

While at it, drop get_last_commit_sha_linux_chrome() and its use - we
can refer to the most recent commit in a branch by using the 'HEAD'
reference, without knowing the actual SHA.

BUG=None
TEST=Run patch robot

Change-Id: I68cfe434753d20cdf1f00929b8f1e2ff490b76b5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2254304
Reviewed-by: Curtis Malainey <cujomalainey@chromium.org>
Commit-Queue: Guenter Roeck <groeck@chromium.org>
Tested-by: Guenter Roeck <groeck@chromium.org>
diff --git a/contrib/findmissing/git_interface.py b/contrib/findmissing/git_interface.py
index 4cb283e..eee761c 100755
--- a/contrib/findmissing/git_interface.py
+++ b/contrib/findmissing/git_interface.py
@@ -123,17 +123,6 @@
         return None
 
 
-def get_last_commit_sha_linux_chrome():
-    """Retrieves the last SHA in linux_chrome repository."""
-    chrome_absolute_path = common.get_kernel_absolute_path(common.CHROMEOS_PATH)
-    try:
-        cmd = ['git', '-C', chrome_absolute_path, 'rev-parse', 'HEAD']
-        last_commit = subprocess.check_output(cmd, encoding='utf-8')
-        return last_commit.rstrip()
-    except subprocess.CalledProcessError as e:
-        raise type(e)('Couldnt retrieve most recent commit in linux_chrome', e.cmd) from e
-
-
 def get_git_push_cmd(chromeos_branch, reviewers):
     """Generates git push command with added reviewers and autogenerated tag.
 
@@ -165,8 +154,18 @@
 
         # commit has been cherry-picked and committed locally, precommit hook
         #  in git repository adds changeid to the commit message
-        last_commit = get_last_commit_sha_linux_chrome()
-        fixer_changeid = get_commit_changeid_linux_chrome(last_commit)
+        fixer_changeid = get_commit_changeid_linux_chrome('HEAD')
+
+        # Sometimes the commit hook doesn't attach the Change-Id to the last
+        # paragraph in the commit message. This seems to happen if the commit
+        # message includes '---' which would normally identify the start of
+        # comments. If the Change-Id is not in the last paragraph, uploading
+        # the patch is rejected by Gerrit. Force-move the Change-Id to the end
+        # of the commit message to solve the problem.
+        commit_message = get_chrome_commit_message('HEAD').rstrip()
+        commit_message = re.sub(r'Change-Id:.*\n?', '', commit_message)
+        commit_message += '\nChange-Id: %s\n' % fixer_changeid
+        subprocess.run(['git', 'commit', '--amend', '-m', commit_message], check=True)
 
         git_push_cmd = get_git_push_cmd(chromeos_branch, reviewers)
         subprocess.run(git_push_cmd.split(' '), check=True)