-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: share STDIN across different commands on pre-push hook (#732)
* fix: error and bail if multiple commands useStdin Since Stdin from Git is not intercepted and forwared to each commands/scripts, this could lead to hang. * fix: CommandExecutor.RawExecute forward os.Stdin This is only used by LFS pre-push Hook which require Stdin. * fix: use cached reader for stdin when use_stdin is specified * chore: remove special checks for pre-push hook * chore: fix typos * fix: fail the hook if git-lfs command failed * chore: add more verbose error --------- Co-authored-by: Thomas Desveaux <[email protected]>
- Loading branch information
Showing
11 changed files
with
148 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package runner | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
) | ||
|
||
// cachedReader reads from the provided `io.Reader` until `io.EOF` and saves | ||
// the read content into the inner buffer. | ||
// | ||
// After `io.EOF` it will be providing the read data again and again. | ||
type cachedReader struct { | ||
in io.Reader | ||
useBuffer bool | ||
buf []byte | ||
reader *bytes.Reader | ||
} | ||
|
||
func NewCachedReader(in io.Reader) *cachedReader { | ||
return &cachedReader{ | ||
in: in, | ||
buf: []byte{}, | ||
reader: bytes.NewReader([]byte{}), | ||
} | ||
} | ||
|
||
func (r *cachedReader) Read(p []byte) (int, error) { | ||
if r.useBuffer { | ||
n, err := r.reader.Read(p) | ||
if err == io.EOF { | ||
_, seekErr := r.reader.Seek(0, io.SeekStart) | ||
if seekErr != nil { | ||
panic(seekErr) | ||
} | ||
|
||
return n, err | ||
} | ||
|
||
return n, err | ||
} | ||
|
||
n, err := r.in.Read(p) | ||
r.buf = append(r.buf, p[:n]...) | ||
if err == io.EOF { | ||
r.useBuffer = true | ||
r.reader = bytes.NewReader(r.buf) | ||
} | ||
return n, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package runner | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestCachedReader(t *testing.T) { | ||
testSlice := []byte("Some example string\nMultiline") | ||
|
||
cachedReader := NewCachedReader(bytes.NewReader(testSlice)) | ||
|
||
for range 5 { | ||
res, err := io.ReadAll(cachedReader) | ||
if err != nil { | ||
t.Errorf("unexpected err: %s", err) | ||
} | ||
|
||
if !bytes.Equal(res, testSlice) { | ||
t.Errorf("expected %v to be equal to %v", res, testSlice) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package runner | ||
|
||
import "io" | ||
|
||
// nullReader always returns `io.EOF`. | ||
type nullReader struct{} | ||
|
||
func NewNullReader() io.Reader { | ||
return nullReader{} | ||
} | ||
|
||
// Implements io.Reader interface. | ||
func (nullReader) Read(b []byte) (int, error) { | ||
return 0, io.EOF | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package runner | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestNullReader(t *testing.T) { | ||
nullReader := NewNullReader() | ||
|
||
res, err := io.ReadAll(nullReader) | ||
if err != nil { | ||
t.Errorf("unexpected err: %s", err) | ||
} | ||
|
||
if !bytes.Equal(res, []byte{}) { | ||
t.Errorf("expected %v to be equal to %v", res, []byte{}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters