Skip to content

Commit

Permalink
feat: Add exec CLI command to simplify executing scripts (#135)
Browse files Browse the repository at this point in the history
- Add `git hooks exec` command to launch executables and run configuration the
  same as Githooks executes when run normally. This features simplifies
  executing add-on scripts/executables distributed in shared hook repositories 
  (and also locally with `ns:gh-self`).

  For example execute the following
  [add-on 'format-all' script](https://github.com/gabyx/Githooks-Docs/blob/main/githooks/scripts/format-docs-all.yaml)
  with:

  ```shell
  git hooks exec --containerized \
    ns:githooks-docs/scripts/format-docs-all.yaml -- \
    --force \
    --dir ./
  ```

  This will launch the specified container and run the script.

  **Note**: You need to have configured this shared repository inside 
  your repo where you use Githooks.
  • Loading branch information
gabyx authored Dec 16, 2023
1 parent e73e446 commit 258a815
Show file tree
Hide file tree
Showing 55 changed files with 1,036 additions and 296 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
45 changes: 23 additions & 22 deletions .githooks/pre-commit/cli-docs-up-to-date
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,34 @@ repoDir="$DIR/../.."
cd "$repoDir" || exit 1

if command -v "gstat" &>/dev/null; then
statCmd="gstat"
statCmd="gstat"
else
statCmd="stat"
statCmd="stat"
fi

for file in $STAGED_FILES; do
if echo "$file" | grep -qE '^.*/cmd/.*\.go$'; then

docDate=$("$statCmd" -c %X "$repoDir/docs/cli/git_hooks.md")

if [ "$((docDate + 2 * 5))" -lt "$(date +%s)" ]; then
if [ "$autoRegenerate" = "true" ]; then
# shellcheck disable=SC2015
cd githooks && "scripts/build-doc.sh" || {
echo "! Doc regeneration failed!" >&2
exit 1
}

git add "$repoDir/docs/cli/"*

echo "Docs regenerated." >&2
else
echo "! You need to regenerate the CLI docs (its too old)!" >&2
exit 1
fi
fi
if echo "$file" | grep -qE '^.*/cmd/.*\.go$'; then

docDate=$("$statCmd" -c %X "$repoDir/docs/cli/git_hooks.md")

# Check if 7 minutes are passed.
if [ "$((docDate + 7 * 60))" -lt "$(date +%s)" ]; then
if [ "$autoRegenerate" = "true" ]; then
# shellcheck disable=SC2015
cd githooks && "scripts/build-doc.sh" || {
echo "! Doc regeneration failed!" >&2
exit 1
}

git add "$repoDir/docs/cli/"*

echo "* Docs regenerated." >&2
else
echo "! You need to regenerate the CLI docs (its too old)!" >&2
exit 1
fi
fi
fi
done

echo "* CLI docs is up to date" >&2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
.direnv/
cover/
39 changes: 39 additions & 0 deletions .nvim/nvim-dap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local dap = require("dap")

dap.adapters.delve = {
type = "server",
port = "${port}",
executable = {
command = "dlv",
args = { "dap", "-l", "127.0.0.1:${port}" },
options = {
env = {
GOPATH = "${workspaceFolder}/githooks/.go",
GOBIN = "${workspaceFolder}/githooks/bin",
},
},
},
}

dap.configurations.go = {
-- This are the requests documented here:
-- https://github.com/go-delve/delve/blob/master/Documentation/api/dap/README.md
{
type = "delve",
name = "[githooks] Debug",
request = "launch",
program = "${file}",
},

-- works with go.mod packages and sub packages
{
type = "delve",
name = "[githooks] Debug Test (go.mod)",
request = "launch",
mode = "test",
program = "${fileDirname}",

-- Because we are in a subdirevtory, this is needed.
dlvCwd = "githooks",
},
}
3 changes: 3 additions & 0 deletions .nvim/nvim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Load debug adapters in this project
nvimdap = require("nvim-dap-projects")
nvimdap.search_project_config()
56 changes: 33 additions & 23 deletions docs/changes/Braking-Changes-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,49 @@

## Namespace Path & Ignore Specification

Pull request [#35](https://github.com/gabyx/Githooks/pull/35) introduces a breaking change in the
definition of a hook's *namespace path*.
The original definition is not explicit enough which poses problems for properly distinguishing
patterns which contain a namespace from ones which don't.
Pull request [#35](https://github.com/gabyx/Githooks/pull/35) introduces a
breaking change in the definition of a hook's _namespace path_. The original
definition is not explicit enough which poses problems for properly
distinguishing patterns which contain a namespace from ones which don't.

The following changes are made:

- Before, the namespace of a hook was simply prefixed by `<hookNamespace>/...`, e.g. examples were
- Before, the namespace of a hook was simply prefixed by `<hookNamespace>/...`,
e.g. examples were

- `myhooks/pre-commit/format.sh` for a shared hook repo with `<hooksDir>/.namespace` containing `myhooks`.
- `pre-commit/format.sh` for a repository hook in `<repoDir>/.githooks` where Githooks is running.
- `myhooks/pre-commit/format.sh` for a shared hook repo with
`<hooksDir>/.namespace` containing `myhooks`.
- `pre-commit/format.sh` for a repository hook in `<repoDir>/.githooks` where
Githooks is running.

Now, new namespace paths (see documentation) are written with a explicit suffix `ns:` like:
Now, new namespace paths (see documentation) are written with a explicit
suffix `ns:` like:

- `ns:myhooks/pre-commit/format.sh` for a shared hook repository with `myhooks` in `<hooksDir>/.namespace`.
- `ns:gh-self/pre-commit/format.sh` for a repository hook in `<repoDir>/.githooks` where Githooks
is running.
- `ns:myhooks/pre-commit/format.sh` for a shared hook repository with
`myhooks` in `<hooksDir>/.namespace`.
- `ns:gh-self/pre-commit/format.sh` for a repository hook in
`<repoDir>/.githooks` where Githooks is running.

- `ns:myhooks/pre-commit/format.sh` for a repository hook in `<repoDir>/.githooks` where Githooks
is running where `<repoDir>/.githooks/.namespace` contains `myhooks`.
- `ns:myhooks/pre-commit/format.sh` for a repository hook in
`<repoDir>/.githooks` where Githooks is running where
`<repoDir>/.githooks/.namespace` contains `myhooks`.

- Furthermore, ignore patterns in `.ignore.yaml` which contain no prefix `ns:` are now considered
relative patterns. Before they matched the whole namespace path of a hook, including the namespace itself.
Now, these patterns are relative and are made absolute to the namespace of the current `<hooksDir>`, e.g. a pattern
- Furthermore, ignore patterns in `.ignore.yaml` which contain no prefix `ns:`
are now considered relative patterns. Before they matched the whole namespace
path of a hook, including the namespace itself. Now, these patterns are
relative and are made absolute to the namespace of the current `<hooksDir>`,
e.g. a pattern

- `**/pre-commit/format.sh` in `.githooks/.ignore.yaml` will be the same as `ns:gh-self/**/pre-commit/format.sh`
and a pattern
- `**/batch*/format.sh` in `.githooks/pre-commit/.ignore.yaml` will be the same as
`ns:gh-self/pre-commit/**/batch*/format.sh` where `gh-self` is the internal default namespace
for the repository where Githooks is running.
- `**/pre-commit/format.sh` in `.githooks/.ignore.yaml` will be the same as
`ns:gh-self/**/pre-commit/format.sh` and a pattern
- `**/batch*/format.sh` in `.githooks/pre-commit/.ignore.yaml` will be the
same as `ns:gh-self/pre-commit/**/batch*/format.sh` where `gh-self` is the
internal default namespace for the repository where Githooks is running.

- Replaced hooks are now addressed by a namespace `gh-replaced`, e.g. `ns:gh-replaced/pre-commit.replaced.githook`
- Replaced hooks are now addressed by a namespace `gh-replaced`, e.g.
`ns:gh-replaced/pre-commit.replaced.githook`

## Upgrade Resolution

- Check `git hooks list` and adapt the `.ignore.yaml` files to adapt to the new namespace path.
- Check `git hooks list` and adapt the `.ignore.yaml` files to adapt to the new
namespace path.
Loading

0 comments on commit 258a815

Please sign in to comment.