@rvdbreemen/adr-kit-opencode 0.52.0 → 0.53.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +29 -29
- package/README.md +865 -850
- package/bin/adr +0 -0
- package/bin/adr-audit +0 -0
- package/bin/adr-context +0 -0
- package/bin/adr-discover +0 -0
- package/bin/adr-doctor +0 -0
- package/bin/adr-generate-scripts +0 -0
- package/bin/adr-grill-signal +0 -0
- package/bin/adr-guardian +0 -0
- package/bin/adr-index +0 -0
- package/bin/adr-judge +0 -0
- package/bin/adr-judge-precommit +0 -0
- package/bin/adr-lint +0 -0
- package/bin/adr-mcp +96 -4
- package/bin/adr-migrate +0 -0
- package/bin/adr-quality +0 -0
- package/bin/adr-readiness +0 -0
- package/bin/adr-readiness-ci +0 -0
- package/bin/adr-related +0 -0
- package/bin/adr-renumber +254 -254
- package/bin/adr-retire +0 -0
- package/bin/adr-settings +0 -0
- package/bin/adr-status +0 -0
- package/bin/adr-suggest +0 -0
- package/bin/adr-watch +0 -0
- package/bin/adr_doctor_probes.py +2 -0
- package/bin/bump-version +0 -0
- package/clients/workflows.json +229 -229
- package/hooks/__init__.py +1 -1
- package/hooks/adapters/__init__.py +13 -13
- package/hooks/adapters/claude.py +27 -27
- package/hooks/adapters/codex.py +26 -26
- package/hooks/adapters/copilot.py +11 -11
- package/hooks/adr-hook.py +128 -128
- package/hooks/adr_hook_core.py +843 -843
- package/hooks/adr_pr_guard.py +367 -367
- package/instructions/ADR-guide.md +101 -48
- package/instructions/adr.coding.md +82 -82
- package/instructions/adr.review.md +182 -182
- package/opencode/plugin.ts +1 -4
- package/package.json +5 -2
- package/schemas/adr-context-probes.schema.json +56 -56
- package/schemas/adr-enforcement.schema.json +57 -57
- package/schemas/adr-frontmatter.schema.json +144 -144
- package/schemas/adr-index.schema.json +276 -276
- package/schemas/adr-kit-config.schema.json +371 -371
- package/schemas/adr-readiness.schema.json +41 -41
- package/schemas/client-capabilities.schema.json +503 -503
- package/schemas/client-certification.schema.json +101 -101
- package/schemas/doctor-output.schema.json +92 -92
- package/skills/adr/SKILL.md +780 -780
- package/skills/audit/SKILL.md +103 -103
- package/skills/context/SKILL.md +77 -77
- package/skills/grill/SKILL.md +84 -84
- package/skills/grill/agents/openai.yaml +4 -4
- package/skills/guardian/SKILL.md +316 -316
- package/skills/init/SKILL.md +499 -499
- package/skills/install-hooks/SKILL.md +154 -154
- package/skills/judge/SKILL.md +168 -168
- package/skills/lint/SKILL.md +321 -321
- package/skills/migrate/SKILL.md +313 -313
- package/skills/related/SKILL.md +62 -62
- package/skills/retire/SKILL.md +41 -41
- package/skills/review/SKILL.md +167 -167
- package/skills/settings/SKILL.md +73 -73
- package/skills/setup/SKILL.md +128 -128
- package/skills/supersede/SKILL.md +136 -136
- package/skills/upgrade/SKILL.md +273 -273
- package/templates/adr-kit-guide.md +354 -354
- package/templates/adr-template.canonical.md +115 -115
- package/templates/adr-template.madr.md +131 -131
- package/templates/adr-template.md +131 -131
- package/templates/adr-template.nygard.md +111 -111
- package/templates/cc-settings/guardian-hook-entry.json +9 -9
- package/templates/githooks/pre-commit +1 -1
- package/templates/github-workflows/adr-audit.yml +58 -58
- package/templates/github-workflows/adr-guardian-audit.yml +133 -133
- package/templates/github-workflows/adr-index-check.yml +30 -30
- package/templates/github-workflows/adr-judge.yml +31 -31
- package/templates/github-workflows/adr-readiness.yml +18 -18
- package/templates/validate_adr_template.py +38 -38
- package/templates/validate_adr_template.sh +74 -74
package/bin/adr-renumber
CHANGED
|
@@ -1,254 +1,254 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""adr-renumber: safely renumber one ADR and update all cross-references.
|
|
3
|
-
|
|
4
|
-
Built for the merge-collision workflow: two parallel branches both claim
|
|
5
|
-
ADR-043, CI fails on the duplicate-number check in adr-lint, and one of the
|
|
6
|
-
two files must move to a free number. This tool renames the file, rewrites
|
|
7
|
-
the heading, and updates every whole-token reference to the old id across
|
|
8
|
-
all ADRs in the directory: Related Decisions entries, "Superseded by" and
|
|
9
|
-
"Amended by" status strings, Status History mentions, and plain ADR-NNN
|
|
10
|
-
references.
|
|
11
|
-
|
|
12
|
-
Safety properties:
|
|
13
|
-
- Dry-run by default: prints the full plan (file rename plus every rewrite
|
|
14
|
-
with file:line) and changes nothing. --apply executes the plan.
|
|
15
|
-
- Whole-token matching only: renumbering ADR-043 never touches ADR-0430.
|
|
16
|
-
- Refuses (exit 2) when the target number is already taken or the source
|
|
17
|
-
does not exist.
|
|
18
|
-
- All regexes are linear: no nested or lazy quantifiers (see the
|
|
19
|
-
0.19.1/0.19.2 ReDoS history in CHANGELOG.md).
|
|
20
|
-
|
|
21
|
-
Exit codes:
|
|
22
|
-
0 success (plan printed, or --apply completed)
|
|
23
|
-
2 input error (source missing, target taken, ambiguous source, bad id)
|
|
24
|
-
|
|
25
|
-
Usage:
|
|
26
|
-
adr-renumber ADR-043 # dry-run, target = next free number
|
|
27
|
-
adr-renumber ADR-043 --to ADR-051 # dry-run with explicit target
|
|
28
|
-
adr-renumber ADR-043 --to ADR-051 --apply # execute
|
|
29
|
-
adr-renumber docs/adr/ADR-043-foo.md --to ADR-051 # disambiguate duplicates
|
|
30
|
-
adr-renumber ADR-043 --adr-dir docs/decisions
|
|
31
|
-
|
|
32
|
-
Deterministic, stdlib-only, Python 3.10+.
|
|
33
|
-
"""
|
|
34
|
-
|
|
35
|
-
from __future__ import annotations
|
|
36
|
-
|
|
37
|
-
import argparse
|
|
38
|
-
import re
|
|
39
|
-
import sys
|
|
40
|
-
from pathlib import Path
|
|
41
|
-
from typing import Dict, List, Optional, Tuple
|
|
42
|
-
|
|
43
|
-
ADR_FILENAME_RE = re.compile(r"(?i)^ADR-(\d{1,4})-")
|
|
44
|
-
ADR_ID_RE = re.compile(r"(?i)^ADR-(\d{1,4})$")
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
class RenumberError(Exception):
|
|
48
|
-
"""Raised on input errors; maps to exit code 2."""
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def parse_adr_id(raw: str) -> int:
|
|
52
|
-
"""Return the integer number of an ADR-NNN id, or raise RenumberError."""
|
|
53
|
-
m = ADR_ID_RE.match(raw.strip())
|
|
54
|
-
if not m:
|
|
55
|
-
raise RenumberError(f"{raw!r} does not match ADR-NNN (e.g. ADR-043)")
|
|
56
|
-
return int(m.group(1))
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def discover_adrs(adr_dir: Path) -> List[Path]:
|
|
60
|
-
"""Return sorted ADR markdown files in adr_dir (non-recursive)."""
|
|
61
|
-
if not adr_dir.is_dir():
|
|
62
|
-
raise RenumberError(f"ADR directory not found: {adr_dir}")
|
|
63
|
-
return sorted(
|
|
64
|
-
(p for p in adr_dir.glob("*.md") if ADR_FILENAME_RE.match(p.name)),
|
|
65
|
-
key=lambda p: p.name.lower(),
|
|
66
|
-
)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def numbers_in_use(files: List[Path]) -> Dict[int, List[Path]]:
|
|
70
|
-
"""Map ADR number -> list of files carrying that number."""
|
|
71
|
-
grouped: Dict[int, List[Path]] = {}
|
|
72
|
-
for fp in files:
|
|
73
|
-
m = ADR_FILENAME_RE.match(fp.name)
|
|
74
|
-
if m:
|
|
75
|
-
grouped.setdefault(int(m.group(1)), []).append(fp)
|
|
76
|
-
return grouped
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
def next_free_number(in_use: Dict[int, List[Path]]) -> int:
|
|
80
|
-
"""Return max(used) + 1. Gaps are not reused: a gap usually means a
|
|
81
|
-
retired or reserved number, and reusing it would resurrect stale
|
|
82
|
-
references."""
|
|
83
|
-
return (max(in_use) + 1) if in_use else 1
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
def token_pattern(num: int) -> "re.Pattern[str]":
|
|
87
|
-
"""Whole-token pattern for every common spelling of an ADR id.
|
|
88
|
-
|
|
89
|
-
Matches ADR-43, ADR-043, ADR-0043 (whichever spellings differ), but
|
|
90
|
-
never ADR-0430: a digit may not follow the number. \\b before 'ADR'
|
|
91
|
-
rejects mid-word hits. Pure alternation of literals: linear time.
|
|
92
|
-
"""
|
|
93
|
-
spellings = sorted({str(num), f"{num:03d}", f"{num:04d}"}, key=len, reverse=True)
|
|
94
|
-
alt = "|".join(spellings)
|
|
95
|
-
return re.compile(rf"\bADR-(?:{alt})(?!\d)")
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def renamed_filename(name: str, new_num: int) -> str:
|
|
99
|
-
"""Replace the leading ADR-NNN- of a filename with the new number."""
|
|
100
|
-
m = ADR_FILENAME_RE.match(name)
|
|
101
|
-
if not m:
|
|
102
|
-
raise RenumberError(f"{name!r} is not an ADR filename (ADR-NNN-...)")
|
|
103
|
-
return f"ADR-{new_num:03d}-" + name[m.end():]
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
def find_source(source_arg: str, adr_dir: Path, files: List[Path]) -> Tuple[Path, int]:
|
|
107
|
-
"""Resolve the source argument (ADR-NNN id or file path) to one file."""
|
|
108
|
-
candidate = Path(source_arg)
|
|
109
|
-
if candidate.suffix.lower() == ".md":
|
|
110
|
-
if not candidate.is_file():
|
|
111
|
-
candidate = adr_dir / candidate.name
|
|
112
|
-
if not candidate.is_file():
|
|
113
|
-
raise RenumberError(f"source file not found: {source_arg}")
|
|
114
|
-
m = ADR_FILENAME_RE.match(candidate.name)
|
|
115
|
-
if not m:
|
|
116
|
-
raise RenumberError(
|
|
117
|
-
f"{candidate.name!r} is not an ADR filename (ADR-NNN-...)"
|
|
118
|
-
)
|
|
119
|
-
return candidate, int(m.group(1))
|
|
120
|
-
|
|
121
|
-
num = parse_adr_id(source_arg)
|
|
122
|
-
matches = [
|
|
123
|
-
fp for fp in files
|
|
124
|
-
if (m := ADR_FILENAME_RE.match(fp.name)) and int(m.group(1)) == num
|
|
125
|
-
]
|
|
126
|
-
if not matches:
|
|
127
|
-
raise RenumberError(f"no ADR with number {num:03d} found in {adr_dir}")
|
|
128
|
-
if len(matches) > 1:
|
|
129
|
-
names = ", ".join(fp.name for fp in matches)
|
|
130
|
-
raise RenumberError(
|
|
131
|
-
f"ADR-{num:03d} is ambiguous ({names}); "
|
|
132
|
-
f"pass the file path of the one to renumber"
|
|
133
|
-
)
|
|
134
|
-
return matches[0], num
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
def build_plan(
|
|
138
|
-
files: List[Path], source: Path, old_num: int, new_num: int
|
|
139
|
-
) -> List[Tuple[Path, int, str, str]]:
|
|
140
|
-
"""Return rewrite entries: (file, 1-based line, old line, new line)."""
|
|
141
|
-
pat = token_pattern(old_num)
|
|
142
|
-
new_id = f"ADR-{new_num:03d}"
|
|
143
|
-
plan: List[Tuple[Path, int, str, str]] = []
|
|
144
|
-
for fp in files:
|
|
145
|
-
text = fp.read_text(encoding="utf-8", errors="replace")
|
|
146
|
-
for lineno, line in enumerate(text.splitlines(), start=1):
|
|
147
|
-
new_line, n = pat.subn(new_id, line)
|
|
148
|
-
if n:
|
|
149
|
-
plan.append((fp, lineno, line, new_line))
|
|
150
|
-
return plan
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
def apply_plan(
|
|
154
|
-
files: List[Path], source: Path, old_num: int, new_num: int
|
|
155
|
-
) -> Path:
|
|
156
|
-
"""Rewrite references in every file, then rename the source. Returns the
|
|
157
|
-
new source path."""
|
|
158
|
-
pat = token_pattern(old_num)
|
|
159
|
-
new_id = f"ADR-{new_num:03d}"
|
|
160
|
-
for fp in files:
|
|
161
|
-
text = fp.read_text(encoding="utf-8")
|
|
162
|
-
new_text, n = pat.subn(new_id, text)
|
|
163
|
-
if n:
|
|
164
|
-
fp.write_text(new_text, encoding="utf-8")
|
|
165
|
-
new_path = source.with_name(renamed_filename(source.name, new_num))
|
|
166
|
-
source.rename(new_path)
|
|
167
|
-
return new_path
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
def main(argv: Optional[List[str]] = None) -> int:
|
|
171
|
-
parser = argparse.ArgumentParser(
|
|
172
|
-
prog="adr-renumber",
|
|
173
|
-
description=(
|
|
174
|
-
"Renumber one ADR and update all cross-references. "
|
|
175
|
-
"Dry-run by default; pass --apply to execute."
|
|
176
|
-
),
|
|
177
|
-
)
|
|
178
|
-
parser.add_argument(
|
|
179
|
-
"source",
|
|
180
|
-
help="ADR id (ADR-043) or file path of the ADR to renumber",
|
|
181
|
-
)
|
|
182
|
-
parser.add_argument(
|
|
183
|
-
"--to", metavar="ADR-NNN", default=None,
|
|
184
|
-
help="Target id (default: next free number, max in use + 1)",
|
|
185
|
-
)
|
|
186
|
-
parser.add_argument(
|
|
187
|
-
"--adr-dir", metavar="DIR", default="docs/adr",
|
|
188
|
-
help="ADR directory (default: docs/adr)",
|
|
189
|
-
)
|
|
190
|
-
parser.add_argument(
|
|
191
|
-
"--apply", action="store_true",
|
|
192
|
-
help="Execute the plan (default is dry-run)",
|
|
193
|
-
)
|
|
194
|
-
parser.add_argument(
|
|
195
|
-
"--version", action="version", version="adr-renumber 0.1.0",
|
|
196
|
-
)
|
|
197
|
-
args = parser.parse_args(argv)
|
|
198
|
-
|
|
199
|
-
try:
|
|
200
|
-
adr_dir = Path(args.adr_dir)
|
|
201
|
-
files = discover_adrs(adr_dir)
|
|
202
|
-
source, old_num = find_source(args.source, adr_dir, files)
|
|
203
|
-
in_use = numbers_in_use(files)
|
|
204
|
-
|
|
205
|
-
if args.to is not None:
|
|
206
|
-
new_num = parse_adr_id(args.to)
|
|
207
|
-
else:
|
|
208
|
-
new_num = next_free_number(in_use)
|
|
209
|
-
|
|
210
|
-
if new_num == old_num:
|
|
211
|
-
raise RenumberError(
|
|
212
|
-
f"target ADR-{new_num:03d} equals the source number"
|
|
213
|
-
)
|
|
214
|
-
if new_num in in_use:
|
|
215
|
-
taken_by = ", ".join(fp.name for fp in in_use[new_num])
|
|
216
|
-
raise RenumberError(
|
|
217
|
-
f"target ADR-{new_num:03d} is already taken by {taken_by}"
|
|
218
|
-
)
|
|
219
|
-
except RenumberError as e:
|
|
220
|
-
print(f"adr-renumber: {e}", file=sys.stderr)
|
|
221
|
-
return 2
|
|
222
|
-
|
|
223
|
-
old_id = f"ADR-{old_num:03d}"
|
|
224
|
-
new_id = f"ADR-{new_num:03d}"
|
|
225
|
-
new_name = renamed_filename(source.name, new_num)
|
|
226
|
-
|
|
227
|
-
print(f"Plan: renumber {old_id} -> {new_id}")
|
|
228
|
-
print(f" rename: {source} -> {source.with_name(new_name)}")
|
|
229
|
-
if len(in_use.get(old_num, [])) > 1:
|
|
230
|
-
others = ", ".join(
|
|
231
|
-
fp.name for fp in in_use[old_num] if fp != source
|
|
232
|
-
)
|
|
233
|
-
print(
|
|
234
|
-
f" warning: {old_id} is duplicated (also: {others}); "
|
|
235
|
-
f"rewritten references may belong to either file. Review the plan."
|
|
236
|
-
)
|
|
237
|
-
|
|
238
|
-
plan = build_plan(files, source, old_num, new_num)
|
|
239
|
-
for fp, lineno, old_line, new_line in plan:
|
|
240
|
-
print(f" {fp}:{lineno}: {old_line.strip()}")
|
|
241
|
-
print(f" -> {new_line.strip()}")
|
|
242
|
-
print(f" {len(plan)} line(s) to rewrite across {len(files)} ADR file(s).")
|
|
243
|
-
|
|
244
|
-
if not args.apply:
|
|
245
|
-
print("Dry run: no changes made. Re-run with --apply to execute.")
|
|
246
|
-
return 0
|
|
247
|
-
|
|
248
|
-
new_path = apply_plan(files, source, old_num, new_num)
|
|
249
|
-
print(f"Applied: {len(plan)} line(s) rewritten; renamed to {new_path}")
|
|
250
|
-
return 0
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
if __name__ == "__main__":
|
|
254
|
-
sys.exit(main())
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""adr-renumber: safely renumber one ADR and update all cross-references.
|
|
3
|
+
|
|
4
|
+
Built for the merge-collision workflow: two parallel branches both claim
|
|
5
|
+
ADR-043, CI fails on the duplicate-number check in adr-lint, and one of the
|
|
6
|
+
two files must move to a free number. This tool renames the file, rewrites
|
|
7
|
+
the heading, and updates every whole-token reference to the old id across
|
|
8
|
+
all ADRs in the directory: Related Decisions entries, "Superseded by" and
|
|
9
|
+
"Amended by" status strings, Status History mentions, and plain ADR-NNN
|
|
10
|
+
references.
|
|
11
|
+
|
|
12
|
+
Safety properties:
|
|
13
|
+
- Dry-run by default: prints the full plan (file rename plus every rewrite
|
|
14
|
+
with file:line) and changes nothing. --apply executes the plan.
|
|
15
|
+
- Whole-token matching only: renumbering ADR-043 never touches ADR-0430.
|
|
16
|
+
- Refuses (exit 2) when the target number is already taken or the source
|
|
17
|
+
does not exist.
|
|
18
|
+
- All regexes are linear: no nested or lazy quantifiers (see the
|
|
19
|
+
0.19.1/0.19.2 ReDoS history in CHANGELOG.md).
|
|
20
|
+
|
|
21
|
+
Exit codes:
|
|
22
|
+
0 success (plan printed, or --apply completed)
|
|
23
|
+
2 input error (source missing, target taken, ambiguous source, bad id)
|
|
24
|
+
|
|
25
|
+
Usage:
|
|
26
|
+
adr-renumber ADR-043 # dry-run, target = next free number
|
|
27
|
+
adr-renumber ADR-043 --to ADR-051 # dry-run with explicit target
|
|
28
|
+
adr-renumber ADR-043 --to ADR-051 --apply # execute
|
|
29
|
+
adr-renumber docs/adr/ADR-043-foo.md --to ADR-051 # disambiguate duplicates
|
|
30
|
+
adr-renumber ADR-043 --adr-dir docs/decisions
|
|
31
|
+
|
|
32
|
+
Deterministic, stdlib-only, Python 3.10+.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
from __future__ import annotations
|
|
36
|
+
|
|
37
|
+
import argparse
|
|
38
|
+
import re
|
|
39
|
+
import sys
|
|
40
|
+
from pathlib import Path
|
|
41
|
+
from typing import Dict, List, Optional, Tuple
|
|
42
|
+
|
|
43
|
+
ADR_FILENAME_RE = re.compile(r"(?i)^ADR-(\d{1,4})-")
|
|
44
|
+
ADR_ID_RE = re.compile(r"(?i)^ADR-(\d{1,4})$")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class RenumberError(Exception):
|
|
48
|
+
"""Raised on input errors; maps to exit code 2."""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def parse_adr_id(raw: str) -> int:
|
|
52
|
+
"""Return the integer number of an ADR-NNN id, or raise RenumberError."""
|
|
53
|
+
m = ADR_ID_RE.match(raw.strip())
|
|
54
|
+
if not m:
|
|
55
|
+
raise RenumberError(f"{raw!r} does not match ADR-NNN (e.g. ADR-043)")
|
|
56
|
+
return int(m.group(1))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def discover_adrs(adr_dir: Path) -> List[Path]:
|
|
60
|
+
"""Return sorted ADR markdown files in adr_dir (non-recursive)."""
|
|
61
|
+
if not adr_dir.is_dir():
|
|
62
|
+
raise RenumberError(f"ADR directory not found: {adr_dir}")
|
|
63
|
+
return sorted(
|
|
64
|
+
(p for p in adr_dir.glob("*.md") if ADR_FILENAME_RE.match(p.name)),
|
|
65
|
+
key=lambda p: p.name.lower(),
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def numbers_in_use(files: List[Path]) -> Dict[int, List[Path]]:
|
|
70
|
+
"""Map ADR number -> list of files carrying that number."""
|
|
71
|
+
grouped: Dict[int, List[Path]] = {}
|
|
72
|
+
for fp in files:
|
|
73
|
+
m = ADR_FILENAME_RE.match(fp.name)
|
|
74
|
+
if m:
|
|
75
|
+
grouped.setdefault(int(m.group(1)), []).append(fp)
|
|
76
|
+
return grouped
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def next_free_number(in_use: Dict[int, List[Path]]) -> int:
|
|
80
|
+
"""Return max(used) + 1. Gaps are not reused: a gap usually means a
|
|
81
|
+
retired or reserved number, and reusing it would resurrect stale
|
|
82
|
+
references."""
|
|
83
|
+
return (max(in_use) + 1) if in_use else 1
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def token_pattern(num: int) -> "re.Pattern[str]":
|
|
87
|
+
"""Whole-token pattern for every common spelling of an ADR id.
|
|
88
|
+
|
|
89
|
+
Matches ADR-43, ADR-043, ADR-0043 (whichever spellings differ), but
|
|
90
|
+
never ADR-0430: a digit may not follow the number. \\b before 'ADR'
|
|
91
|
+
rejects mid-word hits. Pure alternation of literals: linear time.
|
|
92
|
+
"""
|
|
93
|
+
spellings = sorted({str(num), f"{num:03d}", f"{num:04d}"}, key=len, reverse=True)
|
|
94
|
+
alt = "|".join(spellings)
|
|
95
|
+
return re.compile(rf"\bADR-(?:{alt})(?!\d)")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def renamed_filename(name: str, new_num: int) -> str:
|
|
99
|
+
"""Replace the leading ADR-NNN- of a filename with the new number."""
|
|
100
|
+
m = ADR_FILENAME_RE.match(name)
|
|
101
|
+
if not m:
|
|
102
|
+
raise RenumberError(f"{name!r} is not an ADR filename (ADR-NNN-...)")
|
|
103
|
+
return f"ADR-{new_num:03d}-" + name[m.end():]
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def find_source(source_arg: str, adr_dir: Path, files: List[Path]) -> Tuple[Path, int]:
|
|
107
|
+
"""Resolve the source argument (ADR-NNN id or file path) to one file."""
|
|
108
|
+
candidate = Path(source_arg)
|
|
109
|
+
if candidate.suffix.lower() == ".md":
|
|
110
|
+
if not candidate.is_file():
|
|
111
|
+
candidate = adr_dir / candidate.name
|
|
112
|
+
if not candidate.is_file():
|
|
113
|
+
raise RenumberError(f"source file not found: {source_arg}")
|
|
114
|
+
m = ADR_FILENAME_RE.match(candidate.name)
|
|
115
|
+
if not m:
|
|
116
|
+
raise RenumberError(
|
|
117
|
+
f"{candidate.name!r} is not an ADR filename (ADR-NNN-...)"
|
|
118
|
+
)
|
|
119
|
+
return candidate, int(m.group(1))
|
|
120
|
+
|
|
121
|
+
num = parse_adr_id(source_arg)
|
|
122
|
+
matches = [
|
|
123
|
+
fp for fp in files
|
|
124
|
+
if (m := ADR_FILENAME_RE.match(fp.name)) and int(m.group(1)) == num
|
|
125
|
+
]
|
|
126
|
+
if not matches:
|
|
127
|
+
raise RenumberError(f"no ADR with number {num:03d} found in {adr_dir}")
|
|
128
|
+
if len(matches) > 1:
|
|
129
|
+
names = ", ".join(fp.name for fp in matches)
|
|
130
|
+
raise RenumberError(
|
|
131
|
+
f"ADR-{num:03d} is ambiguous ({names}); "
|
|
132
|
+
f"pass the file path of the one to renumber"
|
|
133
|
+
)
|
|
134
|
+
return matches[0], num
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def build_plan(
|
|
138
|
+
files: List[Path], source: Path, old_num: int, new_num: int
|
|
139
|
+
) -> List[Tuple[Path, int, str, str]]:
|
|
140
|
+
"""Return rewrite entries: (file, 1-based line, old line, new line)."""
|
|
141
|
+
pat = token_pattern(old_num)
|
|
142
|
+
new_id = f"ADR-{new_num:03d}"
|
|
143
|
+
plan: List[Tuple[Path, int, str, str]] = []
|
|
144
|
+
for fp in files:
|
|
145
|
+
text = fp.read_text(encoding="utf-8", errors="replace")
|
|
146
|
+
for lineno, line in enumerate(text.splitlines(), start=1):
|
|
147
|
+
new_line, n = pat.subn(new_id, line)
|
|
148
|
+
if n:
|
|
149
|
+
plan.append((fp, lineno, line, new_line))
|
|
150
|
+
return plan
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def apply_plan(
|
|
154
|
+
files: List[Path], source: Path, old_num: int, new_num: int
|
|
155
|
+
) -> Path:
|
|
156
|
+
"""Rewrite references in every file, then rename the source. Returns the
|
|
157
|
+
new source path."""
|
|
158
|
+
pat = token_pattern(old_num)
|
|
159
|
+
new_id = f"ADR-{new_num:03d}"
|
|
160
|
+
for fp in files:
|
|
161
|
+
text = fp.read_text(encoding="utf-8")
|
|
162
|
+
new_text, n = pat.subn(new_id, text)
|
|
163
|
+
if n:
|
|
164
|
+
fp.write_text(new_text, encoding="utf-8")
|
|
165
|
+
new_path = source.with_name(renamed_filename(source.name, new_num))
|
|
166
|
+
source.rename(new_path)
|
|
167
|
+
return new_path
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def main(argv: Optional[List[str]] = None) -> int:
|
|
171
|
+
parser = argparse.ArgumentParser(
|
|
172
|
+
prog="adr-renumber",
|
|
173
|
+
description=(
|
|
174
|
+
"Renumber one ADR and update all cross-references. "
|
|
175
|
+
"Dry-run by default; pass --apply to execute."
|
|
176
|
+
),
|
|
177
|
+
)
|
|
178
|
+
parser.add_argument(
|
|
179
|
+
"source",
|
|
180
|
+
help="ADR id (ADR-043) or file path of the ADR to renumber",
|
|
181
|
+
)
|
|
182
|
+
parser.add_argument(
|
|
183
|
+
"--to", metavar="ADR-NNN", default=None,
|
|
184
|
+
help="Target id (default: next free number, max in use + 1)",
|
|
185
|
+
)
|
|
186
|
+
parser.add_argument(
|
|
187
|
+
"--adr-dir", metavar="DIR", default="docs/adr",
|
|
188
|
+
help="ADR directory (default: docs/adr)",
|
|
189
|
+
)
|
|
190
|
+
parser.add_argument(
|
|
191
|
+
"--apply", action="store_true",
|
|
192
|
+
help="Execute the plan (default is dry-run)",
|
|
193
|
+
)
|
|
194
|
+
parser.add_argument(
|
|
195
|
+
"--version", action="version", version="adr-renumber 0.1.0",
|
|
196
|
+
)
|
|
197
|
+
args = parser.parse_args(argv)
|
|
198
|
+
|
|
199
|
+
try:
|
|
200
|
+
adr_dir = Path(args.adr_dir)
|
|
201
|
+
files = discover_adrs(adr_dir)
|
|
202
|
+
source, old_num = find_source(args.source, adr_dir, files)
|
|
203
|
+
in_use = numbers_in_use(files)
|
|
204
|
+
|
|
205
|
+
if args.to is not None:
|
|
206
|
+
new_num = parse_adr_id(args.to)
|
|
207
|
+
else:
|
|
208
|
+
new_num = next_free_number(in_use)
|
|
209
|
+
|
|
210
|
+
if new_num == old_num:
|
|
211
|
+
raise RenumberError(
|
|
212
|
+
f"target ADR-{new_num:03d} equals the source number"
|
|
213
|
+
)
|
|
214
|
+
if new_num in in_use:
|
|
215
|
+
taken_by = ", ".join(fp.name for fp in in_use[new_num])
|
|
216
|
+
raise RenumberError(
|
|
217
|
+
f"target ADR-{new_num:03d} is already taken by {taken_by}"
|
|
218
|
+
)
|
|
219
|
+
except RenumberError as e:
|
|
220
|
+
print(f"adr-renumber: {e}", file=sys.stderr)
|
|
221
|
+
return 2
|
|
222
|
+
|
|
223
|
+
old_id = f"ADR-{old_num:03d}"
|
|
224
|
+
new_id = f"ADR-{new_num:03d}"
|
|
225
|
+
new_name = renamed_filename(source.name, new_num)
|
|
226
|
+
|
|
227
|
+
print(f"Plan: renumber {old_id} -> {new_id}")
|
|
228
|
+
print(f" rename: {source} -> {source.with_name(new_name)}")
|
|
229
|
+
if len(in_use.get(old_num, [])) > 1:
|
|
230
|
+
others = ", ".join(
|
|
231
|
+
fp.name for fp in in_use[old_num] if fp != source
|
|
232
|
+
)
|
|
233
|
+
print(
|
|
234
|
+
f" warning: {old_id} is duplicated (also: {others}); "
|
|
235
|
+
f"rewritten references may belong to either file. Review the plan."
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
plan = build_plan(files, source, old_num, new_num)
|
|
239
|
+
for fp, lineno, old_line, new_line in plan:
|
|
240
|
+
print(f" {fp}:{lineno}: {old_line.strip()}")
|
|
241
|
+
print(f" -> {new_line.strip()}")
|
|
242
|
+
print(f" {len(plan)} line(s) to rewrite across {len(files)} ADR file(s).")
|
|
243
|
+
|
|
244
|
+
if not args.apply:
|
|
245
|
+
print("Dry run: no changes made. Re-run with --apply to execute.")
|
|
246
|
+
return 0
|
|
247
|
+
|
|
248
|
+
new_path = apply_plan(files, source, old_num, new_num)
|
|
249
|
+
print(f"Applied: {len(plan)} line(s) rewritten; renamed to {new_path}")
|
|
250
|
+
return 0
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
if __name__ == "__main__":
|
|
254
|
+
sys.exit(main())
|
package/bin/adr-retire
CHANGED
|
File without changes
|
package/bin/adr-settings
CHANGED
|
File without changes
|
package/bin/adr-status
CHANGED
|
File without changes
|
package/bin/adr-suggest
CHANGED
|
File without changes
|
package/bin/adr-watch
CHANGED
|
File without changes
|
package/bin/adr_doctor_probes.py
CHANGED
package/bin/bump-version
CHANGED
|
File without changes
|