Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changes/source-control/refactor-9725-convention-yaml-5e1c7a94.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
bump: minor
---

### Changed

- The commit and PR convention keys (`subject_pattern`, `type_list`, `pr_title_pattern`, `trailer_policy`, `pr_body_attribution`, `pr_body_required_sections`, `branch_issue_pattern`) now take their team value from `docs/conventions/source-control.yaml`, validated by `schemas/source-control.schema.json`. `.claude/source-control.md` is read only for a key the YAML lacks, for one release, and a YAML the shared reader rejects stops the lookup instead of falling back. `/source-control:setup apply layer=team` writes the YAML and moves the convention keys out of the legacy file.
- `parse-branch-issue.sh` reads `branch_issue_pattern` from the team YAML before the legacy markdown, and `--explain` prints the layer that supplied the pattern on stderr. Like `/source-control:commit`, it reads either team file only when git tracks it and notes an untracked one it skipped. The deprecation note for the `branch_issue_pattern` userConfig now points at the YAML.

### Added

- `lib/convention-key.sh`, the team-layer reader for the convention keys: `layer ROOT KEY` prints `yaml`, `legacy-md` or `none`, and `value` and `list` print the YAML value.
16 changes: 16 additions & 0 deletions docs/conventions/source-control.yaml
Comment thread
kyle-sexton marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# yaml-language-server: $schema=../../plugins/source-control/schemas/source-control.schema.json
# Team layer for the source-control plugin's YAML settings.
# Resolution: plugins/source-control/reference/config-resolution.md.
#
# pr_body_required_sections is the section list this repository's pull-request contract asks for,
# each non-empty, alongside a native closing keyword. The contract runs in the `ci-status` job of
# .github/workflows/pr-require-checks.yml, in the `check-contract` composite step pinned to
# melodic-software/ci-workflows/.github/actions/pr-require-checks/check-contract, which exempts
# `dependabot[bot]` from the linkage check and no other author. The linkage half is advisory: a body
# that misses a closing keyword or a required section gets a warning, an upserted comment and the
# `needs-issue-linkage` label, and `ci-status` still passes on that account. The composite is the
# authority; this key restates it so /source-control:pull-request drafts a body that conforms. Read
# the composite at the SHA pr-require-checks.yml pins, not at its default branch, since that pin is
# what actually runs. Re-read it before changing either.
pr_open_state: ready
pr_body_required_sections:
- Summary
- Fix
- Verification
- Related
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
type: file_exists
path: .claude/source-control.md
path: docs/conventions/source-control.yaml
arm: both
---
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ tags: [setup, source-control]
runs: 3
max_turns: 30
allowed_tools: [Read, Glob, Grep, Skill, Bash, Write, Edit]
expected_outcome: ".claude/source-control.md exists with the ticket-prefixed convention; .claude/source-control.local.md and docs/conventions/source-control/commit-convention.yml were never written"
expected_outcome: "docs/conventions/source-control.yaml exists with the ticket-prefixed convention; .claude/source-control.local.md and docs/conventions/source-control/commit-convention.yml were never written"
---

/source-control:setup apply layer=team

I am answering the interview up front, so do not ask me anything. Our team convention is ticket-prefixed subjects, `^[A-Z]+-\d+: .+`, and I confirm you write that to the team file `.claude/source-control.md`. I decline two other changes: do not create a personal overlay at `.claude/source-control.local.md`, and do not create the neutral convention file `docs/conventions/source-control/commit-convention.yml`. Write nothing else apart from the team file.
I am answering the interview up front, so do not ask me anything. Our team convention is ticket-prefixed subjects, `^[A-Z]+-\d+: .+`, and I confirm you write that to the team file `docs/conventions/source-control.yaml`. I decline two other changes: do not create a personal overlay at `.claude/source-control.local.md`, and do not create the neutral convention file `docs/conventions/source-control/commit-convention.yml`. Write nothing else apart from the team file.
58 changes: 57 additions & 1 deletion plugins/source-control/lib/config-root.test.sh
Comment thread
kyle-sexton marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
# Tests for config-root.sh: root classification (repo | non-repo | home), the
# physical path-equality helper, and the CLI entry. Every path lives under a
# physical path-equality helper, and the CLI entry; and for convention-key.sh,
# the team-layer reader for the commit and PR convention keys. Every path lives under a
# fixture tree this suite creates; the real home directory is never read.

set -uo pipefail
Expand Down Expand Up @@ -158,6 +159,61 @@ assert_contains "CLI: an unknown subcommand prints usage" "$out" "usage: bash co
assert_silent "sourcing the library prints nothing and runs no subcommand" \
"$(bash -c 'source "$1"' _ "$LIB" 2>&1)"

