Skip to content

Commit 303e3f7

Browse files
authored
chore: small changes on diff printing (#1242)
1 parent a4223e9 commit 303e3f7

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

internal/git/repository.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,24 @@ func (r *Repository) Changeset() (map[string]string, error) {
401401
return changeset, nil
402402
}
403403

404+
func (r *Repository) PrintDiff(files []string) {
405+
slices.Sort(files)
406+
407+
diffCmd := make([]string, 0, 4) //nolint:mnd // 3 or 4 elements
408+
diffCmd = append(diffCmd, "git", "diff")
409+
if log.Colorized() {
410+
diffCmd = append(diffCmd, "--color")
411+
}
412+
diffCmd = append(diffCmd, "--")
413+
diff, err := r.Git.BatchedCmd(diffCmd, files)
414+
if err != nil {
415+
log.Warnf("Couldn't diff changed files: %s", err)
416+
return
417+
}
418+
419+
log.Warn(diff)
420+
}
421+
404422
func (r *Repository) statusShort() ([]string, error) {
405423
return r.Git.WithoutTrim().CmdLines(cmdStatusShort)
406424
}

internal/run/controller/guard.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package controller
33
import (
44
"errors"
55
"maps"
6-
"slices"
76

87
"github.com/evilmartians/lefthook/v2/internal/git"
98
"github.com/evilmartians/lefthook/v2/internal/log"
@@ -102,7 +101,7 @@ func (g *guard) after() error {
102101
log.Warnf("Couldn't get changeset: %s\n", err)
103102
}
104103
if !maps.Equal(g.changesetBefore, changesetAfter) {
105-
g.changesetDiff(changesetAfter)
104+
g.printDiff(changesetAfter)
106105
return ErrFailOnChanges
107106
}
108107
}
@@ -124,34 +123,27 @@ func (g *guard) after() error {
124123
return nil
125124
}
126125

127-
func (g *guard) changesetDiff(changesetAfter map[string]string) {
126+
func (g *guard) printDiff(changesetAfter map[string]string) {
128127
if !g.failOnChangesDiff {
129128
return
130129
}
130+
131131
changed := make([]string, 0, len(g.changesetBefore))
132132
for f, hashBefore := range g.changesetBefore {
133133
if hashAfter, ok := changesetAfter[f]; !ok || hashBefore != hashAfter {
134134
changed = append(changed, f)
135135
}
136136
}
137+
137138
for f := range changesetAfter {
138139
if _, ok := g.changesetBefore[f]; !ok {
139140
changed = append(changed, f)
140141
}
141142
}
143+
142144
if len(changed) == 0 {
143145
return
144146
}
145-
slices.Sort(changed)
146-
diffCmd := make([]string, 0, 4) //nolint:mnd // 3 or 4 elements
147-
diffCmd = append(diffCmd, "git", "diff")
148-
if log.Colorized() {
149-
diffCmd = append(diffCmd, "--color")
150-
}
151-
diffCmd = append(diffCmd, "--")
152-
if diff, err := g.git.Git.BatchedCmd(diffCmd, changed); err != nil {
153-
log.Warnf("Couldn't diff changed files: %s", err)
154-
} else {
155-
log.Println(diff)
156-
}
147+
148+
g.git.PrintDiff(changed)
157149
}

0 commit comments

Comments
 (0)