Skip to content

Commit

Permalink
fix: fallback to fetch default only on error
Browse files Browse the repository at this point in the history
Ignoring commit SHA breaks gerrit when the commit is not merged

Signed-off-by: Yujun Zhang <yujunz@nvidia.com>
  • Loading branch information
yujunz committed Jan 28, 2022
1 parent f387ab8 commit b55627a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion util/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (m *nativeGitClient) Fetch(revision string) error {

var err error
if revision != "" {
err = m.runCredentialedCmd("git", "fetch", "origin", revision, "--tags", "--force")
err = m.runCredentialedCmd("git", "fetch", "origin", revision)
} else {
err = m.runCredentialedCmd("git", "fetch", "origin", "--tags", "--force")
}
Expand Down

0 comments on commit b55627a

Please sign in to comment.