-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
149 lines (124 loc) · 3.68 KB
/
stats.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package codegraph
import (
"context"
"fmt"
"github.com/mloncode/codegraph/git"
"sort"
"strings"
"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/path"
"github.com/cayleygraph/cayley/quad"
)
type (
// CommitStats contains commit statistics
CommitStats struct {
Hash string // commit hash
Label string // metadata
NumParents int // number of parents
NumFiles int // number of files
NumAdded int // number of added files by this commit
NumRemoved int // number of removed files by this commit
NumModified int // number of modified files by this commit
}
// SortBy is a function to sort commit statistics
SortBy func(cs1, cs2 *CommitStats) bool
commitStatsSorter struct {
stats []*CommitStats
by SortBy
}
)
// PrintStats prints commit statistics
func (g *Graph) PrintStats(ctx context.Context, limit int, by SortBy, nomerge bool) error {
it, _ := cayley.StartPath(g.store, git.TypeRepo).In(git.PredType).BuildIterator().Optimize()
it, _ = g.store.OptimizeIterator(it)
defer it.Close()
for it.Next(ctx) {
repo := g.store.NameOf(it.Result())
if err := printStats(ctx, g.store, repo, limit, by, nomerge); err != nil {
return err
}
}
return nil
}
func printStats(ctx context.Context, qs graph.QuadStore, repo quad.Value, limit int, by SortBy, nomerge bool) error {
var stats []*CommitStats
it, _ := cayley.StartPath(qs, repo).Out(git.PredCommit).BuildIterator().Optimize()
it, _ = qs.OptimizeIterator(it)
for it.Next(ctx) {
commit := qs.NameOf(it.Result())
stats = append(stats, commitStats(ctx, qs, commit))
}
fmt.Printf("\n%s\n", repo.String())
by.Sort(stats)
n := 0
for _, s := range stats {
if limit > 0 && n >= limit {
break
}
if s.NumParents > 1 && nomerge {
continue
}
fmt.Printf("--\ncommit: %s", s.Hash)
if s.NumParents > 1 {
fmt.Print(" (merge)")
}
fmt.Printf("\n%s\n", strings.ReplaceAll(s.Label, `\n`, "\n"))
touch := s.NumAdded + s.NumRemoved + s.NumModified
fmt.Printf("%d files, %d touched (+, -, #), %d added(+), %d removed(-), %d modified(#)\n", s.NumFiles, touch, s.NumAdded, s.NumRemoved, s.NumModified)
n++
}
return it.Close()
}
func commitStats(ctx context.Context, qs graph.QuadStore, commit quad.Value) *CommitStats {
cs := &CommitStats{
Hash: commit.String(),
}
it, _ := path.StartPath(qs, commit).Out(git.PredMetadata).BuildIterator().Optimize()
it, _ = qs.OptimizeIterator(it)
if it.Next(ctx) {
if lbl := qs.NameOf(it.Result()); lbl != nil {
cs.Label = lbl.String()
}
}
it.Close()
path := cayley.StartPath(qs, commit)
cs.NumParents = countPaths(ctx, qs, path.Out(git.PredParent))
cs.NumFiles = countPaths(ctx, qs, path.Out(git.PredFile))
cs.NumAdded = countPaths(ctx, qs, path.In(git.PredAdd))
cs.NumRemoved = countPaths(ctx, qs, path.In(git.PredRemove))
cs.NumModified = countPaths(ctx, qs, path.In(git.PredModify))
return cs
}
func countPaths(ctx context.Context, qs graph.QuadStore, path *path.Path) int {
n := 0
it, _ := path.BuildIterator().Optimize()
it, _ = qs.OptimizeIterator(it)
for it.Next(ctx) {
n++
for it.NextPath(ctx) {
n++
}
}
it.Close()
return n
}
// Len is part of sort.Interface.
func (cs *commitStatsSorter) Len() int {
return len(cs.stats)
}
// Swap is part of sort.Interface.
func (cs *commitStatsSorter) Swap(i, j int) {
cs.stats[i], cs.stats[j] = cs.stats[j], cs.stats[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (cs *commitStatsSorter) Less(i, j int) bool {
return cs.by(cs.stats[i], cs.stats[j])
}
//Sort sorts
func (by SortBy) Sort(stats []*CommitStats) {
sort.Sort(&commitStatsSorter{
stats: stats,
by: by,
})
}