Skip to content

Commit 4f85d04

Browse files
authored
fix: replace cmd in jobs (#918)
1 parent 3e3f880 commit 4f85d04

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

internal/config/load.go

+12
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,18 @@ func mergeJobsSlice(src, dest []interface{}) []interface{} {
537537
destSubJobs = mergeJobsSlice(srcSubJobs, destSubJobs)
538538
}
539539

540+
// Replace possible {cmd} before merging the jobs
541+
switch srcRun := srcJob["run"].(type) {
542+
case string:
543+
switch destRun := destJob["run"].(type) {
544+
case string:
545+
newRun := strings.ReplaceAll(srcRun, CMD, destRun)
546+
srcJob["run"] = newRun
547+
default:
548+
}
549+
default:
550+
}
551+
540552
maps.Merge(srcJob, destJob)
541553

542554
if len(destSubJobs) != 0 {

internal/config/load_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,36 @@ pre-commit:
809809
},
810810
},
811811
},
812+
"with jobs overwrite": {
813+
files: map[string]string{
814+
"lefthook.yml": `
815+
pre-commit:
816+
jobs:
817+
- name: job 1
818+
run: echo from job 1
819+
`,
820+
"lefthook-local.yml": `
821+
pre-commit:
822+
jobs:
823+
- name: job 1
824+
run: wrap {cmd}
825+
`,
826+
},
827+
result: &Config{
828+
SourceDir: ".lefthook",
829+
SourceDirLocal: ".lefthook-local",
830+
Hooks: map[string]*Hook{
831+
"pre-commit": {
832+
Jobs: []*Job{
833+
{
834+
Name: "job 1",
835+
Run: "wrap echo from job 1",
836+
},
837+
},
838+
},
839+
},
840+
},
841+
},
812842
} {
813843
fs := afero.Afero{Fs: afero.NewMemMapFs()}
814844
repo := &git.Repository{

0 commit comments

Comments
 (0)