forked from speedata/gogit
-
Notifications
You must be signed in to change notification settings - Fork 32
/
repo_history_handlers.go
125 lines (99 loc) · 2.4 KB
/
repo_history_handlers.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package git
import (
"regexp"
)
func commitRootComparator(current, parent *Commit) bool {
return current.TreeId().Equal(parent.TreeId())
}
func nopComparator(current, parent *Commit) bool {
return false
}
func makePathComparator(path string) CommitComparator {
return func(current, parent *Commit) bool {
centry, cerr := current.GetTreeEntryByPath(path)
pentry, perr := parent.GetTreeEntryByPath(path)
if cerr != nil || perr != nil {
return cerr == ErrNotExist && perr == ErrNotExist
}
return centry.Id.Equal(pentry.Id)
}
}
func makePathChecker(path string) (cb CommitWalkCallback) {
return func(commit *Commit) (HistoryWalkerAction, error) {
_, err := commit.GetTreeEntryByPath(path)
if err != nil {
if err == ErrNotExist {
return HWFollowParents, nil
} else {
return HWStop, err
}
}
return HWTakeAndFollow, nil
}
}
func makeHistorySearcher(needle string) (cb CommitWalkCallback, err error) {
matcher, err := regexp.Compile(needle)
if err != nil {
return nil, err
}
return func(commit *Commit) (HistoryWalkerAction, error) {
if matcher.MatchString(commit.CommitMessage) {
return HWTakeAndFollow, nil
}
return HWFollowParents, nil
}, nil
}
func nopCallback(*Commit) (HistoryWalkerAction, error) {
return HWTakeAndFollow, nil
}
func makePager(cb CommitWalkCallback, skip int, count int) CommitWalkCallback {
if cb == nil {
cb = nopCallback
}
pagerCallback := func(commit *Commit) (HistoryWalkerAction, error) {
pagerAction, err := cb(commit)
if err != nil {
return pagerAction, err
}
// if checker does not want to pick this commit, pager does not want either
if pagerAction&HWTakeCommit == 0 {
return pagerAction, nil
}
if skip != 0 {
skip--
action := pagerAction &^ HWTakeCommit
return action, nil
}
if count != 0 {
count--
// this is last element we want to take
if count == 0 {
pagerAction |= HWStop
}
return pagerAction, nil
}
return HWStop, nil
}
return pagerCallback
}
func makeCounter(cb CommitWalkCallback) (CommitWalkCallback, func() int) {
count := 0
if cb == nil {
cb = nopCallback
}
callback := func(commit *Commit) (HistoryWalkerAction, error) {
action, err := cb(commit)
if err != nil {
return action, err
}
if action&HWTakeCommit > 0 {
count++
action = action &^ HWTakeCommit
}
return action, nil
}
getter := func() int {
return count
}
return callback, getter
}