func gitClone()

in getdeps/git.go [80:138]


func gitClone(label, repo, dest, branch string, hashMode HashMode, hash string, urlOverrides *URLOverrides) (string, error) {
	if urlOverrides != nil {
		repo = urlOverrides.Override(repo)
	}

	if branch == "" {
		return "", fmt.Errorf("%s: branch not specified", label)
	}

	if hashMode == hashModeUpdate {
		hash = ""
	}

	log.Printf("%s: Cloning %s (%s %s)...", label, repo, branch, hash)

	// Try the shallow clone first. This is much faster but requires
	// `uploadpack.allowReachableSHA1InWant` to be enabled on the server,
	// which is not the default.
	ok := false

	ref := hash
	if ref == "" {
		ref = branch
	}
	if err := gitCloneShallow(label, repo, ref, dest); err == nil {
		ok = true
	} else {
		log.Printf(
			"%s: shallow clone failed: %s,\n"+
				"== NOTE: This is likely because uploadpack.allowReachableSHA1InWant is not enabled on the server.",
			label, err)
		// Fall back to full clone
	}

	if !ok {
		if err := runCommand("git", "clone", "-q", "-b", branch, repo, dest); err != nil {
			return "", fmt.Errorf("%s: %w", label, err)
		}
		if hash != "" {
			if err := runCommand("git", "-C", dest, "checkout", "-q", hash); err != nil {
				return "", fmt.Errorf("%s: %w", label, err)
			}
		}
	}

	cmd := exec.Command("git", "-C", dest, "rev-parse", "HEAD")
	out, err := cmd.CombinedOutput()
	if err != nil {
		return "", fmt.Errorf("%s: error running %v: %w", label, cmd, err)
	}
	currentHash := strings.TrimSpace(string(out))
	log.Printf("%s: Current hash is %s", label, currentHash)

	if hashMode == hashModeStrict && hash == "" {
		return currentHash, fmt.Errorf("%s: %s: hash mode is strict and no hash supplied (current is %s)", label, repo, currentHash)
	}

	return currentHash, nil
}