Skip to content

feat(git): format project dictionary before commits - #2020

Merged
josecelano merged 19 commits into
torrust:developfrom
josecelano:2019-automatically-format-project-dictionary
Jul 22, 2026
Merged

josecelano merged 19 commits into
torrust:developfrom
josecelano:2019-automatically-format-project-dictionary

Conversation

@josecelano

Copy link
Copy Markdown
Member

Summary

Adds an interim, independently runnable dictionary formatter that uses LC_ALL=C sort --unique for deterministic ordering and exact de-duplication. The pre-commit hook invokes it before verification checks and aborts with restaging guidance whenever it rewrites project-words.txt.

Also adds focused shell integration coverage for changed and unchanged formatter/hook behavior, formats the checked-in dictionary, and updates the linked workflow skills. This interim implementation may be replaced or refactored by the future EPIC #2003 automation design.

Closes #2019

Validation

  • ./contrib/dev-tools/git/tests/test-format-project-words.sh
  • TORRUST_GIT_HOOKS_LOG_DIR=.tmp ./contrib/dev-tools/git/hooks/pre-commit.sh --format=json
  • TORRUST_GIT_HOOKS_LOG_DIR=.tmp ./contrib/dev-tools/git/hooks/pre-push.sh --format=json
  • Push-triggered pre-push validation (nightly format/check/docs and stable workspace tests)

Copilot AI review requested due to automatic review settings July 22, 2026 08:55
@josecelano
josecelano requested a review from a team as a code owner July 22, 2026 08:55
@josecelano josecelano self-assigned this Jul 22, 2026

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 adds a deterministic formatter for the repository cspell dictionary (project-words.txt) and wires it into the pre-commit hook so commits abort when the formatter rewrites the file (forcing deliberate restaging). It also updates related workflow documentation, adds shell integration coverage, and formats the checked-in dictionary as part of the change.

Changes:

  • Introduce contrib/dev-tools/git/format-project-words.sh to apply LC_ALL=C sort --unique and signal when the dictionary was rewritten.
  • Invoke the formatter as the first pre-commit step and provide restaging guidance when it triggers a failure.
  • Add a focused shell integration test and update issue/spec + skill documentation to reflect the new workflow.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
contrib/dev-tools/git/format-project-words.sh Adds standalone deterministic formatter for project-words.txt.
contrib/dev-tools/git/hooks/pre-commit.sh Runs the formatter before verification checks; adds failure guidance.
contrib/dev-tools/git/tests/test-format-project-words.sh Adds shell integration coverage for formatter + pre-commit behavior.
project-words.txt Applies formatter output (sorted + exact de-duplication).
docs/issues/open/2019-automatically-format-project-dictionary.md Adds the #2019 issue spec describing the interim formatter approach.
docs/issues/open/2003-overhaul-guardrails-and-automation/EPIC.md Notes the approved interim formatter exception in EPIC scope.
.github/skills/dev/git-workflow/run-pre-commit-checks/SKILL.md Documents formatter step in the pre-commit workflow.
.github/skills/dev/git-workflow/run-linters/SKILL.md Updates cspell guidance to reference the formatter.
.github/skills/dev/git-workflow/run-linters/references/linters.md Adds formatter usage to linter reference docs.
.github/skills/dev/git-workflow/commit-changes/SKILL.md Updates commit checklist to reference formatter + restaging behavior.

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

Copilot AI review requested due to automatic review settings July 22, 2026 09:06
josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Jul 22, 2026

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

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

Copilot AI review requested due to automatic review settings July 22, 2026 09:11

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

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

contrib/dev-tools/git/format-project-words.sh:16

  • This script does not enable set -e and it also does not check failures from the cd ... && pwd command substitutions or mktemp. If any of these fail (e.g. mktemp can fail due to permissions/disk/full temp dir), the script can continue with empty/incorrect paths and produce misleading follow-on errors instead of cleanly exiting with the intended code 2 and a clear message.
set -uo pipefail

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
PROJECT_ROOT=$(cd -- "${SCRIPT_DIR}/../../.." && pwd)
DICTIONARY_PATH="${PROJECT_ROOT}/project-words.txt"

