Skip to content

Commit

Permalink
fix: Omit lfs output unless it is required (evilmartians#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Nov 18, 2022
1 parent 08120e6 commit 6161e77
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

- fix: Omit LFS output unless it is required ([PR #373](https://github.com/evilmartians/lefthook/pull/373) by @mrexox)

## 1.2.1 (2022-11-17)

- fix: Remove quoting for scripts ([PR #371](https://github.com/evilmartians/lefthook/pull/371) by @stonesbg + @mrexox)
Expand Down
4 changes: 4 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,13 @@ SHA1=$3

### Git LFS support

> :warning: If git-lfs binary is not installed and not required in your project, LFS hooks won't be executed, and you won't be warned about it.
Lefthook runs LFS hooks internally for the following hooks:

- post-checkout
- post-commit
- post-merge
- pre-push

Errors are suppressed if git LFS is not required for the project. You can use [`LEFTHOOK_VERBOSE`](#lefthook_verbose) ENV to make lefthook show git LFS output.
9 changes: 6 additions & 3 deletions internal/lefthook/runner/execute_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ func (e CommandExecutor) Execute(opts ExecuteOptions) (*bytes.Buffer, error) {
return out, command.Wait()
}

func (e CommandExecutor) RawExecute(command string, args ...string) error {
func (e CommandExecutor) RawExecute(command string, args ...string) (*bytes.Buffer, error) {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout

var out bytes.Buffer

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

return cmd.Run()
return &out, cmd.Run()
}
9 changes: 6 additions & 3 deletions internal/lefthook/runner/execute_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ func (e CommandExecutor) Execute(opts ExecuteOptions) (*bytes.Buffer, error) {
return &out, command.Wait()
}

func (e CommandExecutor) RawExecute(command string, args ...string) error {
func (e CommandExecutor) RawExecute(command string, args ...string) (*bytes.Buffer, error) {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout

var out bytes.Buffer

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

return cmd.Run()
return &out, cmd.Run()
}
2 changes: 1 addition & 1 deletion internal/lefthook/runner/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ type ExecuteOptions struct {
// It is used here for testing purpose mostly.
type Executor interface {
Execute(opts ExecuteOptions) (*bytes.Buffer, error)
RawExecute(command string, args ...string) error
RawExecute(command string, args ...string) (*bytes.Buffer, error)
}
36 changes: 27 additions & 9 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,37 @@ func (r *Runner) runLFSHook(hookName string) error {

if git.IsLFSAvailable() {
log.Debugf(
"Executing LFS Hook: `git lfs %s %s", hookName, strings.Join(r.args, " "),
"[git-lfs] executing hook: git lfs %s %s", hookName, strings.Join(r.args, " "),
)
err := r.exec.RawExecute(
out, err := r.exec.RawExecute(
"git",
append(
[]string{"lfs", hookName},
r.args...,
)...,
)

output := strings.Trim(out.String(), "\n")
if output != "" {
log.Debug("[git-lfs] output: ", output)
}
if err != nil {
return errors.New("git-lfs command failed")
log.Debug("[git-lfs] error: ", err)
}

if err == nil && output != "" {
log.Info(output)
}

if err != nil && (requiredExists || configExists) {
log.Warn(output)
return fmt.Errorf("git-lfs command failed: %w", err)
}
} else if requiredExists || configExists {

return nil
}

if requiredExists || configExists {
log.Errorf(
"This repository requires Git LFS, but 'git-lfs' wasn't found.\n"+
"Install 'git-lfs' or consider reviewing the files:\n"+
Expand Down Expand Up @@ -204,7 +222,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo)

// Skip non-regular files (dirs, symlinks, sockets, etc.)
if !file.Mode().IsRegular() {
log.Debugf("File %s is not a regular file, skipping", file.Name())
log.Debugf("[lefthook] file %s is not a regular file, skipping", file.Name())
return
}

Expand Down Expand Up @@ -377,7 +395,7 @@ func (r *Runner) buildCommandArgs(command *config.Command) ([]string, error) {
runString = strings.ReplaceAll(runString, fmt.Sprintf("{%d}", i+1), gitArg)
}

log.Debug("Executing command is: ", runString)
log.Debug("[lefthook] executing: ", runString)

return strings.Split(runString, " "), nil
}
Expand All @@ -387,13 +405,13 @@ func prepareFiles(command *config.Command, files []string) []string {
return []string{}
}

log.Debug("Files before filters:\n", files)
log.Debug("[lefthook] files before filters:\n", files)

files = filterGlob(files, command.Glob)
files = filterExclude(files, command.Exclude)
files = filterRelative(files, command.Root)

log.Debug("Files after filters:\n", files)
log.Debug("[lefthook] files after filters:\n", files)

// Escape file names to prevent unexpected bugs
var filesEsc []string
Expand All @@ -403,7 +421,7 @@ func prepareFiles(command *config.Command, files []string) []string {
}
}

log.Debug("Files after escaping:\n", filesEsc)
log.Debug("[lefthook] files after escaping:\n", filesEsc)

return filesEsc
}
Expand Down
4 changes: 2 additions & 2 deletions internal/lefthook/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func (e TestExecutor) Execute(opts ExecuteOptions) (out *bytes.Buffer, err error
return
}

func (e TestExecutor) RawExecute(command string, args ...string) error {
return nil
func (e TestExecutor) RawExecute(command string, args ...string) (*bytes.Buffer, error) {
return nil, nil
}

func TestRunAll(t *testing.T) {
Expand Down

0 comments on commit 6161e77

Please sign in to comment.