modules/git: resolve GetRefCommitID via got
giteabot backport / giteabot (push) Skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Canceled after 0s
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Canceled after 0s
release-nightly / nightly-binary (push) Canceled after 0s
release-nightly / nightly-container (push) Canceled after 0s
giteabot backport / giteabot (push) Skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Canceled after 0s
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Canceled after 0s
release-nightly / nightly-binary (push) Canceled after 0s
release-nightly / nightly-container (push) Canceled after 0s
Resolve ref names through got ref -l instead of CatFileBatch/gogit ref lookups. Plain object hashes are returned directly, as before. Missing refs still map to ErrNotExist. Verified: GOTMPDIR=/home/lotherk/.cache/gotmp go test ./modules/got ./modules/git -count=1
This commit is contained in:
1 parent
264453a934
commit
1e81fa4a14
3 files changed
+44
-24
No files matched your search
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user