Skip to content

Commit

Permalink
feat: add support for MATCHED_FILES and PACKAGE_FILES step argument
Browse files Browse the repository at this point in the history
  • Loading branch information
LMaxence committed Aug 29, 2024
1 parent 2efd890 commit fae0045
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 7 deletions.
10 changes: 7 additions & 3 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ A set of arguments is provided by Mookme, that can be directly used in the hooks

````json title="hooks/commit-msg.json"
{
"command": "run-something $1" // Will be replaced with the value of `args`
"command": "run-something $1" // (1)!
"command": "run-something $MATCHED_FILES" // (2)!
"command": "run-something $PACKAGE_FILES" // (3)!
}
````

- `$1`
1. The argument being passed by git to the hook file. See [the Git documentation on the hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) for more details about what it contains depending on the hook type being executed.

The argument being passed by git to the hook file. See [the Git documentation on the hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) for more details about what it contains depending on the hook type being executed.
2. The list of changed files matched by the onlyOn option of this step, separated by a space. If the onlyOn option is not set, it will be replaced with an empty string.

3. The list of changed files in the package folder, separated by a space.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ theme:

features:
- content.tabs.link
- content.code.annotate

palette:
- media: "(prefers-color-scheme)"
Expand Down
1 change: 1 addition & 0 deletions packages/configuration/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ var ALL_HOOKS_STRING = []string{

type Hook struct {
Path string
Files []string
Steps []Step
}
2 changes: 2 additions & 0 deletions packages/configuration/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,15 @@ func LoadHooksConfiguration(directory string, hookType HookType) ([]Hook, error)
OnlyOn: step.OnlyOn,
From: step.From,
ID: uuid.NewString(),
Files: make([]string, 0),
})
}

}

hooks = append(hooks, Hook{
Path: filepath.Dir(filepath.Dir(path)),
Files: make([]string, 0),
Steps: steps,
})
}
Expand Down
1 change: 1 addition & 0 deletions packages/configuration/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ type Step struct {
Serial bool
From *string
PackageRelativePath string
Files []string
}
3 changes: 3 additions & 0 deletions packages/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func (h *HookExecutor) RunStep(step *configuration.Step) {
h.onStepStatusChange(step, StepExecutionStatusRunning)

cmd := strings.ReplaceAll(step.Command, "$1", strings.Join(h.gitCommandArguments, " "))
cmd = strings.ReplaceAll(cmd, "$MATCHED_FILES", strings.Join(step.Files, " "))
cmd = strings.ReplaceAll(cmd, "$PACKAGE_FILES", strings.Join(h.hook.Files, ""))

command := exec.Command("sh", "-c", cmd)
command.Dir = h.hook.Path

Expand Down
2 changes: 1 addition & 1 deletion packages/meta/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package meta

const GOOKME_CLI_VERSION = "0.4.0"
const GOOKME_CLI_VERSION = "0.5.0"
8 changes: 5 additions & 3 deletions packages/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ func FilterHooksWithChangeset(
// If the hook is not in the changeset, skip it

for _, hook := range hooks {
matchedPaths := filterChangesetWithPrefix(changedPaths, hook.Path)
if len(matchedPaths) > 0 {
hook.Files = append(hook.Files, filterChangesetWithPrefix(changedPaths, hook.Path)...)

if len(hook.Files) > 0 {
filtered = append(filtered, hook)
} else {
logger.Debugf("Hook %s did not match any file, dropping", hook.Path)
Expand Down Expand Up @@ -97,8 +98,9 @@ func FilterStepsWithOnlyOn(
if err != nil {
continue
}
step.Files = append(step.Files, changedPathsWithPattern...)

if len(changedPathsWithPattern) > 0 {
if len(step.Files) > 0 {
steps = append(steps, step)
} else {
logger.Debugf("Step %s:%s did not match any file using pattern %s, dropping", step.PackageRelativePath, step.Name, *onlyOn)
Expand Down

0 comments on commit fae0045

Please sign in to comment.