@@ -10,7 +10,6 @@ import (
10
10
"context"
11
11
"fmt"
12
12
"math"
13
- "os"
14
13
"runtime"
15
14
"strconv"
16
15
"strings"
@@ -36,24 +35,21 @@ func EnsureValidGitRepository(ctx context.Context, repoPath string) error {
36
35
}
37
36
38
37
func returnClosedReaderWriters (err error ) (WriteCloserError , * bufio.Reader , func ()) {
39
- wr := & writeCloserError {}
40
- rd := & readCloserError {}
41
-
42
- _ = wr .CloseWithError (err )
43
- _ = rd .CloseWithError (err )
44
-
45
- return wr , bufio .NewReader (rd ), func () {}
38
+ wr := & ClosedReadWriteCloserError {err }
39
+ return wr , bufio .NewReader (wr ), func () {}
46
40
}
47
41
48
42
// CatFileBatchCheck opens git cat-file --batch-check in the provided repo and returns a stdin pipe, a stdout reader and cancel function
49
43
func CatFileBatchCheck (ctx context.Context , repoPath string ) (WriteCloserError , * bufio.Reader , func ()) {
50
- batchStdinReader , batchStdinWriter , err := os . Pipe ()
44
+ batchStdinReader , batchStdinWriter , err := Pipe ()
51
45
if err != nil {
52
46
log .Critical ("Unable to open pipe to write to: %v" , err )
53
47
return returnClosedReaderWriters (err )
54
48
}
55
- batchStdoutReader , batchStdoutWriter , err := os . Pipe ()
49
+ batchStdoutReader , batchStdoutWriter , err := Pipe ()
56
50
if err != nil {
51
+ _ = batchStdinReader .Close ()
52
+ _ = batchStdinWriter .Close ()
57
53
log .Critical ("Unable to open pipe to write to: %v" , err )
58
54
return returnClosedReaderWriters (err )
59
55
}
@@ -69,9 +65,6 @@ func CatFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError,
69
65
_ , filename , line , _ := runtime .Caller (2 )
70
66
filename = strings .TrimPrefix (filename , callerPrefix )
71
67
72
- wr := newWriteCloserError (batchStdinWriter )
73
- rd := newReadCloserError (batchStdoutReader )
74
-
75
68
go func () {
76
69
stderr := strings.Builder {}
77
70
err := NewCommand (ctx , "cat-file" , "--batch-check" ).
@@ -84,10 +77,8 @@ func CatFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError,
84
77
})
85
78
if err != nil {
86
79
err := ConcatenateError (err , (& stderr ).String ())
87
- _ = wr .CloseWithError (err )
88
- _ = rd .CloseWithError (err )
89
- _ = batchStdoutWriter .Close ()
90
- _ = batchStdinReader .Close ()
80
+ _ = batchStdinReader .CloseWithError (err )
81
+ _ = batchStdoutWriter .CloseWithError (err )
91
82
} else {
92
83
_ = batchStdoutWriter .Close ()
93
84
_ = batchStdinReader .Close ()
@@ -96,29 +87,27 @@ func CatFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError,
96
87
}()
97
88
98
89
// For simplicities sake we'll use a buffered reader to read from the cat-file --batch-check
99
- batchReader := bufio .NewReader (rd )
100
-
101
- return wr , batchReader , cancel
90
+ batchReader := bufio .NewReader (batchStdoutReader )
91
+ return batchStdinWriter , batchReader , cancel
102
92
}
103
93
104
94
// CatFileBatch opens git cat-file --batch in the provided repo and returns a stdin pipe, a stdout reader and cancel function
105
95
func CatFileBatch (ctx context.Context , repoPath string ) (WriteCloserError , * bufio.Reader , func ()) {
106
96
// We often want to feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
107
97
// so let's create a batch stdin and stdout
108
- batchStdinReader , batchStdinWriter , err := os . Pipe ()
98
+ batchStdinReader , batchStdinWriter , err := Pipe ()
109
99
if err != nil {
110
100
log .Critical ("Unable to open pipe to write to: %v" , err )
111
101
return returnClosedReaderWriters (err )
112
102
}
113
- batchStdoutReader , batchStdoutWriter , err := os . Pipe ()
103
+ batchStdoutReader , batchStdoutWriter , err := Pipe ()
114
104
if err != nil {
105
+ _ = batchStdinReader .Close ()
106
+ _ = batchStdinWriter .Close ()
115
107
log .Critical ("Unable to open pipe to write to: %v" , err )
116
108
return returnClosedReaderWriters (err )
117
109
}
118
110
119
- wr := newWriteCloserError (batchStdinWriter )
120
- rd := newReadCloserError (batchStdoutReader )
121
-
122
111
ctx , ctxCancel := context .WithCancel (ctx )
123
112
closed := make (chan struct {})
124
113
cancel := func () {
@@ -143,10 +132,8 @@ func CatFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
143
132
})
144
133
if err != nil {
145
134
err := ConcatenateError (err , (& stderr ).String ())
146
- _ = wr .CloseWithError (err )
147
- _ = rd .CloseWithError (err )
148
- _ = batchStdoutWriter .Close ()
149
- _ = batchStdinReader .Close ()
135
+ _ = batchStdinReader .CloseWithError (err )
136
+ _ = batchStdoutWriter .CloseWithError (err )
150
137
} else {
151
138
_ = batchStdoutWriter .Close ()
152
139
_ = batchStdinReader .Close ()
@@ -155,9 +142,9 @@ func CatFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
155
142
}()
156
143
157
144
// For simplicities sake we'll us a buffered reader to read from the cat-file --batch
158
- batchReader := bufio .NewReaderSize (rd , 32 * 1024 )
145
+ batchReader := bufio .NewReaderSize (batchStdoutReader , 32 * 1024 )
159
146
160
- return wr , batchReader , cancel
147
+ return batchStdinWriter , batchReader , cancel
161
148
}
162
149
163
150
// ReadBatchLine reads the header line from cat-file --batch
0 commit comments