Skip to content

Latest commit

 

History

History
147 lines (116 loc) · 9.29 KB

constitution_layout.md

File metadata and controls

147 lines (116 loc) · 9.29 KB

constitution_layout.sh

Revision: 1 Last modified: 2026-12-04T18:15:21Z

Overview

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 .gitmodules declares, 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.

Prerequisites

  • POSIX shell, awk, dirname, basename, pwd.
  • git is optional — needed only for the superproject / .gitmodules / gitlink probes and cl_git_hooks_dir. Each function degrades to a non-git path when git is absent.
  • Optional input: CONSTITUTION_DIR — an explicit absolute override, honoured only by cl_find_in_project (never by the _no_override variant).

Usage examples

. "$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"

Exit codes / return values

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.

Edge cases

  • CONSTITUTION_DIR set 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 -z output 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 does set -f so 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 not cl_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_dir is deliberately permissive (no CLAUDE.md/AGENTS.md requirement) because every layout fixture builds a constitution dir from Constitution.md + scripts/ only. The stronger triad test belongs to the self claim.
  • GIT_CEILING_DIRECTORIES and GIT_DISCOVERY_ACROSS_FILESYSTEM are 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) and GIT_TEMPLATE_DIR are likewise left alone.
  • cl_scrub_git_env restores IFS around its word split, because the resolver itself runs with IFS=newline and a leaked IFS would 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, and tests/layout/test_round8_git_env.sh asserts the four lists are identical and that every git-calling script carries a scrub.

Internal behaviour

  1. cl_scrub_git_env iterates CL_GIT_SCRUB_VARS, testing each with eval "[ -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.
  2. cl__git runs ( 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.
  3. cl_find_in_project_no_override first asks cl_is_constitution_self (self), then builds a candidate list from the two named layouts plus cl__gitmodules_paths, prefers the candidate that is a gitlink (checked with git ls-files -s under --literal-pathspecs, matching mode 160000 exactly), then falls back to the first plain candidate, then to any .gitmodules path.
  4. cl_project_root_of prefers the logical superproject form when the logical path ends in a known layout, so a symlinked parent keeps the path it was reached by.
  5. cl_git_hooks_dir prefers git rev-parse --path-format=absolute --git-path hooks and falls back to the relative form with $1/ prefixed, so worktrees and submodules (which have a .git file) resolve correctly.

Related scripts

  • 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 and pu_owner_of (checked for agreement by tests/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.

Last verified

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.