GetCommitFileDiffs returns per-line additions and deletions for files modified in a single commit, computed against that commit's first parent. Line numbers in Additions reference the commit's post-image; line numbers in Deletions reference the parent's content.
(repoPath, commitID string)
| 71 | // Line numbers in Additions reference the commit's post-image; line numbers |
| 72 | // in Deletions reference the parent's content. |
| 73 | func GetCommitFileDiffs(repoPath, commitID string) (map[string]vcs.FileDiff, error) { |
| 74 | if !isGitRepository(repoPath) { |
| 75 | return nil, fmt.Errorf("%s is not a git repository", repoPath) |
| 76 | } |
| 77 | repoRoot, err := GetRepositoryRoot(repoPath) |
| 78 | if err != nil { |
| 79 | return nil, fmt.Errorf("failed to get repository root: %w", err) |
| 80 | } |
| 81 | |
| 82 | // `git diff <commit>~ <commit>` is undefined for the root commit; fall back |
| 83 | // to `--root <commit>` which diffs against the empty tree. |
| 84 | stdout, _, err := runGitCommand(repoPath, "diff", "--unified=0", "--no-color", commitID+"~", commitID) |
| 85 | if err != nil { |
| 86 | // Root commit has no parent; fall back to diff-tree --root which emits |
| 87 | // a synthetic diff against the empty tree. |
| 88 | var stderr string |
| 89 | stdout, stderr, err = runGitCommand(repoPath, "diff-tree", "--root", "--unified=0", "--no-color", "-p", commitID) |
| 90 | if err != nil { |
| 91 | return nil, gitCommandError(err, stderr) |
| 92 | } |
| 93 | } |
| 94 | return absolutize(repoRoot, parseUnifiedDiff(string(stdout))), nil |
| 95 | } |
| 96 | |
| 97 | // GetCommitRangeFileDiffs returns per-line additions and deletions for files |
| 98 | // changed between two commits. Inputs are the same as `git diff from..to`. |