feat(got): read branch commit IDs with got
Add BranchCommitID to modules/got and route GetBranchCommitID through it. Cover existing and missing branches and the no-fallback case. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
This commit is contained in:
1 parent
d6bb7fb157
commit
2051891fde
6 files changed
+119
-13
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 <test@example.com>")
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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 <test@example.com>")
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user