| 236 | } |
| 237 | |
| 238 | func TestCommitAll(t *testing.T) { |
| 239 | t.Run("returns ErrEmptyCommit when there are no changes", func(t *testing.T) { |
| 240 | tempDir := setupTestRepository(t) |
| 241 | |
| 242 | _, err := CommitAll(context.Background(), tempDir, "", "noop", "Rill", "noreply@rilldata.com") |
| 243 | require.ErrorIs(t, err, ErrEmptyCommit) |
| 244 | }) |
| 245 | |
| 246 | t.Run("returns ErrEmptyCommit when changes exist but fall outside the pathspec", func(t *testing.T) { |
| 247 | tempDir := setupTestRepository(t) |
| 248 | |
| 249 | // Seed the pathspec directory with a committed file so the pathspec resolves to a real path. |
| 250 | require.NoError(t, os.MkdirAll(filepath.Join(tempDir, "sub"), 0755)) |
| 251 | require.NoError(t, os.WriteFile(filepath.Join(tempDir, "sub", "seed.txt"), []byte("seed"), 0644)) |
| 252 | require.NoError(t, execGit(tempDir, "add", "sub/seed.txt")) |
| 253 | require.NoError(t, execGit(tempDir, "commit", "-m", "seed")) |
| 254 | |
| 255 | // Introduce a change *outside* the pathspec. |
| 256 | require.NoError(t, os.WriteFile(filepath.Join(tempDir, "outside.txt"), []byte("outside"), 0644)) |
| 257 | |
| 258 | _, err := CommitAll(context.Background(), tempDir, "sub", "noop", "Rill", "noreply@rilldata.com") |
| 259 | require.ErrorIs(t, err, ErrEmptyCommit) |
| 260 | |
| 261 | // The outside file must not have been committed. |
| 262 | out, err := Run(context.Background(), tempDir, "status", "--porcelain") |
| 263 | require.NoError(t, err) |
| 264 | require.Contains(t, out, "outside.txt") |
| 265 | }) |
| 266 | |
| 267 | t.Run("only commits files matching the pathspec", func(t *testing.T) { |
| 268 | tempDir := setupTestRepository(t) |
| 269 | |
| 270 | require.NoError(t, os.MkdirAll(filepath.Join(tempDir, "sub"), 0755)) |
| 271 | require.NoError(t, os.WriteFile(filepath.Join(tempDir, "sub", "inside.txt"), []byte("inside"), 0644)) |
| 272 | require.NoError(t, os.WriteFile(filepath.Join(tempDir, "outside.txt"), []byte("outside"), 0644)) |
| 273 | |
| 274 | hash, err := CommitAll(context.Background(), tempDir, "sub", "scoped commit", "Rill", "noreply@rilldata.com") |
| 275 | require.NoError(t, err) |
| 276 | require.NotEmpty(t, hash) |
| 277 | |
| 278 | // The outside file should still be untracked (not committed). |
| 279 | out, err := Run(context.Background(), tempDir, "status", "--porcelain") |
| 280 | require.NoError(t, err) |
| 281 | require.Contains(t, out, "outside.txt") |
| 282 | require.NotContains(t, out, "inside.txt") |
| 283 | |
| 284 | // The commit should include only the inside file. |
| 285 | filesOut, err := Run(context.Background(), tempDir, "show", "--name-only", "--pretty=format:", "HEAD") |
| 286 | require.NoError(t, err) |
| 287 | require.Contains(t, filesOut, "sub/inside.txt") |
| 288 | require.NotContains(t, filesOut, "outside.txt") |
| 289 | }) |
| 290 | |
| 291 | t.Run("uses the provided author name and email", func(t *testing.T) { |
| 292 | tempDir := setupTestRepository(t) |
| 293 | |
| 294 | require.NoError(t, os.WriteFile(filepath.Join(tempDir, "new.txt"), []byte("hello"), 0644)) |
| 295 | |