-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathcommand.go
More file actions
229 lines (201 loc) · 6.07 KB
/
command.go
File metadata and controls
229 lines (201 loc) · 6.07 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
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"bytes"
"context"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/sourcegraph/run"
)
// CommandOptions contains options for running a command.
type CommandOptions struct {
Args []string
Envs []string
}
// DefaultTimeout is the default timeout duration for all commands. It is
// applied when the context does not already have a deadline.
const DefaultTimeout = time.Minute
// gitCmd builds a *run.Command for "git" with the given arguments, environment
// variables and working directory. If the context does not already have a
// deadline, DefaultTimeout will be applied automatically.
func gitCmd(ctx context.Context, dir string, args []string, envs []string) (*run.Command, context.CancelFunc) {
cancel := func() {}
// Apply default timeout if the context doesn't already have a deadline.
if _, ok := ctx.Deadline(); !ok {
var timeoutCancel context.CancelFunc
ctx, timeoutCancel = context.WithTimeout(ctx, DefaultTimeout)
cancel = timeoutCancel
}
// run.Cmd joins all parts into a single string and then shell-parses it.
// We must quote each argument so that special characters (spaces, quotes,
// angle brackets, etc.) are preserved correctly.
parts := make([]string, 0, 1+len(args))
parts = append(parts, "git")
for _, arg := range args {
parts = append(parts, run.Arg(arg))
}
cmd := run.Cmd(ctx, parts...)
if dir != "" {
cmd = cmd.Dir(dir)
}
if len(envs) > 0 {
cmd = cmd.Environ(append(os.Environ(), envs...))
}
return cmd, cancel
}
// gitRun executes a git command in the given directory and returns stdout as
// bytes. Stderr is included in the error message on failure. If the command's
// context does not have a deadline, DefaultTimeout will be applied
// automatically. It returns an ErrExecTimeout if the execution was timed out.
func gitRun(ctx context.Context, dir string, args []string, envs []string) ([]byte, error) {
cmd, cancel := gitCmd(ctx, dir, args, envs)
defer cancel()
var logBuf *bytes.Buffer
if logOutput != nil {
logBuf = new(bytes.Buffer)
logBuf.Grow(512)
defer func() {
logf(dir, args, logBuf.Bytes())
}()
}
// Use Stream to a buffer to preserve raw bytes (including NUL bytes from
// commands like "ls-tree -z"). The String/Lines methods process output
// line-by-line which corrupts binary-ish output.
stdout := new(bytes.Buffer)
err := cmd.StdOut().Run().Stream(stdout)
// Capture (partial) stdout for logging even on error, so failed commands
// produce a useful log entry rather than an empty one.
if logOutput != nil {
data := stdout.Bytes()
limit := len(data)
if limit > 512 {
limit = 512
}
logBuf.Write(data[:limit])
if len(data) > 512 {
logBuf.WriteString("... (more omitted)")
}
}
if err != nil {
return nil, mapContextError(err, ctx)
}
return stdout.Bytes(), nil
}
// gitPipeline executes a git command in the given directory, streaming stdout
// to the given writer. If stderr writer is provided and the command fails,
// stderr content extracted from the error is written to it.
func gitPipeline(ctx context.Context, dir string, args []string, envs []string, stdout, stderr io.Writer) error {
cmd, cancel := gitCmd(ctx, dir, args, envs)
defer cancel()
var buf *bytes.Buffer
w := stdout
if logOutput != nil {
buf = new(bytes.Buffer)
buf.Grow(512)
w = &limitDualWriter{
W: buf,
N: int64(buf.Cap()),
w: stdout,
}
defer func() {
logf(dir, args, buf.Bytes())
}()
}
streamErr := cmd.StdOut().Run().Stream(w)
if streamErr != nil {
if stderr != nil {
_, _ = fmt.Fprint(stderr, extractStderr(streamErr))
}
return mapContextError(streamErr, ctx)
}
return nil
}
// committerEnvs returns environment variables for setting the Git committer.
func committerEnvs(committer *Signature) []string {
return []string{
"GIT_COMMITTER_NAME=" + committer.Name,
"GIT_COMMITTER_EMAIL=" + committer.Email,
}
}
// logf logs a git command execution with optional output.
func logf(dir string, args []string, output []byte) {
cmdStr := "git"
if len(args) > 0 {
quoted := make([]string, len(args))
for i, a := range args {
if strings.ContainsAny(a, " \t\n\"'\\<>") {
quoted[i] = strconv.Quote(a)
} else {
quoted[i] = a
}
}
cmdStr = "git " + strings.Join(quoted, " ")
}
if len(dir) == 0 {
log("%s\n%s", cmdStr, output)
} else {
log("%s: %s\n%s", dir, cmdStr, output)
}
}
// A limitDualWriter writes to W but limits the amount of data written to just N
// bytes. On the other hand, it passes everything to w.
type limitDualWriter struct {
W io.Writer // underlying writer
N int64 // max bytes remaining
prompted bool
w io.Writer
}
func (w *limitDualWriter) Write(p []byte) (int, error) {
if w.N > 0 {
limit := int64(len(p))
if limit > w.N {
limit = w.N
}
n, _ := w.W.Write(p[:limit])
w.N -= int64(n)
}
if !w.prompted && w.N <= 0 {
w.prompted = true
_, _ = w.W.Write([]byte("... (more omitted)"))
}
return w.w.Write(p)
}
// mapContextError maps context errors to the appropriate sentinel errors used
// by this package.
func mapContextError(err error, ctx context.Context) error {
if ctx == nil {
return err
}
if ctxErr := ctx.Err(); ctxErr != nil {
if ctxErr == context.DeadlineExceeded {
return ErrExecTimeout
}
return ctxErr
}
return err
}
// isExitStatus reports whether err represents a specific process exit status
// code, using the run.ExitCoder interface provided by sourcegraph/run.
func isExitStatus(err error, code int) bool {
exitCoder, ok := err.(run.ExitCoder)
return ok && exitCoder.ExitCode() == code
}
// extractStderr attempts to extract the stderr portion from a sourcegraph/run
// error. The error format is typically "exit status N: <stderr content>".
func extractStderr(err error) string {
if err == nil {
return ""
}
msg := err.Error()
// sourcegraph/run error format: "exit status N: <stderr>"
if idx := strings.Index(msg, ": "); idx >= 0 && strings.HasPrefix(msg, "exit status") {
return msg[idx+2:]
}
return msg
}