Skip to content

feat(hooks): one branch-name rule via vig-utils validate-branch-name - #1806

Merged
c-vigo merged 7 commits into
devfrom
feature/1760-validate-branch-name
Oct 1, 2026
Merged

c-vigo merged 7 commits into
devfrom
feature/1760-validate-branch-name

Conversation

@c-vigo

@c-vigo c-vigo commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

The branch-name rule now has one implementation: validate-branch-name in vig-utils. Before this, it lived in four hand-kept renderings: the no-commit-to-branch negative-lookahead regex in both committed hook configs plus the flake surface, and the ALLOWED bash alternations in the scaffold and devkit ci.yml. Those renderings disagreed on shape: CI accepted release/X.Y.Z and the local hook did not. Now the local hook, the scaffolded CI step and devkit's own CI step all call the same validator, so the accepted shapes cannot drift.

Type of Change

  • feat -- New feature

Changes Made

  • Validator (packages/vig-utils/src/vig_utils/validate_branch_name.py, plus a [project.scripts] entry). It takes only arguments, the same contract as validate-commit-range, and never reads .vig-os:
    • --branch: defaults to git symbolic-ref --short HEAD. A detached HEAD, or running outside a repo, passes with a note on stderr. This is what no-commit-to-branch did.
    • --types and --issueless-types: comma-separated and whitespace-tolerant. An empty value falls back to the stock set.
    • --workflow gitflow|trunk: under gitflow, dev is also allowed.
    • Accepted shapes are exactly the old union: main, dev (gitflow only), <issueless>/<slug>, <type>/<n>-<slug>, worktree/<n>, renovate/.+ and release/X.Y.Z. One further tightening: the old regex's bare $ let a value with a trailing or embedded newline through (feature/1-foo\n); the validator's fullmatch rejects it. Unreachable through real git refs, which forbid control characters.
    • Trunk semantics are unchanged: the old trunk render dropped (?!dev$), which blocked dev.
    • The error message keeps the CI wording and its pointer to the docs and the branch-naming skill.
  • nix/hooks.nix:
    • branchNamePatternFor is replaced by validateBranchNameArgs.
    • The new validate-branch-name hook has two renders, the same way validate-commit-msg does. The scaffold YAML uses uv run validate-branch-name. The consumer surface uses the vig-utils store path.
    • Hook settings: always_run, pass_filenames: false, stages: [pre-commit], and no check fragment.
    • The args are single --flag=value lines, so each scaffold render owns exactly one line. This keeps them clear of validate-commit-msg's "--types", "…" anchor.
  • Committed YAMLs: in both the repo and the scaffold config, the no-commit-to-branch entry is gone. It is replaced by a repo: local block right after the pre-commit-hooks block. The drift gate stays green.
  • CI:
    • The scaffold step forwards resolve-toolchain's branch-types and issueless-branch-types.
    • Devkit's step passes no type lists. The validator's defaults are the stock set, and a new test pins them to nix/hooks.nix defaultBranchTypes, so the list is not restated.
    • HEAD_REF still reaches the step only via env.
    • The last stderr line from the validator becomes the ::error:: annotation.
    • Neither step passes --workflow, so CI keeps accepting dev in both models, as it does today.
  • init-workspace.sh:
    • render_branch_types, render_issueless_branch_types and render_branch_guard_model now rewrite the hook's args through render_branch_guard_arg. That sed is scoped to the validate-branch-name entry.
    • render_branch_guard_model no longer edits comments.

