docstr: Fetch the repo into dst_dir and refine githubref based on what we learn.
docstr_tok: ["Fetch","the","repo","into","dst_dir","and","refine","githubref","based","on","what","we","learn","."]
code: def fetch(self, dst_dir: str) -> None:
      """Fetch the repo into dst_dir and refine githubref based on what we learn."""
      # We defer importing git until the last moment, because the import requires that the git
      # executable is available on the PATH, so we only want to fail if we actually need it.
      import git  # type: ignore
  
      repo = git.Repo.init(dst_dir)
      self.path = repo.working_dir
      origin = repo.create_remote("origin", self.uri or "")
  
      try:
          # We fetch the origin so that we have branch and tag references
          origin.fetch(depth=1)
      except git.exc.GitCommandError as e:
          raise LaunchError(
              f"Unable to fetch from git remote repository {self.url}:\n{e}"
          )
  
      ref: Union[git.RemoteReference, str]
      if self.ref:
          if self.ref in origin.refs:
              ref = origin.refs[self.ref]
          else:
              ref = self.ref
          head = repo.create_head(self.ref, ref)
          head.checkout()
          self.commit_hash = head.commit.hexsha
  
      else:
          # TODO: Is there a better way to do this?
          default_branch = None
          for ref in repo.references:
              if hasattr(ref, "tag"):  # Skip tag references
                  continue
              refname = ref.name
              if refname.startswith("origin/"):  # Trim off "origin/"
                  refname = refname[7:]
              if refname == "main":
                  default_branch = "main"
                  break
              if refname == "master":
                  default_branch = "master"
                  # Keep looking in case we also have a main, which we let take precedence
                  # (While the references appear to be sorted, not clear if that's guaranteed.)
          if not default_branch:
              raise LaunchError(
                  f"Unable to determine branch or commit to checkout from {self.url}"
              )
          self.default_branch = default_branch
          self.ref = default_branch
          head = repo.create_head(
              default_branch, origin.refs[default_branch]
          )
          head.checkout()
          self.commit_hash = head.commit.hexsha
      repo.submodule_update(init=True, recursive=True)
  
file_path: wandb/wandb/sdk/launch/git_reference.py
imports: import re
  from dataclasses import dataclass
  from enum import IntEnum
  from typing import Optional, Tuple, Union
  from wandb.sdk.launch.errors import LaunchError
label_desc: Use image node_id 3 for calling the GitReference obj's underlying member method code with example usage: obj.fetch(dst_dir) without return types