feat: Add route handler for file-specific commit diffs
1 file changed, 55 insertions(+), 0 deletions(-)
changed files
M git/diff.go → git/diff.go
@@ -45,6 +45,61 @@ Stat Stat Diff []Diff } +// DiffFile returns a diff for a specific file in the current commit +func (g *GitRepo) DiffFile(filePath string) (*NiceDiff, error) { + niceDiff, err := g.Diff() + if err != nil { + return nil, err + } + + // Filter diffs to only include the specified file + var filteredDiffs []Diff + for _, d := range niceDiff.Diff { + if d.Name.New == filePath || d.Name.Old == filePath { + filteredDiffs = append(filteredDiffs, d) + } + } + + // If no matching files were found, return an empty diff + if len(filteredDiffs) == 0 { + return &NiceDiff{ + Commit: niceDiff.Commit, + Stat: Stat{ + FilesChanged: 0, + Insertions: 0, + Deletions: 0, + }, + Diff: []Diff{}, + }, nil + } + + // Recalculate stats for the filtered diffs + stat := Stat{ + FilesChanged: len(filteredDiffs), + Insertions: 0, + Deletions: 0, + } + + for _, d := range filteredDiffs { + for _, tf := range d.TextFragments { + for _, l := range tf.Lines { + switch l.Op { + case gitdiff.OpAdd: + stat.Insertions++ + case gitdiff.OpDelete: + stat.Deletions++ + } + } + } + } + + return &NiceDiff{ + Commit: niceDiff.Commit, + Stat: stat, + Diff: filteredDiffs, + }, nil +} + func (g *GitRepo) Diff() (*NiceDiff, error) { c, err := g.r.CommitObject(g.h) if err != nil {