Skip to content

refactor(dev-tools): consolidate git hook scripts under contrib/dev-tools/git - #1704

Merged
josecelano merged 2 commits into
torrust:developfrom
josecelano:refactor/consolidate-dev-scripts
Apr 22, 2026
Merged

josecelano merged 2 commits into
torrust:developfrom
josecelano:refactor/consolidate-dev-scripts

Conversation

@josecelano

Copy link
Copy Markdown
Member

Summary

Consolidates the duplicate/misplaced scripts from scripts/ into their canonical location under contrib/dev-tools/git/.

Changes

Files moved / deleted

  • scripts/install-git-hooks.sh → contrib/dev-tools/git/install-git-hooks.sh
  • scripts/pre-commit.sh → deleted (duplicate; canonical version now lives in hooks dir)

Scripts rewritten in new structured style

Both hook scripts now use a STEPS array with per-step timing and clear PASSED/FAILED output, matching the style of the new pre-commit.sh.

contrib/dev-tools/git/hooks/pre-commit.sh (fast, ~3 min):

  1. cargo machete — unused dependency check
  2. linter all — all linters
  3. cargo test --doc --workspace — documentation tests
  4. cargo test --tests --benches --examples --workspace --all-targets --all-features — all tests

Nightly toolchain checks and e2e tests are intentionally excluded — they are too slow for pre-commit and are covered by CI and pre-push.

contrib/dev-tools/git/hooks/pre-push.sh (comprehensive, ~15 min):

  1. cargo machete
  2. linter all
  3. cargo +nightly fmt --check
  4. cargo +nightly check …
  5. cargo +nightly doc …
  6. cargo test --doc --workspace
  7. cargo test … --all-targets --all-features
  8. E2E tests (e2e_tests_runner)

References updated

All references to the old scripts/ paths have been updated across:

  • .githooks/pre-commit
  • AGENTS.md
  • .github/workflows/copilot-setup-steps.yml
  • .github/agents/committer.agent.md
  • .github/agents/implementer.agent.md
  • .github/skills/dev/git-workflow/commit-changes/SKILL.md
  • .github/skills/dev/git-workflow/run-pre-commit-checks/SKILL.md
  • .github/skills/dev/maintenance/setup-dev-environment/SKILL.md
  • .github/skills/dev/maintenance/update-dependencies/SKILL.md
  • docs/issues/1697-ai-agent-configuration.md

…ools/git

Move scripts/ content to canonical locations:
- scripts/install-git-hooks.sh → contrib/dev-tools/git/install-git-hooks.sh
- scripts/pre-commit.sh → deleted (duplicate)
- contrib/dev-tools/git/hooks/pre-commit.sh → rewritten in new structured style
- contrib/dev-tools/git/hooks/pre-push.sh → rewritten in new structured style

The new pre-commit.sh uses a STEPS array with per-step timing and
PASSED/FAILED output. It excludes nightly toolchain checks and e2e tests
(too slow for pre-commit; covered by CI and pre-push).

The new pre-push.sh follows the same structured style and retains all
checks from the old script including nightly fmt/check/doc and e2e tests.

Update all references across agents, skills, workflows, and documentation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR consolidates git-hook related developer tooling by removing the old scripts/ entrypoints and standardizing on the canonical location under contrib/dev-tools/git/, while updating all internal documentation/workflow references accordingly.

Changes:

  • Delete the duplicate scripts/pre-commit.sh and migrate callers to contrib/dev-tools/git/hooks/pre-commit.sh.
  • Move/standardize the hook installer to contrib/dev-tools/git/install-git-hooks.sh.
  • Rewrite pre-commit.sh and pre-push.sh hook scripts into a structured “timed steps” format, and update docs/skills/agents/workflows to reference the new paths.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
