-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathgit.go
More file actions
696 lines (595 loc) · 23.5 KB
/
Copy pathgit.go
File metadata and controls
696 lines (595 loc) · 23.5 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package cli
import (
"errors"
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/github/gh-aw/pkg/constants"
"github.com/github/gh-aw/pkg/console"
"github.com/github/gh-aw/pkg/fileutil"
"github.com/github/gh-aw/pkg/gitutil"
"github.com/github/gh-aw/pkg/logger"
)
var gitLog = logger.New("cli:git")
func isGitRepo() bool {
_, err := gitutil.FindGitRoot()
return err == nil
}
// findGitRootForPath finds the root directory of the git repository containing the specified path
func findGitRootForPath(path string) (string, error) {
gitLog.Printf("Finding git root for path: %s", path)
// Get absolute path first
absPath, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("failed to get absolute path: %w", err)
}
// Validate the absolute path
absPath, err = fileutil.ValidateAbsolutePath(absPath)
if err != nil {
return "", fmt.Errorf("invalid path: %w", err)
}
// Use the directory containing the file
dir := filepath.Dir(absPath)
// Find git root using filesystem traversal from the file's directory
gitRoot, err := gitutil.FindGitRootFrom(dir)
if err != nil {
return "", fmt.Errorf("failed to get repository root for path %s: %w", path, err)
}
gitLog.Printf("Found git root for path: %s", gitRoot)
return gitRoot, nil
}
// parseGitHubRepoSlugFromURL extracts owner/repo from a GitHub URL
// Supports HTTPS (https://github.com/owner/repo), SCP-style SSH ([user@]github.com:owner/repo),
// and SSH URL scheme (ssh://git@github.com/owner/repo) formats.
// Also supports GitHub Enterprise URLs and non-standard SSH usernames (e.g. example@ghe.host:owner/repo).
func parseGitHubRepoSlugFromURL(url string) string {
gitLog.Printf("Parsing GitHub repo slug from URL: %s", url)
// Remove .git suffix if present
url = strings.TrimSuffix(url, ".git")
githubHost := getGitHubHost()
githubHostWithoutScheme := strings.TrimPrefix(strings.TrimPrefix(githubHost, "https://"), "http://")
// Handle HTTPS URLs: https://github.com/owner/repo or https://enterprise.github.com/owner/repo
if after, ok := strings.CutPrefix(url, githubHost+"/"); ok {
slug := after
gitLog.Printf("Extracted slug from HTTPS URL: %s", slug)
return slug
}
// Handle SCP-style SSH URLs with any username: git@github.com:owner/repo,
// or non-standard usernames like example@example.ghe.com:owner/repo (GHE),
// or username-less like github.com:owner/repo.
// Match: [user@]<host>:<owner>/<repo>
scpHostColon := githubHostWithoutScheme + ":"
// Try with username (user@host:path)
if _, afterAt, hasAt := strings.Cut(url, "@"); hasAt {
if after, ok := strings.CutPrefix(afterAt, scpHostColon); ok {
gitLog.Printf("Extracted slug from SCP-style SSH URL with username: %s", after)
return after
}
}
// Try without username (host:path)
if after, ok := strings.CutPrefix(url, scpHostColon); ok {
gitLog.Printf("Extracted slug from SCP-style SSH URL without username: %s", after)
return after
}
// Handle SSH URL scheme: ssh://git@github.com/owner/repo or ssh://github.com/owner/repo
if after, ok := strings.CutPrefix(url, "ssh://"); ok {
// Strip optional user info (e.g. "git@")
if _, userStripped, hasAt := strings.Cut(after, "@"); hasAt {
after = userStripped
}
if slug, ok := strings.CutPrefix(after, githubHostWithoutScheme+"/"); ok {
gitLog.Printf("Extracted slug from SSH URL scheme: %s", slug)
return slug
}
}
gitLog.Print("Could not extract slug from URL")
return ""
}
// extractHostFromRemoteURL extracts the host (optionally including port) from a git remote URL.
// Supports HTTPS (https://host[:port]/path), HTTP (http://host[:port]/path), and SSH (git@host[:port]:path or ssh://git@host[:port]/path) formats.
// Returns the host portion as "host[:port]" when parsed, or "github.com" as the default if the URL cannot be parsed.
func extractHostFromRemoteURL(remoteURL string) string {
// HTTPS / HTTP format: https://[userinfo@]host/path or http://[userinfo@]host/path
// Use net/url.Parse to correctly handle all userinfo variants (user@, user:pass@,
// and passwords containing '@') and to extract the bare host without credentials.
for _, scheme := range []string{"https://", "http://"} {
if strings.HasPrefix(remoteURL, scheme) {
if u, err := url.Parse(remoteURL); err == nil && u.Host != "" {
return u.Host
}
// Fallback: strip scheme and any userinfo manually.
after := remoteURL[len(scheme):]
if host, _, found := strings.Cut(after, "/"); found {
// Strip optional userinfo (everything up to and including the last '@').
if idx := strings.LastIndex(host, "@"); idx >= 0 {
host = host[idx+1:]
}
return host
}
if idx := strings.LastIndex(after, "@"); idx >= 0 {
return after[idx+1:]
}
return after
}
}
// SSH URL format: ssh://git@host/path or ssh://host/path
if after, ok := strings.CutPrefix(remoteURL, "ssh://"); ok {
// Strip optional user info (e.g. "git@")
if _, userStripped, hasAt := strings.Cut(after, "@"); hasAt {
after = userStripped
}
if host, _, found := strings.Cut(after, "/"); found {
return host
}
return after
}
// SSH scp-like format: [user@]host:path — any username, not just git@
// Try with username first (user@host:path)
if _, afterAt, hasAt := strings.Cut(remoteURL, "@"); hasAt {
if host, _, found := strings.Cut(afterAt, ":"); found {
return host
}
}
// Try without username (host:path)
if host, _, found := strings.Cut(remoteURL, ":"); found {
// Make sure this looks like an scp-style URL (has no slashes before the colon)
// to avoid matching "ssh://host:port/path" or "https://host:port/path"
if !strings.Contains(host, "/") {
return host
}
}
return "github.com"
}
// resolveRemoteURL resolves the best git remote URL to use for a given directory.
// It first tries the 'origin' remote for backward compatibility. If 'origin' is not
// configured but exactly one other remote exists, that remote is used instead.
// Returns the remote URL, the remote name used, and any error.
// dir may be empty to use the current working directory.
func resolveRemoteURL(dir string) (string, string, error) {
gitArgs := func(args ...string) *exec.Cmd {
if dir != "" {
return exec.Command("git", append([]string{"-C", dir}, args...)...)
}
return exec.Command("git", args...)
}
// First try 'origin' for backward compatibility
if output, err := gitArgs("config", "--get", "remote.origin.url").Output(); err == nil {
url := strings.TrimSpace(string(output))
if url != "" {
gitLog.Print("Using 'origin' remote")
return url, "origin", nil
}
}
// Fall back: list all remotes
output, err := gitArgs("remote").Output()
if err != nil {
return "", "", fmt.Errorf("failed to list git remotes: %w", err)
}
remoteNames := strings.Fields(strings.TrimSpace(string(output)))
if len(remoteNames) == 0 {
return "", "", errors.New("no git remotes configured")
}
if len(remoteNames) > 1 {
return "", "", fmt.Errorf("multiple git remotes configured (%s), no 'origin' remote found", strings.Join(remoteNames, ", "))
}
// Exactly one remote — use it
remoteName := remoteNames[0]
urlOutput, err := gitArgs("config", "--get", "remote."+remoteName+".url").Output()
if err != nil {
return "", "", fmt.Errorf("failed to get URL for remote %q: %w", remoteName, err)
}
url := strings.TrimSpace(string(urlOutput))
gitLog.Printf("No 'origin' remote found; using single configured remote %q", remoteName)
return url, remoteName, nil
}
// getHostFromOriginRemote returns the hostname of the git remote.
// It prefers the 'origin' remote for backward compatibility. If 'origin' is not
// configured but exactly one other remote exists, that remote is used instead.
// For example, a remote URL of "https://ghes.example.com/org/repo.git" returns "ghes.example.com",
// and "git@github.com:owner/repo.git" returns "github.com".
// Returns "github.com" as the default if the remote URL cannot be determined.
func getHostFromOriginRemote() string {
remoteURL, remoteName, err := resolveRemoteURL("")
if err != nil {
gitLog.Printf("Failed to resolve remote URL: %v", err)
return "github.com"
}
host := extractHostFromRemoteURL(remoteURL)
gitLog.Printf("Detected GitHub host from remote %q: %s", remoteName, host)
return host
}
// getRepositorySlugFromRemote extracts the repository slug (owner/repo) from git remote URL.
// It prefers the 'origin' remote for backward compatibility. If 'origin' is not
// configured but exactly one other remote exists, that remote is used instead.
func getRepositorySlugFromRemote() string {
gitLog.Print("Getting repository slug from git remote")
remoteURL, _, err := resolveRemoteURL("")
if err != nil {
gitLog.Printf("Failed to resolve remote URL: %v", err)
return ""
}
slug := parseGitHubRepoSlugFromURL(remoteURL)
if slug != "" {
gitLog.Printf("Repository slug: %s", slug)
}
return slug
}
// getRepositorySlugFromRemotePreferringUpstream extracts the repository slug (owner/repo)
// from git remotes, preferring the 'upstream' remote when available.
// This keeps schedule scattering stable for fork checkouts where origin points to the fork.
func getRepositorySlugFromRemotePreferringUpstream() string {
return getRepositorySlugFromDirPreferringUpstream("")
}
// getRepositorySlugFromDirPreferringUpstream extracts the repository slug (owner/repo)
// for a git working directory, preferring the 'upstream' remote when available.
func getRepositorySlugFromDirPreferringUpstream(dir string) string {
gitArgs := func(args ...string) *exec.Cmd {
if dir != "" {
return exec.Command("git", append([]string{"-C", dir}, args...)...)
}
return exec.Command("git", args...)
}
if output, err := gitArgs("config", "--get", "remote.upstream.url").Output(); err == nil {
upstreamURL := strings.TrimSpace(string(output))
if upstreamURL != "" {
slug := parseGitHubRepoSlugFromURL(upstreamURL)
if slug != "" {
gitLog.Printf("Repository slug from upstream remote: %s", slug)
return slug
}
gitLog.Printf("Unable to parse repository slug from upstream remote URL %q; falling back", upstreamURL)
}
}
remoteURL, _, err := resolveRemoteURL(dir)
if err != nil {
if dir == "" {
gitLog.Printf("Failed to resolve remote URL: %v", err)
} else {
gitLog.Printf("Failed to resolve remote URL for path: %v", err)
}
return ""
}
slug := parseGitHubRepoSlugFromURL(remoteURL)
if slug != "" {
if dir == "" {
gitLog.Printf("Repository slug: %s", slug)
} else {
gitLog.Printf("Repository slug for path: %s", slug)
}
}
return slug
}
// getRepositorySlugFromRemoteForPath extracts the repository slug (owner/repo) from the git remote URL
// of the repository containing the specified file path.
// It prefers the 'upstream' remote when available, and otherwise follows standard
// remote resolution (origin first, then single-remote fallback).
func getRepositorySlugFromRemoteForPath(path string) string {
gitLog.Printf("Getting repository slug for path: %s", path)
// Get absolute path first
absPath, err := filepath.Abs(path)
if err != nil {
gitLog.Printf("Failed to get absolute path: %v", err)
return ""
}
// Validate the absolute path
absPath, err = fileutil.ValidateAbsolutePath(absPath)
if err != nil {
gitLog.Printf("Invalid path: %v", err)
return ""
}
// Use the directory containing the file
dir := filepath.Dir(absPath)
return getRepositorySlugFromDirPreferringUpstream(dir)
}
func stageWorkflowChanges() {
// Find git root and add .github/workflows relative to it
if gitRoot, err := gitutil.FindGitRoot(); err == nil {
workflowsPath := filepath.Join(gitRoot, constants.WorkflowsDirSlash)
_ = exec.Command("git", "-C", gitRoot, "add", workflowsPath).Run()
// Also stage .gitattributes if it was modified
_ = stageGitAttributesIfChanged()
} else {
// Fallback to relative path if git root can't be found
_ = exec.Command("git", "add", constants.WorkflowsDirSlash).Run()
_ = exec.Command("git", "add", ".gitattributes").Run()
}
}
// ensureGitAttributes ensures that .gitattributes contains the entry to mark .lock.yml files as generated.
// It returns true if the file was modified, false if it was already up to date.
func ensureGitAttributes() (bool, error) {
gitLog.Print("Ensuring .gitattributes is updated")
gitRoot, err := gitutil.FindGitRoot()
if err != nil {
return false, err // Not in a git repository, skip
}
gitAttributesPath := filepath.Join(gitRoot, ".gitattributes")
lockYmlEntry := constants.WorkflowsLockYmlGitAttributesEntry
requiredEntries := []string{lockYmlEntry}
// Read existing .gitattributes file if it exists
var lines []string
if content, err := os.ReadFile(gitAttributesPath); err == nil {
lines = strings.Split(string(content), "\n")
gitLog.Printf("Read existing .gitattributes with %d lines", len(lines))
} else {
gitLog.Print("No existing .gitattributes file found")
}
modified := false
for _, required := range requiredEntries {
found := false
for i, line := range lines {
trimmedLine := strings.TrimSpace(line)
if trimmedLine == required {
found = true
break
}
// Only clean up the exact legacy gh-aw entry (with the ineffective
// "merge=ours" attribute); never rewrite other repository-owned lines
// that happen to start with the lock-yml glob.
if trimmedLine == constants.WorkflowsLockYmlGitAttributesEntryLegacy && required == lockYmlEntry {
gitLog.Print("Updating legacy .gitattributes entry format")
lines[i] = lockYmlEntry
found = true
modified = true
break
}
}
if !found {
gitLog.Printf("Adding new .gitattributes entry: %s", required)
if len(lines) > 0 && lines[len(lines)-1] != "" {
lines = append(lines, "")
}
lines = append(lines, required)
modified = true
}
}
if !modified {
gitLog.Print(".gitattributes already contains required entries")
return false, nil
}
// Write back to file with owner-only read/write permissions (0600) for security best practices
content := strings.Join(lines, "\n")
if err := os.WriteFile(gitAttributesPath, []byte(content), constants.FilePermSensitive); err != nil {
gitLog.Printf("Failed to write .gitattributes: %v", err)
return false, fmt.Errorf("failed to write .gitattributes: %w", err)
}
gitLog.Print("Successfully updated .gitattributes")
return true, nil
}
// stageGitAttributesIfChanged stages .gitattributes if it was modified
func stageGitAttributesIfChanged() error {
gitRoot, err := gitutil.FindGitRoot()
if err != nil {
return err
}
gitAttributesPath := filepath.Join(gitRoot, ".gitattributes")
return exec.Command("git", "-C", gitRoot, "add", gitAttributesPath).Run()
}
// ensureLogsGitignore ensures that .github/aw/logs/.gitignore exists to ignore log files
func ensureLogsGitignore() error {
gitLog.Print("Ensuring .github/aw/logs/.gitignore exists")
gitRoot, err := gitutil.FindGitRoot()
if err != nil {
return err // Not in a git repository, skip
}
gitignorePath := filepath.Join(gitRoot, ".github", "aw", "logs", ".gitignore")
// Check if .gitignore already exists
if fileutil.FileExists(gitignorePath) {
gitLog.Print(".github/aw/logs/.gitignore already exists") //nolint:hardcodedfilepath
return nil
}
gitLog.Print("Creating .github/aw/logs directory and .gitignore")
// Create the logs directory if it doesn't exist
if err := fileutil.EnsureParentDir(gitignorePath, constants.DirPermPublic); err != nil {
gitLog.Printf("Failed to ensure .github/aw/logs/.gitignore parent directory: %v", err)
return fmt.Errorf("failed to create parent directory for .github/aw/logs/.gitignore: %w", err)
}
// Write the .gitignore file with owner-only read/write permissions (0600) for security best practices
gitignoreContent := `# Ignore all downloaded workflow logs
*
# But keep the .gitignore file itself
!.gitignore
`
if err := os.WriteFile(gitignorePath, []byte(gitignoreContent), constants.FilePermSensitive); err != nil {
gitLog.Printf("Failed to write .gitignore: %v", err)
return fmt.Errorf("failed to write .github/aw/logs/.gitignore: %w", err)
}
gitLog.Print("Successfully created .github/aw/logs/.gitignore")
return nil
}
// getCurrentBranch gets the current git branch name in the current working directory.
func getCurrentBranch() (string, error) {
return getCurrentBranchIn("")
}
// getCurrentBranchIn gets the current git branch name in the given directory.
// Pass an empty string to use the current working directory.
func getCurrentBranchIn(dir string) (string, error) {
if dir == "" {
gitLog.Print("Getting current git branch")
} else {
gitLog.Printf("Getting current git branch in %s", dir)
}
cmd := exec.Command("git", "branch", "--show-current")
if dir != "" {
cmd.Dir = dir
}
output, err := cmd.Output()
if err != nil {
gitLog.Printf("Failed to get current branch: %v", err)
return "", fmt.Errorf("failed to get current branch: %w", err)
}
branch := strings.TrimSpace(string(output))
if branch == "" {
gitLog.Print("Could not determine current branch")
return "", errors.New("could not determine current branch")
}
gitLog.Printf("Current branch: %s", branch)
return branch, nil
}
// createAndSwitchBranch creates a new branch and switches to it
func createAndSwitchBranch(branchName string, verbose bool) error {
console.LogVerbose(verbose, "Creating and switching to branch: "+branchName)
cmd := exec.Command("git", "checkout", "-b", branchName)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to create and switch to branch %s: %w", branchName, err)
}
return nil
}
// switchBranch switches to the specified branch
func switchBranch(branchName string, verbose bool) error {
console.LogVerbose(verbose, "Switching to branch: "+branchName)
cmd := exec.Command("git", "checkout", branchName)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to switch to branch %s: %w", branchName, err)
}
return nil
}
// commitChanges commits all staged changes with the given message
func commitChanges(message string, verbose bool) error {
console.LogVerbose(verbose, "Committing changes with message: "+message)
cmd := exec.Command("git", "commit", "-m", message)
if output, err := cmd.CombinedOutput(); err != nil {
gitLog.Printf("Failed to commit: %v, output: %s", err, string(output))
outputStr := strings.TrimSpace(string(output))
return fmt.Errorf("failed to commit changes: %w\n%s", err, outputStr)
}
return nil
}
// pushBranch pushes the specified branch to origin
func pushBranch(branchName string, verbose bool) error {
console.LogVerbose(verbose, "Pushing branch: "+branchName)
cmd := exec.Command("git", "push", "-u", "origin", branchName)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to push branch %s: %w", branchName, err)
}
return nil
}
// hasPendingChanges reports whether the working directory has any uncommitted
// changes (staged or unstaged). Returns (false, nil) for a clean tree.
func hasPendingChanges() (bool, error) {
cmd := exec.Command("git", "status", "--porcelain")
output, err := cmd.Output()
if err != nil {
return false, fmt.Errorf("failed to check git status: %w", err)
}
return strings.TrimSpace(string(output)) != "", nil
}
// checkCleanWorkingDirectory checks if there are uncommitted changes
func checkCleanWorkingDirectory(verbose bool) error {
console.LogVerbose(verbose, "Checking for uncommitted changes...")
cmd := exec.Command("git", "status", "--porcelain")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to check git status: %w", err)
}
if strings.TrimSpace(string(output)) != "" {
return errors.New("working directory has uncommitted changes, please commit or stash them first")
}
console.LogVerbose(verbose, "Working directory is clean")
return nil
}
// WorkflowFileStatus represents the status of a workflow file in git
type WorkflowFileStatus struct {
IsModified bool // File has unstaged changes
IsStaged bool // File has staged changes
HasUnpushedCommits bool // File has unpushed commits affecting it
}
// checkWorkflowFileStatus checks if a workflow file has local modifications, staged changes, or unpushed commits
func checkWorkflowFileStatus(workflowPath string) (*WorkflowFileStatus, error) {
gitLog.Printf("Checking status for workflow file: %s", workflowPath)
status := &WorkflowFileStatus{}
// Check if we're in a git repository
if !isGitRepo() {
gitLog.Print("Not in a git repository")
return status, nil
}
// Get the absolute path relative to git root
gitRoot, err := gitutil.FindGitRoot()
if err != nil {
gitLog.Printf("Failed to find git root: %v", err)
return status, nil // Not in a git repository, return empty status
}
// Make path relative to git root if it's absolute
var relPath string
if filepath.IsAbs(workflowPath) {
var err error
relPath, err = filepath.Rel(gitRoot, workflowPath)
if err != nil {
gitLog.Printf("Failed to make path relative: %v", err)
relPath = workflowPath
}
} else {
relPath = workflowPath
}
gitLog.Printf("Checking git status for: %s", relPath)
// Check for modified or staged changes using git status --porcelain
cmd := exec.Command("git", "-C", gitRoot, "status", "--porcelain", relPath)
output, err := cmd.Output()
if err != nil {
gitLog.Printf("Failed to check git status: %v", err)
return status, nil // Ignore error, return empty status
}
statusOutput := string(output) // Don't trim - the leading space is significant!
if statusOutput != "" {
gitLog.Printf("Git status output: %q", statusOutput)
// Parse the status line (format: XY filename)
// X = index (staged) status, Y = working tree (unstaged) status
// The format is exactly 2 characters followed by a space and then the filename
if len(statusOutput) >= 2 {
stagedStatus := statusOutput[0]
unstagedStatus := statusOutput[1]
// Check if file is staged (first character is not space or ?)
if stagedStatus != ' ' && stagedStatus != '?' {
status.IsStaged = true
gitLog.Print("File has staged changes")
}
// Check if file is modified in working tree (second character is M or other modification indicators)
if unstagedStatus == 'M' || unstagedStatus == 'D' || unstagedStatus == 'A' {
status.IsModified = true
gitLog.Print("File has unstaged modifications")
}
}
}
// Check for unpushed commits that affect this file
// First, check if there's a remote tracking branch
cmd = exec.Command("git", "-C", gitRoot, "rev-parse", "--abbrev-ref", "@{u}")
output, err = cmd.Output()
if err != nil {
// No upstream branch configured, skip unpushed commits check
gitLog.Print("No upstream branch configured")
return status, nil
}
upstream := strings.TrimSpace(string(output))
gitLog.Printf("Upstream branch: %s", upstream)
// Check if there are commits in the current branch that affect this file and aren't in upstream
cmd = exec.Command("git", "-C", gitRoot, "log", upstream+"..HEAD", "--oneline", "--", relPath)
output, err = cmd.Output()
if err != nil {
gitLog.Printf("Failed to check unpushed commits: %v", err)
return status, nil // Ignore error, return current status
}
if strings.TrimSpace(string(output)) != "" {
status.HasUnpushedCommits = true
gitLog.Print("File has unpushed commits")
}
return status, nil
}
// stageAllChanges stages all modified files using git add -A
func stageAllChanges(verbose bool) error {
gitLog.Print("Staging all changes")
addCmd := exec.Command("git", "add", "-A")
if output, err := addCmd.CombinedOutput(); err != nil {
gitLog.Printf("Failed to stage changes: %v, output: %s", err, string(output))
return fmt.Errorf("failed to stage changes: %w", err)
}
gitLog.Print("Successfully staged all changes")
return nil
}
func isGHCLIAvailable() bool {
cmd := exec.Command("gh", "--version")
available := cmd.Run() == nil
gitLog.Printf("Checked gh CLI availability: available=%v", available)
return available
}