findmissing: Add pre-existing Change-Id to new commits

In some cases a patch is already present in Gerrit. If so, use existing
Change-Ids to create new commits. This enables Gerrit to show commits into
multiple branches in a single UI instance.

BUG=None
TEST=Apply same commit to multiple branches

Change-Id: I6cc9285066cbdc6506cca45139974406acfe9191
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2280246
Commit-Queue: Guenter Roeck <groeck@chromium.org>
Tested-by: Guenter Roeck <groeck@chromium.org>
Reviewed-by: Curtis Malainey <cujomalainey@chromium.org>
diff --git a/contrib/findmissing/gerrit_interface.py b/contrib/findmissing/gerrit_interface.py
index ecd9bf9..4342df2 100755
--- a/contrib/findmissing/gerrit_interface.py
+++ b/contrib/findmissing/gerrit_interface.py
@@ -208,7 +208,7 @@
 
 
 # Note: Stable patches won't have a fixee_change_id since they come into chromeos as merges
-def create_change(fixee_kernel_sha, fixer_upstream_sha, branch):
+def create_change(fixee_kernel_sha, fixer_upstream_sha, branch, fixer_changeid=None):
     """Creates a Patch in gerrit given a ChangeInput object.
 
     Determines whether a change for a fix has already been created,
@@ -247,7 +247,7 @@
 
     try:
         # Cherry pick changes and generate commit message indicating fix from upstream
-        fixer_changeid = git_interface.cherry_pick_and_push_fix(fixer_upstream_sha,
+        fixer_changeid = git_interface.cherry_pick_and_push_fix(fixer_upstream_sha, fixer_changeid,
                                                     chromeos_branch, fix_commit_message, reviewers)
     except ValueError:
         # Error cherry-picking and pushing fix patch
diff --git a/contrib/findmissing/git_interface.py b/contrib/findmissing/git_interface.py
index 83f86be..ac62892 100755
--- a/contrib/findmissing/git_interface.py
+++ b/contrib/findmissing/git_interface.py
@@ -136,7 +136,7 @@
     return git_push_head + '%' + tags
 
 
-def cherry_pick_and_push_fix(fixer_upstream_sha, chromeos_branch,
+def cherry_pick_and_push_fix(fixer_upstream_sha, fixer_changeid, chromeos_branch,
                                 fix_commit_message, reviewers):
     """Cherry picks upstream commit into chrome repo.
 
@@ -153,15 +153,19 @@
         subprocess.run(['git', 'commit', '-s', '-m', fix_commit_message], check=True)
 
         # commit has been cherry-picked and committed locally, precommit hook
-        #  in git repository adds changeid to the commit message
-        fixer_changeid = get_commit_changeid_linux_chrome('HEAD')
+        # in git repository adds changeid to the commit message. Pick it unless
+        # we already have one passed as parameter.
+        if not fixer_changeid:
+            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.
+        # of the commit message to solve the problem. This conveniently also
+        # replaces the auto-generated Change-Id with the optional Change-Id
+        # passed as parameter.
         commit_message = get_chrome_commit_message('HEAD')
         commit_message = re.sub(r'Change-Id:.*\n?', '', commit_message)
         commit_message = commit_message.rstrip()