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
225 lines
5.7 KiB
Go
225 lines
5.7 KiB
Go
// Copyright 2026 The Gotea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package got
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gotea.dev/modules/setting"
|
|
)
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|