-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit.go
More file actions
396 lines (365 loc) · 10.1 KB
/
Copy pathgit.go
File metadata and controls
396 lines (365 loc) · 10.1 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"bufio"
"context"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
)
// CommitInfo is a single commit shown in the UI.
type CommitInfo struct {
Hash string `json:"hash"`
Short string `json:"short"`
Subject string `json:"subject"`
Time int64 `json:"time"` // unix seconds
Author string `json:"author"`
}
// FileChange is one entry from `git status`.
type FileChange struct {
Path string `json:"path"`
Code string `json:"code"` // two-char XY status
Staged bool `json:"staged"`
}
// RepoInfo is the full status snapshot for a single repository.
type RepoInfo struct {
Name string `json:"name"`
Path string `json:"path"`
Branch string `json:"branch"`
Upstream string `json:"upstream"`
Remote string `json:"remote"`
Detached bool `json:"detached"`
HasUpstream bool `json:"hasUpstream"`
Ahead int `json:"ahead"`
Behind int `json:"behind"`
Staged int `json:"staged"`
Unstaged int `json:"unstaged"`
Untracked int `json:"untracked"`
Conflicts int `json:"conflicts"`
Dirty bool `json:"dirty"`
State string `json:"state"` // synced|ahead|behind|diverged|no-upstream|detached
LastCommit *CommitInfo `json:"lastCommit,omitempty"`
LastFetch int64 `json:"lastFetch"` // unix seconds, 0 if never
Error string `json:"error,omitempty"`
}
// OpResult is the outcome of a git operation (fetch/pull/push/commit).
type OpResult struct {
Repo string `json:"repo"`
Action string `json:"action"`
OK bool `json:"ok"`
Output string `json:"output"`
}
// runGit runs a git command inside dir and returns combined output.
// GIT_TERMINAL_PROMPT=0 prevents the server from hanging on a credential
// prompt; cached credential helpers (e.g. osxkeychain) still work.
func runGit(dir string, timeout time.Duration, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(),
"GIT_TERMINAL_PROMPT=0",
"GIT_OPTIONAL_LOCKS=0",
"LC_ALL=C",
)
out, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
return string(out), ctx.Err()
}
return string(out), err
}
// runGitStdin is like runGit but pipes stdin into git (used for `git apply`).
func runGitStdin(dir string, timeout time.Duration, stdin string, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(),
"GIT_TERMINAL_PROMPT=0",
"GIT_OPTIONAL_LOCKS=0",
"LC_ALL=C",
)
cmd.Stdin = strings.NewReader(stdin)
out, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
return string(out), ctx.Err()
}
return string(out), err
}
// findRepos returns immediate subdirectories of root that contain a .git entry.
func findRepos(root string) []string {
var names []string
entries, err := os.ReadDir(root)
if err != nil {
return names
}
for _, e := range entries {
if !e.IsDir() {
continue
}
if _, err := os.Stat(filepath.Join(root, e.Name(), ".git")); err == nil {
names = append(names, e.Name())
}
}
sort.Strings(names)
return names
}
// scanRepos collects status for every repo under root, in parallel.
func scanRepos(root string) []RepoInfo {
names := findRepos(root)
results := make([]RepoInfo, len(names))
var wg sync.WaitGroup
sem := make(chan struct{}, 8)
for i, n := range names {
wg.Add(1)
go func(i int, n string) {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
results[i] = repoStatus(root, n)
}(i, n)
}
wg.Wait()
return results
}
func repoStatus(root, name string) RepoInfo {
dir := filepath.Join(root, name)
info := RepoInfo{Name: name, Path: dir}
out, err := runGit(dir, 10*time.Second, "status", "--porcelain=v2", "--branch")
if err != nil {
info.Error = strings.TrimSpace(out)
if info.Error == "" {
info.Error = err.Error()
}
return info
}
parseStatus(&info, out)
if r, err := runGit(dir, 5*time.Second, "remote", "get-url", "origin"); err == nil {
info.Remote = strings.TrimSpace(r)
}
if c, err := runGit(dir, 5*time.Second, "log", "-1", "--format=%H%x1f%h%x1f%s%x1f%ct%x1f%an"); err == nil {
if cm := parseCommitLine(strings.TrimRight(c, "\n")); cm != nil {
info.LastCommit = cm
}
}
if st, err := os.Stat(filepath.Join(dir, ".git", "FETCH_HEAD")); err == nil {
info.LastFetch = st.ModTime().Unix()
}
info.deriveState()
return info
}
func parseStatus(info *RepoInfo, out string) {
sc := bufio.NewScanner(strings.NewReader(out))
sc.Buffer(make([]byte, 1024*1024), 4*1024*1024)
for sc.Scan() {
line := sc.Text()
if line == "" {
continue
}
if strings.HasPrefix(line, "# ") {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
switch fields[1] {
case "branch.head":
if len(fields) >= 3 {
if fields[2] == "(detached)" {
info.Detached = true
} else {
info.Branch = fields[2]
}
}
case "branch.upstream":
if len(fields) >= 3 {
info.Upstream = fields[2]
info.HasUpstream = true
}
case "branch.ab":
for _, f := range fields[2:] {
switch {
case strings.HasPrefix(f, "+"):
info.Ahead = atoi(f[1:])
case strings.HasPrefix(f, "-"):
info.Behind = atoi(f[1:])
}
}
}
continue
}
switch line[0] {
case '1', '2': // ordinary / renamed change
if len(line) >= 4 {
x, y := line[2], line[3]
if x != '.' {
info.Staged++
}
if y != '.' {
info.Unstaged++
}
}
case 'u': // unmerged
info.Conflicts++
case '?': // untracked
info.Untracked++
}
}
info.Dirty = info.Staged+info.Unstaged+info.Untracked+info.Conflicts > 0
}
func (info *RepoInfo) deriveState() {
switch {
case info.Detached:
info.State = "detached"
case !info.HasUpstream:
info.State = "no-upstream"
case info.Ahead > 0 && info.Behind > 0:
info.State = "diverged"
case info.Ahead > 0:
info.State = "ahead"
case info.Behind > 0:
info.State = "behind"
default:
info.State = "synced"
}
}
// repoFiles lists changed files for the detail view.
func repoFiles(root, name string) []FileChange {
dir := filepath.Join(root, name)
out, err := runGit(dir, 10*time.Second, "-c", "core.quotepath=false", "status", "--short")
if err != nil {
return nil
}
var files []FileChange
sc := bufio.NewScanner(strings.NewReader(out))
for sc.Scan() {
l := sc.Text()
if len(l) < 4 {
continue
}
x := l[0]
path := strings.TrimSpace(l[3:])
files = append(files, FileChange{
Path: path,
Code: l[:2],
Staged: x != ' ' && x != '?',
})
}
return files
}
// recentLog returns the most recent n commits.
func recentLog(root, name string, n int) []CommitInfo {
dir := filepath.Join(root, name)
out, err := runGit(dir, 10*time.Second, "log", "-n", strconv.Itoa(n),
"--format=%H%x1f%h%x1f%s%x1f%ct%x1f%an")
if err != nil {
return nil
}
var logs []CommitInfo
sc := bufio.NewScanner(strings.NewReader(out))
for sc.Scan() {
if cm := parseCommitLine(sc.Text()); cm != nil {
logs = append(logs, *cm)
}
}
return logs
}
func parseCommitLine(line string) *CommitInfo {
parts := strings.Split(line, "\x1f")
if len(parts) != 5 {
return nil
}
t, _ := strconv.ParseInt(parts[3], 10, 64)
return &CommitInfo{
Hash: parts[0],
Short: parts[1],
Subject: parts[2],
Time: t,
Author: parts[4],
}
}
// ---- operations ----
func fetchRepo(root, name string) OpResult {
out, err := runGit(filepath.Join(root, name), 120*time.Second, "fetch", "--all", "--prune")
return mkResult(name, "fetch", out, err)
}
// pullRepo pulls using the given mode: "ff" (default, safe), "rebase", "merge".
func pullRepo(root, name, mode string) OpResult {
dir := filepath.Join(root, name)
var args []string
switch mode {
case "rebase":
args = []string{"pull", "--rebase"}
case "merge":
args = []string{"pull", "--no-rebase"}
default:
args = []string{"pull", "--ff-only"}
}
out, err := runGit(dir, 180*time.Second, args...)
return mkResult(name, "pull", out, err)
}
// pushRepo pushes the current branch. If no upstream is set it creates one.
// force uses --force-with-lease (safe force).
func pushRepo(root, name string, force bool) OpResult {
dir := filepath.Join(root, name)
info := repoStatus(root, name)
args := []string{"push"}
if force {
args = append(args, "--force-with-lease")
}
if !info.HasUpstream && info.Branch != "" {
args = append(args, "-u", "origin", info.Branch)
}
out, err := runGit(dir, 180*time.Second, args...)
return mkResult(name, "push", out, err)
}
func stageRepo(root, name string) OpResult {
out, err := runGit(filepath.Join(root, name), 30*time.Second, "add", "-A")
return mkResult(name, "stage", out, err)
}
// commitRepo stages everything, commits with msg, and optionally pushes.
func commitRepo(root, name, msg string, push bool) OpResult {
dir := filepath.Join(root, name)
if out, err := runGit(dir, 30*time.Second, "add", "-A"); err != nil {
return mkResult(name, "commit", out, err)
}
out, err := runGit(dir, 30*time.Second, "commit", "-m", msg)
res := mkResult(name, "commit", out, err)
if res.OK && push {
p := pushRepo(root, name, false)
res.Output = strings.TrimSpace(res.Output + "\n\n" + p.Output)
res.OK = p.OK
}
return res
}
func mkResult(name, action, out string, err error) OpResult {
r := OpResult{Repo: name, Action: action, Output: strings.TrimSpace(out), OK: err == nil}
if err != nil && r.Output == "" {
r.Output = err.Error()
}
return r
}
// batchOp runs fn over names concurrently (network bound), bounded pool.
func batchOp(root string, names []string, fn func(root, name string) OpResult) []OpResult {
results := make([]OpResult, len(names))
var wg sync.WaitGroup
sem := make(chan struct{}, 6)
for i, n := range names {
wg.Add(1)
go func(i int, n string) {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
results[i] = fn(root, n)
}(i, n)
}
wg.Wait()
return results
}
func atoi(s string) int {
n, _ := strconv.Atoi(strings.TrimSpace(s))
return n
}