diff --git a/modules/git/branch.go b/modules/git/branch.go index c781953593..ae36b979e5 100644 --- a/modules/git/branch.go +++ b/modules/git/branch.go @@ -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/. 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) } diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index d216184881..29c3fb5492 100644 --- a/modules/git/repo_branch_gogit.go +++ b/modules/git/repo_branch_gogit.go @@ -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. diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 16098bbae5..4a3b7a215a 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -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. diff --git a/modules/git/repo_branch_test.go b/modules/git/repo_branch_test.go index 17f06c8ba3..0358fc719f 100644 --- a/modules/git/repo_branch_test.go +++ b/modules/git/repo_branch_test.go @@ -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) { diff --git a/modules/got/ref.go b/modules/got/ref.go index f0cdec5575..0261fdc84b 100644 --- a/modules/got/ref.go +++ b/modules/got/ref.go @@ -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 diff --git a/modules/got/ref_test.go b/modules/got/ref_test.go index 3f2c76d758..7d2fc8b10f 100644 --- a/modules/got/ref_test.go +++ b/modules/got/ref_test.go @@ -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")