Skip to content

Commit 69610d4

Browse files
committed
feat: add mix setup/git_hooks, activate a pre-push conventional-commit hook
mix git_hooks sets core.hooksPath to githooks/ (idempotent - setting the same git config value repeatedly is a no-op). mix setup runs it, and is expected to grow more steps over time as more repo-management tasks land in this project. Adds a new pre-push hook alongside the existing commit-msg one - it reuses ci/conventional_commits.sh as-is (no changes needed: with no env vars set it already falls back to main/origin/main as base and the current branch as head, exactly right for a local push) to catch anything that slipped past commit-msg (a commit made before the hooks were installed, an amend, etc.), not just the current HEAD. Removes app/lib/mix/tasks/githooks.install.ex - an exact duplicate of this same idea living in app/'s own mix project instead of the new top-level one. Updates Readme.adoc/AGENTS.md/app/usage-rules.md's references to the old manual `git config core.hooksPath githooks` / `mix githooks.install` steps to point at `mix setup` instead.
1 parent d95bcb7 commit 69610d4

7 files changed

Lines changed: 72 additions & 33 deletions

File tree

AGENTS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ and best practices for agents to follow.
1414

1515
## Standards
1616

17-
- Conventional Commits: app/usage-rules.md — enforced by the `commit-msg` hook
18-
at `githooks/commit-msg` (run `git config core.hooksPath githooks` once per
19-
clone to activate it).
17+
- Conventional Commits: app/usage-rules.md — enforced by the `commit-msg`
18+
and `pre-push` hooks at `githooks/` (run `mix setup` once per clone to
19+
activate them).
2020

2121
<!-- usage-rules-start -->
2222
<!-- ash-start -->

Readme.adoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,11 @@ $ lproj list --mine
331331
== Development
332332

333333
First, activate the repo's git hooks (enforces conventional-commit subjects
334-
before you even push):
334+
on every commit, and again on every commit about to be pushed):
335335

336336
[source,sh]
337337
----
338-
$ cd app
339-
$ mise exec -- mix githooks.install
338+
$ mix setup
340339
----
341340

342341
The project uses ExUnit and `mix format`. Run tests with:

app/lib/mix/tasks/githooks.install.ex

Lines changed: 0 additions & 21 deletions
This file was deleted.

app/usage-rules.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
- Use the imperative, present tense in the description (`add`, not `added`/`adds`).
88
- Mark breaking changes with `!` before the colon (e.g. `feat!: ...`).
99
- Bare `Merge branch ...` subjects are rejected — reword as `chore: Merge branch ...`.
10-
- Enforced locally by the `commit-msg` hook at `githooks/commit-msg`, which
11-
delegates to `ci/validate_conventional_commit.sh` — run
12-
`git config core.hooksPath githooks` once per clone to activate it.
13-
- Enforced in CI across a whole PR's commit range by `ci/conventional_commits.sh`
14-
(same validator, run per-commit; skips GitHub's own auto-generated
15-
update-branch merge commits).
10+
- Enforced locally by the `commit-msg` hook at `githooks/commit-msg` (each
11+
commit's own subject, via `ci/validate_conventional_commit.sh`) and the
12+
`pre-push` hook at `githooks/pre-push` (every commit about to be pushed,
13+
via `ci/conventional_commits.sh` - catches anything that slipped past
14+
`commit-msg`, e.g. a commit made before the hooks were installed) — run
15+
`mix setup` once per clone to activate both.
16+
- Enforced in CI across a whole PR's commit range by the same
17+
`ci/conventional_commits.sh` the `pre-push` hook uses (skips GitHub's own
18+
auto-generated update-branch merge commits).

githooks/pre-push

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
# Enforces Conventional Commits on every commit about to be pushed, not
3+
# just whatever commit-msg already checked at commit time (catches a
4+
# commit made before this hook was installed, an amend, a rebase, etc.).
5+
# See app/usage-rules.md for the rule. Activate with:
6+
# git config core.hooksPath githooks
7+
8+
repo_top=$(git rev-parse --show-toplevel) || exit 1
9+
exec "$repo_top/ci/conventional_commits.sh"

lib/mix/tasks/git_hooks.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule Mix.Tasks.GitHooks do
2+
@shortdoc "Installs this repo's git hooks (commit-msg, pre-push - Conventional Commits)"
3+
4+
@moduledoc """
5+
#{@shortdoc}.
6+
7+
mix git_hooks
8+
9+
Sets `core.hooksPath` to `githooks/` (this repo's own `commit-msg` and
10+
`pre-push` hooks, both enforcing Conventional Commits via
11+
`ci/validate_conventional_commit.sh`/`ci/conventional_commits.sh`) -
12+
the same one-line `git config` this repo's docs already told you to run
13+
by hand, just idempotent and easy to re-run. Safe to run repeatedly:
14+
setting the same git config value twice is a no-op. Wired into
15+
`mix setup` - see that task.
16+
"""
17+
18+
use Mix.Task
19+
20+
alias RepoTasks.Shell
21+
22+
@impl Mix.Task
23+
def run(_argv) do
24+
Shell.run!("git", ["config", "core.hooksPath", "githooks"])
25+
Mix.shell().info("==> Git hooks installed (core.hooksPath = githooks)")
26+
:ok
27+
end
28+
end

lib/mix/tasks/setup.ex

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule Mix.Tasks.Setup do
2+
@shortdoc "Sets up this repo for development"
3+
4+
@moduledoc """
5+
#{@shortdoc}.
6+
7+
mix setup
8+
9+
Runs every one-time/idempotent setup step this repo needs. Currently
10+
just `mix git_hooks` - expected to grow (e.g. app/'s own `mix deps.get`)
11+
as more repo-management tasks land here.
12+
"""
13+
14+
use Mix.Task
15+
16+
@impl Mix.Task
17+
def run(_argv) do
18+
Mix.Task.run("git_hooks")
19+
:ok
20+
end
21+
end

0 commit comments

Comments
 (0)