From 7031852bb70c7f796979d97266632b6efae0ab5e Mon Sep 17 00:00:00 2001 From: Bastien Orivel Date: Wed, 15 Apr 2026 15:12:54 +0200 Subject: [PATCH] Allow extra refs to be marked as optional in run-task This prevents failures if a fork doesn't have any notes at `refs/notes/decision-parameters`. I chose to pay for the `ls-remote` time if an optional ref failed to fetch to avoid having to parse the stderr from git fetch which felt very fragile. It adds a potential ~1.5s to fetches per optional remote that is missing which isn't ideal, but I don't have anything better at this point in time. The new log message is slightly misleading since technically ls-remote might find one ref but not another and it'd be reported that both are missing but I don't want to have to ls-remote for each ref, adding 1.5s each time, to fix this. --- src/taskgraph/run-task/run-task | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/taskgraph/run-task/run-task b/src/taskgraph/run-task/run-task index 85aa29815..ea9562af3 100755 --- a/src/taskgraph/run-task/run-task +++ b/src/taskgraph/run-task/run-task @@ -574,6 +574,7 @@ def git_fetch( tags: bool = False, shallow: bool = False, env: Optional[Dict[str, str]] = None, + optional: bool = False, ): args = ["git", "fetch"] if tags: @@ -584,6 +585,30 @@ def git_fetch( args.append("--depth=1") args.extend([remote, *set(targets)]) + if not optional: + retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env) + return + + try: + retry_required_command( + b"vcs", args, cwd=destination_path, extra_env=env, retries=0 + ) + return + except SystemExit: + # Ignore the first failure to check if the ref even exists + pass + + ref_names = [t.split(":", 1)[0] for t in targets] + ls = subprocess.run( + ["git", "ls-remote", "--exit-code", remote, *ref_names], + cwd=destination_path, + capture_output=True, + ) + + if ls.returncode == 2: + print_line(b"vcs", b"optional refs %r not present on remote, skipping\n" % ref_names) + return + retry_required_command(b"vcs", args, cwd=destination_path, extra_env=env) @@ -735,11 +760,15 @@ def git_checkout( env=env, ) - if extra_refs: + for ref in extra_refs or []: + optional = ref.startswith("?") + if optional: + ref = ref[1:] git_fetch( destination_path, - *[f"{ref}:{ref}" for ref in extra_refs], + f"{ref}:{ref}", remote=head_repo, + optional=optional, env=env, )