Revision: 1 Last modified: 2026-12-04T18:15:21Z
This is a library. It is sourced, never executed. Mode rw-rw-r--, and it is written to POSIX sh
(# shellcheck shell=sh) so it works under sh, dash and bash.
It is the one resolver for two questions: where is the constitution? and where is the consuming project?
Every hub script that needs either path must resolve it here rather than hard-coding <project>/constitution.
Three layouts are supported:
submodules/constitution— the standard for new installs (§11.4.28 dependency layout).constitution— the legacy layout, still fully supported.- any other path a project's
.gitmodulesdeclares, found by content, not by name.
It also handles a case that used to produce false alarms: the canon working on itself. When you run inside the
constitution checkout, the project root is the constitution, so demanding a nested submodules/constitution
was both a false positive and contrary to §11.4.28(C) (a nested constitution is forbidden).
And it scrubs the ambient git environment: GIT_DIR, GIT_WORK_TREE and friends choose which repository
git -C <path> really reads and writes, and GIT_DIR beats -C. Left over from another repository, they
silently redirect every answer.
- POSIX shell,
awk,dirname,basename,pwd. gitis optional — needed only for the superproject /.gitmodules/ gitlink probes andcl_git_hooks_dir. Each function degrades to a non-git path whengitis absent.- Optional input:
CONSTITUTION_DIR— an explicit absolute override, honoured only bycl_find_in_project(never by the_no_overridevariant).
. "$CONSTITUTION/scripts/lib/constitution_layout.sh"
# drop the ambient repository-selecting git environment (call FIRST in an entry point)
cl_scrub_git_env # or: cl_scrub_git_env quiet
root="$(cl_project_root_of "$const_dir")" # the consuming project's root
dir="$(cl_find_in_project "$project_root")" # the constitution inside a project
dir="$(cl_find_in_project_no_override "$wt")" # same, ignoring $CONSTITUTION_DIR
cl_is_constitution_dir "$dir" && echo "is a checkout"
cl_is_constitution_self "$dir" && echo "the canon itself"
rel="$(cl_rel_path "$project_root" "$dir")" # e.g. submodules/constitution
owner="$(cl_owner_project_of "$const_dir")" # the project that OWNS this checkout
hooks="$(cl_git_hooks_dir "$project_root")" # honours worktrees and core.hooksPath
cl_is_git_hook_name post-merge && echo "a real hook event"
cl_same_dir "$a" "$b" && echo "same directory"Every resolver prints one path on stdout and returns 0, or prints nothing and returns 1 when it
cannot resolve. No function ever guesses: 1 means "not found", never "assume".
| Function | Contract |
|---|---|
cl_scrub_git_env [quiet] |
unsets CL_GIT_SCRUB_VARS in the current shell; prints one stderr notice naming the dropped variables (never values) unless quiet or CL_GIT_SCRUB_QUIET=1; always returns 0 |
cl_is_constitution_dir DIR |
0 iff DIR/Constitution.md exists and DIR/scripts is a directory |
cl_is_constitution_self DIR |
0 iff DIR is the canon itself: the triad (Constitution.md + CLAUDE.md + AGENTS.md) + scripts/, and DIR is the top level of a git work tree |
cl_project_root_of CONST_DIR |
superproject (when CONST_DIR lives under it) → strip /submodules/constitution → strip /constitution → else the parent directory |
cl_owner_project_of CONST_DIR |
the project that owns the checkout: submodule → superproject; plain vendored copy in a work tree → that top level; standalone clone or no git → 1 (no owner) |
cl_find_in_project ROOT |
$CONSTITUTION_DIR if it is a constitution dir, else cl_find_in_project_no_override |
cl_find_in_project_no_override ROOT |
self → gitlink layout (mode 160000) → plain layout (standard first) → any .gitmodules path that is a constitution dir; else 1 |
cl_rel_path ROOT DIR |
DIR relative to ROOT when under it, else DIR unchanged |
cl_git_hooks_dir ROOT |
the absolute hooks dir git really uses (--path-format=absolute, else a fallback with $1/ prefixing); 1 if git is absent or cannot answer |
cl_is_git_hook_name NAME |
0 iff NAME is a git hook event per githooks(5) |
cl_same_dir A B |
0 iff the two physical paths are equal |
Set by the library: CL_GIT_SCRUB_VARS, CONSTITUTION_LAYOUT_*_REL.
CONSTITUTION_DIRset to something that is not a constitution dir — ignored (falls through to the normal search), rather than answering with a wrong path.- A mid-migration tree holding both a vendored plain copy and the real submodule — the gitlink (mode 160000) wins over every plain copy, so the stale copy cannot win by being checked first.
- A submodule name containing spaces — handled:
git config -zoutput is transposed (LF→TAB,NUL→LF) so splitting at the first space would not work and is not attempted. A name containing a TAB, or a path containing a newline, is skipped, never guessed. - A path holding glob characters (
v*) — the search doesset -fso a path is treated as a name, never as a glob. - A directory that merely contains
Constitution.md(a copy, a fixture, an unpacked archive) is notcl_is_constitution_self: the git-top-level test is what separates the canon from an arbitrary directory, and claiming otherwise would be a false positive (§11.4.201(1)). cl_is_constitution_diris deliberately permissive (noCLAUDE.md/AGENTS.mdrequirement) because every layout fixture builds a constitution dir fromConstitution.md+scripts/only. The stronger triad test belongs to the self claim.GIT_CEILING_DIRECTORIESandGIT_DISCOVERY_ACROSS_FILESYSTEMare deliberately NOT scrubbed — they never point git at another repository, they only bound discovery, and clearing a caller's ceiling widens discovery past a fence the caller set on purpose (a measured review finding: the installer then planned an install into the enclosing repository). Configuration (GIT_CONFIG_*), identity, transport/auth (GIT_SSH*,GIT_ASKPASS,SSH_AUTH_SOCK) andGIT_TEMPLATE_DIRare likewise left alone.cl_scrub_git_envrestoresIFSaround its word split, because the resolver itself runs withIFS=newlineand a leakedIFSwould change every later expansion.- Callers that must run as lone copies (
scripts/post_update_hook.sh,scripts/hooks/post-merge,scripts/hooks/constitution-submodule-post-checkout) carry the same variable list inline, andtests/layout/test_round8_git_env.shasserts the four lists are identical and that every git-calling script carries a scrub.
cl_scrub_git_enviteratesCL_GIT_SCRUB_VARS, testing each witheval "[ -n \"\${V+x}\" ]"so an unset and an empty variable are handled the same way, unsets the present ones, collects their names, and prints one notice.cl__gitruns( cl_scrub_git_env quiet; exec git "$@" )— a subshell, so the caller's environment is untouched and no notice is printed. Every git call inside this file goes through it.cl_find_in_project_no_overridefirst askscl_is_constitution_self(self), then builds a candidate list from the two named layouts pluscl__gitmodules_paths, prefers the candidate that is a gitlink (checked withgit ls-files -sunder--literal-pathspecs, matching mode160000exactly), then falls back to the first plain candidate, then to any.gitmodulespath.cl_project_root_ofprefers the logical superproject form when the logical path ends in a known layout, so a symlinked parent keeps the path it was reached by.cl_git_hooks_dirprefersgit rev-parse --path-format=absolute --git-path hooksand falls back to the relative form with$1/prefixed, so worktrees and submodules (which have a.gitfile) resolve correctly.
scripts/find_constitution.sh— the walk-up locator for code running inside a nested submodule; it carries its own copy of the two-layout list (a tracked, not-yet-unified duplication, noted in the header).scripts/post_update_hook.sh— carries an inlined scrub list andpu_owner_of(checked for agreement bytests/layout/test_round7_ambient.sh).scripts/hooks/post-merge,scripts/hooks/constitution-submodule-post-checkout— lone copies, same list.scripts/lib/default_scan_root.sh— the scanner-gate analogue of "do not depend on the caller's cwd".tests/layout/test_constitution_layout_lib.sh,tests/layout/test_round8_git_env.sh,tests/layout/test_round7_ambient.sh— the suites that bind these guarantees.
2026-12-04 — bash -n scripts/lib/constitution_layout.sh → exit 0 (a library: sourced, not executed).
Behaviour a reader can repeat in this checkout (the canon working on itself):
sh -c '. scripts/lib/constitution_layout.sh; cl_is_constitution_self "$PWD" && echo self-ok; \
cl_find_in_project "$PWD"; cl_scrub_git_env quiet; echo scrub-rc=$?'Expected: self-ok, then this directory's absolute path, then scrub-rc=0.