Repository navigation
Expand file tree
/
Copy pathgit.go
More file actions
208 lines (186 loc) · 5.74 KB
/
Copy pathgit.go
File metadata and controls
208 lines (186 loc) · 5.74 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
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
var execCommand = exec.Command
func isGitRepo() error {
cmd := execCommand("git", "rev-parse", "--is-inside-work-tree")
output, err := cmd.CombinedOutput()
if err != nil {
return errorf("not a git repository: %s", strings.TrimSpace(string(output)))
}
return nil
}
func stageAll() error {
cmd := execCommand("git", "add", "--all", ":/")
output, err := cmd.CombinedOutput()
if err != nil {
return errorf("failed to stage changes: %s", strings.TrimSpace(string(output)))
}
return nil
}
// stagePaths stages additions, modifications, and deletions limited to the
// given pathspecs.
func stagePaths(paths []string) error {
args := append([]string{"add", "--all", "--"}, paths...)
cmd := execCommand("git", args...)
output, err := cmd.CombinedOutput()
if err != nil {
return errorf("failed to stage %s: %s", strings.Join(paths, " "), strings.TrimSpace(string(output)))
}
return nil
}
// stagedFiles lists files with staged changes, optionally limited to pathspecs.
func stagedFiles(paths []string) ([]string, error) {
args := []string{"diff", "--cached", "--name-only"}
if len(paths) > 0 {
args = append(append(args, "--"), paths...)
}
cmd := execCommand("git", args...)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, errorf("failed to list staged files: %s", strings.TrimSpace(string(output)))
}
var files []string
for _, line := range strings.Split(string(output), "\n") {
if line = strings.TrimSpace(line); line != "" {
files = append(files, line)
}
}
return files, nil
}
// ensureIndexWithinPaths fails when the index holds staged changes outside the
// given pathspecs, so a path-limited commit never silently includes them.
func ensureIndexWithinPaths(paths []string) error {
all, err := stagedFiles(nil)
if err != nil {
return err
}
if len(all) == 0 {
return nil
}
within, err := stagedFiles(paths)
if err != nil {
return err
}
inside := make(map[string]bool, len(within))
for _, file := range within {
inside[file] = true
}
var outside []string
for _, file := range all {
if !inside[file] {
outside = append(outside, file)
}
}
if len(outside) > 0 {
return errorf("index has staged changes outside the given paths: %s; commit or unstage them first (git restore --staged -- <file>), or run without paths", strings.Join(outside, ", "))
}
return nil
}
func snapshotIndex() (string, error) {
cmd := execCommand("git", "diff", "--cached", "--binary")
output, err := cmd.CombinedOutput()
if err != nil {
return "", errorf("failed to snapshot staged changes: %s", strings.TrimSpace(string(output)))
}
return string(output), nil
}
func restoreIndex(snapshot string) error {
resetCmd := execCommand("git", "reset")
resetOutput, err := resetCmd.CombinedOutput()
if err != nil {
return errorf("failed to reset index: %s", strings.TrimSpace(string(resetOutput)))
}
if strings.TrimSpace(snapshot) == "" {
return nil
}
applyCmd := execCommand("git", "apply", "--cached", "--whitespace=nowarn", "-")
applyCmd.Stdin = bytes.NewBufferString(snapshot)
applyOutput, err := applyCmd.CombinedOutput()
if err != nil {
return errorf("failed to restore staged changes: %s", strings.TrimSpace(string(applyOutput)))
}
return nil
}
func getStagedDiff() (string, error) {
cmd := execCommand("git", "diff", "--cached")
output, err := cmd.CombinedOutput()
if err != nil {
return "", errorf("failed to get diff: %s", strings.TrimSpace(string(output)))
}
diff := strings.TrimSpace(string(output))
if diff == "" {
return "", withExit(exitNoChanges, &gitError{msg: "no changes to commit"})
}
return diff, nil
}
func commit(message string) error {
cmd := execCommand("git", "commit", "-m", message)
output, err := cmd.CombinedOutput()
if err != nil {
return errorf("failed to commit: %s", strings.TrimSpace(string(output)))
}
return nil
}
func commitWithEditor(message string) error {
cmd := execCommand("git", "commit", "--edit", "-m", message)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return errorf("failed to commit: %v", err)
}
return nil
}
// commitInfo describes the commit at HEAD after a successful commit.
type commitInfo struct {
SHA string
Short string
Branch string
}
func headCommitInfo() (commitInfo, error) {
cmd := execCommand("git", "log", "-1", "--format=%H%n%h")
output, err := cmd.CombinedOutput()
if err != nil {
return commitInfo{}, errorf("failed to read HEAD after commit: %s", strings.TrimSpace(string(output)))
}
lines := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(lines) != 2 || lines[0] == "" {
return commitInfo{}, errorf("failed to read HEAD after commit: unexpected output %q", string(output))
}
info := commitInfo{SHA: lines[0], Short: lines[1]}
branchCmd := execCommand("git", "rev-parse", "--abbrev-ref", "HEAD")
branchOutput, err := branchCmd.CombinedOutput()
if err != nil {
return commitInfo{}, errorf("failed to read current branch: %s", strings.TrimSpace(string(branchOutput)))
}
info.Branch = strings.TrimSpace(string(branchOutput))
return info, nil
}
func push() error {
cmd := execCommand("git", "push")
output, err := cmd.CombinedOutput()
if err != nil {
return withExit(exitPush, &gitError{msg: fmt.Sprintf("failed to push: %s", strings.TrimSpace(string(output)))})
}
return nil
}
// errorf builds a git error. Callers override the code for special cases
// such as "no changes" or push failures.
func errorf(format string, args ...interface{}) error {
return withExit(exitGit, &gitError{msg: formatArgs(format, args)})
}
func formatArgs(format string, args []interface{}) string {
return fmt.Sprintf(format, args...)
}
type gitError struct {
msg string
}
func (e *gitError) Error() string {
return e.msg
}