Skip to content

Commit d4c8fef

Browse files
committed
111
1 parent ffbfd0a commit d4c8fef

9 files changed

Lines changed: 35 additions & 36 deletions

File tree

blob.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type Blob struct {
1515
*TreeEntry
1616
}
1717

18-
// Bytes reads and returns the content of the blob all at once in bytes. This
19-
// can be very slow and memory consuming for huge content.
18+
// Bytes reads and returns the content of the blob all at once in bytes. This can
19+
// be very slow and memory consuming for huge content.
2020
func (b *Blob) Bytes(ctx context.Context) ([]byte, error) {
2121
stdout := new(bytes.Buffer)
2222

@@ -25,14 +25,13 @@ func (b *Blob) Bytes(ctx context.Context) ([]byte, error) {
2525
stdout.Grow(int(size))
2626
}
2727

28-
if err := b.Pipeline(ctx, stdout); err != nil {
28+
if err := b.Pipe(ctx, stdout); err != nil {
2929
return nil, err
3030
}
3131
return stdout.Bytes(), nil
3232
}
3333

34-
// Pipeline reads the content of the blob and pipes stdout to the supplied
35-
// io.Writer.
36-
func (b *Blob) Pipeline(ctx context.Context, stdout io.Writer) error {
34+
// Pipe reads the content of the blob and pipes stdout to the supplied io.Writer.
35+
func (b *Blob) Pipe(ctx context.Context, stdout io.Writer) error {
3736
return pipe(ctx, b.parent.repo.path, []string{"show", b.id.String()}, nil, stdout)
3837
}

blob_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ This demo also includes an image with changes on a branch for examination of ima
4848
assert.Equal(t, expOutput, string(p))
4949
})
5050

