-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrepo.go
More file actions
546 lines (487 loc) · 14.2 KB
/
repo.go
File metadata and controls
546 lines (487 loc) · 14.2 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package cmd
import (
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
type repoInfo struct {
Main string
Host string
Owner string
Name string
}
func getDefaultBase() string {
cmd := exec.Command("git", "symbolic-ref", "refs/remotes/origin/HEAD")
output, err := cmd.Output()
if err != nil {
return "main"
}
ref := strings.TrimSpace(string(output))
return strings.TrimPrefix(ref, "refs/remotes/origin/")
}
func getRepoInfo() (repoInfo, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
output, err := cmd.Output()
var repoRoot string
isBare := false
if err == nil {
repoRoot = strings.TrimSpace(string(output))
} else {
cmd = exec.Command("git", "rev-parse", "--is-bare-repository")
output, err = cmd.Output()
if err != nil || strings.TrimSpace(string(output)) != "true" {
return repoInfo{}, fmt.Errorf("not in a git repository")
}
isBare = true
cmd = exec.Command("git", "rev-parse", "--absolute-git-dir")
output, err = cmd.Output()
if err != nil {
return repoInfo{}, fmt.Errorf("not in a git repository")
}
repoRoot = strings.TrimSpace(string(output))
}
repoName := ""
remoteURL := ""
cmd = exec.Command("git", "remote", "get-url", "origin")
output, err = cmd.Output()
if err == nil {
remoteURL = strings.TrimSpace(string(output))
if parsed, ok := parseRemoteURL(remoteURL); ok {
repoName = parsed.Name
}
}
if repoName == "" {
repoName = strings.TrimSuffix(filepath.Base(repoRoot), ".git")
if commonCmd := exec.Command("git", "rev-parse", "--git-common-dir"); commonCmd != nil {
if commonOutput, commonErr := commonCmd.Output(); commonErr == nil {
commonDir := strings.TrimSpace(string(commonOutput))
if commonDir != "" {
if !filepath.IsAbs(commonDir) {
commonDir = filepath.Join(repoRoot, commonDir)
}
commonDir = filepath.Clean(commonDir)
base := filepath.Base(commonDir)
if base == ".git" {
repoName = filepath.Base(filepath.Dir(commonDir))
} else {
repoName = strings.TrimSuffix(base, ".git")
}
}
}
}
}
info := repoInfo{
Main: getMainWorktreePath(getDefaultBase(), repoName, repoRoot, isBare),
Name: repoName,
}
if remoteURL != "" {
if parsed, ok := parseRemoteURL(remoteURL); ok {
info.Host = parsed.Host
info.Owner = parsed.Owner
}
}
return info, nil
}
func getMainWorktreePath(defaultBranch, repoName, repoRoot string, isBare bool) string {
cmd := exec.Command("git", "worktree", "list", "--porcelain")
output, err := cmd.Output()
if err == nil {
type entry struct {
path string
branch string
}
var entries []entry
var current entry
for _, line := range strings.Split(string(output), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if strings.HasPrefix(line, "worktree ") {
if current.path != "" {
entries = append(entries, current)
}
current = entry{path: strings.TrimPrefix(line, "worktree ")}
continue
}
if strings.HasPrefix(line, "branch ") {
current.branch = strings.TrimPrefix(line, "branch ")
}
}
if current.path != "" {
entries = append(entries, current)
}
if defaultBranch != "" {
target := "refs/heads/" + defaultBranch
for _, e := range entries {
if e.branch == target {
return e.path
}
}
}
for _, e := range entries {
if filepath.Base(e.path) == repoName {
return e.path
}
}
for _, e := range entries {
if stat, err := os.Stat(filepath.Join(e.path, ".git")); err == nil && stat.IsDir() {
return e.path
}
}
if len(entries) > 0 {
return entries[0].path
}
}
if isBare {
return filepath.Join(filepath.Dir(repoRoot), repoName)
}
return repoRoot
}
func parseRemoteURL(remoteURL string) (repoInfo, bool) {
trimmed := strings.TrimSpace(remoteURL)
if trimmed == "" {
return repoInfo{}, false
}
if strings.Contains(trimmed, "://") {
parsed, err := url.Parse(trimmed)
if err != nil || parsed.Hostname() == "" {
return repoInfo{}, false
}
host := parsed.Hostname()
path := strings.Trim(parsed.Path, "/")
parts := strings.Split(path, "/")
if len(parts) < 2 {
return repoInfo{}, false
}
repo := strings.TrimSuffix(parts[len(parts)-1], ".git")
owner := strings.Join(parts[:len(parts)-1], "/")
return repoInfo{Host: host, Owner: owner, Name: repo}, true
}
if scpLike := strings.SplitN(trimmed, ":", 2); len(scpLike) == 2 {
hostPart := scpLike[0]
path := scpLike[1]
if atIdx := strings.LastIndex(hostPart, "@"); atIdx != -1 {
hostPart = hostPart[atIdx+1:]
}
host := hostPart
parts := strings.Split(strings.Trim(path, "/"), "/")
if len(parts) < 2 {
return repoInfo{}, false
}
repo := strings.TrimSuffix(parts[len(parts)-1], ".git")
owner := strings.Join(parts[:len(parts)-1], "/")
return repoInfo{Host: host, Owner: owner, Name: repo}, true
}
return repoInfo{}, false
}
type RemoteType int
const (
RemoteGitHub RemoteType = iota
RemoteGitLab
RemoteUnknown
)
func getPRNumber(input string) (string, error) {
githubRegex := regexp.MustCompile(`^https://github\.com/.*/pull/([0-9]+)`)
if matches := githubRegex.FindStringSubmatch(input); matches != nil {
return matches[1], nil
}
gitlabRegex := regexp.MustCompile(`^https://gitlab\.com/.*/-/merge_requests/([0-9]+)`)
if matches := gitlabRegex.FindStringSubmatch(input); matches != nil {
return matches[1], nil
}
numRegex := regexp.MustCompile(`^[0-9]+$`)
if numRegex.MatchString(input) {
return input, nil
}
return "", fmt.Errorf("invalid PR/MR number or URL: %s", input)
}
func worktreeExists(branch string) (string, bool) {
entries, err := getWorktreeListPorcelain()
if err != nil {
return "", false
}
return findWorktreeByBranch(entries, branch, filesystemCaseInsensitive(".") || filesystemCaseInsensitive(worktreeRoot))
}
func findWorktreeByBranch(entries []worktreeListEntry, branch string, caseInsensitive bool) (string, bool) {
if branch == "" {
return "", false
}
for _, entry := range entries {
if entry.Branch == branch {
return entry.Path, true
}
}
if caseInsensitive {
for _, entry := range entries {
if strings.EqualFold(entry.Branch, branch) {
return entry.Path, true
}
}
}
return "", false
}
func branchExists(branch string) bool {
cmd := exec.Command("git", "show-ref", "--verify", "--quiet", fmt.Sprintf("refs/heads/%s", branch))
if cmd.Run() == nil {
return true
}
cmd = exec.Command("git", "show-ref", "--verify", "--quiet", fmt.Sprintf("refs/remotes/origin/%s", branch))
return cmd.Run() == nil
}
// matchRemoteBranch parses git ls-remote --heads output and returns the
// correctly-cased branch name if a case-insensitive match is found.
func matchRemoteBranch(branch string, lsRemoteOutput string) string {
lowerBranch := strings.ToLower(branch)
for _, line := range strings.Split(lsRemoteOutput, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.SplitN(line, "\t", 2)
if len(parts) != 2 {
continue
}
ref := strings.TrimPrefix(parts[1], "refs/heads/")
if strings.ToLower(ref) == lowerBranch {
return ref
}
}
return branch
}
// resolveRemoteBranchCase calls git ls-remote and resolves the branch name
// using case-insensitive matching.
func resolveRemoteBranchCase(branch string) string {
cmd := exec.Command("git", "ls-remote", "--heads", "origin")
output, err := cmd.Output()
if err != nil {
return branch
}
return matchRemoteBranch(branch, string(output))
}
func getAvailableBranches() ([]string, error) {
cmd := exec.Command("git", "branch", "-a", "--format=%(refname:short)")
output, err := cmd.Output()
if err != nil {
return nil, err
}
branchMap := make(map[string]bool)
for _, line := range strings.Split(string(output), "\n") {
branch := strings.TrimSpace(line)
if branch == "" {
continue
}
if strings.HasPrefix(branch, "origin/HEAD") || strings.Contains(branch, "->") || strings.Contains(branch, "HEAD") {
continue
}
branch = strings.TrimPrefix(branch, "origin/")
if branch == "origin" || branch == "upstream" {
continue
}
branchMap[branch] = true
}
branches := []string{}
for branch := range branchMap {
branches = append(branches, branch)
}
return branches, nil
}
func getExistingWorktreeBranches() ([]string, error) {
cmd := exec.Command("git", "worktree", "list")
output, err := cmd.Output()
if err != nil {
return nil, err
}
branches := []string{}
lines := strings.Split(string(output), "\n")
for _, line := range lines[1:] { // Skip first line (main worktree)
if line == "" {
continue
}
if matches := regexp.MustCompile(`\[([^\]]+)\]`).FindStringSubmatch(line); matches != nil {
branches = append(branches, matches[1])
}
}
return branches, nil
}
func getMergedBranches(base string) ([]string, error) {
cmd := exec.Command("git", "branch", "--merged", base, "--format=%(refname:short)")
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to get merged branches: %w", err)
}
var branches []string
for _, line := range strings.Split(string(output), "\n") {
branch := strings.TrimSpace(line)
if branch == "" || branch == base || branch == "main" || branch == "master" {
continue
}
branches = append(branches, branch)
}
return branches, nil
}
func parsePROutput(output string) ([]string, []string) {
var numbers []string
var labels []string
for _, line := range strings.Split(strings.TrimSpace(output), "\n") {
if line == "" {
continue
}
parts := strings.SplitN(line, "\t", 2)
if len(parts) == 2 {
numbers = append(numbers, parts[0])
labels = append(labels, fmt.Sprintf("#%s: %s", parts[0], parts[1]))
}
}
return numbers, labels
}
func getOpenPRs() ([]string, []string, error) {
cmd := exec.Command("gh", "pr", "list", "--json", "number,title", "--jq", ".[] | \"\\(.number)\\t\\(.title)\"")
output, err := cmd.Output()
if err != nil {
return nil, nil, err
}
numbers, labels := parsePROutput(string(output))
return numbers, labels, nil
}
func parseMROutput(output string) ([]string, []string) {
var numbers []string
var labels []string
mrRegex := regexp.MustCompile(`^!(\d+)\s+[^\s]+\s+(.+?)\s+\(`)
for _, line := range strings.Split(output, "\n") {
if matches := mrRegex.FindStringSubmatch(line); matches != nil {
numbers = append(numbers, matches[1])
labels = append(labels, fmt.Sprintf("!%s: %s", matches[1], strings.TrimSpace(matches[2])))
}
}
return numbers, labels
}
func getOpenMRs() ([]string, []string, error) {
cmd := exec.Command("glab", "mr", "list")
output, err := cmd.Output()
if err != nil {
return nil, nil, err
}
numbers, labels := parseMROutput(string(output))
return numbers, labels, nil
}
// parseGitHubBranchName extracts the branch name from gh pr view JSON output.
func parseGitHubBranchName(jsonOutput string) (string, error) {
var result struct {
HeadRefName string `json:"headRefName"`
}
if err := json.Unmarshal([]byte(jsonOutput), &result); err != nil {
return "", fmt.Errorf("failed to parse GitHub PR JSON: %w", err)
}
if result.HeadRefName == "" {
return "", fmt.Errorf("empty branch name in GitHub PR response")
}
return result.HeadRefName, nil
}
// parseGitLabBranchName extracts the branch name from glab mr view JSON output.
func parseGitLabBranchName(jsonOutput string) (string, error) {
var result struct {
SourceBranch string `json:"source_branch"`
}
if err := json.Unmarshal([]byte(jsonOutput), &result); err != nil {
return "", fmt.Errorf("failed to parse GitLab MR JSON: %w", err)
}
if result.SourceBranch == "" {
return "", fmt.Errorf("empty branch name in GitLab MR response")
}
return result.SourceBranch, nil
}
// getPRBranchName looks up the actual branch name for a PR/MR using the gh/glab CLI.
func getPRBranchName(prNumber string, remoteType RemoteType) (string, error) {
switch remoteType {
case RemoteGitHub:
cmd := exec.Command("gh", "pr", "view", prNumber, "--json", "headRefName")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to get PR branch name: %w", err)
}
return parseGitHubBranchName(string(output))
case RemoteGitLab:
cmd := exec.Command("glab", "mr", "view", prNumber, "--output", "json")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to get MR branch name: %w", err)
}
return parseGitLabBranchName(string(output))
default:
return "", fmt.Errorf("invalid remote type")
}
}
type worktreeListEntry struct {
Path string `json:"path"`
HEAD string `json:"head,omitempty"`
Branch string `json:"branch,omitempty"`
Bare bool `json:"bare,omitempty"`
Detached bool `json:"detached,omitempty"`
Locked string `json:"locked,omitempty"`
Prunable string `json:"prunable,omitempty"`
}
func getWorktreeListPorcelain() ([]worktreeListEntry, error) {
cmd := exec.Command("git", "worktree", "list", "--porcelain")
output, err := cmd.Output()
if err != nil {
return nil, err
}
entries := make([]worktreeListEntry, 0)
current := worktreeListEntry{}
for _, rawLine := range strings.Split(string(output), "\n") {
line := strings.TrimSpace(rawLine)
if line == "" {
if current.Path != "" {
entries = append(entries, current)
current = worktreeListEntry{}
}
continue
}
switch {
case strings.HasPrefix(line, "worktree "):
if current.Path != "" {
entries = append(entries, current)
}
current = worktreeListEntry{Path: strings.TrimPrefix(line, "worktree ")}
case strings.HasPrefix(line, "HEAD "):
current.HEAD = strings.TrimPrefix(line, "HEAD ")
case strings.HasPrefix(line, "branch "):
branch := strings.TrimPrefix(line, "branch ")
current.Branch = strings.TrimPrefix(branch, "refs/heads/")
case line == "bare":
current.Bare = true
case line == "detached":
current.Detached = true
case strings.HasPrefix(line, "locked"):
current.Locked = strings.TrimSpace(strings.TrimPrefix(line, "locked"))
case strings.HasPrefix(line, "prunable"):
current.Prunable = strings.TrimSpace(strings.TrimPrefix(line, "prunable"))
}
}
if current.Path != "" {
entries = append(entries, current)
}
return entries, nil
}
func isDirEmpty(path string) (bool, error) {
dir, err := os.Open(path)
switch {
case os.IsNotExist(err):
return true, nil
case err != nil:
return false, err
}
defer func() { _ = dir.Close() }()
_, err = dir.Readdirnames(1)
if err == io.EOF {
return true, nil
}
return false, err
}