scripts/pre-commit.sh Removes the legacy/duplicate pre-commit entrypoint.
contrib/dev-tools/git/install-git-hooks.sh Canonical git hook installer path and updated usage comment.
contrib/dev-tools/git/hooks/pre-commit.sh Canonical structured pre-commit hook script.
contrib/dev-tools/git/hooks/pre-push.sh Canonical structured pre-push hook script (nightly + e2e).
.githooks/pre-commit Updates wrapper to call canonical pre-commit script under contrib/.
AGENTS.md Updates hook/script paths in repo guidance.
docs/issues/1697-ai-agent-configuration.md Updates referenced hook install + pre-commit invocation paths.
.github/workflows/copilot-setup-steps.yml Updates workflow path filters and install command to new locations.
.github/agents/committer.agent.md Updates “pre-commit validation command” path.
.github/agents/implementer.agent.md Updates “pre-commit validation command” path.
.github/skills/dev/git-workflow/commit-changes/SKILL.md Updates hook installation and pre-commit command references.
.github/skills/dev/git-workflow/run-pre-commit-checks/SKILL.md Updates hook installation and pre-commit command references.
.github/skills/dev/maintenance/setup-dev-environment/SKILL.md Updates hook installation and pre-commit command references.
.github/skills/dev/maintenance/update-dependencies/SKILL.md Updates verification command reference to canonical pre-commit script.
Comments suppressed due to low confidence (1)

contrib/dev-tools/git/install-git-hooks.sh:8

  • This installer copies hook wrappers from .githooks/ into .git/hooks/, but the repo currently only contains .githooks/pre-commit. As a result, the canonical contrib/dev-tools/git/hooks/pre-push.sh script will not be installed as a Git pre-push hook and won’t run automatically. Consider adding a .githooks/pre-push wrapper (like .githooks/pre-commit) or otherwise wiring pre-push into the installation process.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread AGENTS.md Resolved
Comment thread contrib/dev-tools/git/hooks/pre-push.sh Resolved
@codecov

codecov Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.43%. Comparing base (4b8a21f) to head (b3d1e6b).
⚠️ Report is 3 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1704   +/-   ##
========================================
  Coverage    86.43%   86.43%           
========================================
  Files          288      288           
  Lines        22672    22672           
  Branches     22672    22672           
========================================
+ Hits         19596    19597    +1     
  Misses        2838     2838           
+ Partials       238      237    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- AGENTS.md: clarify nightly toolchain is also needed for pre-push checks
- pre-push.sh: explicitly use cargo +stable for non-nightly steps to
  ensure consistent results across machines regardless of default toolchain
@josecelano

Copy link
Copy Markdown
Member Author

ACK b3d1e6b

@josecelano
josecelano merged commit d51ef29 into torrust:develop Apr 22, 2026
22 checks passed

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a post-merge review of #1704 as it stands at e170b9620 on the default branch.

What still runs from it is the runner skeleton shared by contrib/dev-tools/git/hooks/pre-commit.sh and contrib/dev-tools/git/hooks/pre-push.sh (the STEPS array, format_time, per-step timing, the main loop and the result banners), the installer at its new path contrib/dev-tools/git/install-git-hooks.sh, and the path filters and install step in .github/workflows/copilot-setup-steps.yml. The step lists, the command runner, the failure handling and the body of the .githooks/pre-commit dispatcher were rewritten afterwards, mostly in #1783. Two points of the merged state are now history: at d51ef2988 .githooks/ held only pre-commit, so the nightly checks the description calls "covered by CI and pre-push" ran locally only when pre-push.sh was started by hand, until d2d142049 added the pre-push dispatcher; and the word-splitting command runner and the relative E2E configuration path have since been replaced or removed.

[Minor] contrib/dev-tools/git/install-git-hooks.sh:15 resolves the destination with git rev-parse --git-path hooks, which honours core.hooksPath. For a developer whose global configuration points core.hooksPath at a shared directory, line 29 copies this repository's pre-commit and pre-push dispatchers into that directory, replacing the hooks already there without a backup; every other repository using that directory then runs a dispatcher that executes $repo_root/contrib/dev-tools/git/hooks/pre-commit.sh, which does not exist there, so its commits and pushes fail. Line 32 still prints → .git/hooks/<name> and the script exits 0. With core.hooksPath=.githooks the copy lands on the file itself and the script stops with "are the same file". Both cases reproduce with git 2.55.0 against a scratch repository. Smallest fix: stop with an explanatory error when git config --get core.hooksPath returns a value, refuse to overwrite an existing hook whose content differs, and print the resolved ${dest} instead of a fixed .git/hooks path.