# --- convention key layers (convention-key.sh) -------------------------------
# The team layer of the commit and PR convention keys: docs/conventions/source-control.yaml
# wins, and the legacy .claude/source-control.md is read only for a key the YAML lacks.
KEYLIB="$SCRIPT_DIR/convention-key.sh"
# shellcheck source=convention-key.sh
source "$KEYLIB"
KROOT="$TEST_TMPDIR/key-repo"
mkdir -p "$KROOT/docs/conventions" "$KROOT/.claude"
git init -q "$KROOT"
YAML_FILE="$KROOT/docs/conventions/source-control.yaml"
MD_FILE="$KROOT/.claude/source-control.md"
printf '%s\n' 'pr_open_state: ready' 'pr_body_required_sections:' ' - Summary' ' - Fix' \
'branch_issue_pattern: "^[^/]+/([0-9]+)-#x"' >"$YAML_FILE"
printf '# Source control\n\n## pr_body_required_sections\n\n- Why\n\n## subject_pattern\n\nConventional Commits\n' >"$MD_FILE"

assert_eq "the YAML value wins over the legacy file: layer is yaml" yaml \
"$(convention_key_layer "$KROOT" pr_body_required_sections)"
assert_eq "the YAML list is the one the YAML holds, not the legacy list" $'Summary\nFix' \
"$(convention_key_yaml_list "$KROOT" pr_body_required_sections)"
assert_eq "a quoted YAML scalar keeps a # inside the value" '^[^/]+/([0-9]+)-#x' \
"$(convention_key_yaml_value "$KROOT" branch_issue_pattern)"
SLASH_ROOT="$TEST_TMPDIR/slash-repo"
mkdir -p "$SLASH_ROOT/docs/conventions"
printf '%s\n' "branch_issue_pattern: '^[a-z]+/([0-9]+)/'" >"$SLASH_ROOT/docs/conventions/source-control.yaml"
assert_eq "a trailing slash in a YAML regex is kept, not normalized away" '^[a-z]+/([0-9]+)/' \
"$(convention_key_yaml_value "$SLASH_ROOT" branch_issue_pattern)"
assert_eq "the legacy file is read when the YAML lacks the key: layer is legacy-md" legacy-md \
"$(convention_key_layer "$KROOT" subject_pattern)"
assert_eq "a key the YAML lacks yields no YAML value" "" "$(convention_key_yaml_value "$KROOT" subject_pattern)"
assert_eq "a key in neither file resolves to none" none "$(convention_key_layer "$KROOT" trailer_policy)"
assert_eq "a legacy heading with a closing # sequence still counts" legacy-md \
"$(printf '## trailer_policy ##\n\nnone\n' >"$MD_FILE.tmp" && mv "$MD_FILE.tmp" "$MD_FILE" && convention_key_layer "$KROOT" trailer_policy)"

printf '%s\n' 'pr_open_state: ready' >"$YAML_FILE"
assert_eq "a YAML and a legacy file that both lack the key resolve to none" none \
"$(convention_key_layer "$KROOT" pr_body_required_sections)"
rm -f "$MD_FILE"
assert_eq "no legacy file and no YAML key resolves to none" none "$(convention_key_layer "$KROOT" subject_pattern)"
rm -f "$YAML_FILE"
assert_eq "no YAML file resolves to none" none "$(convention_key_layer "$KROOT" subject_pattern)"

printf '%s\n' 'subject_pattern: a' 'subject_pattern: b' >"$YAML_FILE"
convention_key_layer "$KROOT" subject_pattern >/dev/null
assert_exit "a YAML the shared reader rejects stops the layer lookup with exit 3" 3 "$?"
printf '## subject_pattern\n\nConventional Commits\n' >"$MD_FILE"
assert_eq "a rejected YAML never falls through to the legacy file" "" \
"$(convention_key_layer "$KROOT" subject_pattern 2>/dev/null)"

printf '%s\n' 'trailer_policy: none' >"$YAML_FILE"
assert_eq "CLI layer names the YAML" yaml "$(bash "$KEYLIB" layer "$KROOT" trailer_policy)"
assert_eq "CLI value prints the YAML scalar" none "$(bash "$KEYLIB" value "$KROOT" trailer_policy)"
out="$(bash "$KEYLIB" bogus 2>&1)"
assert_exit "CLI: an unknown subcommand exits 2" 2 "$?"
assert_contains "CLI: an unknown subcommand prints usage" "$out" "usage: bash convention-key.sh"

