Skip to content

Commit 336bfaa

Browse files
committed
refactor(run-task): rename ref -> head_ref and {commit, revision} -> head_rev
This makes the naming consistent with what we use in .taskcluster.yml and the rest of Taskgraph. Previously, I always had to look up where "ref" and "commit" / "revision" were coming from to double check they were the values I was expecting. This rename makes that much more obvious.
1 parent aa64552 commit 336bfaa

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

src/taskgraph/run-task/run-task

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,8 @@ def git_checkout(
611611
base_repo: Optional[str],
612612
base_ref: Optional[str],
613613
base_rev: Optional[str],
614-
ref: Optional[str],
615-
commit: Optional[str],
614+
head_ref: Optional[str],
615+
head_rev: Optional[str],
616616
ssh_key_file: Optional[Path],
617617
ssh_known_hosts_file: Optional[Path],
618618
):
@@ -686,31 +686,31 @@ def git_checkout(
686686

687687
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
688688

689-
# If a ref was provided, it might be tag, so we need to make sure we fetch
689+
# If a head_ref was provided, it might be tag, so we need to make sure we fetch
690690
# those. This is explicitly only done when base and head repo match,
691691
# because it is the only scenario where tags could be present. (PRs, for
692692
# example, always include an explicit rev.) Failure to do this could result
693693
# in not having a tag, or worse: having an outdated version of one.
694694
# `--force` is needed to be able to update an existing tag.
695-
if ref and base_repo == head_repo:
695+
if head_ref and base_repo == head_repo:
696696
args = [
697697
"git",
698698
"fetch",
699699
"--tags",
700700
"--force",
701701
base_repo,
702-
ref,
702+
head_ref,
703703
]
704704

705705
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
706706

707-
# If a ref isn't provided, we fetch all refs from head_repo, which may be slow
707+
# If a head_ref isn't provided, we fetch all refs from head_repo, which may be slow
708708
args = [
709709
"git",
710710
"fetch",
711711
"--no-tags",
712712
head_repo,
713-
ref if ref else "+refs/heads/*:refs/remotes/work/*",
713+
head_ref if head_ref else "+refs/heads/*:refs/remotes/work/*",
714714
]
715715

716716
retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env)
@@ -721,11 +721,11 @@ def git_checkout(
721721
"-f",
722722
]
723723

724-
if ref:
725-
args.extend(["-B", ref])
724+
if head_ref:
725+
args.extend(["-B", head_ref])
726726

727727
# `git fetch` set `FETCH_HEAD` reference to the last commit of the desired branch
728-
args.append(commit if commit else "FETCH_HEAD")
728+
args.append(head_rev if head_rev else "FETCH_HEAD")
729729

730730
run_required_command(b"vcs", args, cwd=destination_path)
731731

@@ -912,8 +912,8 @@ def collect_vcs_options(args, project, name):
912912
base_ref = os.environ.get(f"{env_prefix}_BASE_REF")
913913
base_rev = os.environ.get(f"{env_prefix}_BASE_REV")
914914
head_repo = os.environ.get(f"{env_prefix}_HEAD_REPOSITORY")
915-
revision = os.environ.get(f"{env_prefix}_HEAD_REV")
916-
ref = os.environ.get(f"{env_prefix}_HEAD_REF")
915+
head_ref = os.environ.get(f"{env_prefix}_HEAD_REF")
916+
head_rev = os.environ.get(f"{env_prefix}_HEAD_REV")
917917
pip_requirements = os.environ.get(f"{env_prefix}_PIP_REQUIREMENTS")
918918
private_key_secret = os.environ.get(f"{env_prefix}_SSH_SECRET_NAME")
919919

@@ -945,8 +945,8 @@ def collect_vcs_options(args, project, name):
945945
"base-ref": base_ref,
946946
"base-rev": base_rev,
947947
"head-repo": head_repo,
948-
"revision": revision,
949-
"ref": ref,
948+
"head-ref": head_ref,
949+
"head-rev": head_rev,
950950
"repo-type": repo_type,
951951
"ssh-secret-name": private_key_secret,
952952
"pip-requirements": pip_requirements,
@@ -955,13 +955,13 @@ def collect_vcs_options(args, project, name):
955955

956956
def vcs_checkout_from_args(options):
957957
if not options["checkout"]:
958-
if options["ref"] and not options["revision"]:
958+
if options["head-ref"] and not options["head-rev"]:
959959
print("task should be defined in terms of non-symbolic revision")
960960
sys.exit(1)
961961
return
962962

