-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.go
More file actions
451 lines (392 loc) · 13.3 KB
/
git.go
File metadata and controls
451 lines (392 loc) · 13.3 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
// internal/git/git.go
package git
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/cli/safeexec"
)
// ErrDirtyWorkTree is returned when the working tree has uncommitted changes.
var ErrDirtyWorkTree = errors.New("working tree has uncommitted changes")
var (
gitPath string
gitPathOnce sync.Once
gitPathErr error
)
// resolveGitPath finds the git executable using safeexec to prevent PATH injection.
func resolveGitPath() (string, error) {
gitPathOnce.Do(func() {
gitPath, gitPathErr = safeexec.LookPath("git")
})
return gitPath, gitPathErr
}
// Git provides git operations for a repository.
type Git struct {
repoPath string
}
// New creates a Git instance for the repository at the given path.
func New(repoPath string) *Git {
return &Git{repoPath: repoPath}
}
// run executes a git command and returns stdout. Stderr is captured for error messages.
func (g *Git) run(args ...string) (string, error) {
gitBin, err := resolveGitPath()
if err != nil {
return "", fmt.Errorf("failed to find git: %w", err)
}
fullArgs := append([]string{"-C", g.repoPath}, args...)
cmd := exec.Command(gitBin, fullArgs...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
if stderr.Len() > 0 {
return "", fmt.Errorf("git %s: %s", args[0], strings.TrimSpace(stderr.String()))
}
return "", fmt.Errorf("git %s: %w", args[0], err)
}
return strings.TrimSpace(stdout.String()), nil
}
// runInteractive executes a git command with stdout/stderr connected to the terminal.
func (g *Git) runInteractive(args ...string) error {
gitBin, err := resolveGitPath()
if err != nil {
return fmt.Errorf("failed to find git: %w", err)
}
fullArgs := append([]string{"-C", g.repoPath}, args...)
cmd := exec.Command(gitBin, fullArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// runSilent executes a git command and discards output, returning only success/failure.
func (g *Git) runSilent(args ...string) error {
_, err := g.run(args...)
return err
}
// CurrentBranch returns the name of the current branch.
func (g *Git) CurrentBranch() (string, error) {
return g.run("rev-parse", "--abbrev-ref", "HEAD")
}
// BranchExists checks if a branch exists.
func (g *Git) BranchExists(branch string) bool {
err := g.runSilent("rev-parse", "--verify", "refs/heads/"+branch)
return err == nil
}
// CreateBranch creates a new branch at the current HEAD.
func (g *Git) CreateBranch(name string) error {
return g.runSilent("branch", name)
}
// Checkout switches to the specified branch.
func (g *Git) Checkout(branch string) error {
return g.runSilent("checkout", branch)
}
// CreateAndCheckout creates a new branch and switches to it.
func (g *Git) CreateAndCheckout(name string) error {
return g.runSilent("checkout", "-b", name)
}
// IsDirty returns true if there are uncommitted changes (staged or unstaged).
func (g *Git) IsDirty() (bool, error) {
out, err := g.run("status", "--porcelain")
if err != nil {
return false, err
}
return len(out) > 0, nil
}
// HasStagedChanges returns true if there are staged changes.
func (g *Git) HasStagedChanges() (bool, error) {
err := g.runSilent("diff", "--cached", "--quiet")
if err != nil {
// Exit code 1 means there are differences
return true, nil
}
return false, nil
}
// Commit creates a commit with the given message.
func (g *Git) Commit(message string) error {
return g.runSilent("commit", "-m", message)
}
// Push force-pushes a branch to origin with lease.
func (g *Git) Push(branch string, force bool) error {
args := []string{"push", "origin", branch}
if force {
args = append(args, "--force-with-lease")
}
return g.runInteractive(args...)
}
// GetMergeBase returns the merge base of two branches.
func (g *Git) GetMergeBase(a, b string) (string, error) {
return g.run("merge-base", a, b)
}
// GetTip returns the commit SHA at the tip of a branch.
func (g *Git) GetTip(branch string) (string, error) {
return g.run("rev-parse", branch)
}
// NeedsRebase returns true if branch needs to be rebased onto parent.
func (g *Git) NeedsRebase(branch, parent string) (bool, error) {
mergeBase, err := g.GetMergeBase(branch, parent)
if err != nil {
return false, err
}
parentTip, err := g.GetTip(parent)
if err != nil {
return false, err
}
return mergeBase != parentTip, nil
}
// Rebase rebases the current branch onto target.
func (g *Git) Rebase(onto string) error {
return g.runInteractive("rebase", onto)
}
// CommitExists checks if a commit SHA exists in the repository.
func (g *Git) CommitExists(sha string) bool {
err := g.runSilent("cat-file", "-e", sha+"^{commit}")
return err == nil
}
// HasUnmergedCommits returns true if the branch has commits not yet in upstream.
// Uses git cherry to detect by diff content, which works for cherry-picks
// where the commit SHAs differ but the content is the same.
// Note: This does NOT detect squash merges where multiple commits are combined.
// Use IsContentMerged for squash merge detection.
func (g *Git) HasUnmergedCommits(branch, upstream string) (bool, error) {
// git cherry -v upstream branch
// Returns lines prefixed with + (unmerged) or - (merged/equivalent)
out, err := g.run("cherry", "-v", upstream, branch)
if err != nil {
return false, err
}
// Empty output means no commits unique to branch
if out == "" {
return false, nil
}
// If any line starts with +, there are unmerged commits
for _, line := range strings.Split(out, "\n") {
if strings.HasPrefix(line, "+ ") {
return true, nil
}
}
return false, nil
}
// IsContentMerged returns true if the branch has no content differences from upstream.
// This detects squash merges where the tree content is identical even though
// the commit history differs. Returns true if `git diff upstream branch` is empty.
func (g *Git) IsContentMerged(branch, upstream string) (bool, error) {
// git diff upstream branch --quiet exits 0 if no diff, 1 if diff exists
err := g.runSilent("diff", "--quiet", upstream, branch)
if err != nil {
// Exit code 1 means there are differences
return false, nil
}
// Exit code 0 means no differences - content is merged
return true, nil
}
// RemoteBranchExists checks if a branch exists on the remote (origin).
func (g *Git) RemoteBranchExists(branch string) bool {
err := g.runSilent("ls-remote", "--exit-code", "--heads", "origin", branch)
return err == nil
}
// ListRemoteBranches returns the set of branch names that exist on origin,
// based on locally-cached remote-tracking refs (no network call).
// This is accurate as long as a fetch or push has been done recently.
func (g *Git) ListRemoteBranches() (map[string]bool, error) {
out, err := g.run("for-each-ref", "--format=%(refname:strip=3)", "refs/remotes/origin/")
if err != nil {
return nil, err
}
branches := make(map[string]bool)
for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if line != "" && line != "HEAD" {
branches[line] = true
}
}
return branches, nil
}
// RebaseOnto rebases a branch onto a new base, replaying only commits after oldBase.
// Checks out the branch first, then runs: git rebase --onto <newBase> <oldBase>
// Useful when a parent branch was squash-merged and we need to replay only
// the commits unique to the child branch.
func (g *Git) RebaseOnto(newBase, oldBase, branch string) error {
// Checkout the branch to rebase (git rebase operates on HEAD)
if err := g.Checkout(branch); err != nil {
return err
}
return g.runInteractive("rebase", "--onto", newBase, oldBase)
}
// RebaseContinue continues an in-progress rebase.
func (g *Git) RebaseContinue() error {
return g.runInteractive("rebase", "--continue")
}
// RebaseAbort aborts an in-progress rebase.
func (g *Git) RebaseAbort() error {
return g.runSilent("rebase", "--abort")
}
// IsRebaseInProgress checks if a rebase is in progress.
func (g *Git) IsRebaseInProgress() bool {
rebaseMerge := filepath.Join(g.repoPath, ".git", "rebase-merge")
rebaseApply := filepath.Join(g.repoPath, ".git", "rebase-apply")
_, err1 := os.Stat(rebaseMerge)
_, err2 := os.Stat(rebaseApply)
return err1 == nil || err2 == nil
}
// GetGitDir returns the .git directory path.
func (g *Git) GetGitDir() string {
return filepath.Join(g.repoPath, ".git")
}
// Fetch fetches from origin.
func (g *Git) Fetch() error {
return g.runInteractive("fetch", "origin")
}
// FastForward fast-forwards a branch to its remote tracking branch.
func (g *Git) FastForward(branch string) error {
// First checkout the branch
if err := g.Checkout(branch); err != nil {
return err
}
// Then merge with fast-forward only
return g.runSilent("merge", "--ff-only", "origin/"+branch)
}
// DeleteBranch deletes a local branch.
func (g *Git) DeleteBranch(branch string) error {
return g.runSilent("branch", "-D", branch)
}
// SetBranchRef sets a branch ref to point to a specific SHA.
// This is equivalent to `git branch -f <branch> <sha>`.
func (g *Git) SetBranchRef(branch, sha string) error {
return g.runSilent("branch", "-f", branch, sha)
}
// CreateBranchAt creates a new branch at a specific SHA.
// This is equivalent to `git branch <name> <sha>`.
func (g *Git) CreateBranchAt(name, sha string) error {
return g.runSilent("branch", name, sha)
}
// Stash creates a stash with the given message and returns the stash commit hash.
// Returns an empty string if there was nothing to stash.
// Includes untracked files (-u) to capture all working tree changes.
// The returned hash is stable and can be used to restore this specific stash
// even if other stashes are created later.
func (g *Git) Stash(message string) (string, error) {
// Check if there's anything to stash first
dirty, err := g.IsDirty()
if err != nil {
return "", err
}
if !dirty {
return "", nil
}
// Create the stash with -u to include untracked files
if stashErr := g.runSilent("stash", "push", "-u", "-m", message); stashErr != nil {
return "", stashErr
}
// Resolve the created stash to a stable identifier (its commit hash)
// This is more reliable than stash@{0} which can shift if user creates more stashes
out, parseErr := g.run("rev-parse", "stash@{0}")
if parseErr != nil {
return "", parseErr
}
return out, nil
}
// StashPop restores a specific stash entry when a reference is provided.
// If ref is empty, it pops the most recent stash entry (matching `git stash pop`).
// When a commit hash is provided (from Stash()), uses apply+drop since pop only accepts stash refs.
// Returns an error if there are conflicts or no matching stash entry.
func (g *Git) StashPop(ref string) error {
if ref == "" {
return g.runInteractive("stash", "pop")
}
// git stash pop only accepts stash references (stash@{n}), not commit hashes.
// Use apply instead which accepts commit hashes.
if err := g.runInteractive("stash", "apply", ref); err != nil {
return err
}
// Find and drop the stash entry by matching its commit hash
stashRef, err := g.findStashByHash(ref)
if err != nil {
// Apply succeeded but we couldn't find stash to drop - not fatal
return nil
}
if stashRef != "" {
// Silently drop; errors aren't critical since apply already succeeded
_ = g.runSilent("stash", "drop", stashRef) //nolint:errcheck // best effort cleanup
}
return nil
}
// findStashByHash finds the stash@{n} reference for a given commit hash.
// Returns empty string if not found.
func (g *Git) findStashByHash(hash string) (string, error) {
// List stashes with their hashes
out, err := g.run("stash", "list", "--format=%H %gd")
if err != nil {
return "", err
}
for line := range strings.SplitSeq(out, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.SplitN(line, " ", 2)
if len(parts) == 2 && parts[0] == hash {
return parts[1], nil
}
}
return "", nil
}
// StashList returns true if there are any stash entries.
func (g *Git) StashList() (bool, error) {
out, err := g.run("stash", "list")
if err != nil {
return false, err
}
return len(out) > 0, nil
}
// Commit represents a git commit with its subject and body.
type Commit struct {
Subject string // First line of the commit message
Body string // Everything after the first line (may be empty)
}
// GetCommits returns the commits from base..head (commits in head not in base).
// Returns commits in reverse chronological order (newest first).
func (g *Git) GetCommits(base, head string) ([]Commit, error) {
// Use null byte separators for reliable parsing
// Format: subject\x00body\x00\x00 (double null between commits)
format := "%s%x00%b%x00%x00"
out, err := g.run("log", "--format="+format, base+".."+head)
if err != nil {
return nil, err
}
if out == "" {
return nil, nil
}
var commits []Commit
// Split by double null (between commits)
for entry := range strings.SplitSeq(out, "\x00\x00") {
entry = strings.TrimSpace(entry)
if entry == "" {
continue
}
// Split by single null (between subject and body)
parts := strings.SplitN(entry, "\x00", 2)
subject := strings.TrimSpace(parts[0])
var body string
if len(parts) > 1 {
body = strings.TrimSpace(parts[1])
}
if subject != "" {
commits = append(commits, Commit{Subject: subject, Body: body})
}
}
return commits, nil
}
// AbbrevSHA safely abbreviates a SHA to 7 characters.
// Returns the full string if it's shorter than 7 characters.
func AbbrevSHA(sha string) string {
if len(sha) <= 7 {
return sha
}
return sha[:7]
}