From d516f4754f4ddf99ee383452d4ff529f1cc0530c Mon Sep 17 00:00:00 2001 From: Alexander Matyushentsev Date: Tue, 26 Jan 2021 12:59:51 -0800 Subject: [PATCH] fix: fix fetching commit SHAs (#5312) (#5320) Signed-off-by: Alexander Matyushentsev --- reposerver/repository/repository.go | 28 +++++++++++++++++++++------- util/git/client.go | 3 +-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/reposerver/repository/repository.go b/reposerver/repository/repository.go index 57a6838730da2..fb916a0fafdff 100644 --- a/reposerver/repository/repository.go +++ b/reposerver/repository/repository.go @@ -1336,14 +1336,28 @@ func checkoutRevision(gitClient git.Client, revision string) error { if err != nil { return status.Errorf(codes.Internal, "Failed to initialize git repo: %v", err) } - err = gitClient.Fetch(revision) - if err != nil { - return status.Errorf(codes.Internal, "Failed to fetch %s: %v", revision, err) - } - err = gitClient.Checkout("FETCH_HEAD") - if err != nil { - return status.Errorf(codes.Internal, "Failed to checkout FETCH_HEAD: %v", err) + + // Some git providers don't support fetching commit sha + if revision != "" && !git.IsCommitSHA(revision) && !git.IsTruncatedCommitSHA(revision) { + err = gitClient.Fetch(revision) + if err != nil { + return status.Errorf(codes.Internal, "Failed to fetch %s: %v", revision, err) + } + err = gitClient.Checkout("FETCH_HEAD") + if err != nil { + return status.Errorf(codes.Internal, "Failed to checkout FETCH_HEAD: %v", err) + } + } else { + err = gitClient.Fetch("") + if err != nil { + return status.Errorf(codes.Internal, "Failed to fetch %s: %v", revision, err) + } + err = gitClient.Checkout(revision) + if err != nil { + return status.Errorf(codes.Internal, "Failed to checkout %s: %v", revision, err) + } } + return err } diff --git a/util/git/client.go b/util/git/client.go index 712fd639b08fd..6c6e75718d827 100644 --- a/util/git/client.go +++ b/util/git/client.go @@ -263,8 +263,7 @@ func (m *nativeGitClient) IsLFSEnabled() bool { // Fetch fetches latest updates from origin func (m *nativeGitClient) Fetch(revision string) error { var err error - // Some git providers don't support fetching commit sha - if revision != "" && !IsCommitSHA(revision) && !IsTruncatedCommitSHA(revision) { + if revision != "" { err = m.runCredentialedCmd("git", "fetch", "origin", revision, "--tags", "--force") } else { err = m.runCredentialedCmd("git", "fetch", "origin", "--tags", "--force")