Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remote issue with worktrees #960

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/config/skip_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import (
"testing"

"github.com/evilmartians/lefthook/internal/git"
"github.com/evilmartians/lefthook/internal/system"
)

type mockCmd struct{}

func (mc mockCmd) WithEnvs(...string) system.Command {
return mc
}

func (mc mockCmd) Run(cmd []string, _root string, _in io.Reader, _out io.Writer, _errOut io.Writer) error {
if len(cmd) == 3 && cmd[2] == "success" {
return nil
Expand Down
4 changes: 4 additions & 0 deletions internal/git/command_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func NewExecutor(cmd system.Command) *CommandExecutor {
return &CommandExecutor{cmd: cmd}
}

func (c CommandExecutor) WithEnvs(envs ...string) CommandExecutor {
return CommandExecutor{cmd: c.cmd.WithEnvs(envs...), root: c.root}
}

// Cmd runs plain string command. Trims spaces around output.
func (c CommandExecutor) Cmd(cmd []string) (string, error) {
out, err := c.execute(cmd, c.root)
Expand Down
12 changes: 9 additions & 3 deletions internal/git/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,29 @@ func (r *Repository) SyncRemote(url, ref string, force bool) error {
func (r *Repository) updateRemote(path, ref string) error {
log.Debugf("Updating remote config repository: %s", path)

// This is overwriting values for worktrees, otherwise it does not work.
git := r.Git.WithEnvs(
"GIT_DIR", filepath.Join(path, ".git"),
"GIT_INDEX_FILE", filepath.Join(path, ".git", "index"),
)

if len(ref) != 0 {
_, err := r.Git.Cmd([]string{
_, err := git.Cmd([]string{
"git", "-C", path, "fetch", "--quiet", "--depth", "1",
"origin", ref,
})
if err != nil {
return err
}

_, err = r.Git.Cmd([]string{
_, err = git.Cmd([]string{
"git", "-C", path, "checkout", "FETCH_HEAD",
})
if err != nil {
return err
}
} else {
_, err := r.Git.Cmd([]string{"git", "-C", path, "pull", "--quiet"})
_, err := git.Cmd([]string{"git", "-C", path, "pull", "--quiet"})
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions internal/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import (
"io"
"strings"
"testing"

"github.com/evilmartians/lefthook/internal/system"
)

type gitCmd struct {
cases map[string]string
}

func (g gitCmd) WithEnvs(...string) system.Command {
return g
}

func (g gitCmd) Run(cmd []string, _root string, _in io.Reader, out io.Writer, _errOut io.Writer) error {
res, ok := g.cases[(strings.Join(cmd, " "))]
if !ok {
Expand Down
5 changes: 5 additions & 0 deletions internal/lefthook/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import (
"github.com/stretchr/testify/assert"

"github.com/evilmartians/lefthook/internal/git"
"github.com/evilmartians/lefthook/internal/system"
)

type gitCmd struct{}

func (g gitCmd) WithEnvs(...string) system.Command {
return g
}

func (g gitCmd) Run([]string, string, io.Reader, io.Writer, io.Writer) error {
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions internal/lefthook/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/evilmartians/lefthook/internal/git"
"github.com/evilmartians/lefthook/internal/lefthook/runner/exec"
"github.com/evilmartians/lefthook/internal/log"
"github.com/evilmartians/lefthook/internal/system"
)

type (
Expand All @@ -43,6 +44,10 @@ func (e cmd) RunWithContext(context.Context, []string, string, io.Reader, io.Wri
return nil
}

func (g *gitCmd) WithEnvs(...string) system.Command {
return g
}

func (g *gitCmd) Run(cmd []string, _root string, _in io.Reader, out io.Writer, _errOut io.Writer) error {
g.mux.Lock()
g.commands = append(g.commands, strings.Join(cmd, " "))
Expand Down
43 changes: 29 additions & 14 deletions internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,50 @@ package system

import (
"context"
"fmt"
"io"
"os"
"os/exec"

"github.com/evilmartians/lefthook/internal/log"
)

type osCmd struct{}
type osCmd struct {
env []string
}

var Cmd = osCmd{}

type Command interface {
WithEnvs(...string) Command
Run([]string, string, io.Reader, io.Writer, io.Writer) error
}

type CommandWithContext interface {
RunWithContext(context.Context, []string, string, io.Reader, io.Writer, io.Writer) error
}

func (c osCmd) WithEnvs(envs ...string) Command {
if len(envs)%2 != 0 {
panic("usage: WithEnvs(name, value, name, value...")
}

if c.env == nil {
//nolint:mnd
c.env = make([]string, 0, len(envs)/2)
}

for i := 0; i < len(envs); i += 2 {
c.env = append(c.env, fmt.Sprintf("%s=%s", envs[i], envs[i+1]))
}

return c
}

func (c osCmd) Run(command []string, root string, in io.Reader, out io.Writer, errOut io.Writer) error {
return c.RunWithContext(context.Background(), command, root, in, out, errOut)
}

// Run runs system command with LEFTHOOK=0 in order to prevent calling
// subsequent lefthook hooks.
func (c osCmd) RunWithContext(
Expand All @@ -35,20 +60,10 @@ func (c osCmd) RunWithContext(
log.Debug("[lefthook] cmd: ", command)

cmd := exec.CommandContext(ctx, command[0], command[1:]...)
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Env = append(cmd.Env, c.env...)
cmd.Env = append(cmd.Env, "LEFTHOOK=0")

return run(cmd, root, in, out, errOut)
}

func (c osCmd) Run(command []string, root string, in io.Reader, out io.Writer, errOut io.Writer) error {
log.Debug("[lefthook] cmd: ", command)

cmd := exec.Command(command[0], command[1:]...)

return run(cmd, root, in, out, errOut)
}

func run(cmd *exec.Cmd, root string, in io.Reader, out io.Writer, errOut io.Writer) error {
cmd.Env = append(os.Environ(), "LEFTHOOK=0")
if len(root) > 0 {
cmd.Dir = root
log.Debug("[lefthook] dir: ", root)
Expand Down
2 changes: 1 addition & 1 deletion schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@
"type": "object"
}
},
"$comment": "Last updated on 2025.01.31.",
"$comment": "Last updated on 2025.02.24.",
"properties": {
"min_version": {
"type": "string",
Expand Down
Loading