-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
253 lines (230 loc) · 7.43 KB
/
main.go
File metadata and controls
253 lines (230 loc) · 7.43 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// git-ownership: visualise % of code per author over every commit in a repo.
//
// Strategy: replay git history incrementally.
// - Fetch all commit hashes, split into N chunks.
// - Run N parallel git log -p workers; each pre-parses its chunk into
// lightweight diffEvent structs.
// - Main goroutine applies chunks in order to the RLE ownership state.
// - Record a snapshot of running totals every stride commits.
//
// Cost: O(total lines changed) across N parallel git processes.
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
"time"
)
func main() {
branchFlag := flag.String("branch", "HEAD", "branch/ref to analyse")
outputFlag := flag.String("output", "", "output HTML file (default: <reponame>.html)")
maxPtsFlag := flag.Int("max-points", 1000,
"max chart data points — commits are strided to fit; 0 = record every commit")
maxGraphFlag := flag.Int("max-graph", 50,
"max authors included as individual chart datasets; 0 = all")
folderFlag := flag.Int("folder", 10,
"number of largest folders to break down (searched at any depth); 0 = whole project only")
workersFlag := flag.Int("workers", runtime.NumCPU(),
"parallel git log workers (default: number of CPUs)")
excludeRegexFlag := flag.String("exclude-regex", "",
"exclude file paths matching this regex (e.g. ^vendor/)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: git-ownership [flags] <repo-path>\n\nFlags:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
Examples:
git-ownership .
git-ownership --branch main /path/to/repo
git-ownership --output graph.html --max-points 0 .
git-ownership --exclude-regex '^vendor/' /path/to/repo
`)
}
flag.Parse()
repo := "."
if flag.NArg() > 0 {
repo = flag.Arg(0)
}
var excludeRe *regexp.Regexp
if *excludeRegexFlag != "" {
var reErr error
excludeRe, reErr = regexp.Compile(*excludeRegexFlag)
if reErr != nil {
log.Fatalf("invalid --exclude-regex: %v", reErr)
}
}
absRepo, err := filepath.Abs(repo)
if err != nil {
log.Fatalf("invalid repo path: %v", err)
}
if _, err := gitCmd(absRepo, "rev-parse", "--git-dir"); err != nil {
log.Fatalf("%s is not a git repository", absRepo)
}
outFile := *outputFlag
if outFile == "" {
outFile = filepath.Base(absRepo) + ".html"
}
fmt.Printf("Repository : %s\n", absRepo)
fmt.Printf("Branch : %s\n", *branchFlag)
fmt.Printf("Workers : %d\n", *workersFlag)
// 1. Pre-scan repo tree to select folders to break down (file count as proxy).
var selectedDirs []string
var selectedFileCounts map[string]int
var totalFiles int
if *folderFlag > 0 {
fmt.Print("Folders : scanning… ")
var dirErr error
selectedDirs, selectedFileCounts, totalFiles, dirErr = selectFolders(absRepo, *branchFlag, *folderFlag, excludeRe)
if dirErr != nil {
fmt.Printf("\rFolders : (skipped: %v)\n", dirErr)
selectedDirs = nil
}
}
// Print selected folders as a Unicode tree with file counts (available immediately).
if len(selectedDirs) > 0 {
fmt.Printf("\rFolders : \033[1;36m/\033[0m %d files\n", totalFiles)
// Build the full node set: selected dirs + all their ancestor directories,
// so the tree has no gaps and renders correctly.
type treeNode struct {
path string
name string
depth int
files int
selected bool
}
nodeMap := make(map[string]*treeNode)
for _, d := range selectedDirs {
parts := strings.Split(d, "/")
for depth := 0; depth < len(parts); depth++ {
path := strings.Join(parts[:depth+1], "/")
if _, exists := nodeMap[path]; !exists {
nodeMap[path] = &treeNode{path: path, name: parts[depth], depth: depth}
}
}
nodeMap[d].selected = true
nodeMap[d].files = selectedFileCounts[d]
}
nodes := make([]*treeNode, 0, len(nodeMap))
for _, n := range nodeMap {
nodes = append(nodes, n)
}
sort.Slice(nodes, func(i, j int) bool { return nodes[i].path < nodes[j].path })
// Does the ancestor of nodes[i] at `level` have any later sibling?
hasLaterSiblingAt := func(i, level int) bool {
parts := strings.Split(nodes[i].path, "/")
myAncestor := strings.Join(parts[:level+1], "/")
parentPath := strings.Join(parts[:level], "/")
for _, other := range nodes[i+1:] {
op := strings.Split(other.path, "/")
if len(op) > level {
if strings.Join(op[:level], "/") == parentPath &&
strings.Join(op[:level+1], "/") != myAncestor {
return true
}
}
}
return false
}
// Is nodes[i] the last child of its parent?
isLastSibling := func(i int) bool {
n := nodes[i]
parts := strings.Split(n.path, "/")
myParent := strings.Join(parts[:n.depth], "/")
for _, other := range nodes[i+1:] {
if other.depth == n.depth {
op := strings.Split(other.path, "/")
if strings.Join(op[:other.depth], "/") == myParent {
return false
}
}
}
return true
}
for i, n := range nodes {
prefix := ""
for level := 0; level < n.depth; level++ {
if hasLaterSiblingAt(i, level) {
prefix += "│ "
} else {
prefix += " "
}
}
connector := "├── "
if isLastSibling(i) {
connector = "└── "
}
if n.selected {
fmt.Printf(" %s%s\033[1;36m%s/\033[0m %d files\n", prefix, connector, n.name, n.files)
} else {
fmt.Printf(" %s%s%s/\n", prefix, connector, n.name)
}
}
}
trackDirs := make(map[string]bool, len(selectedDirs))
for _, d := range selectedDirs {
trackDirs[d] = true
}
// 2. Fetch commit hashes and determine snapshot stride.
fmt.Print("Snapshots : loading commit list… ")
hashes, err := getHashes(absRepo, *branchFlag)
if err != nil {
log.Fatalf("\n%v", err)
}
total := len(hashes)
if total == 0 {
log.Fatal("no commits found")
}
stride := 1
if *maxPtsFlag > 0 && total > *maxPtsFlag {
stride = total / *maxPtsFlag
}
if stride > 1 {
fmt.Printf("\rSnapshots : every %d commits (%d commits total)\n\n", stride, total)
} else {
fmt.Printf("\rSnapshots : every commit (%d commits)\n\n", total)
}
// 4. Replay history with parallel git workers.
state := newState()
var snaps []Snapshot
emailToName := make(map[string]string)
start := time.Now()
i := 0
if err := streamLog(absRepo, *branchFlag, *workersFlag, state, excludeRe, func(c CommitMeta) error {
if c.AuthorName != "" {
emailToName[c.AuthorEmail] = c.AuthorName
}
wantSnap := (i == 0) || (i == total-1) || ((i+1)%stride == 0)
if wantSnap {
snaps = append(snaps, Snapshot{
Label: c.Date.Format("2006-01-02") + " " + c.Hash[:7],
Totals: state.copyTotals(),
Total: state.totalLines(),
DirTotals: state.computeDirTotals(trackDirs),
})
}
if (i+1)%200 == 0 || i == total-1 {
elapsed := time.Since(start)
rate := float64(i+1) / elapsed.Seconds()
remaining := float64(total-i-1) / rate
eta := time.Duration(remaining) * time.Second
fmt.Printf("\r[%d/%d] %.0f commits/s ETA %-8s lines tracked: %-8d",
i+1, total, rate, eta.Round(time.Second), state.totalLines())
}
i++
return nil
}); err != nil {
log.Fatalf("\nstream log: %v", err)
}
fmt.Printf("\n\nDone in %s\n", time.Since(start).Round(time.Millisecond*100))
fmt.Printf("Snapshots : %d\n", len(snaps))
// 5. Build per-folder chart data and render HTML.
folders := buildAllFolderData(snaps, emailToName, *maxGraphFlag, selectedDirs)
vars := buildTemplateVars(filepath.Base(absRepo), *branchFlag, outFile, total, snaps, folders)
renderHTML(outFile, vars)
fmt.Printf("Output : %s\n", outFile)
}