diff --git a/diff.go b/diff.go index 63f3453..63fa867 100644 --- a/diff.go +++ b/diff.go @@ -190,6 +190,46 @@ func (diff *Diff) FindSimilar(opts *DiffFindOptions) error { return nil } +type DiffStats struct { + ptr *C.git_diff_stats +} + +func (stats *DiffStats) Free() error { + if stats.ptr == nil { + return ErrInvalid + } + runtime.SetFinalizer(stats, nil) + C.git_diff_stats_free(stats.ptr) + stats.ptr = nil + return nil +} + +func (stats *DiffStats) Insertions() int { + return int(C.git_diff_stats_insertions(stats.ptr)) +} + +func (stats *DiffStats) Deletions() int { + return int(C.git_diff_stats_deletions(stats.ptr)) +} + +func (stats *DiffStats) FilesChanged() int { + return int(C.git_diff_stats_files_changed(stats.ptr)) +} + +func (diff *Diff) Stats() (*DiffStats, error) { + stats := new(DiffStats) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if ecode := C.git_diff_get_stats(&stats.ptr, diff.ptr); ecode < 0 { + return nil, MakeGitError(ecode) + } + runtime.SetFinalizer(stats, (*DiffStats).Free) + + return stats, nil +} + type diffForEachData struct { FileCallback DiffForEachFileCallback HunkCallback DiffForEachHunkCallback diff --git a/diff_test.go b/diff_test.go index 7c54f4e..fc6fed9 100644 --- a/diff_test.go +++ b/diff_test.go @@ -148,6 +148,20 @@ func TestDiffTreeToTree(t *testing.T) { if want1, want2 := "x1/README", "y1/README"; !strings.Contains(patches[0], want1) || !strings.Contains(patches[0], want2) { t.Errorf("Diff patch doesn't contain %q or %q\n\n%s", want1, want2, patches[0]) + + } + + stats, err := diff.Stats() + checkFatal(t, err) + + if stats.Insertions() != 1 { + t.Fatal("Incorrect number of insertions in diff") + } + if stats.Deletions() != 1 { + t.Fatal("Incorrect number of deletions in diff") + } + if stats.FilesChanged() != 1 { + t.Fatal("Incorrect number of changed files in diff") } errTest := errors.New("test error")