Skip to content

Commit

Permalink
fix: execute files command within configured root
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Jan 12, 2024
1 parent 02e6f77 commit 356b2f6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
23 changes: 18 additions & 5 deletions internal/git/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/evilmartians/lefthook/internal/log"
Expand All @@ -12,6 +13,7 @@ type Exec interface {
SetRootPath(root string)
Cmd(cmd []string) (string, error)
CmdLines(cmd []string) ([]string, error)
CmdLinesWithinFolder(cmd []string, folder string) ([]string, error)
}

type OsExec struct {
Expand All @@ -30,7 +32,7 @@ func (o *OsExec) SetRootPath(root string) {

// Cmd runs plain string command. Trims spaces around output.
func (o *OsExec) Cmd(cmd []string) (string, error) {
out, err := o.rawExecArgs(cmd)
out, err := o.rawExecArgs(cmd, "")
if err != nil {
return "", err
}
Expand All @@ -40,7 +42,17 @@ func (o *OsExec) Cmd(cmd []string) (string, error) {

// CmdLines runs plain string command, returns its output split by newline.
func (o *OsExec) CmdLines(cmd []string) ([]string, error) {
out, err := o.rawExecArgs(cmd)
out, err := o.rawExecArgs(cmd, "")
if err != nil {
return nil, err
}

return strings.Split(strings.TrimSpace(out), "\n"), nil
}

// CmdLines runs plain string command, returns its output split by newline.
func (o *OsExec) CmdLinesWithinFolder(cmd []string, folder string) ([]string, error) {
out, err := o.rawExecArgs(cmd, folder)
if err != nil {
return nil, err
}
Expand All @@ -50,15 +62,16 @@ func (o *OsExec) CmdLines(cmd []string) ([]string, error) {

// rawExecArgs executes git command with LEFTHOOK=0 in order
// to prevent calling subsequent lefthook hooks.
func (o *OsExec) rawExecArgs(args []string) (string, error) {
func (o *OsExec) rawExecArgs(args []string, folder string) (string, error) {
log.Debug("[lefthook] cmd: ", args)

root := filepath.Join(o.root, folder)
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = o.root
cmd.Dir = root
cmd.Env = append(os.Environ(), "LEFTHOOK=0")

out, err := cmd.CombinedOutput()
log.Debug("[lefthook] dir: ", o.root)
log.Debug("[lefthook] dir: ", root)
log.Debug("[lefthook] err: ", err)
log.Debug("[lefthook] out: ", string(out))
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ func NewRepository(fs afero.Fs, git Exec) (*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
}
Expand All @@ -129,7 +129,7 @@ func (r *Repository) PushFiles() ([]string, error) {
break
}
}
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
Expand Down Expand Up @@ -283,8 +283,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
}
Expand Down
4 changes: 4 additions & 0 deletions internal/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (g GitMock) CmdLines(cmd []string) ([]string, error) {
return strings.Split(res, "\n"), nil
}

func (g GitMock) CmdLinesWithinFolder(cmd []string, _folder string) ([]string, error) {
return g.CmdLines(cmd)
}

func TestPartiallyStagedFiles(t *testing.T) {
for i, tt := range [...]struct {
name, gitOut string
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/run/prepare_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (r *Runner) buildRun(command *config.Command) (*run, error, error) {
} else {
cmd = []string{"sh", "-c", filesCmd}
}
return r.Repo.FilesByCommand(cmd)
return r.Repo.FilesByCommand(cmd, command.Root)
},
}

Expand Down
4 changes: 4 additions & 0 deletions internal/lefthook/run/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (g *GitMock) CmdLines(args []string) ([]string, error) {
return nil, 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{}
Expand Down
4 changes: 4 additions & 0 deletions internal/lefthook/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (g GitMock) CmdLines(_cmd []string) ([]string, error) {
return nil, nil
}

func (g GitMock) CmdLinesWithinFolder(_cmd []string, _folder string) ([]string, error) {
return nil, nil
}

func TestRun(t *testing.T) {
root, err := filepath.Abs("src")
if err != nil {
Expand Down

0 comments on commit 356b2f6

Please sign in to comment.