[checkout][coverage] Don't rebase patch for coverage builders.

Bug: 335727014
Change-Id: I2d299fc2f829d1096e37d3ac3e5148709d53eac3
Reviewed-on: https://fuchsia-review.googlesource.com/c/infra/recipes/+/1031077
Fuchsia-Auto-Submit: Ina Huh <ihuh@google.com>
Reviewed-by: Oliver Newman <olivernewman@google.com>
Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
diff --git a/recipe_modules/checkout/api.py b/recipe_modules/checkout/api.py
index 0564d10..e57a05f 100644
--- a/recipe_modules/checkout/api.py
+++ b/recipe_modules/checkout/api.py
@@ -355,6 +355,7 @@
             is_release_version=checkout_spec.is_release_version,
             allow_skipping_patch=checkout_spec.allow_skipping_patch,
             enable_submodules=checkout_spec.enable_submodules,
+            rebase_patch=not checkout_spec.do_not_rebase_patch,
             **kwargs,
         )
 
@@ -477,6 +478,7 @@
         use_incremental_cache=False,
         allow_skipping_patch=False,
         submodule_update=True,
+        rebase_patch=True,
     ):
         """Initializes and populates a jiri checkout from a remote manifest.
 
@@ -514,6 +516,7 @@
                 checkout.
             submodule_update (bool): Whether or not to reconcile differences between
                 integration and submodules.
+            rebase_patch (bool): Whether or not to rebase the patch on top of HEAD.
 
         Returns: A Path to the checkout root.
         """
@@ -714,6 +717,7 @@
                         timeout_secs,
                         enable_submodules,
                         submodule_update,
+                        rebase=rebase_patch,
                     )
                 except self.m.jiri.NoSuchProjectError as e:
                     # Always allow skipping integration patches to support
@@ -953,6 +957,7 @@
         timeout_secs,
         enable_submodules,
         submodule_update,
+        rebase,
     ):
         target_branch = change_details["branch"]
 
@@ -987,7 +992,7 @@
                     patch_ref,
                     host=f"https://{gerrit_change.host}",
                     project=patch_project,
-                    rebase=True,
+                    rebase=rebase,
                     rebase_branch=target_branch,
                 )
             except self.m.jiri.NoSuchProjectError:
diff --git a/recipe_modules/checkout/tests/full.expected/patch_third_party_change.json b/recipe_modules/checkout/tests/full.expected/patch_third_party_change.json
index b131c0a..324216a 100644
--- a/recipe_modules/checkout/tests/full.expected/patch_third_party_change.json
+++ b/recipe_modules/checkout/tests/full.expected/patch_third_party_change.json
@@ -676,7 +676,6 @@
       "libc-tests",
       "-delete=true",
       "-force=true",
-      "-rebase=true",
       "-rebase-branch",
       "main",
       "refs/changes/00/100/7"
diff --git a/recipe_modules/checkout/tests/full.py b/recipe_modules/checkout/tests/full.py
index 394edd2..3022af8 100644
--- a/recipe_modules/checkout/tests/full.py
+++ b/recipe_modules/checkout/tests/full.py
@@ -70,6 +70,11 @@
         help="The manifest revision to set in the input properties",
         default=None,
     ),
+    "do_not_rebase_patch": Property(
+        kind=bool,
+        help="Whether to rebase patch o top of HEAD",
+        default=False,
+    ),
 }
 
 
@@ -84,6 +89,7 @@
     allow_skipping_patch,
     enable_submodules,
     base_manifest_revision,
+    do_not_rebase_patch,
 ):
     if base_manifest_revision:
         api.buildbucket.build.input.properties[
@@ -98,6 +104,7 @@
         attributes=attributes,
         allow_skipping_patch=allow_skipping_patch,
         enable_submodules=enable_submodules,
+        do_not_rebase_patch=do_not_rebase_patch,
     )
     checkout = api.checkout.from_spec(
         checkout_spec,
@@ -417,7 +424,7 @@
             repo="libc-tests",
             status="SUCCESS",
         )
