Skip to content

Latest commit

 

History

History
177 lines (140 loc) · 10.6 KB

update.md

File metadata and controls

177 lines (140 loc) · 10.6 KB

update.sh

Revision: 1 Last modified: 2026-12-04T17:48:16Z

Overview

The rebase-based, recursive update of a constitution checkout and every submodule below it, at every depth.

Run it when: you want each repository in the tree brought onto its upstream branch while keeping your local commits on top. It is the "make my whole tree current without losing my work" tool.

What it explicitly is not: it never pushes, never forces, never deletes a branch, and takes no network action other than git fetch (plus git submodule update --init for a submodule that is not yet checked out). It never undoes history — nothing is written to a branch before the result is verified in a throwaway worktree.

The design point that makes it safe: the rebase runs in a temporary detached linked worktree; your branch, index, work tree and stash are untouched while it runs. Only after the result verifies does a single, uninterruptible publish step move the branch (compare-and-swap) and switch the tree, re-applying your uncommitted changes from a stash entry that is dropped only once it applied.

Do not confuse with update_constitution.sh — see "Related scripts".

Prerequisites

  • bash, git ≥ 2.38 for the merge-commit verification path (git merge-tree --write-tree). On older git, a repository whose local range holds merge commits is SKIPPED rather than risked.
  • flock for the run lock (falls back to a mkdir lock, which offers no protection against a kill -9ed run).
  • timeout/gtimeout optional; without them a built-in watchdog is used, which cannot reap helper processes git started.
  • A clean-enough repository: no rebase/merge/cherry-pick/revert in progress, no unmerged files, no held index.lock.
  • Env vars (NAMES only):
    • UPDATE_STATE_DIR (default ${XDG_STATE_HOME:-~/.local/state}/constitution-update)
    • UPDATE_BRANCH (default main)
    • UPDATE_TIMEOUT (default 60)
    • UPDATE_OWN_ORGS (default vasic-digital helixdevelopment helix helixconstitution)
    • NO_COLOR — same as --no-color.

Usage examples

# Update the checkout this script lives in, and every submodule recursively
bash scripts/update.sh

# A specific repository
bash scripts/update.sh --root /path/to/checkout

# From a consuming project, targeting the embedded constitution
bash submodules/constitution/scripts/update.sh --root submodules/constitution

# See exactly what WOULD happen: no fetch, no lock, no snapshot, no change anywhere
bash scripts/update.sh --dry-run

# Offline: use the remote-tracking refs already present
bash scripts/update.sh --no-fetch

# Narrow the blast radius
bash scripts/update.sh --only submodules/helix_deps --max-depth 2

# Print the commands that would restore every repo of a saved snapshot (runs nothing)
bash scripts/update.sh --restore ~/.local/state/constitution-update/<snapshot>.jsonl

What you should see: one progress line per repository, then a final table with counts for UPDATED / UP-TO-DATE / REBASED / CONFLICT / SKIPPED / FAILED / INTERRUPTED, the parent gitlinks that moved, and the elapsed time. --dry-run prints WOULD-REBASE / WOULD-UPDATE — and prints them only when the verification in the throwaway worktree passed.

Exit codes

Code Meaning
0 Every repository updated or already current. A SKIPPED does not fail the run.
1 At least one FAILED (e.g. every remote unreachable, rebase could not start, a git read that decides a step failed, an unreadable .gitmodules, an unsearchable submodule directory) and no conflict
2 Usage error
3 At least one CONFLICT (rebase conflict, or re-applying your uncommitted changes conflicted)
4 Another update run holds the lock
130 Interrupted (INT/TERM/HUP)

The exit code answers "did the run complete", not "is everything current". A caller that must know whether a specific repository is current reads its progress line or the skipped= count — never the exit code. This asymmetry is deliberate: exit 1 is reserved for FAILED, a step this script could not decide or complete.

SKIPPED covers: an operation in progress, unmerged files, a held index.lock, a detached HEAD that would need a rebase, a third-party pin left where it is, a submodule left uninitialised, and an uncommitted edit to a file marked assume-unchanged.

