Skip to content

Commit

Permalink
fix(lfs): CommandExecutor.RawExecute forward os.Stdin
Browse files Browse the repository at this point in the history
This is only used by LFS pre-push Hook which require Stdin.
  • Loading branch information
tdesveaux committed May 28, 2024
1 parent 7c5166f commit 77a5de9
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/git/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func IsLFSHook(hookName string) bool {
}

// DoesLFSHookConsumeStdin returns whether the LFS hookName will consume Stdin
// meaning it won't be available to following commands
// meaning it won't be available to following commands.
func DoesLFSHookConsumeStdin(hookName string) bool {
for _, lfsHookName := range lfsHookConsumeStdin {
if lfsHookName == hookName {
Expand Down
6 changes: 5 additions & 1 deletion internal/lefthook/runner/exec/execute_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Write
return nil
}

func (e CommandExecutor) RawExecute(ctx context.Context, command []string, out io.Writer) error {
func (e CommandExecutor) RawExecute(ctx context.Context, command []string, out io.Writer, forwardStdin bool) error {
cmd := exec.CommandContext(ctx, command[0], command[1:]...)

cmd.Stdout = out
cmd.Stderr = os.Stderr

if forwardStdin {
cmd.Stdin = os.Stdin
}

return cmd.Run()
}

Expand Down
6 changes: 5 additions & 1 deletion internal/lefthook/runner/exec/execute_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Write
return nil
}

func (e CommandExecutor) RawExecute(ctx context.Context, command []string, out io.Writer) error {
func (e CommandExecutor) RawExecute(ctx context.Context, command []string, out io.Writer, forwardStdin bool) error {
cmd := exec.Command(command[0], command[1:]...)

cmd.Stdout = out
cmd.Stderr = os.Stderr

if forwardStdin {
cmd.Stdin = os.Stdin
}

return cmd.Run()
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/runner/exec/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ type Options struct {
// It is used here for testing purpose mostly.
type Executor interface {
Execute(ctx context.Context, opts Options, out io.Writer) error
RawExecute(ctx context.Context, command []string, out io.Writer) error
RawExecute(ctx context.Context, command []string, out io.Writer, forwardStdin bool) error
}
3 changes: 2 additions & 1 deletion internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (r *Runner) RunAll(ctx context.Context, sourceDirs []string) ([]Result, err
return results, nil
}

// returns whether it ran a LFS hook
// returns whether it ran a LFS hook.
func (r *Runner) runLFSHook(ctx context.Context) (bool, error) {
if !git.IsLFSHook(r.HookName) {
return false, nil
Expand Down Expand Up @@ -153,6 +153,7 @@ func (r *Runner) runLFSHook(ctx context.Context) (bool, error) {
r.GitArgs...,
),
out,
git.DoesLFSHookConsumeStdin(r.HookName),
)

output := strings.Trim(out.String(), "\n")
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (e TestExecutor) Execute(_ctx context.Context, opts exec.Options, _out io.W
return
}

func (e TestExecutor) RawExecute(_ctx context.Context, _command []string, _out io.Writer) error {
func (e TestExecutor) RawExecute(_ctx context.Context, _command []string, _out io.Writer, forwardStdin bool) error {
return nil
}

Expand Down

0 comments on commit 77a5de9

Please sign in to comment.