diff --git a/docs/verification/merge-shape-differential.md b/docs/verification/merge-shape-differential.md new file mode 100644 index 00000000..117a8017 --- /dev/null +++ b/docs/verification/merge-shape-differential.md @@ -0,0 +1,142 @@ +# Verification: merge-shape-differential + +`lib/config/merge-command.sh` has two parsers for the project merge command. `_kit_mc_shape` (the check) refuses forms the reader cannot read. `_kit_toml_get` (the reader) produces the value `kit_merge_exec` runs. They found and named table headers differently, and the check judged keys outside the merge table by their own text, not by their full path. So valid TOML that names the command could pass the check and still read as unset. Unset means `wrap merge`, `wrap land`, `mega-merge` and `stack-merge` fall back to the plain squash, which skips the project's merge guard. The contract says such forms refuse with exit 2. + +## Differential, before the fix + +Probe at `origin/master` `1facce9d`: shape rc (0 = accepted), reader value, Python `tomllib` value. + +| Form (section) | Check | Reader | tomllib | Origin | +|---|---|---|---|---| +| `[dev]` + `merge.command = "guard.sh"` (dev.merge) | 0 | empty | guard.sh | PR 1277 | +| `[dev]` + `merge = { command = "guard.sh" }` | 0 | empty | guard.sh | PR 1277 | +| `dev = { merge = { command = "guard.sh" } }` | 0 | empty | guard.sh | PR 1277 | +| `["dev".merge]` / `[dev."merge"]` + `command = ...` | 0 | empty | guard.sh | PR 1277 | +| `["merge"]` / `['merge']` + `command = ...` (merge) | 0 | empty | g.sh | before 1277 | +| `[merge]#c` + `command = "g.sh #x"` | 0 | g.sh (cut) | g.sh #x | before 1277 | +| `[merge]#c` + `command = 'g.sh'` | 0 | 'g.sh' | g.sh | before 1277 | + +The first five rows fail open: the check accepts, the reader says unset, the kit merges plain. The last two run a value the check never looked at: it skipped a header with `#` right after `]`, which the reader enters. A quoted key such as `"[x]" = 1` inside the table had the reverse effect: the check took it for a header and stopped looking, the reader did not. + +The key is read from the default branch, so a PR cannot use this for its own merge. A config edit that looks like a no-op to anyone reading TOML (moving `command` under `[dev]`) would turn the guard off for every later merge, with no error. + +## Fix + +The reader treats every line that opens with `[` as a header. The check now: + +- accepts such a line only as a plain header (bare-key segments, blanks, an optional `#` comment); +- finds strings, comments and array brackets with one left-to-right scan per line, the way TOML reads them (basic strings with escapes, literal strings without, both multi-line kinds carried across lines); +- refuses a `[` line inside a multi-line array or string only where the reader would leave or enter the merge table by mistake (inside the table, or naming the table or a parent); +- refuses a multi-line string inside the merge table, a blank other than space or tab, and a `\` in a key; +- judges a key outside the merge table by its full path (current table plus key), and refuses any value on `dev` itself. So `[dev]` + `merge.command`, `[dev]` + `merge = {...}` and `dev = { ... }` refuse like the dotted form; +- runs, with the reader, under `LC_ALL=C`, so a locale cannot change what counts as a blank. + +## Independent review + +A fresh-context security reviewer attacked the fix with a growing corpus (129 inputs at the end), under macOS awk, GNU Awk 5.4.2 and mawk 1.3.4. + +| Round | Found | Now | +|---|---|---| +| 1 | nested-array lines, multi-line strings, `\u` escapes in keys and headers, a two-line `dev = {`, form-feed blanks; a regression (carriage return inside a header) | refused, test rows | +| 2 | a nested element alone on its line (` [1]`) still read as a header; triple quotes and nested arrays refused anywhere (over-refusal); `[[:space:]]` depended on the locale (mawk refused `à` in a comment) | refused where it matters, allowed elsewhere, `LC_ALL=C` | +| 3 | quote counting could not track TOML strings: `'''` inside a one-line string, mixed `"""` and `'''`, triples in a closing-line comment, a backslash ending a literal string, a `"` inside a literal string | a left-to-right scanner per line, test rows | +| 4 (fresh reviewer, fuzzing) | no fail-open in about 69,000 generated files (the same harness finds about 730 on master); one low-impact case: `[[dev]]` before `[dev.merge]`; new over-refusal of array element lines with `=` or `\` (regex lists) | `[[parent]]` refused; array element lines are not keys, test rows | + +Final state: the round-4 corpora (68,580 files, both sections) give zero disagreements between check-accepted reads and `tomllib`. The same harness flags failures on `origin/master`, so the zero is a real result. macOS awk, gawk and mawk agree on every corpus they ran. + +## Red run (new tests on the old code) + +`lib/config/merge-command.sh` from `origin/master` with the new tests. The "carriage return inside the header" row passes here because the old code refused that form; it guards the regression the review found in the first version of this fix. The negative controls pass here too. + +``` +Command: bash tests/run-all.sh --only test-merge-command +Exit: 1 +Output: + FAIL slug: kit.toml [dev] with merge.command refuses (exit 2), not unset + FAIL check/reader differential (kit: [dev] then a dotted merge.command): refuses (exit 2) + FAIL check/reader differential (kit: [dev] then an inline merge table): refuses (exit 2) + FAIL check/reader differential (kit: top-level dev inline table): refuses (exit 2) + FAIL check/reader differential (kit: quoted header ["dev".merge]): refuses (exit 2) + FAIL check/reader differential (kit: quoted header [dev."merge"]): refuses (exit 2) + FAIL check/reader differential (kit: header comment with no blank, # in the value): refuses (exit 2) + FAIL check/reader differential (kit: header comment with no blank, single-quoted value): refuses (exit 2) + FAIL check/reader differential (kit: a quoted key that looks like a header): refuses (exit 2) + FAIL check/reader differential (legacy: quoted header ["merge"]): refuses (exit 2) + FAIL check/reader differential (legacy: single-quoted header ['merge']): refuses (exit 2) + FAIL check/reader differential (legacy: header comment with no blank, # in the value): refuses (exit 2) + FAIL check/reader differential (legacy: a quoted key that looks like a header): refuses (exit 2) + FAIL check/reader differential (kit: a nested-array line inside the table): refuses (exit 2) + FAIL check/reader differential (kit: a nested-array line, then [dev] merge.command): refuses (exit 2) + FAIL check/reader differential (kit: a multi-line string holding a header): refuses (exit 2) + FAIL check/reader differential (kit: an escaped key under [dev]): refuses (exit 2) + FAIL check/reader differential (kit: an escaped header): refuses (exit 2) + FAIL check/reader differential (kit: a dev inline table over two lines): refuses (exit 2) + FAIL check/reader differential (legacy: a form feed before the key): refuses (exit 2) + FAIL check/reader differential (legacy: a multi-line string holding a command): refuses (exit 2) + FAIL check/reader differential (legacy: an escaped header): refuses (exit 2) + FAIL check/reader differential (kit: a nested element alone on its line inside the table): refuses (exit 2) + FAIL check/reader differential (kit: a nested element alone on its line, then [dev] merge.command): refuses (exit 2) + FAIL check/reader differential (kit: a comment holding a triple quote, then dev inline table): refuses (exit 2) + FAIL check/reader differential (kit: an inline comment opening a triple quote): refuses (exit 2) + FAIL check/reader differential (kit: a multi-line string in another table holding the header): refuses (exit 2) + FAIL check/reader differential (legacy: a nested date element alone on its line): refuses (exit 2) + FAIL check/reader differential (kit: a triple single quote inside a one-line string): refuses (exit 2) + FAIL check/reader differential (kit: a triple single quote inside a multi-line basic string): refuses (exit 2) + FAIL check/reader differential (kit: a comment with triples on the closing line): refuses (exit 2) + FAIL check/reader differential (kit: a literal string ending in a backslash before a bracket string): refuses (exit 2) + FAIL check/reader differential (kit: a double quote inside a literal string): refuses (exit 2) + FAIL check/reader differential (kit: an array of tables on the parent): refuses (exit 2) + FAIL kit.toml: array element lines with = or a backslash are not keys (exit 0) + FAIL ...and give the value +test-merge-command: 147 passed, 36 FAILED of 183 +Verdict: RED, as expected +``` + +## Green run + +``` +Command: bash tests/run-all.sh --only test-merge-command --time +Exit: 0 +Output: +run-all: all 1 suites passed, 0 skipped for missing tooling +Verdict: PASS +``` + +``` +Command: bash tests/run-all.sh --time +Exit: 0 +Output: +run-all: all 25 suites passed, 0 skipped for missing tooling +Verdict: PASS +``` + +``` +Command: bash tests/run-all.sh --only (one run each) +Exit: 0 +Output: +test-unified-kit-toml rc=0 +test-mega-merge rc=0 +test-wrap-merge rc=0 +test-wrap-land rc=0 +test-gate-opt-in rc=0 +Verdict: PASS +``` + +## Negative controls + +These pass on the fix, so it does not over-refuse. All but the array-element control also pass on `origin/master`: + +- Multi-line strings, `'''` strings, nested arrays (one element alone on its line) and a Vietnamese comment outside the merge table still read (exit 0). +- A `[repo]` multi-line string array, `[dev]` keys, `[ dev.merge ] # comment` and `command = "..." # trailing` still read (exit 0). +- `[dev] merge_own_prs`, `[dev] lane = { ... }`, `[dev.wrap] merge_own_prs`, `[dev.gate] merge_gate` are not the merge table (exit 1). +- 38 real `kit.toml` / `.kit.toml` files (the local workspace plus the operator file) give the same check result before and after the fix, for both sections. +- Array element lines such as regex lists (`"^\\s*foo="`, `"merge=x"`, `"command = not a key"` inside `[dev.merge]`) are not read as keys (exit 0). `origin/master` refuses this fixture (its two rows are in the red run), so the fix reads it where master refused it. +- Over-refusal this fix adds: any value on `dev` itself (`dev = { wrap = ... }`), a quoted header anywhere (`[tool."a b"]`), a multi-line string or nested multi-line array inside the merge table. The kit writes plain `[dev.*]` tables, so these forms do not occur in its own configs. A legacy `[wrap]` + `merge = ...` was already refused and is unchanged. + +## Guard settings + +`[dev.gate]` keys have one reader (`_kit_project_get`) and no separate check, so there is no check/runner split. An unreadable form (`[dev]` + `gate.proof_of_done = false`) reads as empty and falls back to the operator or kit-root value, so a project cannot switch a gate off that way. The same form cannot switch a default-off gate on either: it is ignored without a warning. That is a separate config-UX gap, outside this file. + +## Rollback + +Revert the commit. The change touches one shell function and its tests, and stores no state. diff --git a/lib/config/merge-command.sh b/lib/config/merge-command.sh index 466365f0..ee33a209 100644 --- a/lib/config/merge-command.sh +++ b/lib/config/merge-command.sh @@ -46,7 +46,11 @@ # value without a backslash or a `#`. The dotted form (`merge.command = ...`), an inline table # (`merge = { ... }`), `[[merge]]`, a triple-quoted or single-quoted value, a repeated key and a # `#` inside the value are all REFUSED (exit 2): the reader cannot read them, and unset would -# mean the plain merge. An empty value is a plain "unset". +# mean the plain merge. So are, anywhere in the file, a header that is not plain (quoted, +# escaped, or a multi-line array line opening with `[`), a multi-line string, a `\` in a key and +# a blank other than space or tab; and, in kit.toml, a key under a parent table that lands in +# `dev.merge` (`[dev]` + `merge.command`) or any value on `dev` itself (`dev = { ... }`). An empty +# value is a plain "unset". # The value must also be one simple command: no `;` `&` `|` `<` `>` backtick `$(` or newline. # That denylist guards against mistakes. It is NOT a security boundary: whoever can write the # value can also write the script it names. The PR number is appended as the last argument. @@ -95,42 +99,118 @@ _kit_mc_base_ref() { # _kit_mc_shape [
] -- exit 0 when the file names
(default `merge`; # `dev.merge` for unified project kit.toml) only in the one shape the reader reads (or not at all), # 1 otherwise. It looks at the raw lines, because the reader returns empty for the forms it cannot -# parse and empty would read as "unset". +# parse and empty would read as "unset". The reader treats every line that opens with `[` as a +# header. So the check accepts such a line only as a plain header (bare-key segments, blanks, an +# optional comment), and refuses one inside a multi-line array or string where the reader would +# leave or enter the merge table by mistake; strings and arrays are found by one left-to-right +# scan per line, as TOML reads them. It also refuses a multi-line string in the merge table, a +# blank other than space or tab, and a `\` in a key. A key outside the merge table is judged by +# its full path (current table plus key), so `[dev]` + `merge.command` refuses like +# `dev.merge.command`, and so does any value on a parent (`dev = {`). Both this check and the +# reader run under LC_ALL=C, so a locale cannot change what counts as a blank. _kit_mc_shape() { local f="$1" sec="${2:-merge}" [ -s "$f" ] || return 0 - awk -v q="'" -v sec="$sec" ' + LC_ALL=C awk -v q="'" -v sec="$sec" ' function bad() { exit 1 } # key text with leading blanks and one layer of quotes around the first word removed function firstword(k, w) { w = k; sub(/[ \t]*[.=].*$/, "", w); gsub(/"/, "", w); gsub(q, "", w); return w } function lhs_norm(k, w) { w = k; sub(/[ \t]*=.*$/, "", w); gsub(/[ \t]/, "", w); gsub(/"/, "", w); gsub(q, "", w); return w } - /^[ \t]*#/ { next } - { - line = $0 - h = line; sub(/[ \t]+#.*$/, "", h); gsub(/[ \t\r]/, "", h); gsub(/"/, "", h); gsub(q, "", h) + # p is the merge table or inside it; p is a parent of it (`dev` holds `dev.merge`) + function names_sec(p) { return p == sec || index(p, sec ".") == 1 } + function holds_sec(p) { return index(sec, p ".") == 1 } + # the table name the reader gives a line that opens with `[` + function rname(l, h) { h = l; sub(/#.*/, "", h); gsub(/[][ \t]/, "", h); return h } + # a line opening with `[` that is not a header: the reader still takes it for one + function stray(l, h) { + h = rname(l); gsub(/"/, "", h); gsub(q, "", h) + if (insec || names_sec(h) || holds_sec(h)) bad() + } + # scan(l): walk the line once, left to right, the way TOML does: a basic string (escapes + # apply), a literal string (they do not), a multi-line string of either kind (ml, carried to + # the next line), a comment. Returns the change in array depth outside strings. + function scan(l, i, n, c, c3, st, d) { + n = length(l); i = 1; st = 0; d = 0 + while (i <= n) { + c = substr(l, i, 1); c3 = substr(l, i, 3) + if (ml == 1) { + if (c == "\\") { i += 2; continue } + if (c3 == "\"\"\"") { ml = 0; i += 3; while (substr(l, i, 1) == "\"") i++; continue } + i++; continue + } + if (ml == 2) { + if (c3 == q q q) { ml = 0; i += 3; while (substr(l, i, 1) == q) i++; continue } + i++; continue + } + if (st == 1) { if (c == "\\") { i += 2; continue } if (c == "\"") st = 0; i++; continue } + if (st == 2) { if (c == q) st = 0; i++; continue } + if (c == "#") break + if (c3 == "\"\"\"") { ml = 1; i += 3; continue } + if (c3 == q q q) { ml = 2; i += 3; continue } + if (c == "\"") st = 1 + else if (c == q) st = 2 + else if (c == "[") d++ + else if (c == "]") d-- + i++ + } + return d } - h ~ /^\[/ { - if (h == "[[" sec "]]" || index(h, "[[" sec ".") == 1) bad() - if (h == "[" sec "]") { if (seen++) bad(); insec = 1; ncmd = 0; next } - if (index(h, "[" sec ".") == 1) bad() - insec = 0; next + BEGIN { + key = "[ \t]*[A-Za-z0-9_-]+([ \t]*[.][ \t]*[A-Za-z0-9_-]+)*[ \t]*" + header = "^[ \t]*([[]" key "[]]|[[][[]" key "[]][]])[ \t]*(#.*)?$" } { - k = line; sub(/^[ \t]+/, "", k) - if (!insec) { - if (sec == "merge") { - if (firstword(k) == "merge" && k ~ /[.=]/) bad() - } else if (index(k, "=")) { - lh = lhs_norm(k) - if (lh == sec || index(lh, sec ".") == 1) bad() - } + line = $0; sub(/\r$/, "", line) + t = line; gsub(/[ \t]/, "", t) + if (t ~ /[[:space:]]/) bad() + if (insec && (ml || index(line, "\"\"\"") || index(line, q q q))) bad() + opens = (line ~ /^[ \t]*\[/) + if (ml) { + # the line starts inside a multi-line string; the reader still reads it + if (opens) stray(line) + depth += scan(line); if (depth < 0) depth = 0 next } + } + line ~ /^[ \t]*#/ { next } + { + cont = (depth > 0) + d = scan(line) + if (depth > 0) { + if (opens) stray(line) + depth += d; if (depth < 0) depth = 0 + if (opens) next + } else if (!opens) { + depth += d; if (depth < 0) depth = 0 + } + } + line ~ /^[ \t]*\[/ { + if (line !~ header) bad() + hr = rname(line); arr = (line ~ /^[ \t]*\[[ \t]*\[/) + if (hr == sec && !arr) { if (seen++) bad(); insec = 1; ncmd = 0; next } + # an array of tables or a sub-table: the reader cannot read it; an array of tables on a + # parent (`[[dev]]`) moves the merge table out from under the path the reader reads + if (names_sec(hr) || (arr && holds_sec(hr))) bad() + insec = 0; cur = hr; next + } + { + # an array element line holds no key, but the reader still matches `command =` in the table + if (cont) { if (insec && line ~ /^[ \t]*command[ \t]*=/) bad(); next } + k = line; sub(/^[ \t]+/, "", k) + if (index(k, "=")) { lk = k; sub(/=.*$/, "", lk); if (index(lk, "\\")) bad() } + } + insec { if (firstword(k) == "command" && k ~ /[.=]/) { if (++ncmd > 1) bad() - sub(/\r$/, "", line) if (line !~ /^[ \t]*command[ \t]*=[ \t]*"[^"\\#]*"[ \t]*(#.*)?$/) bad() } + next + } + { + if (sec == "merge" && firstword(k) == "merge" && k ~ /[.=]/) bad() + if (!index(k, "=")) next + lh = lhs_norm(k); full = (cur == "") ? lh : cur "." lh + if (names_sec(full) || holds_sec(full)) bad() } ' "$f" } @@ -144,7 +224,7 @@ _kit_mc_resolve() { sec="${pair%%:*}"; f="${pair#*:}" [ -s "$f" ] || continue _kit_mc_shape "$f" "$sec" || { echo "merge command: $f names [$sec] in a form the kit cannot read (use a plain [$sec] table with command = \"...\", no # or backslash in the value); refusing to merge" >&2; return 2; } - v="$(_kit_toml_get "$f" "$sec" command)" + v="$(LC_ALL=C _kit_toml_get "$f" "$sec" command)" [ -z "$v" ] || { printf '%s' "$v"; return 0; } done return 1 @@ -195,8 +275,8 @@ kit_merge_command() { rm -f "$tmpk" "$tmpl" v="$(_kit_mc_resolve /nonexistent /nonexistent)"; r=$? if [ "$r" -ne 1 ]; then _kit_mc_finish "$r" "$v"; return $?; fi - if [ -n "$(_kit_toml_get "$repo/kit.toml" dev.merge command)" ] || ! _kit_mc_shape "$repo/kit.toml" dev.merge \ - || [ -n "$(_kit_toml_get "$repo/.kit.toml" merge command)" ] || ! _kit_mc_shape "$repo/.kit.toml" merge; then + if [ -n "$(LC_ALL=C _kit_toml_get "$repo/kit.toml" dev.merge command)" ] || ! _kit_mc_shape "$repo/kit.toml" dev.merge \ + || [ -n "$(LC_ALL=C _kit_toml_get "$repo/.kit.toml" merge command)" ] || ! _kit_mc_shape "$repo/.kit.toml" merge; then echo "merge command: $repo names [merge] but origin's default branch cannot be read to verify it (run git fetch); refusing to merge" >&2 return 2 fi diff --git a/tests/test-merge-command.sh b/tests/test-merge-command.sh index 2c47500f..075c4cca 100644 --- a/tests/test-merge-command.sh +++ b/tests/test-merge-command.sh @@ -211,6 +211,7 @@ case "\$*" in case "\${MC_SLUG_MODE:-}" in kit_set) printf '[dev.merge]\ncommand = "bash scripts/kit-merge.sh"\n'; exit 0 ;; kit_dotted) printf 'dev.merge.command = "bash scripts/kit-merge.sh"\n'; exit 0 ;; + kit_parent) printf '[dev]\nmerge.command = "bash scripts/kit-merge.sh"\n'; exit 0 ;; set|dotted|none|hidden) echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;; *) echo "gh: Bad Gateway (HTTP 502)" >&2; exit 1 ;; esac ;; @@ -218,14 +219,14 @@ case "\$*" in case "\${MC_SLUG_MODE:-}" in set|kit_set) printf '[merge]\ncommand = "bash scripts/pr-merge.sh"\n'; exit 0 ;; dotted) printf 'merge.command = "bash scripts/pr-merge.sh"\n'; exit 0 ;; - none|hidden|kit_dotted) echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;; + none|hidden|kit_dotted|kit_parent) echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;; *) echo "gh: Bad Gateway (HTTP 502)" >&2; exit 1 ;; esac ;; *"contents/?ref="*) # the directory listing of the same ref: readable for "none"/"set"/"dotted", also a 404 for "hidden" (no contents access) case "\${MC_SLUG_MODE:-}" in none|set|dotted) printf 'README.md\nsrc\n'; exit 0 ;; - kit_dotted) printf 'README.md\nkit.toml\n'; exit 0 ;; + kit_dotted|kit_parent) printf 'README.md\nkit.toml\n'; exit 0 ;; esac echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;; esac @@ -248,6 +249,8 @@ chk "slug: unified kit.toml [dev.merge] wins over .kit.toml [merge]" "$rc" chk_has "slug: unified kit.toml [dev.merge] value" "$cmd" "bash scripts/kit-merge.sh" PATH="$TMPD/shim:$PATH" MC_SLUG_MODE=kit_dotted kit_merge_command_slug owner/name main >/dev/null 2>&1; rc=$? chk "slug: unified kit.toml dotted dev.merge.command refuses (exit 2)" "$([ "$rc" -eq 2 ]; echo $?)" +PATH="$TMPD/shim:$PATH" MC_SLUG_MODE=kit_parent kit_merge_command_slug owner/name main >/dev/null 2>&1; rc=$? +chk "slug: kit.toml [dev] with merge.command refuses (exit 2), not unset" "$([ "$rc" -eq 2 ]; echo $?)" # Unified kit.toml [dev.merge] in a git repo beats .kit.toml [merge] R_UNI="$(mc_repo_toml uni '[merge] @@ -258,6 +261,76 @@ cmd="$(kit_merge_command "$R_UNI")"; rc=$? chk "repo: unified kit.toml [dev.merge] wins over .kit.toml [merge]" "$rc" chk_has "repo: unified kit.toml [dev.merge] value" "$cmd" "bash scripts/unified-merge.sh" +# mc_repo_kit -- a repo whose default branch commits exactly this kit.toml. +mc_repo_kit() { + local c; c="$(mc_repo "$1" no ok)" + printf '%s\n' "$2" > "$c/kit.toml" + git -C "$c" add -A; git -C "$c" commit -qm "chore: write the key"; git -C "$c" push -q origin main + printf '%s\n' "$c" +} + +# The shape check and the reader must agree on where the merge table is. Each row is valid TOML +# that names the command, but the reader reads it as unset (the kit would merge plain) or reads a +# line the check never looked at. Every one refuses (exit 2). +KFORMN=0 +while IFS='|' read -r file label toml; do + KFORMN=$((KFORMN+1)); toml="$(printf '%b' "$toml")" + if [ "$file" = kit ]; then RF="$(mc_repo_kit "kform-$KFORMN" "$toml")"; else RF="$(mc_repo_toml "lform-$KFORMN" "$toml")"; fi + fo="$(kit_merge_command "$RF" 2>&1)"; rc=$? + chk "check/reader differential ($file: $label): refuses (exit 2)" "$([ "$rc" -eq 2 ]; echo $?)" +done <<'KFORMS' +kit|[dev] then a dotted merge.command|[dev]\nmerge.command = "bash scripts/pr-merge.sh" +kit|[dev] then an inline merge table|[dev]\nmerge = { command = "bash scripts/pr-merge.sh" } +kit|top-level dev inline table|dev = { merge = { command = "bash scripts/pr-merge.sh" } } +kit|quoted header ["dev".merge]|["dev".merge]\ncommand = "bash scripts/pr-merge.sh" +kit|quoted header [dev."merge"]|[dev."merge"]\ncommand = "bash scripts/pr-merge.sh" +kit|header comment with no blank, # in the value|[dev.merge]#note\ncommand = "bash scripts/pr-merge.sh #approved" +kit|header comment with no blank, single-quoted value|[dev.merge]#note\ncommand = 'bash scripts/pr-merge.sh' +kit|a quoted key that looks like a header|[dev.merge]\n"[x]" = 1\ncommand = 'bash scripts/pr-merge.sh' +legacy|quoted header ["merge"]|["merge"]\ncommand = "bash scripts/pr-merge.sh" +legacy|single-quoted header ['merge']|['merge']\ncommand = "bash scripts/pr-merge.sh" +legacy|header comment with no blank, # in the value|[merge]#note\ncommand = "bash scripts/pr-merge.sh #approved" +legacy|a quoted key that looks like a header|[merge]\n"[x]" = 1\ncommand = 'bash scripts/pr-merge.sh' +kit|a nested-array line inside the table|[dev.merge]\nchecks = [\n ["lint", "test"],\n]\ncommand = "bash scripts/pr-merge.sh" +kit|a nested-array line, then [dev] merge.command|[dev]\nx = [\n ["a"],\n]\nmerge.command = "bash scripts/pr-merge.sh" +kit|a multi-line string holding a header|[dev.merge]\nnote = """\n[x]\n"""\ncommand = "bash scripts/pr-merge.sh" +kit|an escaped key under [dev]|[dev]\n"m\\u0065rge".command = "bash scripts/pr-merge.sh" +kit|an escaped header|["dev"."m\\u0065rge"]\ncommand = "bash scripts/pr-merge.sh" +kit|a dev inline table over two lines|dev = { x = [\n 1,\n], merge = { command = "bash scripts/pr-merge.sh" } } +kit|a carriage return inside the header|[dev.merge\r]\ncommand = 'bash scripts/pr-merge.sh' +legacy|a form feed before the key|[merge]\n\fcommand = 'bash scripts/pr-merge.sh' +legacy|a multi-line string holding a command|[merge]\nnote = """\ncommand = "bash scripts/pr-merge.sh"\n""" +legacy|an escaped header|["m\\u0065rge"]\ncommand = "bash scripts/pr-merge.sh" +kit|a nested element alone on its line inside the table|[dev.merge]\nchecks = [\n [1]\n]\ncommand = "bash scripts/pr-merge.sh" +kit|a nested element alone on its line, then [dev] merge.command|[dev]\nx = [\n [1]\n]\nmerge.command = "bash scripts/pr-merge.sh" +kit|a comment holding a triple quote, then dev inline table|# use """ here\ndev = { merge = { command = "bash scripts/pr-merge.sh" } } +kit|an inline comment opening a triple quote|[x]\na = 1 # """\n[dev]\nmerge.command = "bash scripts/pr-merge.sh" +kit|a multi-line string in another table holding the header|[project]\nd = """\n[dev.merge]\n"""\ncommand = "bash scripts/pr-merge.sh" +legacy|a nested date element alone on its line|[merge]\nchecks = [\n [2024-01-01]\n]\ncommand = "bash scripts/pr-merge.sh" +kit|a triple single quote inside a one-line string|[dev]\na = "'''"\nmerge.command = "bash scripts/pr-merge.sh" +kit|a triple single quote inside a multi-line basic string|[dev]\na = """\n'''\n"""\nmerge.command = "bash scripts/pr-merge.sh" +kit|a comment with triples on the closing line|[dev]\na = """\ntext\n""" # don'''t\nmerge.command = "bash scripts/pr-merge.sh" +kit|a literal string ending in a backslash before a bracket string|[dev.merge]\nchecks = ['x\\', ']',\n [1]\n]\ncommand = "bash scripts/pr-merge.sh" +kit|a double quote inside a literal string|[dev.merge]\nchecks = ['a"b', "]",\n [1]\n]\ncommand = "bash scripts/pr-merge.sh" +kit|an array of tables on the parent|[[dev]]\n[dev.merge]\ncommand = "bash scripts/pr-merge.sh" +KFORMS +# Negative controls: the readable shape and unrelated dev keys keep their meaning. +RG="$(mc_repo_kit kform-good $'[repo]\ndoor_paths = [\n "hooks/",\n "lib/gate/",\n]\n[dev]\nprofile = "x"\n\n[ dev.merge ] # the one shape\ncommand = "bash scripts/pr-merge.sh" # trailing')" +fo="$(kit_merge_command "$RG" 2>&1)"; rc=$? +chk "kit.toml: a multi-line string array, [dev] keys and the plain [dev.merge] shape still read (exit 0)" "$rc" +chk_has "...and give the value" "$fo" "bash scripts/pr-merge.sh" +RG="$(mc_repo_kit kform-other $'[dev]\nmerge_own_prs = true\nlane = { normal = "x" }\n[dev.wrap]\nmerge_own_prs = true\n[dev.gate]\nmerge_gate = false')" +kit_merge_command "$RG" >/dev/null 2>&1; rc=$? +chk "kit.toml: other dev keys that mention merge are not the merge table (exit 1)" "$([ "$rc" -eq 1 ]; echo $?)" +RG="$(mc_repo_kit kform-elsewhere $'# ghi chú: cài đặt và à\n[project]\ndescription = """\nmulti [x]\n"""\npattern = \'\'\'a|b\'\'\'\n[dev.lanes]\nmatrix = [\n ["a", "b"],\n [1]\n]\n[dev.merge]\ncommand = "bash scripts/pr-merge.sh" # à')" +fo="$(kit_merge_command "$RG" 2>&1)"; rc=$? +chk "kit.toml: multi-line strings, nested arrays and non-ASCII comments outside the merge table still read (exit 0)" "$rc" +chk_has "...and give the value" "$fo" "bash scripts/pr-merge.sh" +RG="$(mc_repo_kit kform-arrays $'[dev]\nignore = [\n "^\\\\s*foo=",\n "merge=x",\n]\n[dev.merge]\nallow = [\n "command = not a key",\n]\ncommand = "bash scripts/pr-merge.sh"')" +fo="$(kit_merge_command "$RG" 2>&1)"; rc=$? +chk "kit.toml: array element lines with = or a backslash are not keys (exit 0)" "$rc" +chk_has "...and give the value" "$fo" "bash scripts/pr-merge.sh" + # =========================================================================== echo "=== wrap merge --apply: the project command merges, the plain merge is never called ===" # ===========================================================================