if [[ ! -f "${DICTIONARY_PATH}" ]]; then
    printf 'Error: project dictionary not found: %s\n' "${DICTIONARY_PATH}" >&2
    exit 2
fi

if ! temporary_dictionary=$(mktemp "${DICTIONARY_PATH}.XXXXXX"); then
    printf 'Error: failed to create a temporary project dictionary: %s\n' "${DICTIONARY_PATH}" >&2

Copilot AI review requested due to automatic review settings July 22, 2026 09:15

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings July 22, 2026 09:18

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

docs/issues/open/2019-automatically-format-project-dictionary.md:10

  • This issue spec is duplicated: the repository’s docs/issues/open/ conventions require the folder layout {ISSUE_NUMBER}-{desc}/ISSUE.md (see docs/issues/open/AGENTS.md), but this PR adds both that folder spec and this standalone docs/issues/open/2019-automatically-format-project-dictionary.md. Keeping both will create ambiguous spec paths and split future updates.

Please remove this standalone spec and keep docs/issues/open/2019-automatically-format-project-dictionary/ISSUE.md as the single source of truth (and adjust any references if needed).

---
doc-type: issue
issue-type: enhancement
status: open
priority: p2
github-issue: 2019
spec-path: docs/issues/open/2019-automatically-format-project-dictionary.md
branch: "2019-automatically-format-project-dictionary"
related-pr: null
last-updated-utc: 2026-07-22 00:00

Copilot AI review requested due to automatic review settings July 22, 2026 09:23

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

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings July 22, 2026 09:30

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

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings July 22, 2026 09:46

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

Copilot reviewed 11 out of 11 changed files in this pull request and generated 7 comments.

josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Jul 22, 2026

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

Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

docs/pr-reviews/pr-2020-copilot-suggestions.md:28

  • The workflow section says to set Thread State to resolved, but the legend/table use RESOLVED. This makes the documented column values inconsistent and ambiguous.
4. Set `Thread State` to `resolved` once resolved in PR.

contrib/dev-tools/git/hooks/pre-commit.sh:380

  • This failure path computes an exit_code based on the failing step, but text mode always exits with status 1. Exiting with the recorded exit_code would preserve infrastructure failures (e.g., 2) consistently with JSON mode.
if [[ "${failed_step_name}" == "Formatting project dictionary" && "${failed_step_exit_code}" -eq 1 ]]; then
    echo "The formatter changed project-words.txt. Stage 'project-words.txt' and retry the commit."
fi
echo "Fix the errors above before committing."
echo "=========================================="

Copilot AI review requested due to automatic review settings July 22, 2026 11:43
@josecelano
josecelano force-pushed the 2019-automatically-format-project-dictionary branch from 44b36cc to 35cc02a Compare July 22, 2026 11:43
@josecelano

Copy link
Copy Markdown
Member Author

ACK 35cc02a

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

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

docs/pr-reviews/pr-2020-copilot-suggestions.md:28

  • The workflow step uses resolved (lowercase), but the column legend and table use the uppercase enum value RESOLVED. This inconsistency makes it harder to follow the documented process and can lead to mismatched entries.
4. Set `Thread State` to `resolved` once resolved in PR.

@josecelano

Copy link
Copy Markdown
Member Author

ACK 35cc02a

@josecelano
josecelano merged commit 7ed9d02 into torrust:develop Jul 22, 2026
20 of 21 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.

Post-merge review of the change from #2020 as it stands at e170b9620 on the default branch.

What still runs: the dictionary formatter is the first check of the pre-commit hook. contrib/dev-tools/checks/format-project-words.sh (moved from contrib/dev-tools/git/ by e9e1d24, body unchanged apart from header comments) is invoked from contrib/dev-tools/git/hooks/pre-commit.sh:52, and the hook's exit-code propagation and restaging message (contrib/dev-tools/git/hooks/pre-commit.sh:373-385 and :406-408) are as merged. The integration tests now live at contrib/dev-tools/checks/tests/test-format-project-words.sh.