51-
t.Run("get data with pipeline", func(t *testing.T) {
51+
t.Run("get data with pipe", func(t *testing.T) {
5252
stdout := new(bytes.Buffer)
53-
err := blob.Pipeline(ctx, stdout)
53+
err := blob.Pipe(ctx, stdout)
5454
assert.Nil(t, err)
5555
assert.Equal(t, expOutput, stdout.String())
5656
})

command.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package git
77
import (
88
"bytes"
99
"context"
10+
"errors"
1011
"io"
1112
"os"
1213
"strconv"
@@ -26,22 +27,20 @@ type CommandOptions struct {
2627
// applied when the context does not already have a deadline.
2728
const DefaultTimeout = time.Minute
2829

29-
// cmd builds a *run.Command for "git" with the given arguments, environment
30-
// variables and working directory. If the context does not already have a
31-
// deadline, DefaultTimeout will be applied automatically.
30+
// cmd builds a *run.Command for git with the given arguments, environment
31+
// variables and working directory. DefaultTimeout will be applied if the context
32+
// does not already have a deadline.
3233
func cmd(ctx context.Context, dir string, args []string, envs []string) (*run.Command, context.CancelFunc) {
3334
cancel := func() {}
34-
35-
// Apply default timeout if the context doesn't already have a deadline.
3635
if _, ok := ctx.Deadline(); !ok {
3736
var timeoutCancel context.CancelFunc
3837
ctx, timeoutCancel = context.WithTimeout(ctx, DefaultTimeout)
3938
cancel = timeoutCancel
4039
}
4140

42-
// run.Cmd joins all parts into a single string and then shell-parses it.
43-
// We must quote each argument so that special characters (spaces, quotes,
44-
// angle brackets, etc.) are preserved correctly.
41+
// run.Cmd joins all parts into a single string and then shell-parses it. We must
42+
// quote each argument so that special characters (spaces, quotes, angle
43+
// brackets, etc.) are preserved correctly.
4544
parts := make([]string, 0, 1+len(args))
4645
parts = append(parts, "git")
4746
for _, arg := range args {
@@ -59,9 +58,9 @@ func cmd(ctx context.Context, dir string, args []string, envs []string) (*run.Co
5958
}
6059

6160
// exec executes a git command in the given directory and returns stdout as
62-
// bytes. Stderr is included in the error message on failure. If the command's
63-
// context does not have a deadline, DefaultTimeout will be applied
64-
// automatically. It returns an ErrExecTimeout if the execution was timed out.
61+
// bytes. Stderr is included in the error message on failure. DefaultTimeout will
62+
// be applied if the context does not already have a deadline. It returns
63+
// ErrExecTimeout if the execution was timed out.
6564
func exec(ctx context.Context, dir string, args []string, envs []string) ([]byte, error) {
6665
c, cancel := cmd(ctx, dir, args, envs)
6766
defer cancel()
@@ -71,7 +70,7 @@ func exec(ctx context.Context, dir string, args []string, envs []string) ([]byte
7170
logBuf = new(bytes.Buffer)
7271
logBuf.Grow(512)
7372
defer func() {
74-
logf(dir, args, logBuf.Bytes())
73+
log(dir, args, logBuf.Bytes())
7574
}()
7675
}
7776

@@ -81,8 +80,8 @@ func exec(ctx context.Context, dir string, args []string, envs []string) ([]byte
8180
stdout := new(bytes.Buffer)
8281
err := c.StdOut().Run().Stream(stdout)
8382

84-
// Capture (partial) stdout for logging even on error, so failed commands
85-
// produce a useful log entry rather than an empty one.
83+
// Capture (partial) stdout for logging even on error, so failed commands produce
84+
// a useful log entry rather than an empty one.
8685
if logOutput != nil {
8786
data := stdout.Bytes()
8887
limit := len(data)
@@ -102,7 +101,7 @@ func exec(ctx context.Context, dir string, args []string, envs []string) ([]byte
102101
}
103102

104103
// pipe executes a git command in the given directory, streaming stdout to the
105-
// given writer.
104+
// given io.Writer.
106105
func pipe(ctx context.Context, dir string, args []string, envs []string, stdout io.Writer) error {
107106
c, cancel := cmd(ctx, dir, args, envs)
108107
defer cancel()
@@ -119,7 +118,7 @@ func pipe(ctx context.Context, dir string, args []string, envs []string, stdout
119118
}
120119

121120
defer func() {
122-
logf(dir, args, buf.Bytes())
121+
log(dir, args, buf.Bytes())
123122
}()
124123
}
125124

@@ -138,8 +137,8 @@ func committerEnvs(committer *Signature) []string {
138137
}
139138
}
140139

141-
// logf logs a git command execution with optional output.
142-
func logf(dir string, args []string, output []byte) {
140+
// log logs a git command execution with its output.
141+
func log(dir string, args []string, output []byte) {
143142
cmdStr := "git"
144143
if len(args) > 0 {
145144
quoted := make([]string, len(args))
@@ -153,9 +152,9 @@ func logf(dir string, args []string, output []byte) {
153152
cmdStr = "git " + strings.Join(quoted, " ")
154153
}
155154
if len(dir) == 0 {
156-
log("%s\n%s", cmdStr, output)
155+
logf("%s\n%s", cmdStr, output)
157156
} else {
158-
log("%s: %s\n%s", dir, cmdStr, output)
157+
logf("%s: %s\n%s", dir, cmdStr, output)
159158
}
160159
}
161160

@@ -194,7 +193,7 @@ func mapContextError(err error, ctx context.Context) error {
194193
return err
195194
}
196195
if ctxErr := ctx.Err(); ctxErr != nil {
197-
if ctxErr == context.DeadlineExceeded {
196+
if errors.Is(ctxErr, context.DeadlineExceeded) {
198197
return ErrExecTimeout
199198
}
200199
return ctxErr
@@ -205,6 +204,7 @@ func mapContextError(err error, ctx context.Context) error {
205204
// isExitStatus reports whether err represents a specific process exit status
206205
// code, using the run.ExitCoder interface provided by sourcegraph/run.
207206
func isExitStatus(err error, code int) bool {
208-
exitCoder, ok := err.(run.ExitCoder)
207+
var exitCoder run.ExitCoder
208+
ok := errors.As(err, &exitCoder)
209209
return ok && exitCoder.ExitCode() == code
210210
}

commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (c *Commit) isImageFile(ctx context.Context, blob *Blob, err error) (bool,
154154
N: int64(buf.Cap()),
155155
}
156156

157-
err = blob.Pipeline(ctx, stdout)
157+
err = blob.Pipe(ctx, stdout)
158158
if err != nil {
159159
return false, err
160160
}

git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func SetPrefix(prefix string) {
2929
logPrefix = prefix
3030
}
3131

32-
func log(format string, args ...interface{}) {
32+
func logf(format string, args ...interface{}) {
3333
if logOutput == nil {
3434
return
3535
}

git_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func Test_log(t *testing.T) {
8383
var buf bytes.Buffer
8484
SetOutput(&buf)
8585

86-
log(test.format, test.args...)
86+
logf(test.format, test.args...)
8787
assert.Equal(t, test.expOutput, buf.String())
8888
})
8989
}

repo_commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (r *Repository) CatFileCommit(ctx context.Context, rev string, opts ...CatF
8585

8686
cache, ok := r.cachedCommits.Get(rev)
8787
if ok {
88-
log("Cached commit hit: %s", rev)
88+
logf("Cached commit hit: %s", rev)
8989
return cache.(*Commit), nil
9090
}
9191

repo_tag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ l:
5555
func (r *Repository) getTag(ctx context.Context, id *SHA1) (*Tag, error) {
5656
t, ok := r.cachedTags.Get(id.String())
5757
if ok {
58-
log("Cached tag hit: %s", id)
58+
logf("Cached tag hit: %s", id)
5959
return t.(*Tag), nil
6060
}
6161

repo_tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (r *Repository) LsTree(ctx context.Context, treeID string, opts ...LsTreeOp
117117

118118
cache, ok := r.cachedTrees.Get(treeID)
119119
if ok {
120-
log("Cached tree hit: %s", treeID)
120+
logf("Cached tree hit: %s", treeID)
121121
return cache.(*Tree), nil
122122
}
123123

0 commit comments

Comments
 (0)