diff --git a/AGENTS.md b/AGENTS.md index beb7b96dd5..cb71e5b092 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,8 +11,8 @@ storage. - Base: upstream Gitea v28.4.0, full history, `upstream` remote kept. - 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`; the got migration is the main open - backend task. Do not claim it is finished. +- 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. - `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 @@ -86,6 +86,8 @@ make test-migration # after modelmigration changes ``` - Focused Go test: `go test -run '^TestName$' ./path/to/package/`. +- got tests: `go test ./modules/got`; init wiring: + `go test ./modules/git -run 'TestInitRepositoryLocalBare'`. - Raise `ulimit -n 12288` before `make watch`. - The backend binary is `gotea`; run it from the repository root with `./gotea web`. @@ -93,7 +95,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. + execs got or parses repository internals. Bare repo creation is already there; + non-bare temp worktrees still use git and must be migrated deliberately. +- `[git] GOT_PATH` selects the got binary (default `got`). - 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 @@ -149,6 +153,8 @@ make test-migration # after modelmigration changes - Backend: `routers/` (HTTP), `services/` (logic), `models/` + `modelmigration/` (DB), `modules/` (infrastructure), `templates/`. +- Backend VCS: `modules/got` for migrated got calls; `modules/git` is the + remaining git surface. - Frontend: `web_src/` (Vue + Vite) for the Gitea UI; `cgi/` for the HTML 4.01 frontend; `cgi/pages/` for publish/hook scripts. - Vendored: `third_party/bb` (MIT static site generator). diff --git a/README.md b/README.md index a847e4334a..cf81b4b13a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This tree is at the bootstrap milestone: | Layer | State | | --- | --- | -| Go backend | Imported and building as `gotea` (module `gotea.dev`). Still uses `git` internally; the got migration is in progress. | +| Go backend | Imported and building as `gotea` (module `gotea.dev`). Bare repository creation uses `got` via `modules/got`; most other operations still use `git`. | | Legacy frontend | `cgi/gotea-cgi`: read-only browser over `got`, HTML 4.01 Transitional, no JavaScript, CDE palette. | | Pages | Prototype: a `pages` branch is checked out with got, built with vendored `bb`, and served on a separate host. | | Auth and writes | Not yet implemented in the CGI frontend. | @@ -25,6 +25,7 @@ This tree is at the bootstrap milestone: ```sh make gotea # Go backend binary +go test ./modules/got # got wrapper and bare-init tests make build # full Gitea build; needs pnpm and Node make -C cgi # C99 CGI binary make -C cgi check # CGI + pages tests (git is used only for fixtures) @@ -126,6 +127,13 @@ bb is vendored under `third_party/bb` (MIT, upstream commit in `make bb-check` runs bb's own suite; it currently reports 20 upstream theme 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. + ## Design notes - HTML 4.01 Transitional, CSS1, table layout, no JavaScript, no external assets. diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 71aabeb67c..32135067f7 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -751,6 +751,10 @@ LEVEL = Info ;; The path of git executable. If empty, Gitea searches through the PATH environment. ;PATH = ;; +;; The path of the got executable used for repositories migrated to got. +;; If empty, Gotea searches through the PATH environment. +;GOT_PATH = got +;; ;; The HOME directory for Git, defaults to "{APP_DATA_PATH}/home" ;HOME_PATH = ;; diff --git a/modules/git/init_got_test.go b/modules/git/init_got_test.go new file mode 100644 index 0000000000..8ec3613685 --- /dev/null +++ b/modules/git/init_got_test.go @@ -0,0 +1,46 @@ +// Copyright 2026 The Gotea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package git + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "gotea.dev/modules/got" + "gotea.dev/modules/setting" +) + +func TestInitRepositoryLocalBareUsesGot(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 := InitRepositoryLocal(t.Context(), dir, true, "sha1"); err != nil { + t.Fatal(err) + } + if !got.IsRepository(dir) { + t.Fatalf("%s was not initialized by got", dir) + } + head, err := os.ReadFile(filepath.Join(dir, "HEAD")) + if err != nil { + t.Fatal(err) + } + if !strings.HasPrefix(string(head), "ref: refs/heads/") { + t.Fatalf("unexpected HEAD %q", head) + } +} + +func TestInitRepositoryLocalBareDoesNotFallBackToGit(t *testing.T) { + old := setting.Git.GotPath + setting.Git.GotPath = "gotea-test-got-missing" + t.Cleanup(func() { setting.Git.GotPath = old }) + + dir := filepath.Join(t.TempDir(), "repo.git") + if err := InitRepositoryLocal(t.Context(), dir, true, "sha1"); err == nil { + t.Fatal("expected a got failure instead of a silent git fallback") + } +} diff --git a/modules/git/repo.go b/modules/git/repo.go index 6e8c120d08..1dc855422c 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -18,6 +18,7 @@ import ( "gotea.dev/modules/cache" "gotea.dev/modules/git/gitcmd" "gotea.dev/modules/git/gitrepo" + "gotea.dev/modules/got" "gotea.dev/modules/setting" "gotea.dev/modules/util" ) @@ -124,17 +125,21 @@ func InitRepositoryLocal(ctx context.Context, localRepoPath string, bare bool, o return err } - cmd := gitcmd.NewCommand("init") - if !IsValidObjectFormat(objectFormatName) { return fmt.Errorf("invalid object format: %s", objectFormatName) } - if DefaultFeatures().SupportHashSha256 { - cmd.AddOptionValues("--object-format", objectFormatName) - } if bare { - cmd.AddArguments("--bare") + branch := setting.Repository.DefaultBranch + if branch == "" { + branch = "main" + } + return got.InitBare(ctx, localRepoPath, objectFormatName, branch) + } + + cmd := gitcmd.NewCommand("init") + if DefaultFeatures().SupportHashSha256 { + cmd.AddOptionValues("--object-format", objectFormatName) } _, _, err = cmd.WithDir(localRepoPath).RunStdString(ctx) return err diff --git a/modules/got/got.go b/modules/got/got.go new file mode 100644 index 0000000000..7068fe0953 --- /dev/null +++ b/modules/got/got.go @@ -0,0 +1,92 @@ +// Copyright 2026 The Gotea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +// Package got wraps the got(1) binary for repository operations that Gotea +// has migrated off git. Keep all got process handling in this package. +package got + +import ( + "context" + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + + "gotea.dev/modules/setting" +) + +// ErrNotInstalled means the configured got executable could not be started. +var ErrNotInstalled = errors.New("got executable not found") + +// Executable returns the configured got path, defaulting to "got". +func Executable() string { + path := strings.TrimSpace(setting.Git.GotPath) + if path == "" { + return "got" + } + return path +} + +func run(ctx context.Context, args ...string) (stdout, stderr string, err error) { + cmd := exec.CommandContext(ctx, Executable(), args...) + var out, errOut strings.Builder + cmd.Stdout = &out + cmd.Stderr = &errOut + err = cmd.Run() + if err != nil { + if errors.Is(err, exec.ErrNotFound) { + return out.String(), errOut.String(), ErrNotInstalled + } + return out.String(), errOut.String(), fmt.Errorf("got %s: %w: %s", + strings.Join(args, " "), err, strings.TrimSpace(errOut.String())) + } + return out.String(), errOut.String(), nil +} + +// Version returns the got version without the leading "got ". +func Version(ctx context.Context) (string, error) { + out, _, err := run(ctx, "--version") + if err != nil { + return "", err + } + fields := strings.Fields(strings.TrimSpace(out)) + if len(fields) < 2 || fields[0] != "got" { + return "", fmt.Errorf("unexpected got version output: %q", strings.TrimSpace(out)) + } + return fields[1], nil +} + +// IsRepository reports whether path looks like a bare Git-format repository. +func IsRepository(path string) bool { + if st, err := os.Stat(filepath.Join(path, "HEAD")); err != nil || !st.Mode().IsRegular() { + return false + } + if st, err := os.Stat(filepath.Join(path, "objects")); err != nil || !st.IsDir() { + return false + } + return true +} + +// InitBare creates a bare repository at path, or leaves an existing one alone. +// objectFormat must be "sha1" or "sha256". +func InitBare(ctx context.Context, path, objectFormat, defaultBranch string) error { + if objectFormat == "" { + objectFormat = "sha1" + } + if objectFormat != "sha1" && objectFormat != "sha256" { + return fmt.Errorf("invalid got object format: %s", objectFormat) + } + if IsRepository(path) { + return nil + } + if err := os.MkdirAll(path, 0o755); err != nil { + return fmt.Errorf("create repository directory: %w", err) + } + if defaultBranch == "" { + defaultBranch = "main" + } + _, _, err := run(ctx, "init", "-A", objectFormat, "-b", defaultBranch, path) + return err +} diff --git a/modules/got/got_test.go b/modules/got/got_test.go new file mode 100644 index 0000000000..ed3ba74f24 --- /dev/null +++ b/modules/got/got_test.go @@ -0,0 +1,63 @@ +// Copyright 2026 The Gotea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package got + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func requireGot(t *testing.T) { + t.Helper() + if _, err := Version(t.Context()); err != nil { + t.Skipf("got not available: %v", err) + } +} + +func TestVersion(t *testing.T) { + requireGot(t) + version, err := Version(t.Context()) + if err != nil { + t.Fatal(err) + } + if version == "" || strings.Contains(version, " ") { + t.Fatalf("unexpected version %q", version) + } +} + +func TestInitBare(t *testing.T) { + requireGot(t) + dir := filepath.Join(t.TempDir(), "repo.git") + if err := InitBare(t.Context(), dir, "sha1", "main"); err != nil { + t.Fatal(err) + } + if !IsRepository(dir) { + t.Fatalf("%s is not a repository", dir) + } + head, err := os.ReadFile(filepath.Join(dir, "HEAD")) + if err != nil { + t.Fatal(err) + } + if strings.TrimSpace(string(head)) != "ref: refs/heads/main" { + t.Fatalf("unexpected HEAD %q", head) + } + // A second init must not destroy an existing repository. + if err := InitBare(t.Context(), dir, "sha1", "main"); err != nil { + t.Fatalf("second init: %v", err) + } +} + +func TestInitBareInvalidFormat(t *testing.T) { + if err := InitBare(t.Context(), t.TempDir(), "md5", "main"); err == nil { + t.Fatal("expected an error for an invalid object format") + } +} + +func TestIsRepository(t *testing.T) { + if IsRepository(t.TempDir()) { + t.Fatal("empty directory reported as a repository") + } +} diff --git a/modules/setting/git.go b/modules/setting/git.go index 3bd61c0aed..5f974f770a 100644 --- a/modules/setting/git.go +++ b/modules/setting/git.go @@ -15,6 +15,7 @@ import ( // Git settings var Git = struct { Path string + GotPath string HomePath string DisableDiffHighlight bool @@ -38,6 +39,7 @@ var Git = struct { GC int `ini:"GC"` } `ini:"git.timeout"` }{ + GotPath: "got", DisableDiffHighlight: false, MaxGitDiffLines: 1000, MaxGitDiffLineCharacters: 5000,