checkout: Add repo checkout depth property

Bug: b/457783233
Change-Id: I14be005e6a67dc67a1bd147bd8174d8327439950
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/421552
diff --git a/recipe_modules/checkout/api.py b/recipe_modules/checkout/api.py
index 1be4550..9cef7a3 100644
--- a/recipe_modules/checkout/api.py
+++ b/recipe_modules/checkout/api.py
@@ -1152,6 +1152,8 @@
                     kwargs['attempts'] = ctx.options.number_of_attempts
                 if ctx.options.manifest_groups:
                     kwargs['groups'] = ctx.options.manifest_groups
+                if ctx.options.repo_depth:
+                    kwargs['depth'] = ctx.options.repo_depth
 
                 self.m.repo.init(
                     manifest_url=ctx.options.remote,
diff --git a/recipe_modules/checkout/options.proto b/recipe_modules/checkout/options.proto
index 2019b2c..dd09d25 100644
--- a/recipe_modules/checkout/options.proto
+++ b/recipe_modules/checkout/options.proto
@@ -51,21 +51,24 @@
   int32 repo_sync_timeout_sec = 7;
   int32 number_of_attempts = 8;
 
+  // Do shallow checkouts of Android Repo Tool projects.
+  int32 repo_depth = 9;
+
   // In try builds CLs are rebased by default. In ci builds they are not. This
   // option prevents rebasing on try builds.
-  bool force_no_rebase = 9;
+  bool force_no_rebase = 10;
 
   // If a tryjob is triggered by a CL on branch 'foo', try to use branch 'foo'
   // of any superprojects or manifests containing the CL's repo as a submodule
   // or project. (Note: branch matching is not yet implemented for submodules.)
-  bool match_branch = 10;
+  bool match_branch = 11;
 
   // Timeout for 'git submodule update --init --recursive'. Default is
   // 10 minutes.
-  int32 submodule_timeout_sec = 11;
+  int32 submodule_timeout_sec = 12;
 
   // Use buildbucket trigger data.
-  bool use_trigger = 12;
+  bool use_trigger = 13;
 
   // Lists of remotes that should be considered equivalent to each other.
   // This is intended to be used with branches that are automatically kept in
@@ -89,38 +92,38 @@
   // well as d/bar and e/bar, but a/foo and d/bar are unrelated.
   //
   // See also http://go/gob-ctl#copying-git-repository.
-  repeated EquivalentRemotes equivalent_remotes = 13;
+  repeated EquivalentRemotes equivalent_remotes = 14;
 
   // Subdirectory of checkout in which commands should be executed. Uncommon.
-  string root_subdirectory = 14;
+  string root_subdirectory = 15;
 
   // Do not use a cache of the checkout.
-  bool do_not_cache = 15;
+  bool do_not_cache = 16;
 
   // Instead of including all submodules (initialize_submodules == false) or
   // including no submodules (initialize_submodules == true), initialize a
   // subset of the submodules present in the source tree. Setting both of these
   // is not permitted.
-  repeated string included_submodules = 16;
-  repeated string excluded_submodules = 17;
+  repeated string included_submodules = 17;
+  repeated string excluded_submodules = 18;
 
   // Disable downloading precomputed packfiles from a CDN.
-  bool do_not_use_packfiles = 18;
+  bool do_not_use_packfiles = 19;
 
   // Limit the Android Repo Tool checkout to these groups from the manifest
   // file.
-  repeated string manifest_groups = 19;
+  repeated string manifest_groups = 20;
 
   // Rewrites of Git remotes.
-  repeated Rewrite rewrites = 20;
+  repeated Rewrite rewrites = 21;
 
   // Attempt to patch Bazel projects in the given files.
-  repeated string eligible_workspace_paths = 21;
+  repeated string eligible_workspace_paths = 22;
 
   // Whether to use the '--no-clone-bundle' option to 'repo init',
-  bool repo_no_clone_bundle = 22;
+  bool repo_no_clone_bundle = 23;
 
   // Maximum number of attempts for 'git fetch' and 'git submodule update'
   // calls. Defaults to 2.
-  int32 max_attempts = 23;
+  int32 max_attempts = 24;
 }
