Skip to content

Commit e2650b4

Browse files
committed
refactor(run-task): move git fetch logic into helper function
1 parent 19b6817 commit e2650b4

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/taskgraph/run-task/run-task

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import urllib.error
3737
import urllib.request
3838
from pathlib import Path
3939
from threading import Thread
40-
from typing import Optional
40+
from typing import Dict, Optional
4141

4242
SECRET_BASEURL_TPL = "{}/secrets/v1/secret/{{}}".format(os.environ.get("TASKCLUSTER_PROXY_URL", "http://taskcluster").rstrip('/'))
4343

@@ -551,6 +551,22 @@ def configure_volume_posix(volume, user, group, running_as_root):
551551
set_dir_permissions(volume, user.pw_uid, group.gr_gid)
552552

553553

554+
def git_fetch(
555+
destination_path: str,
556+
ref: str,
557+
remote: str = "origin",
558+
tags: bool = False,
559+
env: Optional[Dict[str, str]] = None,
560+
):
561+
args = ["git", "fetch"]
562+
if tags:
563+
# `--force` is needed to be able to update an existing outdated tag.
564+
args.extend(["--tags", "--force"])
565+
566+
args.extend([remote, ref])
567+
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
568+
569+
554570
def _clean_git_checkout(destination_path):
555571
# Delete untracked files (i.e. build products)
556572
print_line(b"vcs", b"cleaning git checkout...\n")
@@ -641,30 +657,23 @@ def git_checkout(
641657

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

644-
# Fetching base_rev allows Taskgraph to compute the files changed by the
645-
# push.
660+
# First fetch the base_rev. This allows Taskgraph to compute the files
661+
# changed by the push.
646662
if base_rev and base_rev != NULL_REVISION:
647-
args = ["git", "fetch", "origin", base_rev]
648-
649-
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
663+
git_fetch(destination_path, base_rev, env=env)
650664

651-
# Fetch head ref
652-
args = ["git", "fetch"]
653-
if ref and base_repo == head_repo:
654-
# If a ref was provided, it might be tag, so we need to make sure we fetch
655-
# those. This is explicitly only done when base and head repo match,
656-
# because it is the only scenario where tags could be present. (PRs, for
657-
# example, always include an explicit rev.) Failure to do this could result
658-
# in not having a tag, or worse: having an outdated version of one.
659-
# `--force` is needed to be able to update an existing tag.
660-
args.extend(["--tags", "--force"])
665+
# Next fetch the head ref.
661666

662-
else:
663-
args.append("--no-tags")
667+
# If a ref was provided, it might be tag, so we need to make sure we fetch
668+
# those. This is explicitly only done when base and head repo match,
669+
# because it is the only scenario where tags could be present. (PRs, for
670+
# example, always include an explicit rev.) Failure to do this could result
671+
# in not having a tag, or worse: having an outdated version of one.
672+
tags = True if ref and base_repo == head_repo else False
664673

665674
# If a ref isn't provided, we fetch all refs from head_repo, which may be slow.
666-
args.extend([head_repo, ref if ref else "+refs/heads/*:refs/remotes/work/*"])
667-
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
675+
target = ref if ref else "+refs/heads/*:refs/remotes/work/*"
676+
git_fetch(destination_path, target, remote=head_repo, tags=tags, env=env)
668677

669678
args = [
670679
"git",

0 commit comments

Comments
 (0)