GetFileContentFromCommit reads the content of a file at a specific commit using 'git show commit:path'. The filePath should be relative to the repository root.
(repoPath, commitID, filePath string)
| 358 | // GetFileContentFromCommit reads the content of a file at a specific commit |
| 359 | // using 'git show commit:path'. The filePath should be relative to the repository root. |
| 360 | func GetFileContentFromCommit(repoPath, commitID, filePath string) ([]byte, error) { |
| 361 | if err := validateGitRef(commitID); err != nil { |
| 362 | return nil, err |
| 363 | } |
| 364 | if err := validateGitRelPath(filePath); err != nil { |
| 365 | return nil, err |
| 366 | } |
| 367 | |
| 368 | // Format: commit:path |
| 369 | ref := fmt.Sprintf("%s:%s", commitID, filePath) |
| 370 | |
| 371 | stdout, stderr, err := runGitCommand(repoPath, "show", ref) |
| 372 | if err != nil { |
| 373 | if stderr != "" { |
| 374 | return nil, fmt.Errorf("git show failed: %s", stderr) |
| 375 | } |
| 376 | return nil, err |
| 377 | } |
| 378 | |
| 379 | return stdout, nil |
| 380 | } |
| 381 | |
| 382 | // GetCommitRangeFiles finds all files changed between two commits. |
| 383 | // Uses: git diff --name-only --diff-filter=d <from> <to> |