pw_presubmit: Use triggering change branch as ref

When uploading to CIPD, use the triggering change's branch as the ref to
update when uploading the packag.

Bug: b/514654165
Change-Id: Iaceff2ab35e522bbb8b3181c1e91df08293097ee
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/411872
diff --git a/recipe_modules/cipd_upload/api.py b/recipe_modules/cipd_upload/api.py
index 9d43080..9493eb3 100644
--- a/recipe_modules/cipd_upload/api.py
+++ b/recipe_modules/cipd_upload/api.py
@@ -89,6 +89,7 @@
         checkout: checkout_api.CheckoutContext,
         cas_digests: dict[str, str],
         upload_to_cipd: bool,
+        **kwargs,
     ) -> None:
         manifest = self.m.file.read_proto(
             f'read cipd manifest {manifest_path}',
@@ -105,4 +106,5 @@
             git_revision=checkout.revision(),
             upload_to_cipd=upload_to_cipd,
             cas_digests=cas_digests,
+            **kwargs,
         )
diff --git a/recipe_modules/pw_presubmit/api.py b/recipe_modules/pw_presubmit/api.py
index a6fe61e..ba5b942 100644
--- a/recipe_modules/pw_presubmit/api.py
+++ b/recipe_modules/pw_presubmit/api.py
@@ -387,6 +387,12 @@
                             self.m.default_timeout(),
                             self.m.step.nest(cipd_manifest_path.stem),
                         ):
+                            kwargs: dict[str, Any] = {}
+                            if len(ctx.checkout.changes) == 1:
+                                change = ctx.checkout.changes[0]
+                                if change.branch not in ('main', 'master'):
+                                    kwargs['refs'] = (change.branch,)
+
                             defer(
                                 self.m.cipd_upload.manifest,
                                 manifest_path=cipd_manifest_path,
@@ -397,6 +403,7 @@
                                     and not ctx.options.dry_run
                                 ),
                                 cas_digests=ctx.cas_digests,
+                                **kwargs,
                             )
 
                 defer(
diff --git a/recipe_modules/pw_presubmit/tests/full.py b/recipe_modules/pw_presubmit/tests/full.py
index 8379e71..6157405 100644
--- a/recipe_modules/pw_presubmit/tests/full.py
+++ b/recipe_modules/pw_presubmit/tests/full.py
@@ -52,6 +52,8 @@
 
 def RunSteps(api: recipe_api.RecipeScriptApi, props: InputProperties):
     checkout = api.checkout.fake_context()
+    if props.checkout_options.branch:
+        checkout.changes[0].branch = props.checkout_options.branch
 
     with api.pw_presubmit(checkout, props.pw_presubmit_options) as presubmit:
         log_dir = api.path.start_dir / 'logs'
@@ -239,7 +241,10 @@
 
     yield api.test(
         'cipd',
-        properties(step=['step1']),
+        properties(
+            step=['step1'],
+            checkout_options=api.checkout.git_options(branch='abc'),
+        ),
         api.checkout.ci_test_data(),
         api.path.files_exist(builder_manifest),
     )