feat(got): set default branch via got and atomic HEAD
Use got ref -s when the target branch exists and write HEAD atomically for unborn branches, where got ref -s cannot operate. Route git.SetDefaultBranch through the got wrapper and cover both paths plus the no-bypass case in tests. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
This commit is contained in:
1 parent
34cfc1e12b
commit
d6bb7fb157
6 files changed
+207
-15
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`, and `GetDefaultBranch` now go through `modules/got`; the
|
||||
rest of the migration is open.
|
||||
creation, `IsEmpty`, `GetDefaultBranch`, and `SetDefaultBranch` 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,12 +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 init, empty-state, and default-branch
|
||||
reads 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, and default
|
||||
branch read/write 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.
|
||||
- `SetDefaultBranch` uses `got ref -s` for existing branches and writes HEAD
|
||||
atomically for unborn branches, because `got ref -s` requires the target to exist.
|
||||
- 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
|
||||
|
||||
@@ -130,10 +130,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`, 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.
|
||||
`IsEmpty`, `GetDefaultBranch`, and `SetDefaultBranch` 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.
|
||||
There is no silent git fallback for the migrated paths.
|
||||
|
||||
## Design notes
|
||||
|
||||
|
||||
@@ -34,11 +34,8 @@ 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)
|
||||
return err
|
||||
return got.SetDefaultBranch(ctx, gitrepo.RepoLocalPath(repo), name)
|
||||
}
|
||||
|
||||
// GetDefaultBranch gets default branch of repository.
|
||||
|
||||
@@ -97,3 +97,52 @@ func TestIsEmptyDoesNotFallBackToGit(t *testing.T) {
|
||||
t.Fatal("expected a got failure instead of a silent git fallback")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultBranchUsesGot(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()
|
||||
|
||||
if err := SetDefaultBranch(t.Context(), repo, "trunk"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
branch, err := GetDefaultBranch(t.Context(), repo)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if branch != "trunk" {
|
||||
t.Fatalf("unexpected default branch %q", branch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultBranchDoesNotFallBackToGit(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 := SetDefaultBranch(t.Context(), repo, "trunk"); err == nil {
|
||||
t.Fatal("expected a got failure instead of a silent git fallback")
|
||||
}
|
||||
}
|
||||
@@ -138,3 +138,67 @@ func DefaultBranch(ctx context.Context, path string) (string, error) {
|
||||
}
|
||||
return "", fmt.Errorf("HEAD not found")
|
||||
}
|
||||
|
||||
func validBranchName(name string) bool {
|
||||
if name == "" || name == "@" || strings.HasPrefix(name, "-") ||
|
||||
strings.HasPrefix(name, "/") || strings.HasSuffix(name, "/") ||
|
||||
strings.HasSuffix(name, ".") || strings.HasSuffix(name, ".lock") ||
|
||||
strings.Contains(name, "..") || strings.Contains(name, "//") ||
|
||||
strings.Contains(name, "@{") {
|
||||
return false
|
||||
}
|
||||
for _, r := range name {
|
||||
if r <= ' ' || r == 0x7f || r == '~' || r == '^' || r == ':' ||
|
||||
r == '?' || r == '*' || r == '[' || r == '\\' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func writeHead(path, branch string) error {
|
||||
tmp, err := os.CreateTemp(path, ".HEAD-")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := tmp.Name()
|
||||
defer os.Remove(name)
|
||||
if _, err := fmt.Fprintf(tmp, "ref: refs/heads/%s\n", branch); err != nil {
|
||||
tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Sync(); err != nil {
|
||||
tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Chmod(name, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(name, filepath.Join(path, "HEAD"))
|
||||
}
|
||||
|
||||
// SetDefaultBranch points HEAD at branch. got ref -s needs the target branch to
|
||||
// exist, so an unborn branch falls back to writing HEAD in the repository format.
|
||||
func SetDefaultBranch(ctx context.Context, path, branch string) error {
|
||||
if !validBranchName(branch) {
|
||||
return fmt.Errorf("invalid branch name: %s", branch)
|
||||
}
|
||||
if !IsRepository(path) {
|
||||
return fmt.Errorf("not a repository: %s", path)
|
||||
}
|
||||
out, _, err := run(ctx, "ref", "-l", "-r", path, "refs/heads/"+branch)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotInstalled) {
|
||||
return err
|
||||
}
|
||||
return writeHead(path, branch)
|
||||
}
|
||||
if strings.Contains(out, ": ") {
|
||||
_, _, err = run(ctx, "ref", "-s", "refs/heads/"+branch, "HEAD", "-r", path)
|
||||
return err
|
||||
}
|
||||
return writeHead(path, branch)
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gotea.dev/modules/setting"
|
||||
)
|
||||
|
||||
func requireGot(t *testing.T) {
|
||||
@@ -111,3 +113,82 @@ func TestDefaultBranch(t *testing.T) {
|
||||
t.Fatalf("unexpected branch %q", branch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultBranchUnborn(t *testing.T) {
|
||||
requireGot(t)
|
||||
repo := filepath.Join(t.TempDir(), "repo.git")
|
||||
if err := InitBare(t.Context(), repo, "sha1", "main"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SetDefaultBranch(t.Context(), repo, "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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultBranchExisting(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)
|
||||
}
|
||||
if _, _, err := run(t.Context(), "branch", "-r", repo, "dev"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SetDefaultBranch(t.Context(), repo, "dev"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
branch, err := DefaultBranch(t.Context(), repo)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if branch != "dev" {
|
||||
t.Fatalf("unexpected branch %q", branch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultBranchInvalid(t *testing.T) {
|
||||
if err := SetDefaultBranch(t.Context(), t.TempDir(), "bad..name"); err == nil {
|
||||
t.Fatal("expected an error for an invalid branch name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultBranchDoesNotBypassGot(t *testing.T) {
|
||||
requireGot(t)
|
||||
repo := filepath.Join(t.TempDir(), "repo.git")
|
||||
if err := InitBare(t.Context(), repo, "sha1", "main"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := setting.Git.GotPath
|
||||
setting.Git.GotPath = "gotea-test-got-missing"
|
||||
t.Cleanup(func() { setting.Git.GotPath = old })
|
||||
|
||||
if err := SetDefaultBranch(t.Context(), repo, "trunk"); err == nil {
|
||||
t.Fatal("expected a got failure instead of a direct HEAD write")
|
||||
}
|
||||
head, err := os.ReadFile(filepath.Join(repo, "HEAD"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.TrimSpace(string(head)) != "ref: refs/heads/main" {
|
||||
t.Fatalf("HEAD changed despite missing got: %q", head)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user