From 5703093180164b8b26a433cff8db3fdb6aa8ca7d Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Thu, 30 Nov 2023 18:35:10 +0300 Subject: [PATCH] fix: use empty stdin by default (#590) --- CHANGELOG.md | 1 + internal/lefthook/run/exec/execute_unix.go | 5 ++++- internal/lefthook/run/exec/execute_windows.go | 7 ++++++- internal/lefthook/run/exec/nullReader.go | 10 ++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 internal/lefthook/run/exec/nullReader.go 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) diff --git a/internal/lefthook/run/exec/execute_unix.go b/internal/lefthook/run/exec/execute_unix.go index da72f8af..cff07375 100644 --- a/internal/lefthook/run/exec/execute_unix.go +++ b/internal/lefthook/run/exec/execute_unix.go @@ -29,7 +29,10 @@ type executeArgs struct { } func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Writer) error { - in := os.Stdin + 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 +}