GetCommitRangeFiles finds all files changed between two commits. Uses: git diff --name-only --diff-filter=d Returns absolute paths to all files added, modified, or renamed between the commits.
(repoPath, fromCommit, toCommit string)
| 383 | // Uses: git diff --name-only --diff-filter=d <from> <to> |
| 384 | // Returns absolute paths to all files added, modified, or renamed between the commits. |
| 385 | func GetCommitRangeFiles(repoPath, fromCommit, toCommit string) ([]string, error) { |
| 386 | // Validate the repository path exists |
| 387 | if _, err := os.Stat(repoPath); os.IsNotExist(err) { |
| 388 | return nil, fmt.Errorf("repository path does not exist: %s", repoPath) |
| 389 | } |
| 390 | |
| 391 | // Verify it's a git repository |
| 392 | if !isGitRepository(repoPath) { |
| 393 | return nil, fmt.Errorf("%s is not a git repository (use 'git init' to initialize)", repoPath) |
| 394 | } |
| 395 | |
| 396 | // Validate both commits exist |
| 397 | if err := validateCommit(repoPath, fromCommit); err != nil { |
| 398 | return nil, err |
| 399 | } |
| 400 | if err := validateCommit(repoPath, toCommit); err != nil { |
| 401 | return nil, err |
| 402 | } |
| 403 | |
| 404 | // Get the repository root |
| 405 | repoRoot, err := GetRepositoryRoot(repoPath) |
| 406 | if err != nil { |
| 407 | return nil, fmt.Errorf("failed to get repository root: %w", err) |
| 408 | } |
| 409 | |
| 410 | // Get files changed between the two commits |
| 411 | // --diff-filter=d excludes deleted files (only include added, modified, and renamed files) |
| 412 | stdout, stderr, err := runGitCommand(repoPath, "diff", "--name-only", "--diff-filter=d", fromCommit, toCommit) |
| 413 | if err != nil { |
| 414 | return nil, gitCommandError(err, stderr) |
| 415 | } |
| 416 | |
| 417 | // Parse the output - one file per line |
| 418 | var files []string |
| 419 | lines := strings.Split(string(stdout), "\n") |
| 420 | for _, line := range lines { |
| 421 | line = strings.TrimSpace(line) |
| 422 | if line != "" { |
| 423 | files = append(files, line) |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // Convert to absolute paths |
| 428 | absolutePaths := toAbsolutePaths(repoRoot, files) |
| 429 | |
| 430 | return absolutePaths, nil |
| 431 | } |