Repository navigation
Expand file tree
/
Copy pathgit.go
More file actions
390 lines (327 loc) · 11.4 KB
/
Copy pathgit.go
File metadata and controls
390 lines (327 loc) · 11.4 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
package git
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"time"
cligit "github.com/cli/cli/v2/git"
)
// client is a shared git client used by all package-level functions.
var client = &cligit.Client{}
// ErrMultipleRemotes is returned by ResolveRemote when multiple remotes
// are configured and none is designated as the push target.
type ErrMultipleRemotes struct {
Remotes []string
}
func (e *ErrMultipleRemotes) Error() string {
return fmt.Sprintf("multiple remotes configured: %s", strings.Join(e.Remotes, ", "))
}
// CommitInfo holds metadata about a single commit.
type CommitInfo struct {
SHA string
Subject string
Body string
Time time.Time
}
// run executes an arbitrary git command via the client and returns trimmed stdout.
func run(args ...string) (string, error) {
cmd, err := client.Command(context.Background(), args...)
if err != nil {
return "", err
}
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// runSilent executes a git command via the client and only returns an error.
func runSilent(args ...string) error {
cmd, err := client.Command(context.Background(), args...)
if err != nil {
return err
}
return cmd.Run()
}
// runInteractive runs a git command with stdin/stdout/stderr connected to
// the terminal, allowing interactive programs like editors to work.
func runInteractive(args ...string) error {
cmd := exec.Command("git", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// rebaseContinueOnce runs a single git rebase --continue without auto-resolve.
func rebaseContinueOnce() error {
cmd := exec.Command("git", "rebase", "--continue")
cmd.Env = append(os.Environ(), "GIT_EDITOR=true")
return cmd.Run()
}
// tryAutoResolveRebase checks whether rerere has resolved all conflicts
// from a failed rebase. If so, it auto-continues the rebase (potentially
// multiple times for multi-commit rebases). Returns originalErr if any
// conflicts remain that need manual resolution.
func tryAutoResolveRebase(originalErr error) error {
for i := 0; i < 1000; i++ {
if !IsRebaseInProgress() {
return nil
}
conflicts, err := ConflictedFiles()
if err != nil {
return originalErr
}
if len(conflicts) > 0 {
return originalErr
}
// Rerere resolved all conflicts — auto-continue.
if rebaseContinueOnce() == nil {
return nil
}
// Continue hit another conflicting commit; loop to check
// if rerere resolved that one too.
}
return originalErr
}
// --- Public functions delegate through the ops interface ---
// GitDir returns the path to the .git directory.
func GitDir() (string, error) {
return ops.GitDir()
}
// CurrentBranch returns the name of the current branch.
func CurrentBranch() (string, error) {
return ops.CurrentBranch()
}
// BranchExists returns whether a local branch with the given name exists.
func BranchExists(name string) bool {
return ops.BranchExists(name)
}
// CheckoutBranch switches to the specified branch.
func CheckoutBranch(name string) error {
return ops.CheckoutBranch(name)
}
// Fetch fetches from the given remote.
func Fetch(remote string) error {
return ops.Fetch(remote)
}
// FetchBranches fetches specific branches from a remote,
// updating their tracking refs.
func FetchBranches(remote string, branches []string) error {
return ops.FetchBranches(remote, branches)
}
// DefaultBranch returns the HEAD branch from origin.
func DefaultBranch() (string, error) {
return ops.DefaultBranch()
}
// CreateBranch creates a new branch from the given base.
func CreateBranch(name, base string) error {
return ops.CreateBranch(name, base)
}
// Push pushes branches to a remote with optional force and atomic flags.
func Push(remote string, branches []string, force, atomic bool) error {
return ops.Push(remote, branches, force, atomic)
}
// ResolveRemote determines the remote for pushing a branch. Checks git
// config in priority order, falls back to listing remotes. Returns
// *ErrMultipleRemotes if multiple remotes exist with no configured default.
func ResolveRemote(branch string) (string, error) {
return ops.ResolveRemote(branch)
}
// Rebase rebases the current branch onto the given base.
// If rerere resolves all conflicts automatically, the rebase continues
// without user intervention.
func Rebase(base string) error {
return ops.Rebase(base)
}
// EnableRerere enables git rerere (reuse recorded resolution) and
// rerere.autoupdate (auto-stage resolved files) for the repository.
func EnableRerere() error {
return ops.EnableRerere()
}
// IsRerereEnabled returns whether rerere.enabled is set to "true" in git config.
func IsRerereEnabled() (bool, error) {
return ops.IsRerereEnabled()
}
// IsRerereDeclined returns whether the user previously declined the rerere prompt.
func IsRerereDeclined() (bool, error) {
return ops.IsRerereDeclined()
}
// SaveRerereDeclined records that the user declined the rerere prompt.
func SaveRerereDeclined() error {
return ops.SaveRerereDeclined()
}
// RebaseOnto rebases a branch using the three-argument form:
//
// git rebase --onto <newBase> <oldBase> <branch>
//
// This replays commits after oldBase from branch onto newBase. It is used
// when a prior branch was merged and the normal rebase cannot detect
// which commits have already been applied.
// If rerere resolves all conflicts automatically, the rebase continues
// without user intervention.
func RebaseOnto(newBase, oldBase, branch string) error {
return ops.RebaseOnto(newBase, oldBase, branch)
}
// RebaseContinue continues an in-progress rebase.
// It sets GIT_EDITOR=true to prevent git from opening an interactive editor
// for the commit message, which would cause the command to hang.
// If rerere resolves subsequent conflicts automatically, the rebase continues
// without user intervention.
func RebaseContinue() error {
return ops.RebaseContinue()
}
// RebaseAbort aborts an in-progress rebase.
func RebaseAbort() error {
return ops.RebaseAbort()
}
// IsRebaseInProgress checks whether a rebase is currently in progress.
func IsRebaseInProgress() bool {
return ops.IsRebaseInProgress()
}
// ConflictedFiles returns the list of files that have merge conflicts.
func ConflictedFiles() ([]string, error) {
return ops.ConflictedFiles()
}
// ConflictMarkerInfo holds the location of conflict markers in a file.
type ConflictMarkerInfo struct {
File string
Sections []ConflictSection
}
// ConflictSection represents a single conflict hunk in a file.
type ConflictSection struct {
StartLine int // line number of <<<<<<<
EndLine int // line number of >>>>>>>
}
// FindConflictMarkers scans a file for conflict markers and returns their locations.
func FindConflictMarkers(filePath string) (*ConflictMarkerInfo, error) {
return ops.FindConflictMarkers(filePath)
}
// IsAncestor returns whether ancestor is an ancestor of descendant.
// This is useful to check if a fast-forward merge is possible.
func IsAncestor(ancestor, descendant string) (bool, error) {
return ops.IsAncestor(ancestor, descendant)
}
// RevParse resolves a ref to its full SHA via git rev-parse.
func RevParse(ref string) (string, error) {
return ops.RevParse(ref)
}
// RevParseMulti resolves multiple refs to their full SHAs in a single
// git rev-parse invocation. Returns SHAs in the same order as the input refs.
func RevParseMulti(refs []string) ([]string, error) {
return ops.RevParseMulti(refs)
}
// RevParseMap resolves multiple refs and returns a ref→SHA map.
func RevParseMap(refs []string) (map[string]string, error) {
shas, err := ops.RevParseMulti(refs)
if err != nil {
return nil, err
}
m := make(map[string]string, len(refs))
for i, ref := range refs {
m[ref] = shas[i]
}
return m, nil
}
// MergeBase returns the best common ancestor commit between two refs.
func MergeBase(a, b string) (string, error) {
return ops.MergeBase(a, b)
}
// Log returns recent commits for the given branch.
func Log(ref string, maxCount int) ([]CommitInfo, error) {
return ops.Log(ref, maxCount)
}
// LogRange returns commits in the range base..head (commits reachable from head
// but not from base). This is useful for seeing all commits unique to a branch.
func LogRange(base, head string) ([]CommitInfo, error) {
return ops.LogRange(base, head)
}
// DiffStatRange returns the total additions and deletions between two refs.
func DiffStatRange(base, head string) (additions, deletions int, err error) {
return ops.DiffStatRange(base, head)
}
// FileDiffStat holds per-file diff statistics.
type FileDiffStat struct {
Path string
Additions int
Deletions int
}
// DiffStatFiles returns per-file additions and deletions between two refs.
func DiffStatFiles(base, head string) ([]FileDiffStat, error) {
return ops.DiffStatFiles(base, head)
}
// DeleteBranch deletes a local branch.
func DeleteBranch(name string, force bool) error {
return ops.DeleteBranch(name, force)
}
// DeleteRemoteBranch deletes a branch on the remote.
func DeleteRemoteBranch(remote, branch string) error {
return ops.DeleteRemoteBranch(remote, branch)
}
// ResetHard resets the current branch to the given ref.
func ResetHard(ref string) error {
return ops.ResetHard(ref)
}
// SetUpstreamTracking sets the upstream tracking branch.
func SetUpstreamTracking(branch, remote string) error {
return ops.SetUpstreamTracking(branch, remote)
}
// MergeFF fast-forwards the currently checked-out branch using a merge.
func MergeFF(target string) error {
return ops.MergeFF(target)
}
// UpdateBranchRef moves a branch pointer to a new commit (for branches not currently checked out).
func UpdateBranchRef(branch, sha string) error {
return ops.UpdateBranchRef(branch, sha)
}
// StageAll stages all changes including untracked files (git add -A).
func StageAll() error {
return ops.StageAll()
}
// StageTracked stages changes to tracked files only (git add -u).
func StageTracked() error {
return ops.StageTracked()
}
// HasStagedChanges returns true if there are staged changes ready to commit.
func HasStagedChanges() bool {
return ops.HasStagedChanges()
}
// Commit creates a commit with the given message and returns the new HEAD SHA.
func Commit(message string) (string, error) {
return ops.Commit(message)
}
// CommitInteractive launches the user's configured editor for the commit
// message, equivalent to running `git commit` without `-m`.
func CommitInteractive() (string, error) {
return ops.CommitInteractive()
}
// ValidateRefName checks whether name is a valid git branch name.
func ValidateRefName(name string) error {
return ops.ValidateRefName(name)
}
// RenameBranch renames a local branch.
func RenameBranch(oldName, newName string) error {
return ops.RenameBranch(oldName, newName)
}
// CherryPick applies the given commits to the current branch.
func CherryPick(commits []string) error {
return ops.CherryPick(commits)
}
// CherryPickAbort clears any in-progress cherry-pick state.
// Errors are silently ignored (no-op if no cherry-pick is in progress).
func CherryPickAbort() {
_ = ops.CherryPickAbort()
}
// CherryPickContinue continues an in-progress cherry-pick after conflicts are resolved.
func CherryPickContinue() error {
return ops.CherryPickContinue()
}
// HasUncommittedChanges returns true if the working tree has uncommitted changes.
func HasUncommittedChanges() (bool, error) {
return ops.HasUncommittedChanges()
}
// LogMerges returns merge commits in the range base..head.
func LogMerges(base, head string) ([]CommitInfo, error) {
return ops.LogMerges(base, head)
}