diff --git a/recipes/README.recipes.md b/recipes/README.recipes.md index c76178105..6a54917cd 100644 --- a/recipes/README.recipes.md +++ b/recipes/README.recipes.md @@ -412,7 +412,7 @@ Returns: Returns a git command step. -— **def [bundle\_create](/recipes/recipe_modules/git/api.py#373)(self, bundle_path, rev_list_args=None, \*\*kwargs):** +— **def [bundle\_create](/recipes/recipe_modules/git/api.py#391)(self, bundle_path, rev_list_args=None, \*\*kwargs):** Runs 'git bundle create' on a Git repository. @@ -426,7 +426,7 @@ Args: Outputs the contents of a file at a given revision. -— **def [checkout](/recipes/recipe_modules/git/api.py#107)(self, url, ref=None, dir_path=None, recursive=False, submodules=True, submodule_update_force=False, keep_paths=None, step_suffix=None, curl_trace_file=None, raise_on_failure=True, set_got_revision=False, remote_name=None, display_fetch_size=None, file_name=None, submodule_update_recursive=True, use_git_cache=False, progress=True, tags=False):** +— **def [checkout](/recipes/recipe_modules/git/api.py#107)(self, url, ref=None, dir_path=None, recursive=False, submodules=True, submodule_update_force=False, keep_paths=None, step_suffix=None, curl_trace_file=None, raise_on_failure=True, set_got_revision=False, remote_name=None, display_fetch_size=None, file_name=None, submodule_update_recursive=True, use_git_cache=False, progress=True, tags=False, depth=None):** Performs a full git checkout and returns sha1 of checked out revision. @@ -461,11 +461,13 @@ Args: * arbitrary refs such refs/whatever/not-fetched-by-default-to-cache progress (bool): whether to show progress for fetch or not * tags (bool): Also fetch tags. + * depth (int): if > 0, limit fetching to the given number of commits from + the tips of the remote tree. Returns: If the checkout was successful, this returns the commit hash of the checked-out-repo. Otherwise this returns None. -— **def [config\_get](/recipes/recipe_modules/git/api.py#342)(self, prop_name, \*\*kwargs):** +— **def [config\_get](/recipes/recipe_modules/git/api.py#360)(self, prop_name, \*\*kwargs):** Returns git config output. @@ -492,7 +494,7 @@ Returns: Fetches all tags from the remote. -— **def [get\_remote\_url](/recipes/recipe_modules/git/api.py#361)(self, remote_name=None, \*\*kwargs):** +— **def [get\_remote\_url](/recipes/recipe_modules/git/api.py#379)(self, remote_name=None, \*\*kwargs):** Returns the remote Git repository URL, or None. @@ -502,11 +504,11 @@ Args: Returns: (str) The URL of the remote Git repository, or None. -— **def [get\_timestamp](/recipes/recipe_modules/git/api.py#313)(self, commit='HEAD', test_data=None, \*\*kwargs):** +— **def [get\_timestamp](/recipes/recipe_modules/git/api.py#331)(self, commit='HEAD', test_data=None, \*\*kwargs):** Find and return the timestamp of the given commit. -— **def [new\_branch](/recipes/recipe_modules/git/api.py#386)(self, branch, name=None, upstream=None, upstream_current=False, \*\*kwargs):** +— **def [new\_branch](/recipes/recipe_modules/git/api.py#404)(self, branch, name=None, upstream=None, upstream_current=False, \*\*kwargs):** Runs git new-branch on a Git repository, to be used before git cl upload. @@ -518,7 +520,7 @@ Args: * upstream_current (bool): whether to use '--upstream_current'. * kwargs: Forwarded to '__call__'. -— **def [number](/recipes/recipe_modules/git/api.py#417)(self, commitrefs=None, test_values=None):** +— **def [number](/recipes/recipe_modules/git/api.py#435)(self, commitrefs=None, test_values=None):** Computes the generation number of some commits. @@ -535,7 +537,7 @@ A list of strings containing the generation numbers of the commits. If non-empty commitrefs was provided, the order of the returned numbers will correspond to the order of the provided commitrefs. -— **def [rebase](/recipes/recipe_modules/git/api.py#322)(self, name_prefix, branch, dir_path, remote_name=None, \*\*kwargs):** +— **def [rebase](/recipes/recipe_modules/git/api.py#340)(self, name_prefix, branch, dir_path, remote_name=None, \*\*kwargs):** Runs rebase HEAD onto branch diff --git a/recipes/recipe_modules/git/api.py b/recipes/recipe_modules/git/api.py index fc3bf92d8..a4c04416f 100644 --- a/recipes/recipe_modules/git/api.py +++ b/recipes/recipe_modules/git/api.py @@ -104,14 +104,26 @@ class GitApi(recipe_api.RecipeApi): raise recipe_api.InfraFailure('count-objects failed: %s' % ex) return None - def checkout(self, url, ref=None, dir_path=None, recursive=False, - submodules=True, submodule_update_force=False, - keep_paths=None, step_suffix=None, - curl_trace_file=None, raise_on_failure=True, - set_got_revision=False, remote_name=None, - display_fetch_size=None, file_name=None, + def checkout(self, + url, + ref=None, + dir_path=None, + recursive=False, + submodules=True, + submodule_update_force=False, + keep_paths=None, + step_suffix=None, + curl_trace_file=None, + raise_on_failure=True, + set_got_revision=False, + remote_name=None, + display_fetch_size=None, + file_name=None, submodule_update_recursive=True, - use_git_cache=False, progress=True, tags=False): + use_git_cache=False, + progress=True, + tags=False, + depth=None): """Performs a full git checkout and returns sha1 of checked out revision. Args: @@ -145,6 +157,8 @@ class GitApi(recipe_api.RecipeApi): * arbitrary refs such refs/whatever/not-fetched-by-default-to-cache progress (bool): whether to show progress for fetch or not * tags (bool): Also fetch tags. + * depth (int): if > 0, limit fetching to the given number of commits from + the tips of the remote tree. Returns: If the checkout was successful, this returns the commit hash of the checked-out-repo. Otherwise this returns None. @@ -246,6 +260,10 @@ class GitApi(recipe_api.RecipeApi): if tags: fetch_args.append('--tags') + if depth: + assert isinstance(depth, int) + fetch_args.append('--depth %d' % depth) + fetch_step_name = 'git fetch%s' % step_suffix if display_fetch_size: count_objects_before_fetch = self.count_objects( diff --git a/recipes/recipe_modules/git/examples/full.expected/git-checkout-with-depth.json b/recipes/recipe_modules/git/examples/full.expected/git-checkout-with-depth.json new file mode 100644 index 000000000..425a23c43 --- /dev/null +++ b/recipes/recipe_modules/git/examples/full.expected/git-checkout-with-depth.json @@ -0,0 +1,232 @@ +[ + { + "cmd": [ + "python3", + "-u", + "RECIPE_MODULE[depot_tools::git]/resources/git_setup.py", + "--path", + "[START_DIR]/src", + "--url", + "https://chromium.googlesource.com/chromium/src.git" + ], + "name": "git setup" + }, + { + "cmd": [ + "git", + "fetch", + "origin", + "main", + "--recurse-submodules", + "--progress", + "--depth 1" + ], + "cwd": "[START_DIR]/src", + "env": { + "PATH": "RECIPE_REPO[depot_tools]:" + }, + "infra_step": true, + "name": "git fetch" + }, + { + "cmd": [ + "git", + "checkout", + "-f", + "FETCH_HEAD" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "git checkout" + }, + { + "cmd": [ + "git", + "rev-parse", + "HEAD" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "read revision", + "~followup_annotations": [ + "@@@STEP_TEXT@
checked out 'deadbeef'
@@@" + ] + }, + { + "cmd": [ + "git", + "clean", + "-f", + "-d", + "-x" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "git clean" + }, + { + "cmd": [ + "git", + "submodule", + "sync" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "submodule sync" + }, + { + "cmd": [ + "git", + "submodule", + "update", + "--init", + "--recursive" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "submodule update" + }, + { + "cmd": [ + "git", + "-c", + "foo=bar", + "count-objects", + "-v" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "count-objects" + }, + { + "cmd": [ + "git", + "config", + "--get", + "remote.origin.url" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "git config remote.origin.url", + "~followup_annotations": [ + "@@@STEP_TEXT@foo@@@" + ] + }, + { + "cmd": [ + "git", + "show", + "HEAD", + "--format=%at", + "-s" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "git show" + }, + { + "cmd": [ + "git", + "fetch", + "origin", + "--tags" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "git fetch tags" + }, + { + "cmd": [ + "git", + "status" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "git status" + }, + { + "cmd": [ + "git", + "status" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "git status can_fail_build" + }, + { + "cmd": [ + "git", + "status" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "git status cannot_fail_build" + }, + { + "cmd": [ + "git", + "new-branch", + "refactor" + ], + "cwd": "[START_DIR]/src", + "env": { + "PATH": "RECIPE_REPO[depot_tools]:" + }, + "infra_step": true, + "name": "git new-branch refactor" + }, + { + "cmd": [ + "git", + "new-branch", + "feature", + "--upstream", + "refactor" + ], + "cwd": "[START_DIR]/src", + "env": { + "PATH": "RECIPE_REPO[depot_tools]:" + }, + "infra_step": true, + "name": "git new-branch feature" + }, + { + "cmd": [ + "git", + "new-branch", + "track_current", + "--upstream_current" + ], + "cwd": "[START_DIR]/src", + "env": { + "PATH": "RECIPE_REPO[depot_tools]:" + }, + "infra_step": true, + "name": "git new-branch track_current" + }, + { + "cmd": [ + "git", + "rebase", + "origin/main" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "my repo rebase" + }, + { + "cmd": [ + "git", + "bundle", + "create", + "[START_DIR]/all.bundle", + "--all" + ], + "cwd": "[START_DIR]/src", + "infra_step": true, + "name": "git bundle" + }, + { + "name": "$result" + } +] \ No newline at end of file diff --git a/recipes/recipe_modules/git/examples/full.expected/new_branch_failed.json b/recipes/recipe_modules/git/examples/full.expected/new_branch_failed.json index 5084d693b..a0e6363f0 100644 --- a/recipes/recipe_modules/git/examples/full.expected/new_branch_failed.json +++ b/recipes/recipe_modules/git/examples/full.expected/new_branch_failed.json @@ -194,10 +194,10 @@ " File \"RECIPE_REPO[recipe_engine]/recipe_engine/internal/property_invoker.py\", in _invoke_with_properties", " return callable_obj(*props, **additional_args)", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", - " File \"RECIPE_REPO[depot_tools]/recipes/recipe_modules/git/examples/full.py\", line 81, in RunSteps", + " File \"RECIPE_REPO[depot_tools]/recipes/recipe_modules/git/examples/full.py\", line 82, in RunSteps", " api.git.new_branch('failed_new_branch', upstream='will_fail', upstream_current=True) #pylint: disable = line-too-long", " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^", - " File \"RECIPE_REPO[depot_tools]/recipes/recipe_modules/git/api.py\", line 403, in new_branch", + " File \"RECIPE_REPO[depot_tools]/recipes/recipe_modules/git/api.py\", line 421, in new_branch", " raise ValueError('Can not define both upstream and upstream_current')", "ValueError: Can not define both upstream and upstream_current" ] diff --git a/recipes/recipe_modules/git/examples/full.py b/recipes/recipe_modules/git/examples/full.py index ca2618a8a..b772e7647 100644 --- a/recipes/recipe_modules/git/examples/full.py +++ b/recipes/recipe_modules/git/examples/full.py @@ -45,7 +45,8 @@ def RunSteps(api): file_name=api.properties.get('checkout_file_name'), submodule_update_recursive=submodule_update_recursive, use_git_cache=api.properties.get('use_git_cache'), - tags=api.properties.get('tags')) + tags=api.properties.get('tags'), + depth=api.properties.get('depth')) assert retVal == "deadbeef", ( "expected retVal to be %r but was %r" % ("deadbeef", retVal)) @@ -172,3 +173,5 @@ def GenTests(api): yield (api.test('new_branch_failed', status="INFRA_FAILURE") + api.properties(set_both_upstream_and_upstream_current=True) + api.expect_exception('ValueError')) + + yield (api.test('git-checkout-with-depth') + api.properties(depth=1))