@@ -566,6 +566,22 @@ def configure_volume_posix(volume, user, group, running_as_root):
566566 set_dir_permissions (volume , user .pw_uid , group .gr_gid )
567567
568568
569+ def git_fetch (
570+ destination_path : str ,
571+ ref : str ,
572+ remote : str = "origin" ,
573+ tags : bool = False ,
574+ env : Optional [dict [str , str ]] = None ,
575+ ):
576+ args = ["git" , "fetch" ]
577+ if tags :
578+ # `--force` is needed to be able to update an existing outdated tag.
579+ args .extend (["--tags" , "--force" ])
580+
581+ args .extend ([remote , ref ])
582+ retry_required_command (b"vcs" , args , cwd = destination_path , extra_env = env )
583+
584+
569585def _clean_git_checkout (destination_path ):
570586 # Delete untracked files (i.e. build products)
571587 print_line (b"vcs" , b"cleaning git checkout...\n " )
@@ -669,29 +685,18 @@ def git_checkout(
669685 # that consumers can compute the merge-base or files modified between the
670686 # two as needed.
671687 if base_rev and base_rev != NULL_REVISION :
672- args = [ "git" , "fetch" , "origin" , base_rev ]
688+ git_fetch ( destination_path , base_rev , env = env )
673689
674- retry_required_command (b"vcs" , args , cwd = destination_path , extra_env = env )
675-
676- # Fetch head_ref
677- args = ["git" , "fetch" ]
678- if head_ref and base_repo == head_repo :
679- # If a head_ref was provided, it might be tag, so we need to make sure we fetch
680- # those. This is explicitly only done when base and head repo match,
681- # because it is the only scenario where tags could be present. (PRs, for
682- # example, always include an explicit rev.) Failure to do this could result
683- # in not having a tag, or worse: having an outdated version of one.
684- # `--force` is needed to be able to update an existing tag.
685- args .extend (["--tags" , "--force" ])
686-
687- else :
688- args .append ("--no-tags" )
690+ # If a head_ref was provided, it might be tag, so we need to make sure we fetch
691+ # those. This is explicitly only done when base and head repo match,
692+ # because it is the only scenario where tags could be present. (PRs, for
693+ # example, always include an explicit rev.) Failure to do this could result
694+ # in not having a tag, or worse: having an outdated version of one.
695+ tags = True if head_ref and base_repo == head_repo else False
689696
690697 # If a head_ref isn't provided, we fetch all refs from head_repo, which may be slow.
691- args .extend (
692- [head_repo , head_ref if head_ref else "+refs/heads/*:refs/remotes/work/*" ]
693- )
694- retry_required_command (b"vcs" , args , cwd = destination_path , extra_env = env )
698+ target = head_ref if head_ref else "+refs/heads/*:refs/remotes/work/*"
699+ git_fetch (destination_path , target , remote = head_repo , tags = tags , env = env )
695700
696701 args = [
697702 "git" ,
0 commit comments