func syncRepo()

in cmd/git-sync/main.go [979:1021]


func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, dest string, authURL string, submoduleMode string) (bool, string, error) {
	if authURL != "" {
		// For ASKPASS Callback URL, the credentials behind is dynamic, it needs to be
		// re-fetched each time.
		if err := callGitAskPassURL(ctx, authURL); err != nil {
			askpassCount.WithLabelValues(metricKeyError).Inc()
			return false, "", fmt.Errorf("failed to call GIT_ASKPASS_URL: %v", err)
		}
		askpassCount.WithLabelValues(metricKeySuccess).Inc()
	}

	currentWorktreeGit := filepath.Join(dest, ".git")
	var hash string
	_, err := os.Stat(currentWorktreeGit)
	switch {
	case os.IsNotExist(err):
		// First time. Just clone it and get the hash.
		err = cloneRepo(ctx, repo, branch, rev, depth, gitRoot)
		if err != nil {
			return false, "", err
		}
		hash, err = localHashForRev(ctx, rev, gitRoot)
		if err != nil {
			return false, "", err
		}
	case err != nil:
		return false, "", fmt.Errorf("error checking if a worktree exists %q: %v", currentWorktreeGit, err)
	default:
		// Not the first time. Figure out if the ref has changed.
		local, remote, err := getRevs(ctx, dest, branch, rev)
		if err != nil {
			return false, "", err
		}
		if local == remote {
			log.V(1).Info("no update required", "rev", rev, "local", local, "remote", remote)
			return false, "", nil
		}
		log.V(0).Info("update required", "rev", rev, "local", local, "remote", remote)
		hash = remote
	}

	return true, hash, addWorktreeAndSwap(ctx, gitRoot, dest, branch, rev, depth, hash, submoduleMode)
}