Skip to content

Latest commit

 

History

History
125 lines (89 loc) · 5.53 KB

sync_step_lib.md

File metadata and controls

125 lines (89 loc) · 5.53 KB

sync_step_lib.sh

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

Overview

This is a library. It is sourced, never executed. Mode rw-rw-r--.

scripts/sync/sync_step_lib.sh is the one integration seam for upstream syncing. It defines a single function, sync_step MODE PROJECT_ROOT ENGINE, and it exists so that other entry points do not each have to invent their own way of invoking the sync engine:

  • scripts/setup.sh — first-time and repeat setup.
  • scripts/post_update_hook.sh — STEP 5c, check only.

The point of the seam is that the caller chooses a mode, not a set of engine flags. That keeps the policy ("the post-update hook only looks; setup may integrate") in one reviewed place instead of scattered across callers, and it means an inherited environment variable can never add engine flags.

Prerequisites

  • bash, git.
  • $ENGINE must be a real file — scripts/sync/sync_all_upstreams.sh.
  • $PROJECT_ROOT must be inside a git work tree (the function resolves its top level itself).
  • For MODE=ask: a usable /dev/tty.

Usage examples

. scripts/sync/sync_step_lib.sh

# read-only, no network: the post-update default
sync_step offline "$PROJECT_ROOT" "$CONSTITUTION/scripts/sync/sync_all_upstreams.sh"

# fetch every remote, change nothing else
sync_step check "$PROJECT_ROOT" "$ENGINE"

# fetch and fast-forward; the flag is the consent, no prompt
sync_step apply "$PROJECT_ROOT" "$ENGINE"

# ask on /dev/tty before fetching and before every change
sync_step ask "$PROJECT_ROOT" "$ENGINE"

# do nothing, but tell the operator the exact command to run
sync_step off "$PROJECT_ROOT" "$ENGINE"

# inspect the outcome
echo "rc=$SYNC_STEP_RC verdict=$SYNC_STEP_VERDICT"

The mode contract

MODE Engine arguments Character
off (none — runs nothing) prints how to run it by hand, returns 0
offline --check --no-fetch --non-interactive read-only, no network; the post-update default
check --check --non-interactive fetches every remote, changes nothing else
apply --apply --non-interactive fetches and fast-forwards; the flag is the consent
ask --apply --interactive the engine asks on /dev/tty before fetching and before every change; without a terminal the step is SKIPPED, never run unasked

Every mode also passes --root <git top level> and --quiet.

Exit codes and the state it sets

sync_step returns (does not exit):

Return When
0 everything except the FAILED verdict
1 the engine exited 1 — an --apply integration step failed

It always sets two variables for the caller:

  • SYNC_STEP_RC — the engine's exit code, or 0 for off / a skip.
  • SYNC_STEP_VERDICT — one word from the closed set: OFF, SKIP, CURRENT, UPDATES-AVAILABLE, ATTENTION, FAILED, INCONCLUSIVE, LOCKED, BLIND, INTERRUPTED.

Engine exit → verdict mapping: 0→CURRENT, 3→UPDATES-AVAILABLE, 4→ATTENTION, 1→FAILED, 5→INCONCLUSIVE, 7→LOCKED, 6→BLIND, 8→INTERRUPTED. Exit 2 (usage) and any other code map to BLIND — an uninterpretable answer is reported as blind, never as clean.

Edge cases

  • Unknown mode — prints SKIP, sets SYNC_STEP_VERDICT=SKIP, returns 0. An unrecognised mode never falls through to a default that would act.
  • Engine file missing — SKIP with a message naming the path, returns 0.
  • PROJECT_ROOT not a git work tree — SKIP, returns 0.
  • MODE=ask without a terminal — SKIP with the advice to use check or apply. This is the important one: an interactive intent must never be silently downgraded into an unattended --apply.
  • SYNC_STEP_EXTRA_ARGS in the environment — only the exact pair --out /absolute/dir is honoured; anything else is ignored with a printed note. An inherited variable therefore cannot inject engine flags. (This is a test seam, added after a review finding.)
  • off never runs the engine at all, so it is safe in any environment.

Internal behaviour

  1. Defaults: SYNC_STEP_RC=0, SYNC_STEP_VERDICT=OFF.
  2. Switches on MODE to build the engine argument array (off returns immediately with the manual command).
  3. Validates the engine file, then resolves PROJECT_ROOT to its git top level.
  4. For ask, probes /dev/tty and skips when it is not readable.
  5. Applies the SYNC_STEP_EXTRA_ARGS filter described above.
  6. Runs bash "$ENGINE" <args> --root "$top" --quiet "${extra[@]}", capturing the status into SYNC_STEP_RC.
  7. Maps the status to SYNC_STEP_VERDICT and prints one human line; returns 1 only for FAILED.

Related scripts

  • scripts/sync/sync_all_upstreams.sh / sync_all_upstreams.py — the engine this drives.
  • scripts/setup.sh — a caller (may integrate).
  • scripts/post_update_hook.sh — a caller (STEP 5c, check only).
  • scripts/sync/verify_upstream_parity.sh — the read-only parity proof, a different question entirely.
  • docs/scripts/sync_all_upstreams.md — the engine's own guide, including its exit-code table.

Last verified

2026-12-04 — bash -n scripts/sync/sync_step_lib.sh → exit 0 (it is a library, so it is sourced, not run).

Behaviour check a reader can repeat in this checkout:

bash -c '. scripts/sync/sync_step_lib.sh; sync_step off . scripts/sync/sync_all_upstreams.sh; \
  echo "rc=$? verdict=$SYNC_STEP_VERDICT"'

Expected: a [sync-upstreams] off; to fetch every remote … line, then rc=0 verdict=OFF — nothing is fetched.