package git import ( "context" "os" "os/exec" "path/filepath" "strings" "testing" ) // TestWriteFileCommit creates files (including nested paths) directly on a // bare repository through git plumbing, without a working tree. func TestWriteFileCommit(t *testing.T) { ctx := context.Background() tmp := t.TempDir() repoPath := filepath.Join(tmp, "repo.git") if err := InitBare(ctx, repoPath, "main"); err != nil { t.Fatalf("InitBare: %v", err) } work := filepath.Join(tmp, "work") if err := os.MkdirAll(work, 0o755); err != nil { t.Fatalf("mkdir: %v", err) } run := func(dir string, args ...string) { t.Helper() cmd := exec.CommandContext(ctx, "git", args...) cmd.Dir = dir if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("git %v: %v (%s)", args, err, string(out)) } } run(work, "init", "-b", "main") run(work, "config", "user.name", "Alice") run(work, "config", "user.email", "alice@example.com") if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("# Repo\n"), 0o644); err != nil { t.Fatalf("write: %v", err) } run(work, "add", ".") run(work, "commit", "-m", "initial") run(work, "push", repoPath, "main") hash, err := WriteFileCommit(ctx, repoPath, "main", "docs/guide.md", []byte("hello\n"), "Add docs/guide.md", "Alice", "alice@example.com") if err != nil { t.Fatalf("WriteFileCommit: %v", err) } if hash == "" { t.Fatalf("expected commit hash") } blob, err := ReadBlob(ctx, repoPath, "main", "docs/guide.md") if err != nil { t.Fatalf("ReadBlob: %v", err) } if string(blob.Data) != "hello\n" { t.Fatalf("unexpected blob content: %q", string(blob.Data)) } out, err := runGit(ctx, repoPath, 10_000_000_000, "log", "-1", "--format=%s|%an|%ae", "main") if err != nil { t.Fatalf("log: %v", err) } line := strings.TrimSpace(string(out)) if line != "Add docs/guide.md|Alice|alice@example.com" { t.Fatalf("unexpected commit metadata: %s", line) } // A second write replaces the file and keeps history. if _, err := WriteFileCommit(ctx, repoPath, "main", "docs/guide.md", []byte("hello v2\n"), "Update docs/guide.md", "Bob", "bob@example.com"); err != nil { t.Fatalf("second WriteFileCommit: %v", err) } if n := CountCommits(ctx, repoPath, "main"); n != 3 { t.Fatalf("expected 3 commits, got %d", n) } blob, err = ReadBlob(ctx, repoPath, "main", "docs/guide.md") if err != nil { t.Fatalf("ReadBlob v2: %v", err) } if string(blob.Data) != "hello v2\n" { t.Fatalf("unexpected blob content: %q", string(blob.Data)) } // Invalid paths and refs must be rejected. if _, err := WriteFileCommit(ctx, repoPath, "main", "../evil.txt", []byte("x"), "", "Alice", "alice@example.com"); err == nil { t.Fatalf("expected invalid path to be rejected") } if _, err := WriteFileCommit(ctx, repoPath, "main", ".git/config", []byte("x"), "", "Alice", "alice@example.com"); err == nil { t.Fatalf("expected .git path to be rejected") } if _, err := WriteFileCommit(ctx, repoPath, "no-such-branch", "file.txt", []byte("x"), "", "Alice", "alice@example.com"); err == nil { t.Fatalf("expected missing branch to be rejected") } }