[Minor] .github/workflows/copilot-setup-steps.yml:13 and :18 trigger the workflow on changes to contrib/dev-tools/git/hooks/pre-commit.sh, but the job never runs that script: line 55 installs the dispatchers and line 58 runs linter all. The environment it provisions has only the stable toolchain (line 37), while the hooks it installs run cargo +nightly fmt --all -- --check (contrib/dev-tools/git/hooks/pre-commit.sh:61, added in bed1a4d77) and the nightly fmt, check and doc commands at contrib/dev-tools/git/hooks/pre-push.sh:46-50. bed1a4d77 changed a listed path, so it triggered this workflow, which contained nothing that could fail on the new check; a commit made in the provisioned environment fails at that check unless nightly is fetched on demand. The filters also omit .githooks/**, the files line 55 actually copies, so a change to a dispatcher does not run the workflow at all. Smallest fix: install the nightly toolchain with its rustfmt component, run ./contrib/dev-tools/git/hooks/pre-commit.sh --format=json as the smoke check so the trigger tests what it lists, and add .githooks/** to both path filters.

[Minor] contrib/dev-tools/git/hooks/pre-commit.sh:210-217 reserves a log name with mktemp and then renames it to ${_tmp}.log, a name nothing reserved, without checking the rename. The script runs without set -e (line 22), so a failed mv is ignored and run_command writes to ${_tmp}.log through a plain > redirect (line 192), which follows a symbolic link. With the default log directory /tmp (line 70), which applies whenever the script is run directly as AGENTS.md describes, another local user watching /tmp can create <name>.log as a link between the two calls: the rename then fails in the sticky directory and the check's output truncates and overwrites the link's target on systems that do not restrict following links in sticky directories (macOS; Linux with fs.protected_symlinks=0), while on a default Linux system the check fails spuriously instead. The same lines are at contrib/dev-tools/git/hooks/pre-push.sh:199-206; both came in with a916d0bb1. Smallest fix: use the mktemp path itself as the log path and drop the rename, or create one private directory per run with mktemp -d and write the logs inside it; at the least, mv -- "$_tmp" "$log_path" || return 2.

[Nit] The two scripts that #1704 shaped from one template have drifted on their result contract under the same "schema_version": 1. On a failing check contrib/dev-tools/git/hooks/pre-commit.sh:378-391 puts the command's own status into the JSON exit_code and exits with it (a failing cargo test gives 101), while contrib/dev-tools/git/hooks/pre-push.sh:365-380 maps every failure to 1 or 2, so a check that itself exits 2 is reported there as an infrastructure error. A consumer of the JSON output cannot read exit_code the same way for both hooks, and the duplicated runner invites the next divergence. Smallest fix: choose one contract and move the shared runner into one file that both hooks source.

[Nit] contrib/dev-tools/git/hooks/pre-commit.sh:52 and :63 run ./contrib/dev-tools/checks/... by a path relative to the working directory, and neither hook script changes to the repository root. Started from a subdirectory (for example ../contrib/dev-tools/git/hooks/pre-commit.sh from packages/), the first check exits 127 with "No such file or directory" before anything else runs. Git runs hooks from the top level, so only direct invocation is affected. Smallest fix: cd "$(git rev-parse --show-toplevel)" after ensure_cargo_on_path in both scripts.

[Nit] .github/workflows/copilot-setup-steps.yml:49 installs cargo-machete unversioned and without --locked, while lines 46 and 52 pin [email protected] and [email protected] with --locked, so each run builds whatever release and dependency resolution the registry serves that day into the environment where the hooks then run cargo machete --with-metadata. The line predates #1704. Smallest fix: cargo install --locked cargo-machete@<version>.

Checked and found sound: no reference to the removed scripts/ paths remains at the pull request's head; the workflow keeps permissions: contents: read and uses no secrets and no pull_request_target; the dispatchers and the installer quote every path they use; .tmp/, where the dispatchers send hook logs, is git-ignored; format_time, the IFS='|' step parsing and the per-step timing behave correctly for the current step lists; the $? read in the else branch of pre-commit.sh and the PIPESTATUS[0] read after tee both capture the intended status; and json_escape removes or escapes every control character JSON forbids.

This branch was previously deployed

1 inactive deployment
coverage — b3d1e6b7 Deployed Apr 22, 2026 by josecelano via Generate Coverage Report #1109
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants