diff --git a/modules/git/repo_commit_gogit.go b/modules/git/repo_commit_gogit.go index 63c067d6f0..d1e238dcc5 100644 --- a/modules/git/repo_commit_gogit.go +++ b/modules/git/repo_commit_gogit.go @@ -7,38 +7,32 @@ package git import ( + "errors" "context" "strings" "gotea.dev/modules/git/gitcmd" + "gotea.dev/modules/git/gitrepo" + "gotea.dev/modules/got" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) // GetRefCommitID returns the last commit ID string of given reference. -func (repo *Repository) GetRefCommitID(_ context.Context, name string) (string, error) { +func (repo *Repository) GetRefCommitID(ctx context.Context, name string) (string, error) { if plumbing.IsHash(name) { return name, nil } - refName := plumbing.ReferenceName(name) - if err := refName.Validate(); err != nil { - // Match the nogogit behavior: an unresolvable/invalid ref name - // is reported as not-existing rather than a generic validation error, - // so callers can rely on IsErrNotExist regardless of build tag. - return "", ErrNotExist{ID: name} - } - ref, err := repo.gogitRepo.Reference(refName, true) + id, err := got.ResolveRefID(ctx, gitrepo.RepoLocalPath(repo), name) if err != nil { - if err == plumbing.ErrReferenceNotFound { - return "", ErrNotExist{ - ID: name, - } + var notFound got.ErrRefNotFound + if errors.As(err, ¬Found) { + return "", ErrNotExist{ID: name} } return "", err } - - return ref.Hash().String(), nil + return id, nil } // ConvertToHash returns a Hash object from a potential ID string diff --git a/modules/git/repo_commit_nogogit.go b/modules/git/repo_commit_nogogit.go index 62593da9b0..6a325e8190 100644 --- a/modules/git/repo_commit_nogogit.go +++ b/modules/git/repo_commit_nogogit.go @@ -10,23 +10,27 @@ import ( "errors" "io" + "gotea.dev/modules/git/gitrepo" + "gotea.dev/modules/got" "gotea.dev/modules/setting" + + "github.com/go-git/go-git/v5/plumbing" ) // GetRefCommitID returns the last commit ID string of given reference (branch or tag). func (repo *Repository) GetRefCommitID(ctx context.Context, name string) (string, error) { - batch, cancel, err := repo.CatFileBatch() + if plumbing.IsHash(name) { + return name, nil + } + id, err := got.ResolveRefID(ctx, gitrepo.RepoLocalPath(repo), name) if err != nil { + var notFound got.ErrRefNotFound + if errors.As(err, ¬Found) { + return "", ErrNotExist{ID: name} + } return "", err } - defer cancel() - info, err := batch.QueryInfo(name) - if IsErrNotExist(err) { - return "", ErrNotExist{name, ""} - } else if err != nil { - return "", err - } - return info.ID, nil + return id, nil } func (repo *Repository) getCommit(ctx context.Context, id ObjectID) (*Commit, error) { diff --git a/modules/got/ref.go b/modules/got/ref.go index fb4b4012d6..90c0333777 100644 --- a/modules/got/ref.go +++ b/modules/got/ref.go @@ -124,6 +124,28 @@ func ObjectExists(ctx context.Context, path, name string) (bool, error) { return false, err } +// ResolveRefID returns the object ID a reference points at. It accepts full +// ref names (refs/...) and HEAD. For annotated tags this is the tag object ID, +// matching git show-ref / ref hash semantics. +func ResolveRefID(ctx context.Context, path, name string) (string, error) { + if strings.TrimSpace(name) == "" { + return "", fmt.Errorf("empty reference") + } + id, err := RefObjectID(ctx, path, name) + if err == nil { + return id, nil + } + var notFound ErrRefNotFound + if errors.As(err, ¬Found) { + return "", err + } + msg := strings.ToLower(err.Error()) + if strings.Contains(msg, "not found") || strings.Contains(msg, "reference ") { + return "", ErrRefNotFound{Name: name} + } + return "", err +} + // ResolveCommitID returns the full commit ID for a revision argument. It accepts // full/abbreviated commit IDs, refs and symbolic refs such as HEAD. Only commit // objects are resolved; callers that need arbitrary object IDs should use