-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
254 lines (218 loc) · 5.99 KB
/
git.go
File metadata and controls
254 lines (218 loc) · 5.99 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
package keyring
import (
"bufio"
"context"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"strings"
"github.com/launchrctl/launchr"
)
// GitCredentialOp represents a git credential helper operation.
type GitCredentialOp string
const (
GitCredentialGet GitCredentialOp = "get"
GitCredentialStore GitCredentialOp = "store"
GitCredentialErase GitCredentialOp = "erase"
)
// GitCredential represents a git credential request/response.
type GitCredential struct {
Protocol string
Host string
Path string
Username string
Password string
}
// ParseGitCredential parses git credential helper input from stdin.
func ParseGitCredential(r io.Reader) (*GitCredential, error) {
cred := &GitCredential{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
break // Empty line terminates input
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
key, value := parts[0], parts[1]
switch key {
case "protocol":
cred.Protocol = value
case "host":
cred.Host = value
case "path":
cred.Path = value
case "username":
cred.Username = value
case "password":
cred.Password = value
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return cred, nil
}
// ToURL constructs a URL from the credential fields.
func (c *GitCredential) ToURL() string {
u := &url.URL{
Scheme: c.Protocol,
Host: c.Host,
}
if c.Path != "" {
u.Path = "/" + c.Path
}
return u.String()
}
// BaseURL returns the base URL (protocol://host) without path.
func (c *GitCredential) BaseURL() string {
return fmt.Sprintf("%s://%s", c.Protocol, c.Host)
}
// Write outputs the credential in git credential helper format.
func (c *GitCredential) Write(w io.Writer) error {
if c.Protocol != "" {
fmt.Fprintf(w, "protocol=%s\n", c.Protocol)
}
if c.Host != "" {
fmt.Fprintf(w, "host=%s\n", c.Host)
}
if c.Username != "" {
fmt.Fprintf(w, "username=%s\n", c.Username)
}
if c.Password != "" {
fmt.Fprintf(w, "password=%s\n", c.Password)
}
fmt.Fprintln(w) // Empty line to terminate
return nil
}
// HandleGitCredential handles git credential helper operations.
func HandleGitCredential(k Keyring, op GitCredentialOp, in io.Reader, out io.Writer) error {
switch op {
case GitCredentialGet:
return handleGitCredentialGet(k, in, out)
case GitCredentialStore:
return handleGitCredentialStore(k, in)
case GitCredentialErase:
return handleGitCredentialErase(k, in)
default:
return fmt.Errorf("unknown git credential operation: %s", op)
}
}
func handleGitCredentialGet(k Keyring, in io.Reader, out io.Writer) error {
cred, err := ParseGitCredential(in)
if err != nil {
return err
}
// Try to find credentials for this URL
baseURL := cred.BaseURL()
storedCreds, err := k.GetForURL(baseURL)
if err != nil {
// Try with https:// prefix if not found
if !strings.HasPrefix(baseURL, "https://") {
baseURL = "https://" + cred.Host
storedCreds, err = k.GetForURL(baseURL)
}
if err != nil {
return nil // No credentials found, git will try other methods
}
}
// Check if OAuth token needs refresh
if storedCreds.IsOAuth() && storedCreds.IsExpired() {
refreshed, changed, refreshErr := RefreshCredentials(context.Background(), storedCreds)
if refreshErr != nil {
launchr.Log().Warn("token refresh failed", "error", refreshErr)
// Continue with expired token, it might still work
} else if changed {
storedCreds = *refreshed
// Save the refreshed credentials
if err := k.AddItem(storedCreds); err == nil {
_ = k.Save()
}
}
}
// Return credentials to git
response := &GitCredential{
Protocol: cred.Protocol,
Host: cred.Host,
Username: storedCreds.Username,
Password: storedCreds.GetSecret(),
}
return response.Write(out)
}
func handleGitCredentialStore(k Keyring, in io.Reader) error {
cred, err := ParseGitCredential(in)
if err != nil {
return err
}
// Don't overwrite OAuth credentials with basic auth from git
baseURL := cred.BaseURL()
existing, err := k.GetForURL(baseURL)
if err == nil && existing.IsOAuth() {
// Don't overwrite OAuth credentials
return nil
}
// Store as basic auth credentials
item := CredentialsItem{
URL: baseURL,
Username: cred.Username,
Password: cred.Password,
AuthType: AuthTypeBasic,
}
if err := k.AddItem(item); err != nil {
return err
}
return k.Save()
}
func handleGitCredentialErase(k Keyring, in io.Reader) error {
cred, err := ParseGitCredential(in)
if err != nil {
return err
}
baseURL := cred.BaseURL()
if err := k.RemoveByURL(baseURL); err != nil {
return nil // Ignore not found errors
}
return k.Save()
}
// SetupGitCredentialHelper configures git to use plasmactl as credential helper.
func SetupGitCredentialHelper(global bool, urlPattern string, printer *launchr.Terminal) error {
// Get the path to the current executable
execPath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get executable path: %w", err)
}
// Build the credential helper command
// Git will call: plasmactl keyring:git --credential get
helperCmd := fmt.Sprintf("!%s keyring:git --credential", execPath)
// Build git config command
args := []string{"config"}
if global {
args = append(args, "--global")
}
if urlPattern != "" {
// URL-specific credential helper
// git config credential.https://example.com.helper "!plasmactl keyring:git --credential"
args = append(args, fmt.Sprintf("credential.%s.helper", urlPattern), helperCmd)
} else {
// Global credential helper
args = append(args, "credential.helper", helperCmd)
}
cmd := exec.Command("git", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to configure git: %w", err)
}
if urlPattern != "" {
printer.Success().Printfln("Git credential helper configured for %s", urlPattern)
} else if global {
printer.Success().Println("Git credential helper configured globally")
} else {
printer.Success().Println("Git credential helper configured for this repository")
}
return nil
}