-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff.go
92 lines (81 loc) · 1.94 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package easyrepo
import (
//import git "gopkg.in/libgit2/git2go.v26"
git "gopkg.in/libgit2/git2go.v25"
)
type DiffResponse struct {
Body string
}
func DiffCommits(repo *git.Repository, ours, theirs string) (*DiffResponse, error) {
ourCommit, err := GetCommit(repo, ours)
if err != nil {
return nil, err
}
ourTree, err := ourCommit.Tree()
if err != nil {
return nil, err
}
theirCommit, err := GetCommit(repo, theirs)
if err != nil {
return nil, err
}
theirTree, err := theirCommit.Tree()
if err != nil {
return nil, err
}
//tree, err := repo.LookupTree(treeId)
//if err != nil {
//return nil, err
//}
//defer tree.Free()
callbackInvoked := false
opts := git.DiffOptions{
NotifyCallback: func(diffSoFar *git.Diff, delta git.DiffDelta, matchedPathSpec string) error {
callbackInvoked = true
return nil
},
OldPrefix: "x1/",
NewPrefix: "y1/",
}
diff, err := repo.DiffTreeToTree(ourTree, theirTree, &opts)
if err != nil {
return nil, err
}
//if !callbackInvoked {
//return nil, fmt.Errorf("callback not invoked")
//}
if diff == nil {
return nil, nil
}
files := make([]string, 0)
hunks := make([]git.DiffHunk, 0)
lines := make([]git.DiffLine, 0)
patches := make([]string, 0)
err = diff.ForEach(func(file git.DiffDelta, progress float64) (git.DiffForEachHunkCallback, error) {
patch, err := diff.Patch(len(patches))
if err != nil {
return nil, err
}
defer patch.Free()
patchStr, err := patch.String()
if err != nil {
return nil, err
}
patches = append(patches, patchStr)
files = append(files, file.OldFile.Path)
return func(hunk git.DiffHunk) (git.DiffForEachLineCallback, error) {
hunks = append(hunks, hunk)
return func(line git.DiffLine) error {
lines = append(lines, line)
return nil
}, nil
}, nil
}, git.DiffDetailLines)
if err != nil {
return nil, err
}
if len(patches) == 0 {
return &DiffResponse{Body: ""}, nil
}
return &DiffResponse{Body: patches[0]}, nil
}