• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evilmartians / lefthook / 9445529249

10 Jun 2024 09:03AM UTC coverage: 78.473%. Remained the same
9445529249

push

github

web-flow
fix: skip overwriting hooks when fetching data from remotes (#745)

1 of 1 new or added line in 1 file covered. (100.0%)

2621 of 3340 relevant lines covered (78.47%)

4.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.84
/internal/git/command_executor.go
1
package git
2

3
import (
4
        "bytes"
5
        "fmt"
6
        "path/filepath"
7
        "strings"
8

9
        "github.com/evilmartians/lefthook/internal/log"
10
        "github.com/evilmartians/lefthook/internal/system"
11
)
12

13
// CommandExecutor provides some methods that take some effect on execution and/or result data.
14
type CommandExecutor struct {
15
        cmd  system.Command
16
        root string
17
}
18

19
// NewExecutor returns an object that executes given commands in the OS.
20
func NewExecutor(cmd system.Command) *CommandExecutor {
4✔
21
        return &CommandExecutor{cmd: cmd}
4✔
22
}
4✔
23

24
// Cmd runs plain string command. Trims spaces around output.
25
func (c CommandExecutor) Cmd(cmd []string) (string, error) {
4✔
26
        out, err := c.execute(cmd, c.root)
4✔
27
        if err != nil {
7✔
28
                return "", err
3✔
29
        }
3✔
30

31
        return strings.TrimSpace(out), nil
4✔
32
}
33

34
// BatchedCmd runs the command with any number of appended arguments batched in chunks to match the OS limits.
35
func (c CommandExecutor) BatchedCmd(cmd []string, args []string) (string, error) {
4✔
36
        maxlen := system.MaxCmdLen()
4✔
37
        result := strings.Builder{}
4✔
38

4✔
39
        argsBatched := batchByLength(args, maxlen-len(cmd))
4✔
40
        for i, batch := range argsBatched {
8✔
41
                out, err := c.Cmd(append(cmd, batch...))
4✔
42
                if err != nil {
4✔
43
                        return "", fmt.Errorf("error in batch %d: %w", i, err)
×
44
                }
×
45
                result.WriteString(out)
4✔
46
                result.WriteString("\n")
4✔
47
        }
48

49
        return result.String(), nil
4✔
50
}
51

52
// CmdLines runs plain string command, returns its output split by newline.
53
func (c CommandExecutor) CmdLines(cmd []string) ([]string, error) {
7✔
54
        out, err := c.execute(cmd, c.root)
7✔
55
        if err != nil {
7✔
56
                return nil, err
×
57
        }
×
58

59
        return strings.Split(strings.TrimSpace(out), "\n"), nil
7✔
60
}
61

62
// CmdLines runs plain string command, returns its output split by newline.
63
func (c CommandExecutor) CmdLinesWithinFolder(cmd []string, folder string) ([]string, error) {
×
64
        root := filepath.Join(c.root, folder)
×
65
        out, err := c.execute(cmd, root)
×
66
        if err != nil {
×
67
                return nil, err
×
68
        }
×
69

70
        return strings.Split(strings.TrimSpace(out), "\n"), nil
×
71
}
72

73
func (c CommandExecutor) execute(cmd []string, root string) (string, error) {
7✔
74
        out := bytes.NewBuffer(make([]byte, 0))
7✔
75
        err := c.cmd.Run(cmd, root, system.NullReader, out)
7✔
76
        strOut := out.String()
7✔
77

7✔
78
        log.Debug("[lefthook] out: ", strOut)
7✔
79

7✔
80
        return strOut, err
7✔
81
}
7✔
82

83
func batchByLength(s []string, length int) [][]string {
4✔
84
        batches := make([][]string, 0)
4✔
85

4✔
86
        var acc, prev int
4✔
87
        for i := range s {
8✔
88
                acc += len(s[i])
4✔
89
                if acc > length {
4✔
90
                        if i == prev {
×
91
                                batches = append(batches, s[prev:i+1])
×
92
                                prev = i + 1
×
93
                        } else {
×
94
                                batches = append(batches, s[prev:i])
×
95
                                prev = i
×
96
                        }
×
97
                        acc = len(s[i])
×
98
                }
99
        }
100
        if acc > 0 {
8✔
101
                batches = append(batches, s[prev:])
4✔
102
        }
4✔
103

104
        return batches
4✔
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc