feat(got): read empty state and default branch with got
Add IsEmpty and DefaultBranch to modules/got and route Repository.IsEmpty and GetDefaultBranch through them. Keep SetDefaultBranch on git because got ref -s requires the target branch to exist, which excludes unborn branches. Cover the wiring and the no-fallback behaviour with tests. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
This commit is contained in:
1 parent
0e54cb8a02
commit
34cfc1e12b
7 files changed
+169
-28
No files matched your search
@@ -12,7 +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 now goes through `modules/got`; the rest of the migration is open.
|
||||
creation, `IsEmpty`, and `GetDefaultBranch` 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
|
||||
@@ -97,9 +98,12 @@ 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 repo creation is already there;
|
||||
non-bare temp worktrees still use git and must be migrated deliberately.
|
||||
execs got or parses repository internals. Bare init, empty-state, and default-branch
|
||||
reads 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` still uses git because `got ref -s` requires the target branch
|
||||
to exist; empty repositories need an unborn-HEAD path before that can move.
|
||||
- File changes require a **work tree**, whose metadata is `.got/` (not `.git/`).
|
||||
A bare repository cannot be edited in place: server-side edits and merges need a
|
||||
temporary `got checkout` work tree and a `got commit`. Serialize per repository
|
||||
|
||||
@@ -129,10 +129,11 @@ 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`; `GOT_PATH`
|
||||
in the `[git]` section selects the binary (default `got`). Non-bare temporary
|
||||
worktrees and the remaining plumbing still use git while the migration proceeds.
|
||||
There is no silent git fallback for the migrated init path.
|
||||
Bare repositories are created with `got init` through `modules/got`, and
|
||||
`IsEmpty`/`GetDefaultBranch` read refs through got. `GOT_PATH` in the `[git]`
|
||||
section selects the binary (default `got`). Non-bare temporary worktrees, pushing,
|
||||
and `SetDefaultBranch` still use git while the migration proceeds. There is no
|
||||
silent git fallback for the migrated paths.
|
||||
|
||||
## Design notes
|
||||
|
||||
|
||||
+4
-11
@@ -5,10 +5,10 @@ package git
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"gotea.dev/modules/git/gitcmd"
|
||||
"gotea.dev/modules/git/gitrepo"
|
||||
"gotea.dev/modules/got"
|
||||
)
|
||||
|
||||
// GetBranchesByPath returns a branch by its path
|
||||
@@ -34,6 +34,7 @@ func GetBranchCommitID(ctx context.Context, repo RepositoryFacade, branch string
|
||||
}
|
||||
|
||||
// SetDefaultBranch sets default branch of repository.
|
||||
// got ref -s requires the target branch to exist, so unborn branches still use git.
|
||||
func SetDefaultBranch(ctx context.Context, repo RepositoryFacade, name string) error {
|
||||
_, _, err := gitcmd.NewCommand("symbolic-ref", "HEAD").
|
||||
AddDynamicArguments(BranchPrefix + name).WithRepo(repo).RunStdString(ctx)
|
||||
@@ -42,15 +43,7 @@ func SetDefaultBranch(ctx context.Context, repo RepositoryFacade, name string) e
|
||||
|
||||
// GetDefaultBranch gets default branch of repository.
|
||||
func GetDefaultBranch(ctx context.Context, repo RepositoryFacade) (string, error) {
|
||||
stdout, _, err := gitcmd.NewCommand("symbolic-ref", "HEAD").WithRepo(repo).RunStdString(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
stdout = strings.TrimSpace(stdout)
|
||||
if !strings.HasPrefix(stdout, BranchPrefix) {
|
||||
return "", errors.New("the HEAD is not a branch: " + stdout)
|
||||
}
|
||||
return strings.TrimPrefix(stdout, BranchPrefix), nil
|
||||
return got.DefaultBranch(ctx, gitrepo.RepoLocalPath(repo))
|
||||
}
|
||||
|
||||
// IsReferenceExist returns true if given reference exists in the repository.
|
||||
|
||||
@@ -44,3 +44,56 @@ func TestInitRepositoryLocalBareDoesNotFallBackToGit(t *testing.T) {
|
||||
t.Fatal("expected a got failure instead of a silent git fallback")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryMetadataUsesGot(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", "trunk"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo, err := OpenRepositoryLocal(t.Context(), dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer repo.Close()
|
||||
|
||||
empty, err := repo.IsEmpty(t.Context())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !empty {
|
||||
t.Fatal("new repository is not empty")
|
||||
}
|
||||
branch, err := GetDefaultBranch(t.Context(), repo)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if branch != "trunk" {
|
||||
t.Fatalf("unexpected default branch %q", branch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEmptyDoesNotFallBackToGit(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, err := OpenRepositoryLocal(t.Context(), dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer repo.Close()
|
||||
|
||||
old := setting.Git.GotPath
|
||||
setting.Git.GotPath = "gotea-test-got-missing"
|
||||
t.Cleanup(func() { setting.Git.GotPath = old })
|
||||
|
||||
if _, err := repo.IsEmpty(t.Context()); err == nil {
|
||||
t.Fatal("expected a got failure instead of a silent git fallback")
|
||||
}
|
||||
}
|
||||
+2
-10
@@ -147,19 +147,11 @@ func InitRepositoryLocal(ctx context.Context, localRepoPath string, bare bool, o
|
||||
|
||||
// IsEmpty Check if repository is empty.
|
||||
func (repo *Repository) IsEmpty(ctx context.Context) (bool, error) {
|
||||
stdout, _, err := gitcmd.NewCommand().
|
||||
AddOptionFormat("--git-dir=%s", gitrepo.RepoLocalPath(repo)). // TODO: all git commands should use "--git-dir" or "GIT_DIR=..."
|
||||
AddArguments("rev-list", "-n", "1", "--all").
|
||||
WithRepo(repo).
|
||||
RunStdString(ctx)
|
||||
empty, err := got.IsEmpty(ctx, gitrepo.RepoLocalPath(repo))
|
||||
if err != nil {
|
||||
if (gitcmd.IsErrorExitCode(err, 1) && err.Stderr() == "") || gitcmd.IsErrorExitCode(err, 129) {
|
||||
// git 2.11 exits with 129 if the repo is empty
|
||||
return true, nil
|
||||
}
|
||||
return true, fmt.Errorf("check empty: %w", err)
|
||||
}
|
||||
return strings.TrimSpace(stdout) == "", nil
|
||||
return empty, nil
|
||||
}
|
||||
|
||||
// CloneRepoOptions options when clone a repository
|
||||
|
||||
@@ -90,3 +90,51 @@ func InitBare(ctx context.Context, path, objectFormat, defaultBranch string) err
|
||||
_, _, err := run(ctx, "init", "-A", objectFormat, "-b", defaultBranch, path)
|
||||
return err
|
||||
}
|
||||
|
||||
func isObjectID(s string) bool {
|
||||
if len(s) != 40 && len(s) != 64 {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(s); i++ {
|
||||
c := s[i]
|
||||
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// IsEmpty reports whether the repository has no reference pointing at an object.
|
||||
func IsEmpty(ctx context.Context, path string) (bool, error) {
|
||||
out, _, err := run(ctx, "ref", "-l", "-r", path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
_, value, ok := strings.Cut(line, ": ")
|
||||
if ok && isObjectID(strings.TrimSpace(value)) {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// DefaultBranch returns the branch HEAD points at.
|
||||
func DefaultBranch(ctx context.Context, path string) (string, error) {
|
||||
out, _, err := run(ctx, "ref", "-l", "-r", path, "HEAD")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
ref, ok := strings.CutPrefix(line, "HEAD: ")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
ref = strings.TrimSpace(ref)
|
||||
if branch, ok := strings.CutPrefix(ref, "refs/heads/"); ok {
|
||||
return branch, nil
|
||||
}
|
||||
return "", fmt.Errorf("HEAD is not a branch: %s", ref)
|
||||
}
|
||||
return "", fmt.Errorf("HEAD not found")
|
||||
}
|
||||
@@ -61,3 +61,53 @@ func TestIsRepository(t *testing.T) {
|
||||
t.Fatal("empty directory reported as a repository")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEmpty(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)
|
||||
}
|
||||
empty, err := IsEmpty(t.Context(), repo)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !empty {
|
||||
t.Fatal("new repository is not empty")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
empty, err = IsEmpty(t.Context(), repo)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if empty {
|
||||
t.Fatal("repository with a commit is empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultBranch(t *testing.T) {
|
||||
requireGot(t)
|
||||
repo := filepath.Join(t.TempDir(), "repo.git")
|
||||
if err := InitBare(t.Context(), repo, "sha1", "trunk"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
branch, err := DefaultBranch(t.Context(), repo)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if branch != "trunk" {
|
||||
t.Fatalf("unexpected branch %q", branch)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user