-        + api.properties(enable_submodules=True)
+        + api.properties(enable_submodules=True, do_not_rebase_patch=True)
         + api.checkout.source_info(source_info)
         + api.step_data(
             "checkout.jiri project submodule check patch",
diff --git a/recipes/fuchsia/coverage.expected/collect_absolute.json b/recipes/fuchsia/coverage.expected/collect_absolute.json
index 4b40d1c..fe5fe41 100644
--- a/recipes/fuchsia/coverage.expected/collect_absolute.json
+++ b/recipes/fuchsia/coverage.expected/collect_absolute.json
@@ -875,7 +875,6 @@
       "fuchsia",
       "-delete=true",
       "-force=true",
-      "-rebase=true",
       "-rebase-branch",
       "main",
       "refs/changes/00/100/9"
diff --git a/recipes/fuchsia/coverage.expected/default_led.json b/recipes/fuchsia/coverage.expected/default_led.json
index 64e21ba..ed411ca 100644
--- a/recipes/fuchsia/coverage.expected/default_led.json
+++ b/recipes/fuchsia/coverage.expected/default_led.json
@@ -1093,7 +1093,6 @@
       "fuchsia",
       "-delete=true",
       "-force=true",
-      "-rebase=true",
       "-rebase-branch",
       "main",
       "refs/changes/00/100/7"
diff --git a/recipes/fuchsia/coverage.expected/led_with_child_build_ids.json b/recipes/fuchsia/coverage.expected/led_with_child_build_ids.json
index 03afc2f..79670c4 100644
--- a/recipes/fuchsia/coverage.expected/led_with_child_build_ids.json
+++ b/recipes/fuchsia/coverage.expected/led_with_child_build_ids.json
@@ -676,7 +676,6 @@
       "fuchsia",
       "-delete=true",
       "-force=true",
-      "-rebase=true",
       "-rebase-branch",
       "main",
       "refs/changes/00/100/7"
diff --git a/recipes/fuchsia/coverage.expected/skipped_build.json b/recipes/fuchsia/coverage.expected/skipped_build.json
index 1cc6956..0ae5ce7 100644
--- a/recipes/fuchsia/coverage.expected/skipped_build.json
+++ b/recipes/fuchsia/coverage.expected/skipped_build.json
@@ -895,7 +895,6 @@
       "fuchsia",
       "-delete=true",
       "-force=true",
-      "-rebase=true",
       "-rebase-branch",
       "main",
       "refs/changes/00/100/7"
diff --git a/recipes/fuchsia/coverage.expected/skipped_testing.json b/recipes/fuchsia/coverage.expected/skipped_testing.json
index 2d098e5..d330c42 100644
--- a/recipes/fuchsia/coverage.expected/skipped_testing.json
+++ b/recipes/fuchsia/coverage.expected/skipped_testing.json
@@ -848,7 +848,6 @@
       "fuchsia",
       "-delete=true",
       "-force=true",
-      "-rebase=true",
       "-rebase-branch",
       "main",
       "refs/changes/00/100/3"
diff --git a/recipes/fuchsia/coverage.py b/recipes/fuchsia/coverage.py
index a61b003..de391ca 100644
--- a/recipes/fuchsia/coverage.py
+++ b/recipes/fuchsia/coverage.py
@@ -116,7 +116,9 @@
     assert build_ids, "failed to get builds"
 
     checkout = api.checkout.fuchsia_with_options(
-        manifest=props.manifest, remote=props.remote
+        manifest=props.manifest,
+        remote=props.remote,
+        rebase_patch=not api.buildbucket_util.is_tryjob,
     )
 
     # Need to run api.build.gen() to generate the tool_paths.json to access the
diff --git a/recipes/fuchsia/spec.proto b/recipes/fuchsia/spec.proto
index f5ac4c7..e95e222 100644
--- a/recipes/fuchsia/spec.proto
+++ b/recipes/fuchsia/spec.proto
@@ -62,6 +62,13 @@
 
     // Whether to enable submodules in jiri checkout.
     bool enable_submodules = 7;
+
+    // Whether to not rebase the patch on top of HEAD. This is useful for
+    // coverage builders which would want to collect data based on the line
+    // numbers of the uploaded patchset as is instead of having it rebased on
+    // top of HEAD which could change the line numbers and make the coverage
+    // data not align.
+    bool do_not_rebase_patch = 8;
   }
 
   // Describes how to build Fuchsia.