Edge cases

  • assume-unchanged edits. A local edit to a file marked assume-unchanged is invisible to git status/diff/stash, so no stash entry could hold it. It is detected by comparing the file with the index; the repository is SKIPPED, state unchanged, exit 0.
  • A git read that cannot answer is FAILED, never "no". An unreadable .gitmodules or an unsearchable submodule directory is never taken as "no submodules" / "not checked out" (see tests/update/READS.md).
  • Third-party submodules are only moved to the commit the parent records (the pinned ref), never to upstream. --init-third-party overrides the initialisation refusal.
  • Nested submodules are initialised only where helix-deps.yaml allows (recursive_init: false / third_party: true are honoured, §11.4.28(C)), and never inside a third-party submodule.
  • A detached HEAD follows upstream main (or master). Only commits on no remote ref count as local, and a HEAD sitting on another remote branch is only fast-forwarded — SKIPPED when it would need a rebase, because no copies of pushed commits are made.
  • Own-org classification. A bare --own-org name must equal an ORG (namespace) segment of the declared URL — never the repository name. org/repo matches a run of whole segments. A submodule marked third_party: true in helix-deps.yaml is never own-org, whatever the flag says.
  • Merge commits in your local range. When the local range holds merge commits, the rebased tree is compared with a plain merge (git merge-tree --write-tree). A rebase that would drop manual changes made inside a local merge commit is never published.
  • A commit another git command makes during the publish step is reported as CONFLICT with the work tree left as it is — it would otherwise record your pre-update files on top of the update.
  • kill -9 is not covered. What it leaves behind depends on where it lands: only the script dying leaves the publish step to complete (and a second run refused with exit 4 until it does); the publish child dying before the branch moved changes nothing; dying after the move but before your changes were cleared leaves HEAD at the result with the OLD files staged; dying after the clear leaves your changes only in git stash list (stash@{0}, message update.sh: autostash before updating to ...) — run git stash pop.
  • After a clean run there is nothing to push unless you had local work. A parent whose submodule pointer moved shows the gitlink modified: commit it only when the pointer is a pushed commit that is not older than the one recorded — the report says when it is not.

Internal behaviour

Per repository, root first then every submodule recursively:

  1. Refuse to touch a repository with an operation in progress (rebase/merge/cherry-pick/revert), unmerged files, a held index.lock, or an assume-unchanged edit in a file it needs to update.
  2. Fetch every remote — the branch's configured remote first, then origin, then the rest — each under a timeout. The freshest remote that has the branch wins (a descendant of the others); a diverging one is reported.
  3. Snapshot HEAD into a safety snapshot (JSON Lines, mode 0600) after the fetch, before the first change. --restore FILE prints — never runs — the commands that would put every repository back.
  4. Own-org repositories are brought onto the upstream branch by verify-before-rewrite: a. git rebase --rebase-merges <remote>/<branch> in a temporary detached linked worktree (rebase.updateRefs off, merge backend); b. verify there: no conflict, no stop (a refusing hook, a failed signature), merge-commit tree equality when applicable, your uncommitted changes re-applied cleanly, no untracked file the update would overwrite; c. one publish step in its own process group: stash your changes (after re-checking they still equal what was verified), move the branch with git update-ref HEAD <result> <old> before touching the work tree, check HEAD still is <result>, re-check your changes, git read-tree -m -u <old> <result>, re-apply from the stash, drop it only after it applied, check HEAD again. A failed switch moves the branch back with another compare-and-swap and restores your changes.
  5. Report CONFLICT with the exact files and the command to resolve, and continue with the other repositories.
  6. Print the progress line and the final table.
  7. Run scripts/post_update_hook.sh of the constitution checkout when its HEAD changed — a failing hook never fails the update.

Any failure in 4a or 4b leaves the repository exactly as it was (CONFLICT or FAILED, "nothing changed").

Flags

--root DIR, --dry-run, --no-fetch, --only PATH, --timeout SEC, --own-org PATTERN (repeatable), --no-init, --init-third-party, --max-depth N (default 8), --no-hook, --no-color, --quiet, --verbose, --restore FILE, --help.

Related scripts

  • scripts/update_constitution.sh — the other updater, and the one most often confused with this. It updates only the constitution submodule, accepts only a fast-forward, never rebases, targets the newest helixconstitution-v* release tag by default, stages the new gitlink in the project, and runs the project's post-update hook. It is the low-risk path. update.sh is the rebase-everything path.
  • scripts/post_update_hook.sh — invoked at the end when the constitution's HEAD moved.
  • scripts/lib/constitution_layout.sh — defines CL_GIT_SCRUB_VARS, the git variables this script unsets first.
  • scripts/install.sh / scripts/setup.sh — install paths, not update paths.
  • docs/guides/update_script.md — the full operator guide.

Last verified

2026-12-04 — source read (1848 lines), bash -n clean, flag set and exit codes read from the script's own argument parser and documented contract:

$ bash -n scripts/update.sh && echo PARSE-OK
PARSE-OK

$ grep -n 'UPDATE_STATE_DIR\|UPDATE_BRANCH\|UPDATE_TIMEOUT\|UPDATE_OWN_ORGS' scripts/update.sh | head -4

Not executed in this pass: it rewrites branches in a real tree. To try it safely, use a throwaway clone and --dry-run first, which performs the same verification with no fetch, no lock, no snapshot and no change.