From 4aa614dafb933a2d5c66ed29cc37bd7421e8c01f Mon Sep 17 00:00:00 2001 From: Yujun Zhang Date: Sat, 29 Jan 2022 02:58:36 +0800 Subject: [PATCH] fix: fetch revision only then fallback to default refspec (#5605) * fix: fallback to fetch default only on error Ignoring commit SHA breaks gerrit when the commit is not merged Signed-off-by: Yujun Zhang * revert util/git/client.go changes Signed-off-by: Alexander Matyushentsev Co-authored-by: Alexander Matyushentsev --- reposerver/repository/repository.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/reposerver/repository/repository.go b/reposerver/repository/repository.go index 7399a15475e41..212ef5424fe49 100644 --- a/reposerver/repository/repository.go +++ b/reposerver/repository/repository.go @@ -1583,25 +1583,25 @@ func checkoutRevision(gitClient git.Client, revision string) error { return status.Errorf(codes.Internal, "Failed to initialize git repo: %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(revision) + + if err != nil { + log.Infof("Failed to fetch revision %s: %v", revision, err) + log.Infof("Fallback to fetch default") err = gitClient.Fetch("") if err != nil { - return status.Errorf(codes.Internal, "Failed to fetch %s: %v", revision, err) + return status.Errorf(codes.Internal, "Failed to fetch default: %v", err) } err = gitClient.Checkout(revision) if err != nil { - return status.Errorf(codes.Internal, "Failed to checkout %s: %v", revision, err) + return status.Errorf(codes.Internal, "Failed to checkout revision %s: %v", revision, err) } + return err + } + + err = gitClient.Checkout("FETCH_HEAD") + if err != nil { + return status.Errorf(codes.Internal, "Failed to checkout FETCH_HEAD: %v", err) } return err