-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchanges.go
More file actions
206 lines (179 loc) · 5.57 KB
/
Copy pathchanges.go
File metadata and controls
206 lines (179 loc) · 5.57 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
package git
import (
"bufio"
"context"
"fmt"
"os"
"strings"
)
// Stage
// Stage stages the provided paths without returning command output.
func (r Runner) Stage(ctx context.Context, paths ...string) error {
args := append([]string{"add", "--"}, paths...)
return r.run(ctx, args...)
}
// StageOutput stages the provided paths and returns Git's normal output for the UI.
func (r Runner) StageOutput(ctx context.Context, paths ...string) (string, error) {
args := append([]string{"add", "--"}, paths...)
return r.output(ctx, args...)
}
// StageAllOutput stages every changed and untracked file in the repo.
func (r Runner) StageAllOutput(ctx context.Context) (string, error) {
return r.output(ctx, "add", "--all")
}
// ConflictFiles returns files Git currently considers unmerged.
func (r Runner) ConflictFiles(ctx context.Context) ([]string, error) {
out, err := r.output(ctx, "diff", "--name-only", "--diff-filter=U")
if err != nil {
return nil, err
}
return nonEmptyLines(out), nil
}
// MarkResolvedOutput stages a conflict file after the user resolves it.
func (r Runner) MarkResolvedOutput(ctx context.Context, path string) (string, error) {
return r.StageOutput(ctx, path)
}
// ConflictMarkerReport summarizes conflict marker lines in a working tree file.
func (r Runner) ConflictMarkerReport(path string) string {
file, err := os.Open(r.workingPath(path))
if err != nil {
return "Conflict markers: unable to read file.\n"
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
lineNo := 0
for scanner.Scan() {
lineNo++
line := strings.TrimSpace(scanner.Text())
if isConflictMarker(line) {
lines = append(lines, fmt.Sprintf("%d: %s", lineNo, line))
if len(lines) >= 30 {
lines = append(lines, "...")
break
}
}
}
if len(lines) == 0 {
return "Conflict markers: none found in selected file.\n"
}
return "Conflict markers:\n" + strings.Join(lines, "\n") + "\n"
}
func isConflictMarker(line string) bool {
return strings.HasPrefix(line, "<<<<<<<") ||
strings.HasPrefix(line, "=======") ||
strings.HasPrefix(line, ">>>>>>>")
}
// Unstage
// Unstage removes the provided paths from the index without returning output.
func (r Runner) Unstage(ctx context.Context, paths ...string) error {
args := append([]string{"restore", "--staged", "--"}, paths...)
return r.run(ctx, args...)
}
// UnstageOutput removes paths from the index and returns Git's normal output.
func (r Runner) UnstageOutput(ctx context.Context, paths ...string) (string, error) {
args := append([]string{"restore", "--staged", "--"}, paths...)
return r.output(ctx, args...)
}
// UnstageAllOutput removes every staged path from the index.
func (r Runner) UnstageAllOutput(ctx context.Context) (string, error) {
return r.output(ctx, "restore", "--staged", "--", ".")
}
// DiscardOutput removes all working tree and index changes for one status row.
func (r Runner) DiscardOutput(ctx context.Context, file FileStatus) (string, error) {
path := file.Path
if strings.TrimSpace(path) == "" {
return "", nil
}
if file.Index == '?' {
return r.output(ctx, "clean", "-fd", "--", path)
}
var outputs []string
if file.Renamed() {
args := append([]string{"restore", "--staged", "--"}, file.GitPaths()...)
out, err := r.output(ctx, args...)
outputs = append(outputs, out)
if err != nil {
return joinOutput(outputs...), err
}
clean, cleanErr := r.output(ctx, "clean", "-fd", "--", file.Path)
outputs = append(outputs, clean)
if cleanErr != nil {
return joinOutput(outputs...), cleanErr
}
restore, restoreErr := r.output(ctx, "restore", "--worktree", "--", file.OldPath)
outputs = append(outputs, restore)
if restoreErr != nil {
return joinOutput(outputs...), restoreErr
}
return joinOutput(outputs...), nil
}
if file.Staged() {
out, err := r.output(ctx, "restore", "--staged", "--", path)
outputs = append(outputs, out)
if err != nil {
return joinOutput(outputs...), err
}
}
if file.Index == 'A' {
out, err := r.output(ctx, "clean", "-fd", "--", path)
outputs = append(outputs, out)
if err != nil {
return joinOutput(outputs...), err
}
return joinOutput(outputs...), nil
}
out, err := r.output(ctx, "restore", "--worktree", "--", path)
outputs = append(outputs, out)
if err != nil {
return joinOutput(outputs...), err
}
return joinOutput(outputs...), nil
}
// Diff And Review
// Diff returns the unstaged diff, optionally scoped to a single path.
func (r Runner) Diff(ctx context.Context, paths ...string) (string, error) {
args := []string{"diff"}
if len(paths) > 0 {
args = append(args, "--", paths[0])
}
return r.output(ctx, args...)
}
// StagedDiff returns the staged diff, optionally limited to one path.
func (r Runner) StagedDiff(ctx context.Context, paths ...string) (string, error) {
args := []string{"diff", "--cached"}
if len(paths) > 0 {
args = append(args, "--", paths[0])
}
return r.output(ctx, args...)
}
// Review combines staged and unstaged diffs into one text block for review.
func (r Runner) Review(ctx context.Context, paths ...string) (string, error) {
staged, err := r.StagedDiff(ctx, paths...)
if err != nil {
return "", err
}
unstaged, err := r.Diff(ctx, paths...)
if err != nil {
return "", err
}
var b strings.Builder
if strings.TrimSpace(staged) != "" {
b.WriteString("STAGED\n")
b.WriteString(staged)
if !strings.HasSuffix(staged, "\n") {
b.WriteString("\n")
}
}
if strings.TrimSpace(unstaged) != "" {
if b.Len() > 0 {
b.WriteString("\n")
}
b.WriteString("UNSTAGED\n")
b.WriteString(unstaged)
}
if b.Len() == 0 {
return "No diff to review.\n", nil
}
return b.String(), nil
}