From 05db7a30d094cbb233317d3df6530b4a20998aa8 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Fri, 12 Jan 2024 12:02:46 +0300 Subject: [PATCH 1/2] fix: execute files command within configured root --- internal/git/repository.go | 12 ++++++------ internal/lefthook/runner/prepare_command.go | 2 +- internal/lefthook/runner/runner_test.go | 4 ++++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/git/repository.go b/internal/git/repository.go index cde27ae1..a7ab2f94 100644 --- a/internal/git/repository.go +++ b/internal/git/repository.go @@ -108,19 +108,19 @@ func NewRepository(fs afero.Fs, git *CommandExecutor) (*Repository, error) { // StagedFiles returns a list of staged files // or an error if git command fails. func (r *Repository) StagedFiles() ([]string, error) { - return r.FilesByCommand(cmdStagedFiles) + return r.FilesByCommand(cmdStagedFiles, "") } // StagedFiles returns a list of all files in repository // or an error if git command fails. func (r *Repository) AllFiles() ([]string, error) { - return r.FilesByCommand(cmdAllFiles) + return r.FilesByCommand(cmdAllFiles, "") } // PushFiles returns a list of files that are ready to be pushed // or an error if git command fails. func (r *Repository) PushFiles() ([]string, error) { - res, err := r.FilesByCommand(cmdPushFilesBase) + res, err := r.FilesByCommand(cmdPushFilesBase, "") if err == nil { return res, nil } @@ -147,7 +147,7 @@ func (r *Repository) PushFiles() ([]string, error) { r.headBranch = r.emptyTreeSHA } - return r.FilesByCommand(append(cmdPushFilesHead, r.headBranch)) + return r.FilesByCommand(append(cmdPushFilesHead, r.headBranch), "") } // PartiallyStagedFiles returns the list of files that have both staged and @@ -316,8 +316,8 @@ func (r *Repository) AddFiles(files []string) error { } // FilesByCommand accepts git command and returns its result as a list of filepaths. -func (r *Repository) FilesByCommand(command []string) ([]string, error) { - lines, err := r.Git.CmdLines(command) +func (r *Repository) FilesByCommand(command []string, folder string) ([]string, error) { + lines, err := r.Git.CmdLinesWithinFolder(command, folder) if err != nil { return nil, err } diff --git a/internal/lefthook/runner/prepare_command.go b/internal/lefthook/runner/prepare_command.go index 288b07bb..165e0fa2 100644 --- a/internal/lefthook/runner/prepare_command.go +++ b/internal/lefthook/runner/prepare_command.go @@ -80,7 +80,7 @@ func (r *Runner) buildRun(command *config.Command) (*run, error) { } else { cmd = []string{"sh", "-c", filesCmd} } - return r.Repo.FilesByCommand(cmd) + return r.Repo.FilesByCommand(cmd, command.Root) } } diff --git a/internal/lefthook/runner/runner_test.go b/internal/lefthook/runner/runner_test.go index 8536de55..97a027e5 100644 --- a/internal/lefthook/runner/runner_test.go +++ b/internal/lefthook/runner/runner_test.go @@ -58,6 +58,10 @@ func (g *GitMock) Execute(cmd []string, _root string) (string, error) { return "", nil } +func (g *GitMock) CmdLinesWithinFolder(args []string, _folder string) ([]string, error) { + return g.CmdLines(args) +} + func (g *GitMock) reset() { g.mux.Lock() g.commands = []string{} From 98e852ed9083e0c2c80264150bdc5acea2b1022f Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Tue, 9 Apr 2024 12:39:45 +0300 Subject: [PATCH 2/2] chore: remove unused code --- internal/lefthook/runner/runner_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/lefthook/runner/runner_test.go b/internal/lefthook/runner/runner_test.go index 97a027e5..8536de55 100644 --- a/internal/lefthook/runner/runner_test.go +++ b/internal/lefthook/runner/runner_test.go @@ -58,10 +58,6 @@ func (g *GitMock) Execute(cmd []string, _root string) (string, error) { return "", nil } -func (g *GitMock) CmdLinesWithinFolder(args []string, _folder string) ([]string, error) { - return g.CmdLines(args) -} - func (g *GitMock) reset() { g.mux.Lock() g.commands = []string{}