[depot_tools] add optional param `depth` in git checkout

It can be used to reduce the size of payloads to download in git fetch
operations.

Change-Id: I631af18bee01eaa829d3d4755a57935690a99a85
Bug: 1508826
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5091760
Commit-Queue: Scott Lee <ddoman@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
changes/60/5091760/9
Scott Lee 2 years ago committed by LUCI CQ
parent 60f6c8a860
commit a3f3465f48

@ -412,7 +412,7 @@ Returns:
Returns a git command step.
&mdash; **def [bundle\_create](/recipes/recipe_modules/git/api.py#373)(self, bundle_path, rev_list_args=None, \*\*kwargs):**
&mdash; **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.
&mdash; **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):**
&mdash; **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.
&mdash; **def [config\_get](/recipes/recipe_modules/git/api.py#342)(self, prop_name, \*\*kwargs):**
&mdash; **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.
&mdash; **def [get\_remote\_url](/recipes/recipe_modules/git/api.py#361)(self, remote_name=None, \*\*kwargs):**
&mdash; **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.
&mdash; **def [get\_timestamp](/recipes/recipe_modules/git/api.py#313)(self, commit='HEAD', test_data=None, \*\*kwargs):**
&mdash; **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.
&mdash; **def [new\_branch](/recipes/recipe_modules/git/api.py#386)(self, branch, name=None, upstream=None, upstream_current=False, \*\*kwargs):**
&mdash; **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__'.
&mdash; **def [number](/recipes/recipe_modules/git/api.py#417)(self, commitrefs=None, test_values=None):**
&mdash; **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.
&mdash; **def [rebase](/recipes/recipe_modules/git/api.py#322)(self, name_prefix, branch, dir_path, remote_name=None, \*\*kwargs):**
&mdash; **def [rebase](/recipes/recipe_modules/git/api.py#340)(self, name_prefix, branch, dir_path, remote_name=None, \*\*kwargs):**
Runs rebase HEAD onto branch

@ -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(

@ -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]:<PATH>"
},
"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@<br/>checked out 'deadbeef'<br/>@@@"
]
},
{
"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]:<PATH>"
},
"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]:<PATH>"
},
"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]:<PATH>"
},
"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"
}
]

@ -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"
]

@ -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))

Loading…
Cancel
Save