all repos — elgit @ 59ba5ba7dd66f7bf537d1f17a9d588acce6b5e16

fork of legit: web frontend for git, written in go

feat: Add route handler for file-specific commit diffs

Alan Pearce
commit

59ba5ba7dd66f7bf537d1f17a9d588acce6b5e16

parent

13e02db54aaa6d9fc5198a884e6dd8b4bee7e13f

1 file changed, 55 insertions(+), 0 deletions(-)

changed files
M git/diff.gogit/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 {