write_file_test.go
·
156 строк
·
5.4 KB
| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // TestWriteFileCommit creates files (including nested paths) directly on a |
| 13 | // bare repository through git plumbing, without a working tree. |
| 14 | func TestWriteFileCommit(t *testing.T) { |
| 15 | ctx := context.Background() |
| 16 | tmp := t.TempDir() |
| 17 | |
| 18 | repoPath := filepath.Join(tmp, "repo.git") |
| 19 | if err := InitBare(ctx, repoPath, "main"); err != nil { |
| 20 | t.Fatalf("InitBare: %v", err) |
| 21 | } |
| 22 | |
| 23 | work := filepath.Join(tmp, "work") |
| 24 | if err := os.MkdirAll(work, 0o755); err != nil { |
| 25 | t.Fatalf("mkdir: %v", err) |
| 26 | } |
| 27 | run := func(dir string, args ...string) { |
| 28 | t.Helper() |
| 29 | cmd := exec.CommandContext(ctx, "git", args...) |
| 30 | cmd.Dir = dir |
| 31 | if out, err := cmd.CombinedOutput(); err != nil { |
| 32 | t.Fatalf("git %v: %v (%s)", args, err, string(out)) |
| 33 | } |
| 34 | } |
| 35 | run(work, "init", "-b", "main") |
| 36 | run(work, "config", "user.name", "Alice") |
| 37 | run(work, "config", "user.email", "alice@example.com") |
| 38 | if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("# Repo\n"), 0o644); err != nil { |
| 39 | t.Fatalf("write: %v", err) |
| 40 | } |
| 41 | run(work, "add", ".") |
| 42 | run(work, "commit", "-m", "initial") |
| 43 | run(work, "push", repoPath, "main") |
| 44 | |
| 45 | hash, err := WriteFileCommit(ctx, repoPath, "main", "docs/guide.md", []byte("hello\n"), "Add docs/guide.md", "Alice", "alice@example.com") |
| 46 | if err != nil { |
| 47 | t.Fatalf("WriteFileCommit: %v", err) |
| 48 | } |
| 49 | if hash == "" { |
| 50 | t.Fatalf("expected commit hash") |
| 51 | } |
| 52 | |
| 53 | blob, err := ReadBlob(ctx, repoPath, "main", "docs/guide.md") |
| 54 | if err != nil { |
| 55 | t.Fatalf("ReadBlob: %v", err) |
| 56 | } |
| 57 | if string(blob.Data) != "hello\n" { |
| 58 | t.Fatalf("unexpected blob content: %q", string(blob.Data)) |
| 59 | } |
| 60 | |
| 61 | out, err := runGit(ctx, repoPath, 10_000_000_000, "log", "-1", "--format=%s|%an|%ae", "main") |
| 62 | if err != nil { |
| 63 | t.Fatalf("log: %v", err) |
| 64 | } |
| 65 | line := strings.TrimSpace(string(out)) |
| 66 | if line != "Add docs/guide.md|Alice|alice@example.com" { |
| 67 | t.Fatalf("unexpected commit metadata: %s", line) |
| 68 | } |
| 69 | |
| 70 | // A second write replaces the file and keeps history. |
| 71 | if _, err := WriteFileCommit(ctx, repoPath, "main", "docs/guide.md", []byte("hello v2\n"), "Update docs/guide.md", "Bob", "bob@example.com"); err != nil { |
| 72 | t.Fatalf("second WriteFileCommit: %v", err) |
| 73 | } |
| 74 | if n := CountCommits(ctx, repoPath, "main"); n != 3 { |
| 75 | t.Fatalf("expected 3 commits, got %d", n) |
| 76 | } |
| 77 | blob, err = ReadBlob(ctx, repoPath, "main", "docs/guide.md") |
| 78 | if err != nil { |
| 79 | t.Fatalf("ReadBlob v2: %v", err) |
| 80 | } |
| 81 | if string(blob.Data) != "hello v2\n" { |
| 82 | t.Fatalf("unexpected blob content: %q", string(blob.Data)) |
| 83 | } |
| 84 | |
| 85 | // Invalid paths and refs must be rejected. |
| 86 | if _, err := WriteFileCommit(ctx, repoPath, "main", "../evil.txt", []byte("x"), "", "Alice", "alice@example.com"); err == nil { |
| 87 | t.Fatalf("expected invalid path to be rejected") |
| 88 | } |
| 89 | if _, err := WriteFileCommit(ctx, repoPath, "main", ".git/config", []byte("x"), "", "Alice", "alice@example.com"); err == nil { |
| 90 | t.Fatalf("expected .git path to be rejected") |
| 91 | } |
| 92 | if _, err := WriteFileCommit(ctx, repoPath, "no-such-branch", "file.txt", []byte("x"), "", "Alice", "alice@example.com"); err == nil { |
| 93 | t.Fatalf("expected missing branch to be rejected") |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // TestWriteFilesCommit commits several files (binary content included) in a |
| 98 | // single commit and rejects invalid batches. |
| 99 | func TestWriteFilesCommit(t *testing.T) { |
| 100 | ctx := context.Background() |
| 101 | tmp := t.TempDir() |
| 102 | |
| 103 | repoPath := filepath.Join(tmp, "repo.git") |
| 104 | if err := InitBare(ctx, repoPath, "main"); err != nil { |
| 105 | t.Fatalf("InitBare: %v", err) |
| 106 | } |
| 107 | if _, err := WriteFileCommit(ctx, repoPath, "main", "README.md", []byte("# Repo\n"), "Initial commit", "Alice", "alice@example.com"); err != nil { |
| 108 | t.Fatalf("seed commit: %v", err) |
| 109 | } |
| 110 | |
| 111 | binary := []byte{0x00, 0x01, 0xFF, 0xFE, 'p', 'o', 'l', 'a', 'r'} |
| 112 | hash, err := WriteFilesCommit(ctx, repoPath, "main", []FileToWrite{ |
| 113 | {Path: "assets/logo.bin", Content: binary}, |
| 114 | {Path: "docs/a.md", Content: []byte("A\n")}, |
| 115 | {Path: "docs/b.md", Content: []byte("B\n")}, |
| 116 | }, "Upload 3 files", "Bob", "bob@example.com") |
| 117 | if err != nil { |
| 118 | t.Fatalf("WriteFilesCommit: %v", err) |
| 119 | } |
| 120 | if hash == "" { |
| 121 | t.Fatalf("expected commit hash") |
| 122 | } |
| 123 | if n := CountCommits(ctx, repoPath, "main"); n != 2 { |
| 124 | t.Fatalf("expected 2 commits, got %d", n) |
| 125 | } |
| 126 | for path, want := range map[string][]byte{ |
| 127 | "assets/logo.bin": binary, |
| 128 | "docs/a.md": []byte("A\n"), |
| 129 | "docs/b.md": []byte("B\n"), |
| 130 | } { |
| 131 | blob, err := ReadBlob(ctx, repoPath, "main", path) |
| 132 | if err != nil { |
| 133 | t.Fatalf("ReadBlob %s: %v", path, err) |
| 134 | } |
| 135 | if string(blob.Data) != string(want) { |
| 136 | t.Fatalf("unexpected blob %s: %q", path, string(blob.Data)) |
| 137 | } |
| 138 | } |
| 139 | out, err := runGit(ctx, repoPath, 10_000_000_000, "log", "-1", "--format=%s|%an", "main") |
| 140 | if err != nil { |
| 141 | t.Fatalf("log: %v", err) |
| 142 | } |
| 143 | if line := strings.TrimSpace(string(out)); line != "Upload 3 files|Bob" { |
| 144 | t.Fatalf("unexpected commit metadata: %s", line) |
| 145 | } |
| 146 | |
| 147 | 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 { |
| 148 | t.Fatalf("expected duplicate paths to be rejected") |
| 149 | } |
| 150 | if _, err := WriteFilesCommit(ctx, repoPath, "main", []FileToWrite{{Path: "../evil.txt", Content: []byte("x")}}, "", "Alice", "alice@example.com"); err == nil { |
| 151 | t.Fatalf("expected invalid path to be rejected") |
| 152 | } |
| 153 | if _, err := WriteFilesCommit(ctx, repoPath, "main", nil, "", "Alice", "alice@example.com"); err == nil { |
| 154 | t.Fatalf("expected empty batch to be rejected") |
| 155 | } |
| 156 | } |
| 157 |