Skip to content

Commit 9571791

Browse files
committed
refactor(run-task): move git fetch logic into helper function
1 parent 418fe38 commit 9571791

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
@@ -32,7 +32,7 @@ import time
3232
import urllib.error
3333
import urllib.request
3434
from pathlib import Path
35-
from typing import Optional
35+
from typing import Dict, Optional
3636

3737
SECRET_BASEURL_TPL = "{}/secrets/v1/secret/{{}}".format(
3838
os.environ.get("TASKCLUSTER_PROXY_URL", "http://taskcluster").rstrip("/")
@@ -545,6 +545,22 @@ def configure_volume_posix(volume, user, group, running_as_root):
545545
set_dir_permissions(volume, user.pw_uid, group.gr_gid)
546546

547547

548+
def git_fetch(
549+
destination_path: str,
550+
ref: str,
551+
remote: str = "origin",
552+
tags: bool = False,
553+
env: Optional[Dict[str, str]] = None,
554+
):
555+
args = ["git", "fetch"]
556+
if tags:
557+
# `--force` is needed to be able to update an existing outdated tag.
558+
args.extend(["--tags", "--force"])
559+
560+
args.extend([remote, ref])
561+
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
562+
563+
548564
def _clean_git_checkout(destination_path):
549565
# Delete untracked files (i.e. build products)
550566
print_line(b"vcs", b"cleaning git checkout...\n")
@@ -642,30 +658,23 @@ def git_checkout(
642658

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

645-
# Fetching base_rev allows Taskgraph to compute the files changed by the
646-
# push.
661+
# First fetch the base_rev. This allows Taskgraph to compute the files
662+
# changed by the push.
647663
if base_rev and base_rev != NULL_REVISION:
648-
args = ["git", "fetch", "origin", base_rev]
649-
650-
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
664+
git_fetch(destination_path, base_rev, env=env)
651665

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

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

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

670679
args = [
671680
"git",

0 commit comments

Comments
 (0)