[Minor] The formatter checks the working-tree dictionary, not the staged one, so the hook does not stop an unsorted dictionary from being committed. contrib/dev-tools/checks/format-project-words.sh:34 sorts and :39 compares the working-tree file, while the neighbouring checks at contrib/dev-tools/git/hooks/pre-commit.sh:53-55 read staged content. Sequence: stage an unsorted project-words.txt and run git commit; the hook rewrites the working copy and aborts as intended; run git commit again without git add; the formatter now finds the working copy formatted and exits 0, the remaining checks pass, and the commit records the unsorted staged blob. The closed spec presents the abort as "preventing a stale index from being committed" (docs/issues/closed/2019-automatically-format-project-dictionary/ISSUE.md:60, see also :149), which holds only when the contributor follows the message. No workflow checks the order, so the unsorted file reaches the branch, and the next contributor's commit is aborted for a dictionary change that is not theirs. Conversely, unstaged dictionary edits that are not part of a commit abort it, and following the message stages them into it. Smallest fix: when run inside a work tree, also exit 1 with the restaging message if git show :project-words.txt | LC_ALL=C sort -c -u fails, and run LC_ALL=C sort -c -u project-words.txt in an existing CI lint job.

[Minor] The integration suite fails at this tip on its last case, so the manual run that the script headers prescribe (contrib/dev-tools/checks/format-project-words.sh:6-8, contrib/dev-tools/checks/tests/test-format-project-words.sh:6-8) reports a failure unrelated to any formatter change. The fixture copies only the formatter and the hook (contrib/dev-tools/checks/tests/test-format-project-words.sh:26-27). Since e9e1d24, however, the hook also runs ./contrib/dev-tools/checks/lint-containerfile.sh (contrib/dev-tools/git/hooks/pre-commit.sh:63), which the fixture lacks. That command exits 127 and the hook fails. The subshell at contrib/dev-tools/checks/tests/test-format-project-words.sh:196-202 then returns non-zero under set -e, and the script exits 1 with nothing printed to the terminal while its EXIT trap deletes the hook output. With that script present, contrib/dev-tools/checks/tests/test-format-project-words.sh:205 would still fail: it expects 4 stub invocations, and the hook now makes 11 (contrib/dev-tools/git/hooks/pre-commit.sh:53-62 and :64). Smallest fix: create an executable no-op stub for contrib/dev-tools/checks/lint-containerfile.sh in create_fixture, and replace the exact count with an assertion that does not track the hook's list, for example [[ -s "${fixture_root}/commands.log" ]] beside the existing SUCCESS check. Running the suite from an existing CI job would keep it from drifting again.

[Minor] Three skill pages still give the pre-move formatter path ./contrib/dev-tools/git/format-project-words.sh. That path was removed by e9e1d24, so the manual command they prescribe fails with "No such file or directory": .claude/skills/commit-changes/SKILL.md:176, .claude/skills/run-linters/SKILL.md:151 and .claude/skills/run-linters/references/linters.md:60. .claude/skills/run-pre-commit-checks/SKILL.md:64 and :100 already use the current path. Smallest fix: replace git/format-project-words.sh with checks/format-project-words.sh on those three lines.

[Nit] contrib/dev-tools/checks/tests/test-format-project-words.sh:154 asserts with ! grep -F -q .... A !-negated command never triggers set -e. This assertion therefore fails the case only because it is the function's last command, whose status becomes the function's return value; any assertion appended after it would turn it into a no-op. Smallest fix: if grep -F -q "The formatter changed project-words.txt." "${fixture_root}/hook-output.txt"; then return 1; fi.

Checked and found sound: the same-directory temporary file with cp -p and mv (atomic replacement, file mode kept, EXIT-trap cleanup); the determinism of LC_ALL=C sort -u and its preservation of case variants; and the formatter's 0/1/2 exit contract. The hook prints the restaging message only when the formatter itself exits 1, so a missing script (126/127) or an infrastructure failure (2) is not mislabelled. The JSON output carries the failing check's exit code and the formatter's own restaging line. The formatter runs before, and short-circuits, every other check. The checked-in project-words.txt is in LC_ALL=C sort -u order both at the pull request head and at e170b9620.

This branch was previously deployed

1 inactive deployment
coverage — 35cc02a3 Deployed Jul 22, 2026 by josecelano via Generate Coverage Report #1662
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.

Automatically format the project dictionary

3 participants