diff --git a/AGENTS.md b/AGENTS.md index e58d8b9bdf..72de98cfb8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -12,8 +12,8 @@ storage. - Main module `gotea.dev`; binary `gotea`. Identity strings and the English locale are renamed; upstream copyright notices stay. - The Go backend still calls `modules/git` for most operations. Bare repository - creation, `IsEmpty`, `GetDefaultBranch`, and `SetDefaultBranch` now go through - `modules/got`; the rest of the migration is open. + creation, `IsEmpty`, `GetDefaultBranch`, `SetDefaultBranch`, and + `GetBranchCommitID` now go through `modules/got`; the rest of the migration is open. - `cgi/gotea-cgi` is the read-only old-browser frontend. It browses bare repositories with `got`; it does not authenticate and has no write path. - Gotea Pages is a prototype: a `pages` branch is checked out with got, built @@ -98,9 +98,9 @@ make test-migration # after modelmigration changes ## got integration - Put every got invocation in `modules/got` (replacing `modules/git`). Nothing else - execs got or parses repository internals. Bare init, empty state, and default - branch read/write are already there; non-bare temp worktrees still use git and - must be migrated deliberately. + execs got or parses repository internals. Bare init, empty state, default-branch + read/write, and branch ID lookup are already there; non-bare temp worktrees still + use git and must be migrated deliberately. - `[git] GOT_PATH` selects the got binary (default `got`). - `SetDefaultBranch` uses `got ref -s` for existing branches and writes HEAD atomically for unborn branches, because `got ref -s` requires the target to exist. diff --git a/README.md b/README.md index 3ab94c5d6f..114561ba0f 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ contrast failures (925/945 pass), which do not affect the build path. ## Backend got migration Bare repositories are created with `got init` through `modules/got`, and -`IsEmpty`, `GetDefaultBranch`, and `SetDefaultBranch` use got. Unborn branches +`IsEmpty`, `GetDefaultBranch`, `SetDefaultBranch`, and `GetBranchCommitID` use got. Unborn branches write HEAD atomically because `got ref -s` requires the target branch to exist. `GOT_PATH` in the `[git]` section selects the binary (default `got`). Non-bare temporary worktrees and pushing still use git while the migration proceeds. diff --git a/modules/git/branch.go b/modules/git/branch.go index 1a9f800787..c781953593 100644 --- a/modules/git/branch.go +++ b/modules/git/branch.go @@ -24,13 +24,7 @@ func GetBranchesByPath(ctx context.Context, repo RepositoryFacade, skip, limit i } func GetBranchCommitID(ctx context.Context, repo RepositoryFacade, branch string) (string, error) { - gitRepo, err := OpenRepository(ctx, repo) - if err != nil { - return "", err - } - defer gitRepo.Close() - - return gitRepo.GetBranchCommitID(ctx, branch) + return got.BranchCommitID(ctx, gitrepo.RepoLocalPath(repo), branch) } // SetDefaultBranch sets default branch of repository. diff --git a/modules/git/init_got_test.go b/modules/git/init_got_test.go index 73717825e9..45f5a37e19 100644 --- a/modules/git/init_got_test.go +++ b/modules/git/init_got_test.go @@ -5,6 +5,7 @@ package git import ( "os" + "os/exec" "path/filepath" "strings" "testing" @@ -146,3 +147,60 @@ func TestSetDefaultBranchDoesNotFallBackToGit(t *testing.T) { t.Fatal("expected a got failure instead of a silent git fallback") } } + +func importWithGot(t *testing.T, repoPath, src string) { + t.Helper() + cmd := exec.Command(got.Executable(), "import", "-r", repoPath, + "-b", "main", "-m", "initial", src) + cmd.Env = append(os.Environ(), "GOT_AUTHOR=Test User ") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("got import: %v: %s", err, out) + } +} + +func TestGetBranchCommitIDUsesGot(t *testing.T) { + if _, err := got.Version(t.Context()); err != nil { + t.Skipf("got not available: %v", err) + } + dir := t.TempDir() + repoPath := filepath.Join(dir, "repo.git") + if err := got.InitBare(t.Context(), repoPath, "sha1", "main"); err != nil { + t.Fatal(err) + } + src := filepath.Join(dir, "src") + if err := os.Mkdir(src, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(src, "hello.txt"), []byte("hello\n"), 0o644); err != nil { + t.Fatal(err) + } + importWithGot(t, repoPath, src) + + repo := mockRepository(repoPath) + id, err := GetBranchCommitID(t.Context(), repo, "main") + if err != nil { + t.Fatal(err) + } + if len(id) != 40 { + t.Fatalf("unexpected object ID %q", id) + } +} + +func TestGetBranchCommitIDDoesNotFallBackToGit(t *testing.T) { + if _, err := got.Version(t.Context()); err != nil { + t.Skipf("got not available: %v", err) + } + dir := filepath.Join(t.TempDir(), "repo.git") + if err := got.InitBare(t.Context(), dir, "sha1", "main"); err != nil { + t.Fatal(err) + } + repo := mockRepository(dir) + + old := setting.Git.GotPath + setting.Git.GotPath = "gotea-test-got-missing" + t.Cleanup(func() { setting.Git.GotPath = old }) + + if _, err := GetBranchCommitID(t.Context(), repo, "main"); err == nil { + t.Fatal("expected a got failure instead of a silent git fallback") + } +} diff --git a/modules/got/got.go b/modules/got/got.go index c5e1d47740..5246e6fb46 100644 --- a/modules/got/got.go +++ b/modules/got/got.go @@ -202,3 +202,27 @@ func SetDefaultBranch(ctx context.Context, path, branch string) error { } return writeHead(path, branch) } + +// BranchCommitID returns the object ID of the named local branch. +func BranchCommitID(ctx context.Context, path, branch string) (string, error) { + if !validBranchName(branch) { + return "", fmt.Errorf("invalid branch name: %s", branch) + } + ref := "refs/heads/" + branch + out, _, err := run(ctx, "ref", "-l", "-r", path, ref) + if err != nil { + return "", err + } + prefix := ref + ": " + for _, line := range strings.Split(out, "\n") { + id, ok := strings.CutPrefix(line, prefix) + if ok { + id = strings.TrimSpace(id) + if isObjectID(id) { + return id, nil + } + return "", fmt.Errorf("invalid object ID for %s: %s", ref, id) + } + } + return "", fmt.Errorf("branch not found: %s", branch) +} diff --git a/modules/got/got_test.go b/modules/got/got_test.go index 35ec586a74..61d67958f2 100644 --- a/modules/got/got_test.go +++ b/modules/got/got_test.go @@ -192,3 +192,33 @@ func TestSetDefaultBranchDoesNotBypassGot(t *testing.T) { t.Fatalf("HEAD changed despite missing got: %q", head) } } + +func TestBranchCommitID(t *testing.T) { + requireGot(t) + dir := t.TempDir() + repo := filepath.Join(dir, "repo.git") + if err := InitBare(t.Context(), repo, "sha1", "main"); err != nil { + t.Fatal(err) + } + src := filepath.Join(dir, "src") + if err := os.Mkdir(src, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(src, "hello.txt"), []byte("hello\n"), 0o644); err != nil { + t.Fatal(err) + } + t.Setenv("GOT_AUTHOR", "Test User ") + if _, _, err := run(t.Context(), "import", "-r", repo, "-b", "main", "-m", "initial", src); err != nil { + t.Fatal(err) + } + id, err := BranchCommitID(t.Context(), repo, "main") + if err != nil { + t.Fatal(err) + } + if len(id) != 40 || !isObjectID(id) { + t.Fatalf("unexpected object ID %q", id) + } + if _, err := BranchCommitID(t.Context(), repo, "missing"); err == nil { + t.Fatal("expected an error for a missing branch") + } +}