merge_roll: Merge into HEAD instead of main

Only include commits without equivalents in the new repo for the commit
message.

Also recognize that the 'git cherry' output has the oldest change first,
not last.

Bug: b/512911021
Change-Id: I7a8fc33dd04730343d307d8757832043cba647d7
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/411876
diff --git a/recipe_modules/merge_roll/api.py b/recipe_modules/merge_roll/api.py
index cb052be..4845e20 100644
--- a/recipe_modules/merge_roll/api.py
+++ b/recipe_modules/merge_roll/api.py
@@ -50,7 +50,7 @@
         self,
         checkout: checkout_api.CheckoutContext,
         new_revision: str,
-    ) -> RevisionChange:
+    ) -> RevisionChange | None:
         """Prepares and returns a RevisionChange object for a merge.
 
         A `finalize` function is created that, when called, will merge the
@@ -62,16 +62,25 @@
 
         Returns:
             A `RevisionChange` object containing the old and new revisions,
-            and a `finalize` callable to apply the update.
+            and a `finalize` callable to apply the update, or None if there's
+            nothing to merge.
         """
 
         with self.m.context(cwd=checkout.top):
-            first_new_commit = self.m.git.cherry(
-                source=new_revision,
-                destination='main',
-            )[-1].commit
+            self.m.git.fetch(checkout.options.remote, new_revision)
 
-            old_revision = self.m.git.rev_parse(f'{first_new_commit}~1')
+            new_commits: list[str] = []
+            for pair in self.m.git.cherry(
+                source=new_revision,
+                destination='HEAD',
+            ):
+                if not pair.has_equivalent:
+                    new_commits.append(pair.commit)
+
+            if not new_commits:
+                return None  # pragma: no cover
+
+            old_revision = self.m.git.rev_parse(f'{new_commits[0]}~1')
 
         def finalize() -> None:
             with self.m.context(cwd=checkout.top):
@@ -103,6 +112,9 @@
                 new_revision,
             )
 
+            if not change:
+                return []  # pragma: no cover
+
             try:
                 roll = self.m.git_roll_util.get_roll(
                     repo_url=checkout.options.remote,
diff --git a/recipes/roller.expected/mega.json b/recipes/roller.expected/mega.json
index f63144e..8c84ecf 100644
--- a/recipes/roller.expected/mega.json
+++ b/recipes/roller.expected/mega.json
@@ -7716,8 +7716,46 @@
   {
     "cmd": [
       "git",
+      "fetch",
+      "--jobs",
+      "4",
+      "https://pigweed.googlesource.com/pigweed/pigweed",
+      "h3ll0"
+    ],
+    "cwd": "[START_DIR]/co",
+    "env": {
+      "GIT_ADVICE": "0",
+      "GIT_CONFIG_COUNT": "3",
+      "GIT_CONFIG_KEY_0": "gc.auto",
+      "GIT_CONFIG_KEY_1": "gc.autoDetach",
+      "GIT_CONFIG_KEY_2": "gc.autoPackLimit",
+      "GIT_CONFIG_VALUE_0": "0",
+      "GIT_CONFIG_VALUE_1": "0",
+      "GIT_CONFIG_VALUE_2": "0"
+    },
+    "luci_context": {
+      "realm": {
+        "name": "project:ci"
+      },
+      "resultdb": {
+        "current_invocation": {
+          "name": "invocations/build:8945511751514863184",
+          "update_token": "token"
+        },
+        "hostname": "rdbhost"
+      }
+    },
+    "name": "staging.git fetch",
+    "timeout": 1200.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
       "cherry",
-      "main",
+      "HEAD",
       "h3ll0"
     ],
     "cwd": "[START_DIR]/co",
diff --git a/recipes/roller.expected/noop.json b/recipes/roller.expected/noop.json
index 991139e..940556d 100644
--- a/recipes/roller.expected/noop.json
+++ b/recipes/roller.expected/noop.json
@@ -3600,8 +3600,34 @@
   {
     "cmd": [
       "git",
+      "fetch",
+      "--jobs",
+      "4",
+      "https://pigweed.googlesource.com/pigweed/pigweed",
+      "h3ll0"
+    ],
+    "cwd": "[START_DIR]/co",
+    "env": {
+      "GIT_ADVICE": "0",
+      "GIT_CONFIG_COUNT": "3",
+      "GIT_CONFIG_KEY_0": "gc.auto",
+      "GIT_CONFIG_KEY_1": "gc.autoDetach",
+      "GIT_CONFIG_KEY_2": "gc.autoPackLimit",
+      "GIT_CONFIG_VALUE_0": "0",
+      "GIT_CONFIG_VALUE_1": "0",
+      "GIT_CONFIG_VALUE_2": "0"
+    },
+    "name": "staging.git fetch",
+    "timeout": 1200.0,
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "git",
       "cherry",
-      "main",
+      "HEAD",
       "h3ll0"
     ],
     "cwd": "[START_DIR]/co",