Add got.Branches and route both GetBranchNames implementations through it, so branch listing no longer runs git for-each-ref. Names come from `got ref -l refs/heads/`, not `got branch -l`: the latter also reports refs/remotes copies and can print a name twice. Order comes from `got branch -lt`; names that pass misses are appended, so a failure there degrades to lexical order instead of failing the listing. Two got behaviours needed guarding: - `-r` searches upwards. A path that is not a repository made got answer for the enclosing one, so a broken fixture silently returned the parent repository's refs. run now validates the target first. - got refuses a repository without objects/pack or refs/. Those are empty directories, so a fixture checked into git loses them. run recreates the layout, content-free and idempotent. repo2_empty carried .gitkeep files under refs/heads and refs/tags; got reads them as references and fails with "bad reference name". Dropped both, the layout is now created on demand. Tests: go test ./modules/got ./modules/git ./modules/git -tags gogit ./services/repository/... ./modules/repository/... ./models/git/... ./routers/web/repo/... ./services/context/... - all pass. New tests cover ordering, paging, remote-copy filtering, the missing-directory repair, the enclosing-repository refusal, and absence of a git fallback. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
185 lines
5.4 KiB
Go
185 lines
5.4 KiB
Go
// Copyright 2026 The Gotea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package got
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"gotea.dev/modules/setting"
|
|
)
|
|
|
|
// importTree commits the contents of dir onto branch with got import.
|
|
func importTree(t *testing.T, repo, branch, message, dir string) {
|
|
t.Helper()
|
|
if _, _, err := run(t.Context(), "import", "-r", repo, "-b", branch, "-m", message, dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func writeFile(t *testing.T, dir, name, content string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestBranchesEmpty(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)
|
|
}
|
|
branches, total, err := Branches(t.Context(), repo, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(branches) != 0 || total != 0 {
|
|
t.Fatalf("unexpected branches %v (total %d)", branches, total)
|
|
}
|
|
}
|
|
|
|
func TestBranchesOrderingAndPaging(t *testing.T) {
|
|
requireGot(t)
|
|
t.Setenv("GOT_AUTHOR", "Test User <test@example.com>")
|
|
dir := t.TempDir()
|
|
repo := filepath.Join(dir, "repo.git")
|
|
if err := InitBare(t.Context(), repo, "sha1", "main"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// got branch -lt sorts by commit date, and got stamps commits with the
|
|
// wall clock, so each branch needs its own second to have a defined order.
|
|
src := filepath.Join(dir, "src")
|
|
for _, step := range []struct{ branch, msg string }{
|
|
{"main", "one"}, {"dev", "two"}, {"feature", "three"},
|
|
} {
|
|
writeFile(t, src, step.msg+".txt", step.msg+"\n")
|
|
importTree(t, repo, step.branch, step.msg, src)
|
|
time.Sleep(1100 * time.Millisecond)
|
|
}
|
|
|
|
branches, total, err := Branches(t.Context(), repo, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if total != 3 || len(branches) != 3 {
|
|
t.Fatalf("unexpected branches %v (total %d)", branches, total)
|
|
}
|
|
if want := []string{"feature", "dev", "main"}; !slices.Equal(branches, want) {
|
|
t.Fatalf("branches are not newest first: got %v, want %v", branches, want)
|
|
}
|
|
|
|
page, total, err := Branches(t.Context(), repo, 1, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if total != 3 || len(page) != 1 || page[0] != "dev" {
|
|
t.Fatalf("unexpected page %v (total %d)", page, total)
|
|
}
|
|
|
|
beyond, total, err := Branches(t.Context(), repo, 5, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if total != 3 || len(beyond) != 0 {
|
|
t.Fatalf("unexpected page beyond the end: %v (total %d)", beyond, total)
|
|
}
|
|
}
|
|
|
|
func TestBranchesIgnoresRemoteCopies(t *testing.T) {
|
|
requireGot(t)
|
|
t.Setenv("GOT_AUTHOR", "Test User <test@example.com>")
|
|
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")
|
|
writeFile(t, src, "one.txt", "one\n")
|
|
importTree(t, repo, "main", "one", src)
|
|
|
|
// got branch -l would also report origin/main, got ref -l refs/heads/ does not.
|
|
if _, _, err := run(t.Context(), "ref", "-s", "refs/heads/main", "refs/remotes/origin/main", "-r", repo); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
branches, total, err := Branches(t.Context(), repo, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if total != 1 || len(branches) != 1 || branches[0] != "main" {
|
|
t.Fatalf("unexpected branches %v (total %d)", branches, total)
|
|
}
|
|
}
|
|
|
|
func TestBranchesRepairsMissingPackDirectory(t *testing.T) {
|
|
requireGot(t)
|
|
t.Setenv("GOT_AUTHOR", "Test User <test@example.com>")
|
|
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")
|
|
writeFile(t, src, "one.txt", "one\n")
|
|
importTree(t, repo, "main", "one", src)
|
|
|
|
// A fixture copied through git loses empty directories, and got then
|
|
// refuses the repository until the layout is repaired.
|
|
if err := os.RemoveAll(filepath.Join(repo, "objects", "pack")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
branches, _, err := Branches(t.Context(), repo, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(branches) != 1 || branches[0] != "main" {
|
|
t.Fatalf("unexpected branches %v", branches)
|
|
}
|
|
}
|
|
|
|
func TestBranchesDoesNotFallBackToGit(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 := Branches(t.Context(), repo, 0, 0); err == nil {
|
|
t.Fatal("expected a got failure instead of a silent git fallback")
|
|
}
|
|
}
|
|
|
|
func TestBranchesRefusesEnclosingRepository(t *testing.T) {
|
|
requireGot(t)
|
|
t.Setenv("GOT_AUTHOR", "Test User <test@example.com>")
|
|
dir := t.TempDir()
|
|
outer := filepath.Join(dir, "outer.git")
|
|
if err := InitBare(t.Context(), outer, "sha1", "main"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
src := filepath.Join(dir, "src")
|
|
writeFile(t, src, "one.txt", "one\n")
|
|
importTree(t, outer, "main", "one", src)
|
|
|
|
// A directory got cannot use as a repository must not answer with the
|
|
// enclosing one, which is what an upwards search would do.
|
|
inner := filepath.Join(outer, "not-a-repository")
|
|
if err := os.Mkdir(inner, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := Branches(t.Context(), inner, 0, 0); err == nil {
|
|
t.Fatal("expected an error instead of resolving to the enclosing repository")
|
|
}
|
|
}
|