Skip to content

feat: support shallow git clones #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/taskgraph/run-task/run-task
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ def git_checkout(
commit: Optional[str],
ssh_key_file: Optional[Path],
ssh_known_hosts_file: Optional[Path],
shallow: bool = False,
):
env = {
# abort if transfer speed is lower than 1kB/s for 1 minute
Expand Down Expand Up @@ -633,12 +634,17 @@ def git_checkout(

if not os.path.exists(destination_path):
# Repository doesn't already exist, needs to be cloned
repo_url = base_repo if base_repo else head_repo
args = [
"git",
"clone",
base_repo if base_repo else head_repo,
repo_url,
destination_path,
]

# Only add --depth=1 for non-local repositories
if shallow and not os.path.exists(repo_url):
args.append("--depth=1")

retry_required_command(b"vcs", args, extra_env=env)

Expand Down Expand Up @@ -930,6 +936,7 @@ def collect_vcs_options(args, project, name):
"repo-type": repo_type,
"ssh-secret-name": private_key_secret,
"pip-requirements": pip_requirements,
"shallow": args.shallow,
}


Expand Down Expand Up @@ -978,6 +985,7 @@ def vcs_checkout_from_args(options):
revision,
ssh_key_file,
ssh_known_hosts_file,
options.get("shallow", False),
)
elif options["repo-type"] == "hg":
if not revision and not ref:
Expand Down Expand Up @@ -1061,6 +1069,9 @@ def main(args):
parser.add_argument("--user", default="worker", help="user to run as")
parser.add_argument("--group", default="worker", help="group to run as")
parser.add_argument("--task-cwd", help="directory to run the provided command in")
parser.add_argument("--shallow", action="store_true",
default=False, help="perform a shallow clone (default: false)",
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really sorry, I told you to add an argument here. But on closer look, I was wrong and instead we should add this in collect_vcs_options using an environment variable like most of the other vcs arguments. You should add something like:

shallow = os.environ.get("%s_SHALLOW_CLONE" % env_prefix)

and make sure to return it (like you already are).


repositories = os.environ.get("REPOSITORIES")
if repositories:
Expand Down
20 changes: 14 additions & 6 deletions test/test_scripts_run_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def test_collect_vcs_options(monkeypatch, run_task_mod, env, extra_expected):
args = Namespace()
setattr(args, f"{name}_checkout", checkout)
setattr(args, f"{name}_sparse_profile", False)
setattr(args, "shallow", False)

result = run_task_mod.collect_vcs_options(args, name, name)

Expand All @@ -197,6 +198,7 @@ def test_collect_vcs_options(monkeypatch, run_task_mod, env, extra_expected):
"ssh-secret-name": env.get("SSH_SECRET_NAME"),
"sparse-profile": False,
"store-path": env.get("HG_STORE_PATH"),
"shallow": False,
}
if "PIP_REQUIREMENTS" in env:
expected["pip-requirements"] = os.path.join(
Expand Down Expand Up @@ -354,13 +356,14 @@ def _commit_file(message, filename):


@pytest.mark.parametrize(
"base_ref,ref,files,hash_key",
"base_ref,ref,files,hash_key,shallow",
[
(None, None, ["mainfile"], "main"),
(None, "main", ["mainfile"], "main"),
(None, "mybranch", ["mainfile", "branchfile"], "branch"),
("main", "main", ["mainfile"], "main"),
("main", "mybranch", ["mainfile", "branchfile"], "branch"),
(None, None, ["mainfile"], "main", False),
(None, "main", ["mainfile"], "main", False),
(None, "mybranch", ["mainfile", "branchfile"], "branch", False),
("main", "main", ["mainfile"], "main", False),
("main", "mybranch", ["mainfile", "branchfile"], "branch", False),
("main", "mybranch", ["mainfile", "branchfile"], "branch", True),
],
)
def test_git_checkout(
Expand All @@ -371,6 +374,7 @@ def test_git_checkout(
ref,
files,
hash_key,
shallow,
):
with tempfile.TemporaryDirectory() as workdir:
destination = os.path.join(workdir, "destination")
Expand All @@ -384,6 +388,7 @@ def test_git_checkout(
commit=None,
ssh_key_file=None,
ssh_known_hosts_file=None,
shallow=shallow,
)

# Check desired files exist
Expand All @@ -403,10 +408,12 @@ def test_git_checkout(
assert current_rev == mock_git_repo[hash_key]


@pytest.mark.parametrize("shallow", [True, False])
def test_git_checkout_with_commit(
mock_stdin,
run_task_mod,
mock_git_repo,
shallow,
):
with tempfile.TemporaryDirectory() as workdir:
destination = os.path.join(workdir, "destination")
Expand All @@ -420,6 +427,7 @@ def test_git_checkout_with_commit(
commit=mock_git_repo["branch"],
ssh_key_file=None,
ssh_known_hosts_file=None,
shallow=shallow,
)


Expand Down
Loading