printf '\nResults: %d passed, %d failed, %d skipped\n' "$((CASE_NUM - FAILED))" "$FAILED" "$SKIP_CASES"
[[ $FAILED -eq 0 ]] || exit 1
exit 0
113 changes: 113 additions & 0 deletions plugins/source-control/lib/convention-key.sh
Comment thread
kyle-sexton marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env bash
# shellcheck shell=bash
# Team-layer reader for the commit and PR convention keys of the source-control
# plugin: subject_pattern, type_list, pr_title_pattern, trailer_policy,
# pr_body_attribution, pr_body_required_sections and branch_issue_pattern.
#
# The team value lives in docs/conventions/source-control.yaml (ADR 0064),
# validated by schemas/source-control.schema.json. For one release a key the YAML
# lacks is still read from the legacy `## <key>` section of
# .claude/source-control.md; the YAML wins whenever it carries the key. The YAML
# is parsed by yaml-subset.awk, the shared parser, and the value is printed
# as written: parse-concern-value.sh is not used because it strips a trailing
# slash, which belongs to a directory value and would change a regex.
#
# Sourced by scripts; run directly for the model-run skills:
#
# bash convention-key.sh layer ROOT KEY print the team layer that supplies KEY:
# yaml | legacy-md | none
# bash convention-key.sh value ROOT KEY print KEY's scalar from the YAML
# bash convention-key.sh list ROOT KEY print KEY's list items from the YAML
#
# `layer` names the file in the form callers report: `yaml` is
# docs/conventions/source-control.yaml, `legacy-md` is .claude/source-control.md
# (read only because the YAML lacks the key), `none` means neither carries it.
# ROOT is the repo root from config-root.sh; the caller has already classified it
# `repo`. `value` and `list` print nothing for a key the YAML lacks and exit 3
# for a YAML the parser rejects, so a caller can stop instead of reading the
# legacy file past a broken team file.

CONVENTION_KEY_YAML_REL="docs/conventions/source-control.yaml"
CONVENTION_KEY_MD_REL=".claude/source-control.md"
CONVENTION_KEY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# True when git tracks the repo-relative file REL under ROOT. An untracked or
# gitignored team file is not a config layer (skills/commit/SKILL.md).
convention_key_tracked() {
git -C "$1" ls-files --error-unmatch -- "$2" >/dev/null 2>&1
}

# Print the parser's key<TAB>value records for the team YAML under ROOT, nothing
# when the file is absent. Returns 3 when the parser rejects the file; a key
# repeated in one mapping is a rejection too.
convention_key_records() {
local file="$1/$CONVENTION_KEY_YAML_REL" text
[[ -f "$file" ]] || return 0
text="$(<"$file")"
LC_ALL=C awk -v STRICT=1 -f "$CONVENTION_KEY_DIR/yaml-subset.awk" <<<"$text" 2>/dev/null || return 3
}

# Print the value of KEY from the team YAML under ROOT, or nothing.
convention_key_yaml_value() {
local records
records="$(convention_key_records "$1")" || return 3
PCV_KEY="$2" LC_ALL=C awk -F '\t' '$1 == ENVIRON["PCV_KEY"] && !seen++ { print substr($0, length($1) + 2) }' <<<"$records"
}

# Print the scalar items of the sequence KEY in the team YAML, one per line.
convention_key_yaml_list() {
local records
records="$(convention_key_records "$1")" || return 3
PCV_KEY="$2" LC_ALL=C awk -F '\t' 'index($1, ENVIRON["PCV_KEY"] ".") == 1 && substr($1, length(ENVIRON["PCV_KEY"]) + 2) ~ /^[0-9]+$/ { print substr($0, length($1) + 2) }' <<<"$records"
}

# True when the team YAML carries KEY, as a scalar or as a non-empty list.
convention_key_yaml_has() {
local root="$1" key="$2" scalar items
scalar="$(convention_key_yaml_value "$root" "$key")" || return 3
[[ -z "$scalar" ]] || return 0
items="$(convention_key_yaml_list "$root" "$key")" || return 3
[[ -n "$items" ]]
}

