From 2e3d9ba0f07a87821296a5f975c720bf717bd8d4 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Thu, 30 Nov 2023 18:16:41 +0300 Subject: [PATCH 1/3] fix: use empty stdin by default --- internal/lefthook/run/exec/execute_unix.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/lefthook/run/exec/execute_unix.go b/internal/lefthook/run/exec/execute_unix.go index da72f8af..80ca771c 100644 --- a/internal/lefthook/run/exec/execute_unix.go +++ b/internal/lefthook/run/exec/execute_unix.go @@ -28,8 +28,18 @@ type executeArgs struct { interactive, useStdin bool } +type nullReader struct{} + +func (nullReader) Read(b []byte) (int, error) { + if len(b) == 0 { + return 0, nil + } + + return 0, io.EOF +} + func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Writer) error { - in := os.Stdin + var in io.Reader = nullReader{} if opts.Interactive && !isatty.IsTerminal(os.Stdin.Fd()) { tty, err := os.Open("/dev/tty") if err == nil { From 20c94ae43a94222176fbeaefda97a4ef30a3f3c6 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Thu, 30 Nov 2023 18:25:05 +0300 Subject: [PATCH 2/3] fix: provide a fix for *nix and windows executors --- internal/lefthook/run/exec/execute_unix.go | 13 +++---------- internal/lefthook/run/exec/execute_windows.go | 7 ++++++- internal/lefthook/run/exec/nullReader.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 internal/lefthook/run/exec/nullReader.go diff --git a/internal/lefthook/run/exec/execute_unix.go b/internal/lefthook/run/exec/execute_unix.go index 80ca771c..cff07375 100644 --- a/internal/lefthook/run/exec/execute_unix.go +++ b/internal/lefthook/run/exec/execute_unix.go @@ -28,18 +28,11 @@ type executeArgs struct { interactive, useStdin bool } -type nullReader struct{} - -func (nullReader) Read(b []byte) (int, error) { - if len(b) == 0 { - return 0, nil - } - - return 0, io.EOF -} - func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Writer) error { var in io.Reader = nullReader{} + if opts.UseStdin { + in = os.Stdin + } if opts.Interactive && !isatty.IsTerminal(os.Stdin.Fd()) { tty, err := os.Open("/dev/tty") if err == nil { diff --git a/internal/lefthook/run/exec/execute_windows.go b/internal/lefthook/run/exec/execute_windows.go index 4c88c2c0..6b0ca244 100644 --- a/internal/lefthook/run/exec/execute_windows.go +++ b/internal/lefthook/run/exec/execute_windows.go @@ -29,8 +29,13 @@ func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Write ) } + var in io.Reader = nullReader{} + if opts.Interactive || opts.UseStdin { + in = os.Stdin + } + args := &executeArgs{ - in: os.Stdin, + in: in, out: out, envs: envs, root: root, diff --git a/internal/lefthook/run/exec/nullReader.go b/internal/lefthook/run/exec/nullReader.go new file mode 100644 index 00000000..dd774b5e --- /dev/null +++ b/internal/lefthook/run/exec/nullReader.go @@ -0,0 +1,10 @@ +package exec + +import "io" + +// nullReader always returns EOF. +type nullReader struct{} + +func (nullReader) Read(b []byte) (int, error) { + return 0, io.EOF +} From 0ad5bbacef16e6f70233a4d36465e12a13ac99db Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Thu, 30 Nov 2023 18:30:37 +0300 Subject: [PATCH 3/3] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f1c3573..e43f7028 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## master (unreleased) +- fix: use empty stdin by default ([#590](https://github.com/evilmartians/lefthook/pull/590)) by @mrexox - feat: add priorities to commands ([#589](https://github.com/evilmartians/lefthook/pull/589)) by @mrexox ## 1.5.4 (2023-11-27)