modules/git: migrate IsReferenceExist to got cat
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
Route package-level and Repository.IsReferenceExist through modules/got.ObjectExists, which probes `got cat -r` without -P. got cat resolves full/abbreviated object IDs, refs and tags but not path names, matching the commit_checker/merge_tree callers that pass commit IDs and the ref callers that pass ref names. This also removes the gogit/nogogit inconsistency where gogit rejected blob hashes and short hashes for IsReferenceExist; the got-backed path accepts them in both builds. IsObjectExist is left alone: its nogogit CatFileBatch implementation has different semantics and its only caller is marked FIXME. Verified: GOTMPDIR=/home/lotherk/.cache/gotmp go test ./modules/got ./modules/git -count=1
This commit is contained in:
1 parent
3c6b249810
commit
75fed4414d
6 files changed
+100
-75
No files matched your search
+20
-22
@@ -6,7 +6,6 @@ package git
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gotea.dev/modules/git/gitcmd"
|
||||
"gotea.dev/modules/git/gitrepo"
|
||||
"gotea.dev/modules/got"
|
||||
)
|
||||
@@ -37,10 +36,18 @@ func GetDefaultBranch(ctx context.Context, repo RepositoryFacade) (string, error
|
||||
return got.DefaultBranch(ctx, gitrepo.RepoLocalPath(repo))
|
||||
}
|
||||
|
||||
// IsReferenceExist returns true if given reference exists in the repository.
|
||||
// IsReferenceExist returns true if the given reference name or object ID exists
|
||||
// in the repository. It accepts full/abbreviated object IDs, refs and tags;
|
||||
// it does not resolve path names.
|
||||
func IsReferenceExist(ctx context.Context, repo RepositoryFacade, name string) bool {
|
||||
_, _, err := gitcmd.NewCommand("show-ref", "--verify").AddDashesAndList(name).WithRepo(repo).RunStdString(ctx)
|
||||
return err == nil
|
||||
if name == "" {
|
||||
return false
|
||||
}
|
||||
exists, err := got.ObjectExists(ctx, gitrepo.RepoLocalPath(repo), name)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return exists
|
||||
}
|
||||
|
||||
// IsBranchExist returns true if given branch exists in the repository.
|
||||
@@ -49,31 +56,22 @@ func IsBranchExist(ctx context.Context, repo RepositoryFacade, name string) bool
|
||||
}
|
||||
|
||||
// DeleteBranch delete a branch by name on repository.
|
||||
//
|
||||
// force is accepted for signature compatibility and ignored: got has no
|
||||
// equivalent of git's "branch -d refuses unmerged", and every caller in gotea
|
||||
// passes true. The deletion goes through got.DeleteBranchRef, which only ever
|
||||
// touches refs/heads/<name>.
|
||||
func DeleteBranch(ctx context.Context, repo RepositoryFacade, name string, force bool) error {
|
||||
cmd := gitcmd.NewCommand("branch")
|
||||
|
||||
if force {
|
||||
cmd.AddArguments("-D")
|
||||
} else {
|
||||
cmd.AddArguments("-d")
|
||||
}
|
||||
|
||||
cmd.AddDashesAndList(name)
|
||||
_, _, err := cmd.WithRepo(repo).RunStdString(ctx)
|
||||
return err
|
||||
_ = force
|
||||
return got.DeleteBranchRef(ctx, gitrepo.RepoLocalPath(repo), name)
|
||||
}
|
||||
|
||||
// CreateBranch create a new branch
|
||||
func CreateBranch(ctx context.Context, repo RepositoryFacade, branch, oldbranchOrCommit string) error {
|
||||
cmd := gitcmd.NewCommand("branch")
|
||||
cmd.AddDashesAndList(branch, oldbranchOrCommit)
|
||||
|
||||
_, _, err := cmd.WithRepo(repo).RunStdString(ctx)
|
||||
return err
|
||||
return got.CreateBranch(ctx, gitrepo.RepoLocalPath(repo), branch, oldbranchOrCommit)
|
||||
}
|
||||
|
||||
// RenameBranch rename a branch
|
||||
func RenameBranch(ctx context.Context, repo RepositoryFacade, from, to string) error {
|
||||
_, _, err := gitcmd.NewCommand("branch", "-m").AddDynamicArguments(from, to).WithRepo(repo).RunStdString(ctx)
|
||||
return err
|
||||
return got.RenameBranch(ctx, gitrepo.RepoLocalPath(repo), from, to)
|
||||
}
|
||||
@@ -31,19 +31,19 @@ func (repo *Repository) IsObjectExist(_ context.Context, name string) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// IsReferenceExist returns true if given reference exists in the repository.
|
||||
// FIXME: Inconsistent behavior with nogogit edition
|
||||
// Unlike the implementation of IsObjectExist in nogogit edition, it does not support blob hashes here.
|
||||
// For example, IsObjectExist([existing_blob_hash]) will return false, but it will return true in nogogit edition.
|
||||
// To fix this, the solution could be refusing to support blob hashes in nogogit edition since a blob hash is not a reference.
|
||||
func (repo *Repository) IsReferenceExist(_ context.Context, name string) bool {
|
||||
if name == "" {
|
||||
// IsReferenceExist returns true if the given reference name or object ID exists
|
||||
// in the repository. It accepts full/abbreviated object IDs, refs and tags;
|
||||
// it does not resolve path names.
|
||||
func (repo *Repository) IsReferenceExist(ctx context.Context, name string) bool {
|
||||
if repo == nil || name == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
_, err := repo.gogitRepo.ResolveRevision(plumbing.Revision(name))
|
||||
|
||||
return err == nil
|
||||
exists, err := got.ObjectExists(ctx, gitrepo.RepoLocalPath(repo), name)
|
||||
if err != nil {
|
||||
// keep the gogit build quiet on missing refs; errors are unexpected here
|
||||
return false
|
||||
}
|
||||
return exists
|
||||
}
|
||||
|
||||
// IsBranchExist returns true if given branch exists in current repository.
|
||||
|
||||
@@ -39,20 +39,19 @@ func (repo *Repository) IsObjectExist(ctx context.Context, name string) bool {
|
||||
return strings.HasPrefix(info.ID, name) // FIXME: this logic doesn't seem right, why "HasPrefix"
|
||||
}
|
||||
|
||||
// IsReferenceExist returns true if given reference exists in the repository.
|
||||
// IsReferenceExist returns true if the given reference name or object ID exists
|
||||
// in the repository. It accepts full/abbreviated object IDs, refs and tags;
|
||||
// it does not resolve path names.
|
||||
func (repo *Repository) IsReferenceExist(ctx context.Context, name string) bool {
|
||||
if name == "" {
|
||||
if repo == nil || name == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
batch, cancel, err := repo.CatFileBatch()
|
||||
exists, err := got.ObjectExists(ctx, gitrepo.RepoLocalPath(repo), name)
|
||||
if err != nil {
|
||||
log.Error("Error opening CatFileBatch %v", err)
|
||||
log.Error("IsReferenceExist %s: %v", name, err)
|
||||
return false
|
||||
}
|
||||
defer cancel()
|
||||
_, err = batch.QueryInfo(name)
|
||||
return err == nil
|
||||
return exists
|
||||
}
|
||||
|
||||
// IsBranchExist returns true if given branch exists in current repository.
|
||||
|
||||
@@ -196,45 +196,18 @@ func TestRepository_IsReferenceExist(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer repo.Close()
|
||||
|
||||
// FIXME: Inconsistent behavior between gogit and nogogit editions
|
||||
// See the comment of IsReferenceExist in gogit edition for more details.
|
||||
supportBlobHash := !isGogit
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
arg: "",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "branch",
|
||||
arg: "master",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "commit hash",
|
||||
arg: "ce064814f4a0d337b333e646ece456cd39fab612",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "short commit hash",
|
||||
arg: "ce06481",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "blob hash",
|
||||
arg: "153f451b9ee7fa1da317ab17a127e9fd9d384310",
|
||||
want: supportBlobHash,
|
||||
},
|
||||
{
|
||||
name: "short blob hash",
|
||||
arg: "153f451",
|
||||
want: supportBlobHash,
|
||||
},
|
||||
{name: "empty", arg: "", want: false},
|
||||
{name: "branch", arg: "master", want: true},
|
||||
{name: "commit hash", arg: "ce064814f4a0d337b333e646ece456cd39fab612", want: true},
|
||||
{name: "short commit hash", arg: "ce06481", want: true},
|
||||
{name: "blob hash", arg: "153f451b9ee7fa1da317ab17a127e9fd9d384310", want: true},
|
||||
{name: "short blob hash", arg: "153f451", want: true},
|
||||
{name: "missing", arg: "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef", want: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
@@ -5,7 +5,9 @@ package got
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -92,6 +94,36 @@ func RefObjectID(ctx context.Context, path, name string) (string, error) {
|
||||
return "", ErrRefNotFound{Name: name}
|
||||
}
|
||||
|
||||
// ObjectExists reports whether name resolves to an object known to the
|
||||
// repository. It accepts full and abbreviated object IDs, refs and tags, but it
|
||||
// does not resolve path names (got cat without -P). This matches git cat-file
|
||||
// -e semantics closely enough for gotea's IsReferenceExist callers, which pass
|
||||
// either refs or commit/object IDs.
|
||||
func ObjectExists(ctx context.Context, path, name string) (bool, error) {
|
||||
if strings.TrimSpace(name) == "" {
|
||||
return false, nil
|
||||
}
|
||||
_, stderr, err := run(ctx, "cat", "-r", path, name)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if errors.Is(err, ErrNotInstalled) {
|
||||
return false, err
|
||||
}
|
||||
// got cat exits 1 for "not found"/unresolvable; other exit codes or
|
||||
// transport errors are real failures.
|
||||
msg := strings.ToLower(stderr)
|
||||
if strings.Contains(msg, "not found") || strings.Contains(msg, "no such") ||
|
||||
strings.Contains(msg, "needed a single revision") || strings.Contains(msg, "reference ") {
|
||||
return false, nil
|
||||
}
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
// ErrRefNotFound reports a missing reference.
|
||||
type ErrRefNotFound struct {
|
||||
Name string
|
||||
|
||||
@@ -81,6 +81,29 @@ func TestRefExists(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestObjectExists(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := t.Context()
|
||||
repo, commit := seedRepoWithRefs(t)
|
||||
|
||||
for _, name := range []string{commit, commit[:7], "refs/heads/main", "main"} {
|
||||
exists, err := ObjectExists(ctx, repo, name)
|
||||
if err != nil || !exists {
|
||||
t.Fatalf("%s exists=%v err=%v", name, exists, err)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"", "0000000000000000000000000000000000000000", "deadbeef", "no-such-ref"} {
|
||||
exists, err := ObjectExists(ctx, repo, name)
|
||||
if err != nil || exists {
|
||||
t.Fatalf("%q exists=%v err=%v", name, exists, err)
|
||||
}
|
||||
}
|
||||
exists, err := ObjectExists(ctx, repo, "f")
|
||||
if err != nil || exists {
|
||||
t.Fatalf("path resolved exists=%v err=%v", exists, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefExistsInEmptyRepository(t *testing.T) {
|
||||
requireGot(t)
|
||||
repoPath := filepath.Join(t.TempDir(), "repo.git")
|
||||
|
||||
Reference in new issue
Block a user