diff --git a/recipe_modules/checkout/test_api.py b/recipe_modules/checkout/test_api.py
index d218499..188cbca 100644
--- a/recipe_modules/checkout/test_api.py
+++ b/recipe_modules/checkout/test_api.py
@@ -159,6 +159,7 @@
         rewrites: Sequence[tuple[str, str]] = (),
         eligible_workspace_paths: Sequence[str] = (),
         use_packfiles: bool = True,
+        repo_depth: int | None = None,
     ) -> checkout_options.Options:
         props = checkout_options.Options()
         props.remote = remote
@@ -182,6 +183,8 @@
         )
         props.eligible_workspace_paths.extend(eligible_workspace_paths)
         props.do_not_use_packfiles = not use_packfiles
+        if repo_depth:
+            props.repo_depth = repo_depth
         return props
 
     def repo_options(
diff --git a/recipe_modules/checkout/tests/repo.expected/try-multiple.json b/recipe_modules/checkout/tests/repo.expected/try-multiple.json
index 1ab53c8..ffbf57d 100644
--- a/recipe_modules/checkout/tests/repo.expected/try-multiple.json
+++ b/recipe_modules/checkout/tests/repo.expected/try-multiple.json
@@ -12,7 +12,7 @@
     "name": "checkout pigweed.options",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@1@@@",
-      "@@@STEP_SUMMARY_TEXT@remote: \"https://pigweed.googlesource.com/pigweed/manifest\"\nbranch: \"main\"\nuse_repo: true\nmanifest_file: \"default.xml\"\ninitialize_submodules: true\nmatch_branch: true\nuse_trigger: true\n@@@"
+      "@@@STEP_SUMMARY_TEXT@remote: \"https://pigweed.googlesource.com/pigweed/manifest\"\nbranch: \"main\"\nuse_repo: true\nmanifest_file: \"default.xml\"\ninitialize_submodules: true\nrepo_depth: 20\nmatch_branch: true\nuse_trigger: true\n@@@"
     ]
   },
   {
@@ -20,7 +20,7 @@
     "name": "checkout pigweed.options with defaults",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@1@@@",
-      "@@@STEP_SUMMARY_TEXT@remote: \"https://pigweed.googlesource.com/pigweed/manifest\"\nbranch: \"main\"\nuse_repo: true\nmanifest_file: \"default.xml\"\ninitialize_submodules: true\nrepo_init_timeout_sec: 120\nrepo_sync_timeout_sec: 900\nnumber_of_attempts: 3\nmatch_branch: true\nsubmodule_timeout_sec: 600\nuse_trigger: true\nmax_attempts: 2\n@@@"
+      "@@@STEP_SUMMARY_TEXT@remote: \"https://pigweed.googlesource.com/pigweed/manifest\"\nbranch: \"main\"\nuse_repo: true\nmanifest_file: \"default.xml\"\ninitialize_submodules: true\nrepo_init_timeout_sec: 120\nrepo_sync_timeout_sec: 900\nnumber_of_attempts: 3\nrepo_depth: 20\nmatch_branch: true\nsubmodule_timeout_sec: 600\nuse_trigger: true\nmax_attempts: 2\n@@@"
     ]
   },
   {
@@ -470,6 +470,8 @@
       "all",
       "--manifest-branch",
       "main",
+      "--depth",
+      "20",
       "--repo-url",
       "https://pigweed.googlesource.com/third_party/gerrit/git-repo",
       "--repo-rev",
diff --git a/recipe_modules/checkout/tests/repo.py b/recipe_modules/checkout/tests/repo.py
index b03fd71..821ca1a 100644
--- a/recipe_modules/checkout/tests/repo.py
+++ b/recipe_modules/checkout/tests/repo.py
@@ -93,7 +93,7 @@
 
     yield api.test(
         'try-multiple',
-        properties(),
+        properties(repo_depth=20),
         api.checkout.try_test_data(
             gerrit_changes=[
                 cl('pigweed_name', 1234),