-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
195 lines (167 loc) · 4.76 KB
/
Copy pathgit.go
File metadata and controls
195 lines (167 loc) · 4.76 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
package git
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Git wraps git CLI commands
type Git struct {
repoDir string
}
// New creates a Git wrapper for the given repo directory
func New(repoDir string) *Git {
return &Git{repoDir: repoDir}
}
// run executes a git command and returns stdout
func (g *Git) run(args ...string) (string, error) {
cmd := exec.Command("git", append([]string{"-C", g.repoDir}, args...)...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("git %s: %s", strings.Join(args, " "), stderr.String())
}
return strings.TrimSpace(stdout.String()), nil
}
// runSilent executes a git command, ignoring stderr
func (g *Git) runSilent(args ...string) (string, error) {
cmd := exec.Command("git", append([]string{"-C", g.repoDir}, args...)...)
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = nil
err := cmd.Run()
return strings.TrimSpace(stdout.String()), err
}
// Init initializes a new git repository
func (g *Git) Init() error {
if err := os.MkdirAll(g.repoDir, 0755); err != nil {
return err
}
_, err := g.run("init")
return err
}
// Clone clones a remote repository
func Clone(url, dest string) error {
cmd := exec.Command("git", "clone", url, dest)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// AddAll stages all changes
func (g *Git) AddAll() error {
_, err := g.run("add", "-A")
return err
}
// Commit creates a commit with the given message
func (g *Git) Commit(message string) error {
// -c commit.gpgsign=false avoids failures on machines with GPG signing enabled globally
_, err := g.run("-c", "commit.gpgsign=false", "commit", "-m", message)
return err
}
// HasChanges checks if there are staged changes to commit
func (g *Git) HasChanges() (bool, error) {
_, err := g.runSilent("diff", "--cached", "--quiet")
if err != nil {
// Non-zero exit means there are changes
return true, nil
}
return false, nil
}
// Push pushes to remote
func (g *Git) Push() error {
_, err := g.run("push", "origin", "HEAD")
return err
}
// Pull pulls from remote
func (g *Git) Pull() error {
_, err := g.run("pull", "origin", "HEAD")
if err != nil && strings.Contains(err.Error(), "unrelated histories") {
// Retry with --allow-unrelated-histories
_, err = g.run("pull", "origin", "HEAD", "--allow-unrelated-histories")
}
return err
}
// Fetch fetches from remote
func (g *Git) Fetch() error {
_, _ = g.runSilent("fetch", "origin")
return nil // Ignore errors, fetch is best-effort
}
// HasRemote checks if origin remote exists
func (g *Git) HasRemote() bool {
out, _ := g.runSilent("remote")
return strings.Contains(out, "origin")
}
// AddRemote adds a remote
func (g *Git) AddRemote(name, url string) error {
_, err := g.run("remote", "add", name, url)
return err
}
// RemoveRemote removes a remote
func (g *Git) RemoveRemote(name string) error {
_, err := g.run("remote", "remove", name)
return err
}
// GetLocalCommit returns the current HEAD commit hash
func (g *Git) GetLocalCommit() (string, error) {
return g.runSilent("rev-parse", "HEAD")
}
// GetRemoteCommit returns the origin/HEAD commit hash
func (g *Git) GetRemoteCommit() (string, error) {
return g.runSilent("rev-parse", "origin/HEAD")
}
// IsRepo checks if the directory is a git repository
func (g *Git) IsRepo() bool {
_, err := os.Stat(filepath.Join(g.repoDir, ".git"))
return err == nil
}
// IsInstalled checks if git is available
func IsInstalled() bool {
_, err := exec.LookPath("git")
return err == nil
}
// IsValidRepoURL checks if a string looks like a valid git repo URL
func IsValidRepoURL(url string) bool {
// HTTPS URLs
if strings.HasPrefix(url, "https://") || strings.HasPrefix(url, "http://") {
return strings.Contains(url, "/")
}
// SSH URLs (git@host:user/repo)
if strings.HasPrefix(url, "git@") {
return strings.Contains(url, ":")
}
// SSH URLs (ssh://git@host/user/repo)
if strings.HasPrefix(url, "ssh://") {
return strings.Contains(url, "/")
}
return false
}
// CheckRemote verifies a remote URL is accessible
func CheckRemote(url string) error {
cmd := exec.Command("git", "ls-remote", "--exit-code", url)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
errMsg := strings.TrimSpace(stderr.String())
if errMsg != "" {
return fmt.Errorf("%s", errMsg)
}
return fmt.Errorf("repository not found or not accessible")
}
return nil
}
// CreateInitialCommit creates a README and initial commit
func (g *Git) CreateInitialCommit() error {
readme := filepath.Join(g.repoDir, "README.md")
if err := os.WriteFile(readme, []byte("# Claude Code Sync\n"), 0644); err != nil {
return err
}
if _, err := g.run("add", "README.md"); err != nil {
return err
}
return g.Commit("Initial commit")
}