Merge chore/foreign-code-style into develop

This commit is contained in:
bchanot committed 2026-10-23 17:35:59 +02:00
commit 1a1426586e
9 files changed
+168 -10

No files matched your search

+15 -2
View File
@@ -2,7 +2,20 @@
Repo-specific instructions live in ./CLAUDE.md (project scope). -->
# Global coding preferences
Apply unless repo-specific instructions override.
Strict in our code (project started by the user or Claude). Foreign
code (`bash ~/.claude/lib/code-origin.sh` → foreign) → "Foreign
code" wins. Repo-specific instructions override both.
## Foreign code: keep its style
- Existing code's style is the norm unless the user says otherwise:
function granularity, file layout, naming, comment density, error
handling, idioms. Write like the neighbouring code.
- Neighbours work inline → work inline: no function, helper or
abstraction the file would not have written. Reuse what is in
scope (variables, macros, sizeof).
- Limits become guidelines: exceed them where the file's style
needs it.
- No refactor toward our norms unless asked.
## Code style
- Simple, readable, maintainable > clever or compact.
@@ -17,7 +30,7 @@ Apply unless repo-specific instructions override.
- Assumed shortcut → `shortcut:` comment at the site (limit + upgrade
path), harvested into TODO.md so "later" stays findable.
## Limits (adapt to language)
## Limits (our code: strict; foreign code: see above)
- Max 25 logic lines/function (executable statements; comments and
error-handling boilerplate don't count), 80 chars/line, 5 params, 5 locals.
- Too many params → struct/object. Too many vars → split/extract.
+7 -6
View File
@@ -57,12 +57,13 @@ TESTS PRESENT: yes / no
## NORMS TO APPLY (from CLAUDE.md)
- Max 25 lines per function (excluding comments)
- Max 80 chars per line
- Max 5 parameters per function
- Max 5 local variables per function
- No global variables
- Function comments when role is not obvious
The norms are the "Limits" and "Code style" sections of the global CLAUDE.md,
read them there. Function comments when role is not obvious.
Origin first: run `bash ~/.claude/lib/code-origin.sh` on the target repo.
`foreign` → refactor toward the file's own style (granularity, layout,
naming, idioms, "Foreign code: keep its style"), never toward the limits,
unless the dispatch prompt explicitly asks for the limits.
---
+13
View File
@@ -237,6 +237,19 @@ case "$_pm" in
invalid) printf "│ 🔒 %-46s│\n" "push : manual (autopush bad) — ! git push" ;;
esac
unset _pm _gf_lib
# ── code origin: one line inside a git repo, none outside ──
_co_lib="$(dirname "${BASH_SOURCE[0]}")/../lib/code-origin.sh"
if [ -r "$_co_lib" ] && git rev-parse --git-dir >/dev/null 2>&1; then
_co=$(bash "$_co_lib" 2>/dev/null)
case "$_co" in
ours*) _co_line="code : ours (limites strictes)" ;;
foreign*) _co_line="code : foreign (${_co#foreign })" ;;
*) _co_line="" ;;
esac
[ -n "$_co_line" ] && printf "│ 📐 %-44s│\n" "${_co_line:0:44}"
unset _co _co_line
fi
unset _co_lib
if [ -n "$GRAPHIFY_HINT" ]; then
printf "│ 🕸️ %-44s│\n" "${GRAPHIFY_HINT:0:44}"
printf "│ %-40s│\n" "→ /graphify (AST, seconds) — you decide"
+60
View File
@@ -0,0 +1,60 @@
#!/usr/bin/env bash
# code-origin.sh [path] — is this repo our code or foreign code?
# Prints one line: "ours <reason>" or "foreign <reason>". Always rc 0.
#
# Order: `git config claude.origin` (ours|foreign, set by hand per clone, never
# committed to a third-party repo), then the root commit's author against
# user.email and every global `claude.identity`. Undecidable → foreign.
set -u
repo="${1:-.}"
_git() { git -C "$repo" "$@"; }
_override() {
local value
value="$(_git config --get claude.origin 2>/dev/null)" || return 1
case "$value" in
ours|foreign) echo "$value override claude.origin" ;;
*) echo "foreign claude.origin '$value' invalid (want ours|foreign)" ;;
esac
}
_oldest_root() {
_git rev-list --max-parents=0 --reverse HEAD 2>/dev/null | sed -n 1p
}
_our_emails() {
_git config --get user.email 2>/dev/null
git config --global --get-all claude.identity 2>/dev/null
}
_is_ours() {
local email="$1" mine
while IFS= read -r mine; do
[ -n "$mine" ] || continue
[ "$(echo "$mine" | tr '[:upper:]' '[:lower:]')" = \
"$(echo "$email" | tr '[:upper:]' '[:lower:]')" ] && return 0
done < <(_our_emails)
return 1
}
_root_verdict() {
local root email author year
_git rev-parse --git-dir >/dev/null 2>&1 \
|| { echo "foreign not a git repository"; return; }
[ "$(_git rev-parse --is-shallow-repository 2>/dev/null)" = true ] \
&& { echo "foreign shallow clone, root unknown"; return; }
root="$(_oldest_root)"
[ -n "$root" ] || { echo "foreign no commit, root unknown"; return; }
email="$(_git log -1 --format=%ae "$root")"
author="${email%%@*}"
year="$(_git log -1 --format=%ad --date=format:%Y "$root")"
if _is_ours "$email"; then
echo "ours root $author $year"
else
echo "foreign root $author $year"
fi
}
_override || _root_verdict
+7
View File
@@ -575,6 +575,13 @@ gitflow_delete feature/np >/dev/null 2>&1
git config --unset gitflow.autopush
chk "T24i gitflow.autopush=false → remote copy untouched" 'git ls-remote --exit-code --heads origin feature/np >/dev/null 2>&1'
echo "T25 — CLI start outside a git repo fails"
nogit="$WORK/nogit"; mkdir -p "$nogit"; cd "$nogit" || exit 1
# shellcheck disable=SC2034 # read by the eval in chk
nogit_out="$(bash "$HERE/gitflow.sh" start chore x 2>&1)"; nogit_rc=$?
chk "T25a rc non-zero" "[ $nogit_rc -ne 0 ]"
chk "T25b names the missing base" 'printf "%s" "$nogit_out" | grep -q "base .develop. missing"'
echo
echo "==== RESULT: $PASS passed, $FAIL failed ===="
[ "$FAIL" -eq 0 ]
+57
View File
@@ -0,0 +1,57 @@
#!/usr/bin/env bash
# lib/tests/code-origin.test.sh — code-origin.sh verdicts: override, root
# author, extra identities, undecidable cases. Hermetic: global git config is
# pointed at a temp file and nothing is pushed.
set -u
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
L="$ROOT/lib/code-origin.sh"
pass=0; fail=0
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
export GITFLOW_NO_PUSH=1 GIT_CONFIG_SYSTEM=/dev/null
export GIT_CONFIG_GLOBAL="$tmp/gitconfig"
git config --global user.name me
git config --global user.email me@mine.test
# mkrepo <name> <author-email> <date> → repo with one root commit
mkrepo() {
git init -q "$tmp/$1"
GIT_AUTHOR_EMAIL="$2" GIT_AUTHOR_DATE="$3" GIT_COMMITTER_DATE="$3" \
git -C "$tmp/$1" commit -q --allow-empty -m root
}
verdict() { bash "$L" "$tmp/$1"; }
# --- root author ---
mkrepo mine me@mine.test "2024-05-01T10:00:00"
check T1-own-root "$(verdict mine)" "ours root me 2024"
mkrepo other n.turpault@corp.test "2009-02-25T10:00:00"
check T2-foreign-root "$(verdict other)" "foreign root n.turpault 2009"
mkrepo upper ME@Mine.Test "2024-05-01T10:00:00"
check T3-email-case "$(verdict upper | cut -d' ' -f1)" ours
# --- extra identity ---
mkrepo second old@work.test "2020-01-01T10:00:00"
check T4-before-identity "$(verdict second | cut -d' ' -f1)" foreign
git config --global --add claude.identity old@work.test
check T5-after-identity "$(verdict second | cut -d' ' -f1)" ours
# --- override ---
git -C "$tmp/other" config claude.origin ours
check T6-override-ours "$(verdict other)" "ours override claude.origin"
git -C "$tmp/mine" config claude.origin foreign
check T7-override-foreign "$(verdict mine)" "foreign override claude.origin"
git -C "$tmp/mine" config claude.origin bogus
check T8-override-invalid "$(verdict mine)" \
"foreign claude.origin 'bogus' invalid (want ours|foreign)"
# --- undecidable → foreign, said out loud ---
mkdir "$tmp/plain"
check T9-no-git "$(verdict plain)" "foreign not a git repository"
git init -q "$tmp/empty"
check T10-no-commit "$(verdict empty)" "foreign no commit, root unknown"
git clone -q --depth 1 "file://$tmp/second" "$tmp/shallow" 2>/dev/null
check T11-shallow "$(verdict shallow)" "foreign shallow clone, root unknown"
printf 'PASS=%s FAIL=%s\n' "$pass" "$fail"; [ "$fail" -eq 0 ]
+3 -1
View File
@@ -277,7 +277,9 @@ Then offer to capitalize (per CLAUDE.md): recurring finding patterns →
FIRST, then check changed code against those norms. This repo's:
≤25 logic lines/function, ≤80 chars/line, ≤5 params, ≤5 locals, no
global state, intent-not-mechanics comments, explicit naming, versioned
APIs (`/api/v1/`), no-SPA-for-public-sites, security defaults.
APIs (`/api/v1/`), no-SPA-for-public-sites, security defaults. Foreign
code (`bash ~/.claude/lib/code-origin.sh` → foreign): measure consistency
with the existing style instead of the limits; security stays strict.
- **deadcode** — dead/zombie introduced OR orphaned by the delta: unused
functions/exports/imports/vars, unreachable branches, stale feature
flags, commented-out blocks, references to deleted files/symbols
+4
View File
@@ -16,6 +16,10 @@ model). The scope you name is the only reflection; the agent applies norms.
and dispatch on the new branch; on a working branch dispatch in place. Never
`gitflow finish` — integration is human-gated.
Origin: `bash ~/.claude/lib/code-origin.sh` on the target repo. `foreign` →
the refactorer aligns on the existing style, not on the limits, unless the
user asked for the limits explicitly; add that to the prompt below.
```
Agent(subagent_type="refactorer")
prompt: "Refactor to strict project norms, preserving external behavior
+2 -1
View File
@@ -1,5 +1,6 @@
[
{"id": 1, "prompt": "Refactor src/services/payment.ts — it's gotten messy", "expected": "Read file, apply project norms (≤25 logic lines/function, ≤80 chars/line, ≤5 params), preserve external behavior, run tests after"},
{"id": 2, "prompt": "Refactor the calculateOrderTotal function — too long, too many vars", "expected": "Extract sub-functions, name explicit, no behavior change, verify tests still pass"},
{"id": 3, "prompt": "Clean up the auth module without changing behavior", "expected": "Module-level refactor, dead code removal, consistency, all original public API preserved"}
{"id": 3, "prompt": "Clean up the auth module without changing behavior", "expected": "Module-level refactor, dead code removal, consistency, all original public API preserved"},
{"id": 4, "prompt": "Refactor src/net.c in this third-party repo (code-origin says foreign)", "expected": "Align on the file's own style (inline branches, existing variables and macros), no extraction toward the 25-line limit unless the user asks for the limits"}
]