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") } } // TestWriteFilesCommit commits several files (binary content included) in a // single commit and rejects invalid batches. func TestWriteFilesCommit(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) } if _, err := WriteFileCommit(ctx, repoPath, "main", "README.md", []byte("# Repo\n"), "Initial commit", "Alice", "alice@example.com"); err != nil { t.Fatalf("seed commit: %v", err) } binary := []byte{0x00, 0x01, 0xFF, 0xFE, 'p', 'o', 'l', 'a', 'r'} hash, err := WriteFilesCommit(ctx, repoPath, "main", []FileToWrite{ {Path: "assets/logo.bin", Content: binary}, {Path: "docs/a.md", Content: []byte("A\n")}, {Path: "docs/b.md", Content: []byte("B\n")}, }, "Upload 3 files", "Bob", "bob@example.com") if err != nil { t.Fatalf("WriteFilesCommit: %v", err) } if hash == "" { t.Fatalf("expected commit hash") } if n := CountCommits(ctx, repoPath, "main"); n != 2 { t.Fatalf("expected 2 commits, got %d", n) } for path, want := range map[string][]byte{ "assets/logo.bin": binary, "docs/a.md": []byte("A\n"), "docs/b.md": []byte("B\n"), } { blob, err := ReadBlob(ctx, repoPath, "main", path) if err != nil { t.Fatalf("ReadBlob %s: %v", path, err) } if string(blob.Data) != string(want) { t.Fatalf("unexpected blob %s: %q", path, string(blob.Data)) } } out, err := runGit(ctx, repoPath, 10_000_000_000, "log", "-1", "--format=%s|%an", "main") if err != nil { t.Fatalf("log: %v", err) } if line := strings.TrimSpace(string(out)); line != "Upload 3 files|Bob" { t.Fatalf("unexpected commit metadata: %s", line) } if _, err := WriteFilesCommit(ctx, repoPath, "main", []FileToWrite{{Path: "a.txt", Content: []byte("1")}, {Path: "a.txt", Content: []byte("2")}}, "", "Alice", "alice@example.com"); err == nil { t.Fatalf("expected duplicate paths to be rejected") } if _, err := WriteFilesCommit(ctx, repoPath, "main", []FileToWrite{{Path: "../evil.txt", Content: []byte("x")}}, "", "Alice", "alice@example.com"); err == nil { t.Fatalf("expected invalid path to be rejected") } if _, err := WriteFilesCommit(ctx, repoPath, "main", nil, "", "Alice", "alice@example.com"); err == nil { t.Fatalf("expected empty batch to be rejected") } }