-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
205 lines (178 loc) · 4.38 KB
/
git.go
File metadata and controls
205 lines (178 loc) · 4.38 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
package main
import (
"bufio"
"fmt"
"os/exec"
"strconv"
"strings"
"time"
)
// Commit represents a single git commit
type Commit struct {
Hash string
Author string
Email string
Timestamp time.Time
Message string
FilesChanged int
Insertions int
Deletions int
}
// FileChurn represents change frequency for a file
type FileChurn struct {
Path string
Changes int
Insertions int
Deletions int
Authors map[string]int
}
// GetCommits retrieves commits from the last N days
func GetCommits(repoPath string, days int) ([]Commit, error) {
since := time.Now().AddDate(0, 0, -days).Format("2006-01-02")
// Use a custom format for reliable parsing
// Format: hash|author|email|timestamp|message
format := "%H|%an|%ae|%aI|%s"
cmd := exec.Command("git", "-C", repoPath, "log",
"--since="+since,
"--format="+format,
"--shortstat",
"--no-merges",
)
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("git log failed: %w", err)
}
return parseCommits(string(output))
}
func parseCommits(output string) ([]Commit, error) {
var commits []Commit
scanner := bufio.NewScanner(strings.NewReader(output))
var current *Commit
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
// Try to parse as commit line (has | separators)
parts := strings.SplitN(line, "|", 5)
if len(parts) == 5 && len(parts[0]) == 40 {
// This is a commit line
ts, err := time.Parse(time.RFC3339, parts[3])
if err != nil {
continue
}
c := Commit{
Hash: parts[0],
Author: parts[1],
Email: parts[2],
Timestamp: ts,
Message: parts[4],
}
commits = append(commits, c)
current = &commits[len(commits)-1]
} else if current != nil && strings.Contains(line, "changed") {
// This is a stat line like "3 files changed, 10 insertions(+), 2 deletions(-)"
parseStatLine(line, current)
current = nil
}
}
return commits, nil
}
func parseStatLine(line string, c *Commit) {
parts := strings.Split(line, ",")
for _, part := range parts {
part = strings.TrimSpace(part)
fields := strings.Fields(part)
if len(fields) < 2 {
continue
}
num, err := strconv.Atoi(fields[0])
if err != nil {
continue
}
switch {
case strings.Contains(fields[1], "file"):
c.FilesChanged = num
case strings.Contains(fields[1], "insertion"):
c.Insertions = num
case strings.Contains(fields[1], "deletion"):
c.Deletions = num
}
}
}
// GetFileChurn analyzes which files change most frequently
func GetFileChurn(repoPath string, days int) ([]FileChurn, error) {
since := time.Now().AddDate(0, 0, -days).Format("2006-01-02")
cmd := exec.Command("git", "-C", repoPath, "log",
"--since="+since,
"--format=%H|%an",
"--numstat",
"--no-merges",
)
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("git log failed: %w", err)
}
return parseFileChurn(string(output))
}
func parseFileChurn(output string) ([]FileChurn, error) {
churnMap := make(map[string]*FileChurn)
scanner := bufio.NewScanner(strings.NewReader(output))
var currentAuthor string
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
// Check if it's a commit header line
parts := strings.SplitN(line, "|", 2)
if len(parts) == 2 && len(parts[0]) == 40 {
currentAuthor = parts[1]
continue
}
// Parse numstat line: insertions\tdeletions\tfilepath
fields := strings.Split(line, "\t")
if len(fields) == 3 {
ins, _ := strconv.Atoi(fields[0])
del, _ := strconv.Atoi(fields[1])
path := fields[2]
if path == "" || fields[0] == "-" {
continue // Skip binary files
}
fc, ok := churnMap[path]
if !ok {
fc = &FileChurn{
Path: path,
Authors: make(map[string]int),
}
churnMap[path] = fc
}
fc.Changes++
fc.Insertions += ins
fc.Deletions += del
if currentAuthor != "" {
fc.Authors[currentAuthor]++
}
}
}
// Convert map to sorted slice
result := make([]FileChurn, 0, len(churnMap))
for _, fc := range churnMap {
result = append(result, *fc)
}
// Sort by change frequency (descending)
sortFileChurn(result)
return result, nil
}
func sortFileChurn(files []FileChurn) {
// Simple insertion sort (no external deps)
for i := 1; i < len(files); i++ {
key := files[i]
j := i - 1
for j >= 0 && files[j].Changes < key.Changes {
files[j+1] = files[j]
j--
}
files[j+1] = key
}
}