# True when the legacy markdown file under ROOT has a `## KEY` heading outside a
# code fence, as the resolvers' markdown readers see it.
convention_key_md_has() {
local root="$1" key="$2" file="$1/$CONVENTION_KEY_MD_REL"
[[ -f "$file" ]] || return 1
PCV_KEY="$key" LC_ALL=C awk '
NR == 1 { sub(/^\357\273\277/, "") }
{ sub(/\r$/, ""); sub(/[ \t]+$/, "") }
/^(```|~~~)/ { fenced = !fenced; next }
!fenced && $0 ~ "^##[ \t]+" ENVIRON["PCV_KEY"] "([ \t]+#+)?$" { found = 1; exit }
END { exit !found }
' "$file"
}

# Print yaml | legacy-md | none. Returns 3 for an unparsable YAML.
convention_key_layer() {
local root="$1" key="$2" rc
convention_key_yaml_has "$root" "$key"
rc=$?
if ((rc == 0)); then
echo yaml
elif ((rc == 3)); then
return 3
elif convention_key_md_has "$root" "$key"; then
echo legacy-md
else
echo none
fi
}

if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
case "${1:-}" in
layer) convention_key_layer "${2:?root}" "${3:?key}" ;;
value) convention_key_yaml_value "${2:?root}" "${3:?key}" ;;
list) convention_key_yaml_list "${2:?root}" "${3:?key}" ;;
*)
echo "usage: bash convention-key.sh layer|value|list ROOT KEY" >&2
exit 2
;;
esac
fi
43 changes: 39 additions & 4 deletions plugins/source-control/reference/config-resolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,39 @@ Implements the tracked-rich-config extensibility contract in

## The config surface

Markdown, one `## <key>` H2 per key, the value as the section body. Seven babysit-prs
**Team-layer home of the convention keys.** The seven commit and PR convention keys below
(`subject_pattern`, `type_list`, `pr_title_pattern`, `trailer_policy`, `pr_body_attribution`,
`pr_body_required_sections`, `branch_issue_pattern`) take their team value from
`docs/conventions/source-control.yaml`, validated by
[`schemas/source-control.schema.json`](../schemas/source-control.schema.json)
([ADR 0064](https://raw.githubusercontent.com/melodic-software/claude-code-plugins/main/docs/adr/0064-home-plugin-customization-in-docs-conventions-yaml.md)).
The compatibility read, for one release: when the YAML lacks a key, the team value is the `## <key>`
H2 of `.claude/source-control.md` described below, so a repository that has not migrated keeps
resolving. The YAML wins whenever it carries the key, and a YAML the shared reader rejects stops the
lookup for that key instead of falling back to the markdown. The personal overlay and the user-global
file stay markdown and keep their place in
[The three layers](#the-three-layers). Every resolver reports the layer that supplied a value:
`docs/conventions/source-control.yaml`, `.claude/source-control.md (legacy)`, the overlay, the
user-global file, `userConfig`, or the default. `lib/convention-key.sh` is the reader
(`layer ROOT KEY` prints `yaml`, `legacy-md` or `none`; `value` and `list` print the YAML value), and
`parse-branch-issue.sh --explain` prints the layer for `branch_issue_pattern`.

```yaml
subject_pattern: Conventional Commits
pr_title_pattern: "Same as `subject_pattern`."
trailer_policy: none
pr_body_required_sections:
- Summary
- Test plan
branch_issue_pattern: '^[^/]+/([0-9]+)-'
```

The sections below describe each key's value grammar; read "H2" there as "the key in the YAML, or
its `## <key>` H2 in the legacy file". A list value is a YAML sequence in the YAML and a bullet list
in the H2.

The overlay, the user-global file and the legacy team file are Markdown: one `## <key>` H2 per key,
the value as the section body. Seven babysit-prs
repository-policy keys (`babysit_merge_method`, `babysit_merge_block_labels`,
`babysit_extra_dependency_manager_logins`, `babysit_approval_downgrade_logins`,
`babysit_skip_downgrade_logins`, `babysit_review_gate_context`,
Expand Down Expand Up @@ -145,7 +177,8 @@ Absent sections are absent, never empty.

## YAML keys (`docs/conventions/source-control.yaml`)

A key added to this plugin after the YAML config home
The seven convention keys in the previous section are read from this file first (the compatibility
read of the legacy file applies to them only). A key added to this plugin after the YAML config home
([ADR 0064](https://raw.githubusercontent.com/melodic-software/claude-code-plugins/main/docs/adr/0064-home-plugin-customization-in-docs-conventions-yaml.md))
has no H2 in
`.claude/source-control.md`. Its team value lives in `docs/conventions/source-control.yaml` at the
Expand Down Expand Up @@ -541,8 +574,10 @@ Layers, in resolution order, where a later layer refines an earlier one:

1. **`~/.claude/source-control.md`**: user-global. The operator's own preference, following them
across repos and machines. Their home directory, not consumer repository data.
2. **`${REPO_ROOT}/.claude/source-control.md`**: team, tracked. The shared convention; the layer
`/source-control:setup apply` writes by default.
2. **`${REPO_ROOT}/docs/conventions/source-control.yaml`**: team, tracked. The shared convention
for the seven convention keys; the layer `/source-control:setup apply` writes by default. For one
release a key it lacks is read from the legacy `${REPO_ROOT}/.claude/source-control.md` (the
compatibility read), which also still carries the `babysit_*` keys.
3. **`${REPO_ROOT}/.claude/source-control.local.md`**: personal overlay, gitignored. A per-machine
or per-operator deviation from team policy, never committed.

Expand Down
Loading
Loading