Merge commit '18aea4bfe89b83c5e2d6d55daa68efa6180655cc'

This commit is contained in:
Carlos Martín Nieto 2015-01-04 12:14:12 +00:00
commit dff9badc05
2 changed files with 54 additions and 0 deletions

40
diff.go
View File

@ -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

View File

@ -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")