Migration of a preserved config: spike result

  • How the fold works. fold_retired_hook_blocks compares lines verbatim (verbatim_block_range) and keys on one row per historical shape. Verbatim matching alone cannot recognise the rendered variants of the old block, because the pattern line carries the types, the issue-less set and the gitflow/trunk model.
  • Normalisation, applied to both sides. retired_block_normalize runs on the historical needle and on a copy of the consumer file before they are compared. It erases exactly the slots the pre-The branch-name rule exists only inline in ci.yml, so every consumer's local gate must duplicate it (and drifts) #1760 renders wrote:
    • the two alternations, within the charset the knob guards allow;
    • the (?!dev$) clause;
    • the two comment phrases the trunk render rewrote.
  • Why this is safe. Normalisation never adds or removes lines, so a match range in the copy is the same range in the real file. Everything outside those slots must still match byte for byte.
  • Rows instead of variants. This needs three rows, one per shape the template shipped: 0.3.0, 1.8.0 (which added the renovate clause) and the [FEATURE] Issue-less branch form for every Refs-optional commit type, not just chore #1767 comment that only reached dev. The alternative was one row per types × issue-less × workflow combination, which is unbounded.
  • Accepted trade-off. A hand-edited alternation folds the same way a knob render does. Those were already overwritten by the unconditional renders on every upgrade ([BUG] Clearing a scaffold knob leaves the previous render in the preserved .pre-commit-config.yaml #1640), so no customisation is lost.
  • Placement. The old entry sits inside the pre-commit-hooks hooks: list, so the new repo: local block cannot be spliced into its place. A new optional 4th row field, template, deletes the retired entry and inserts the replacement at its template position. It reuses the insert path's anchor walk, which I extracted into hook_insert_point for both callers. If the file carries no anchor, the fold declines rather than leave the repo with no guard at all.
  • Customised blocks. A customised block (for example tessera's issue-less pattern) does not match. It stays in place and is reported by a new known-bad row as preserved-hook-drift: no-commit-to-branch-pre-1760.
  • Flake-hooks consumers. Their config is a store symlink and generated JSON, so neither the fold nor the scan touches it. Their hook comes straight from hooks.nix.

Decisions where the plan was silent

  • CI steps pass no --workflow. This keeps today's CI behaviour of accepting dev in both models.
  • Customised no-commit-to-branch blocks are not touched, and the new hook is not inserted next to them. The drift warning is the remedy. One consequence: on a gitflow↔trunk switch, render_branch_guard_model no longer edits such a custom regex.
  • The name: is kept as branch-name (enforce <type>/<issue>-<summary>) for continuity in hook output.
  • assets/workspace/flake.nix keeps two now-stale comments. It is scaffold-once and preserved, so editing it would put a template diff in front of every consumer on upgrade.

Known inherited gap (not fixed here)

The scaffold entry uv run validate-branch-name needs uv on non-Python consumers, exactly as validate-commit-msg does today. The new entry point reaches consumers the same way that one does: [project.scripts] → nix/vig-utils.nix (whole package) → devtools.nix puts it on PATH in the image and dev-shell, and the flake consumer surface uses the store path.

Changelog Entry

Added

  • validate-branch-name: one branch-name rule for every enforcement point (#1760)

Changed

  • The local branch guard is the validate-branch-name hook (#1760). This includes the fold and drift reporting for preserved configs.

Testing

TDD order: test(vig-utils) (red) → feat(vig-utils) → test: for the hook, CI and fold (red, 21 failing) → test: for the defaults pin → feat(hooks) → docs.

  • packages/vig-utils/tests/test_validate_branch_name.py: 57 tests covering the rule, the defaults, the CLI, git symbolic-ref and detached HEAD.
  • tests/test_scaffold_preserved_hooks.py: the fold is checked for all 3 shapes × 4 renders (stock, trunk, custom types, all knobs + trunk). Further tests cover:
    • the fold landing at the template position and touching nothing else;
    • a customised block giving drift and no fold;
    • idempotence;
    • preview.
  • tests/test_flake_hooks.py (86 passed): the drift gate, plus consumer-surface args and their semantics, run through the real validator.
  • tests/test_ci_runner.py: both steps call the validator, and the existing behavioural gate tests now execute it.
  • tests/bats/init-workspace.bats (run by file name only): all 312 tests pass.
  • Full pytest suite excluding test_image and test_integration: 2447 passed. just precommit is green.
  • test_image and test_integration were left to CI.

Refs: #1760

c-vigo added 7 commits October 1, 2026 15:59
Replace the no-commit-to-branch regex with a local validate-branch-name
hook in both committed configs and the flake consumer surface, call the
same validator from both CI branch-name steps, render the knobs as hook
args, and fold every rendered shape of the retired guard on upgrade.

Refs: #1760
@c-vigo
c-vigo enabled auto-merge October 1, 2026 16:57
@c-vigo
c-vigo merged commit eed1266 into dev Oct 1, 2026
18 checks passed
@c-vigo
c-vigo deleted the feature/1760-validate-branch-name branch October 1, 2026 17:04
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.

1 participant