963-
revision = options["revision"]
964-
ref = options["ref"]
963+
head_ref = options["head-ref"]
964+
head_rev = options["head-rev"]
965965
ssh_key_file = None
966966
ssh_known_hosts_file = None
967967
ssh_dir = None
@@ -979,13 +979,14 @@ def vcs_checkout_from_args(options):
979979
ssh_known_hosts_file = ssh_dir.joinpath("known_hosts")
980980
ssh_known_hosts_file.write_bytes(GITHUB_SSH_FINGERPRINT)
981981

982-
if options["repo-type"] == "git":
983-
if not revision and not ref:
984-
raise RuntimeError(
985-
"Git requires that either a ref, a revision, or both are provided"
986-
)
982+
if not head_rev and not head_ref:
983+
raise RuntimeError(
984+
f"{options['repo-type'].capitalize()} requires that either a "
985+
"ref, a revision, or both are provided"
986+
)
987987

988-
if not ref:
988+
if options["repo-type"] == "git":
989+
if not head_ref:
989990
print("Providing a ref will improve the performance of this checkout")
990991

991992
revision = git_checkout(
@@ -994,25 +995,20 @@ def vcs_checkout_from_args(options):
994995
options["base-repo"],
995996
options["base-ref"],
996997
options["base-rev"],
997-
ref,
998-
revision,
998+
head_ref,
999+
head_rev,
9991000
ssh_key_file,
10001001
ssh_known_hosts_file,
10011002
)
10021003
elif options["repo-type"] == "hg":
1003-
if not revision and not ref:
1004-
raise RuntimeError(
1005-
"Hg requires that at least one of a ref or revision is provided"
1006-
)
1007-
10081004
revision = hg_checkout(
10091005
options["checkout"],
10101006
options["head-repo"],
10111007
options["base-repo"],
10121008
options["store-path"],
10131009
options["sparse-profile"],
1014-
ref,
1015-
revision,
1010+
head_ref,
1011+
head_rev,
10161012
)
10171013
else:
10181014
raise RuntimeError('Type of VCS must be either "git" or "hg"')

test/test_scripts_run_task.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ def test_collect_vcs_options(monkeypatch, run_task_mod, env, extra_expected):
191191
"name": name,
192192
"pip-requirements": None,
193193
"project": name,
194-
"ref": env.get("HEAD_REF"),
194+
"head-ref": env.get("HEAD_REF"),
195+
"head-rev": env.get("HEAD_REV"),
195196
"repo-type": env.get("REPOSITORY_TYPE"),
196-
"revision": env.get("HEAD_REV"),
197197
"ssh-secret-name": env.get("SSH_SECRET_NAME"),
198198
"sparse-profile": False,
199199
"store-path": env.get("HG_STORE_PATH"),
@@ -354,7 +354,7 @@ def _commit_file(message, filename):
354354

355355

356356
@pytest.mark.parametrize(
357-
"base_ref,ref,files,hash_key",
357+
"base_ref,head_ref,files,hash_key",
358358
[
359359
(None, None, ["mainfile"], "main"),
360360
(None, "main", ["mainfile"], "main"),
@@ -368,7 +368,7 @@ def test_git_checkout(
368368
run_task_mod,
369369
mock_git_repo,
370370
base_ref,
371-
ref,
371+
head_ref,
372372
files,
373373
hash_key,
374374
):
@@ -380,8 +380,8 @@ def test_git_checkout(
380380
base_repo=mock_git_repo["path"],
381381
base_ref=base_ref,
382382
base_rev=None,
383-
ref=ref,
384-
commit=None,
383+
head_ref=head_ref,
384+
head_rev=None,
385385
ssh_key_file=None,
386386
ssh_known_hosts_file=None,
387387
)
@@ -391,13 +391,13 @@ def test_git_checkout(
391391
assert os.path.exists(os.path.join(destination, filename))
392392

393393
# Check repo is on the right branch
394-
if ref:
394+
if head_ref:
395395
current_branch = subprocess.check_output(
396396
args=["git", "rev-parse", "--abbrev-ref", "HEAD"],
397397
cwd=destination,
398398
universal_newlines=True,
399399
).strip()
400-
assert current_branch == ref
400+
assert current_branch == head_ref
401401

402402
current_rev = git_current_rev(destination)
403403
assert current_rev == mock_git_repo[hash_key]
@@ -416,8 +416,8 @@ def test_git_checkout_with_commit(
416416
base_repo=mock_git_repo["path"],
417417
base_ref="mybranch",
418418
base_rev=mock_git_repo["main"],
419-
ref=mock_git_repo["branch"],
420-
commit=mock_git_repo["branch"],
419+
head_ref=mock_git_repo["branch"],
420+
head_rev=mock_git_repo["branch"],
421421
ssh_key_file=None,
422422
ssh_known_hosts_file=None,
423423
)

0 commit comments

Comments
 (0)