diff --git a/README.md b/README.md index 0469edc..c28d443 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,9 @@ parallel. All are measured by physical-cluster overlap (`uwt measure`). A hydrated worktree opens without a reimport (0–6 assets imported, against 4,035–23,451 cold) but not without a compile: the first open rebuilds every script assembly and resolves packages, about 400 s on the production project -with four editors opening at once, most of it contention between them. +with four editors opening at once, most of it contention between them, and +about a minute for one editor alone on the test projects. Seed from a clean +checkout: uncommitted edits in the donor reimport in every worktree. Its cost is about one 4 KiB cluster per file, so it is close to fixed per project and the saving grows with project size. After the first open it grows slowly, except that on Unity 6.6+ a texture or model reimport @@ -71,7 +73,8 @@ uwt=/scripts/unity-worktree-cow.sh $uwt doctor # proves this volume block-clones; run first $uwt seed # snapshot project + Library into .git/uwt/golden $uwt new feature/x # git worktree add + clone + hydrate, ~seconds -$uwt hydrate path/to/wt # give a worktree someone else created a Library +$uwt hydrate path/to/wt # give a worktree someone else created a Library; + # safe to re-run, --json for setup scripts $uwt list --json # every worktree, and where its Library came from $uwt remove path/to/wt # git worktree remove, refused while an editor has it open # (--delete-branch: also the branch, if it holds no work) diff --git a/skills/unity-worktree-cow/SKILL.md b/skills/unity-worktree-cow/SKILL.md index 68dd894..49b433c 100644 --- a/skills/unity-worktree-cow/SKILL.md +++ b/skills/unity-worktree-cow/SKILL.md @@ -42,7 +42,16 @@ that is the editors contending with each other, not the clone: on the 41k-file project one editor alone spends 39 s in `CompileScripts` and 7 s in `ImportOutOfDateAssets`, four at once 160–168 s and 17–41 s. Still much faster than a cold import, but budget minutes, not seconds, and stagger first opens -when disk or CPU is tight. +when disk or CPU is tight. As a range: 42–66 s for one editor alone on the +test projects, up to ~7 minutes with four at once on the production one. + +The 0–6 figure assumes a golden seeded from a clean checkout. The Library is +imported from the donor's working tree, so whatever the worktree does not +share with it reimports on first open: uncommitted edits in the donor (`seed` +warns about them), a branch that differs, changed packages. Seed from a clean +checkout near the branches you will work on. `Search` is denied too, so the +editor rebuilds its index in the background after the refresh; that is +separate from the first-open times above. The first open is also where the private cost comes from. The largest item is usually `PackageCache`: Unity re-creates a fixed set of large package binaries @@ -96,6 +105,25 @@ worktree isolation, such as Claude Code's `isolation: worktree`): $uwt hydrate path/to/that/worktree ``` +The two differ in what they share. `new` clones the whole snapshot, so source +and art files share blocks with the golden as well as the Library. `hydrate` +only adds the Library: the worktree's files are the ones its creator checked +out, and cost their full size. + +`hydrate` is safe to run every time, which is what an agent's worktree setup +script needs (Codex's, for one): a worktree that already has a `Library/` is +left alone and the exit code is 0. With `--json` the script can tell the +outcomes apart: + +| Outcome | Record | +|---|---| +| Library cloned from the golden | `ok`, `value.action` = `hydrated` | +| A Library was already there | `ok`, `value.action` = `kept`; `value.library` is `uwt` or `other` | +| No snapshot yet | `error`, exit 2, `reason` = `no_golden` | +| Editor has the project open | `error`, exit 3, `reason` = `editor_busy` | +| Volume cannot clone | `error`, exit 2, `reason` = `unsupported_volume` | +| Worktree and golden on different volumes | `error`, exit 2, `reason` = `different_volume` | + ## Commands | command | what it does | @@ -103,7 +131,7 @@ $uwt hydrate path/to/that/worktree | `doctor [PATH]` | Proves this volume block-clones by doing one and reading the refcount back. Checks the golden and cache root are on the project's volume, which open-file check the interlock will use, whether an editor is live, and the golden's state. Warns on Windows when long paths are off. | | `seed [PATH] [--force]` | Snapshots the working tree and `Library/` (minus the deny list) plus the git index. Refuses while an editor or Data Store has the project open. Cheap; re-run after editor upgrades. | | `new BRANCH [--path DIR] [--base REF] [--inherit-untracked] [--init-submodules] [--verify] [--json]` | `git worktree add --no-checkout`, clone the snapshot, hand git the snapshot's index, `reset --hard` to the branch, verify zero tracked differences, clean inherited untracked files, hydrate `Library/`. Refuses before registering anything if the volume cannot clone, or if it folds case and the branch has paths that collide by case. Rolls back on failure. | -| `hydrate [PATH] [--force]` | Gives an existing worktree a `Library/` cloned from the golden. | +| `hydrate [PATH] [--force] [--json]` | Gives an existing worktree a `Library/` cloned from the golden. One that already has a `Library/` is left alone (exit 0); `--force` replaces it, and keeps the old one until the new one is in place. | | `list [PATH] [--json]` | Every worktree of the repo, including ones uwt did not create, with its branch, lock state, and whether its `Library/` came from uwt (`uwt`), from an editor (`other`), or is absent (`none`). | | `measure [PATH...] [--by N] [--json]` | Private disk of each worktree and the golden against all the others, by physical-block overlap, with the largest private groups. Read-only. See "Measuring it yourself". | | `remove PATH [--force] [--delete-branch] [--json]` | `git worktree remove` behind the editor interlock. A dirty worktree needs `--force`; a locked one needs `git worktree unlock` first. The branch is kept, or with `--delete-branch` deleted as `git branch -d` would, only when it holds no work of its own. Refuses the main worktree. | @@ -117,10 +145,14 @@ different volume from the repo (the golden must be on the worktrees' volume). Exit codes: `0` ok · `2` failure · `3` refused by the editor interlock. -For agents, `list`, `new`, `remove` and `measure` take `--json`: the human output moves to +For agents, `list`, `new`, `hydrate`, `remove` and `measure` take `--json`: the human output moves to stderr and stdout carries exactly one record, either `{"status":"ok","command":"new","value":{...}}` or -`{"status":"error","exit":2,"message":"..."}`. `new`'s value has `path`, +`{"status":"error","exit":2,"reason":"no_golden","message":"..."}`. `reason` +is there when the failure is one a script can act on: `no_golden`, +`editor_busy`, `unsupported_volume`, `different_volume`, `other_project`. +`hydrate`'s value has `project`, `action`, `library`, `build_target` and +`seconds`. `new`'s value has `path`, `project`, `uwt` (this script's absolute path), `branch`, `head`, `seconds`, `verified` and `submodules_uninitialized`; `remove`'s has `path`, `branch` and `branch_deleted`. @@ -213,10 +245,17 @@ retry. **uwt never closes the editor for you**: `unity close` exits without saving. **Worktrees are reset to their branch.** Uncommitted edits in the checkout you -seeded from do not carry over. That includes `ProjectSettings/ProjectVersion.txt`: +seeded from do not carry over, and because the Library was imported from them, +each worktree reimports those assets on first open. That includes `ProjectSettings/ProjectVersion.txt`: if the running editor is newer than the committed version, commit the bump before seeding, or every worktree declares the old version and hydrate warns. +**Seed on the platform you build for.** The golden carries the active build +target, so every worktree opens on it. That saves a platform switch when the +target is the one you build, and costs one in every worktree when it is not +(a golden left on Android, worktrees building for macOS). `seed`, `hydrate` +and `doctor` print the golden's build target. + **Re-seed after an editor upgrade.** A Library from another editor version is usable (Unity upgrades or reimports it, exactly as when a single checkout switches branches) but the reimport lands as private disk. `hydrate` warns diff --git a/skills/unity-worktree-cow/scripts/unity-worktree-cow.sh b/skills/unity-worktree-cow/scripts/unity-worktree-cow.sh index e98ac33..81042fd 100644 --- a/skills/unity-worktree-cow/scripts/unity-worktree-cow.sh +++ b/skills/unity-worktree-cow/scripts/unity-worktree-cow.sh @@ -69,16 +69,18 @@ fail() { printf ' %sx%s %s\n' "$CR" "$C0" "$*"; } die() { fail "$*"; json_fail $EXIT_FAIL "$*"; exit $EXIT_FAIL; } refuse(){ fail "$*"; json_fail $EXIT_REFUSED "$*"; exit $EXIT_REFUSED; } -# --json (list, new, remove): the human output above moves to stderr and fd 3 -# carries exactly one JSON record, success or failure, for agent callers. -JSON=0 +# --json (list, new, hydrate, remove, measure): the human output above moves to +# stderr and fd 3 carries exactly one JSON record, success or failure, for +# agent callers. REASON, set for the one call (REASON=x die ...), gives a +# failure a name a script can branch on. +JSON=0; REASON="" json_str() { local s="$1" s="${s//\\/\\\\}"; s="${s//\"/\\\"}"; s="${s//$'\n'/\\n}"; s="${s//$'\r'/\\r}"; s="${s//$'\t'/\\t}" printf '"%s"' "$s" } json_ok() { [ "$JSON" = 1 ] && printf '{"status":"ok","command":"%s","value":%s}\n' "$1" "$2" >&3; return 0; } -json_fail() { [ "$JSON" = 1 ] && printf '{"status":"error","exit":%s,"message":%s}\n' "$1" "$(json_str "$2")" >&3; return 0; } +json_fail() { [ "$JSON" = 1 ] && printf '{"status":"error","exit":%s,%s"message":%s}\n' "$1" "${REASON:+\"reason\":\"$REASON\",}" "$(json_str "$2")" >&3; return 0; } now() { date +%s; } stamp(){ date -u +%Y-%m-%dT%H:%M:%SZ; } @@ -274,6 +276,45 @@ unity_version() { } golden_field() { grep -m1 "^$2=" "$1.txt" 2>/dev/null | cut -d= -f2-; } +# The active build target a Library opens on, e.g. "Android"; nothing when it +# cannot be read. It travels with the golden on purpose (no platform switch on +# first open), so a golden seeded on one platform costs a switch in every +# worktree that builds for another. Library/EditorUserBuildSettings.asset is a +# binary serialized file whose layout moves between Unity versions, so this +# anchors on the one stable landmark: the active and selected platform group +# names, two strings in a row ("Standalone", "Android", ...). Before them sit +# the two group ints, and before those the active target; since Unity 6 it is +# the active target, a 16-byte platform GUID and the selected target. Anything +# that does not fit that shape exactly prints nothing. +build_target() { # build_target + local f="$1/EditorUserBuildSettings.asset"; [ -f "$f" ] || return 0 + od -An -v -tu1 "$f" 2>/dev/null | awk ' + { for (i = 1; i <= NF; i++) b[n++] = $i + 0 } + function le(o) { return b[o] + 256 * (b[o+1] + 256 * (b[o+2] + 256 * b[o+3])) } + function be(o) { return b[o+3] + 256 * (b[o+2] + 256 * (b[o+1] + 256 * b[o])) } + # A length-prefixed name of letters, digits and spaces at o: its padded size, else 0. + function name(o, l, j, c) { + if (o + 4 > n) return 0 + l = le(o); if (l < 2 || l > 40 || o + 4 + l > n) return 0 + for (j = 0; j < l; j++) { c = b[o+4+j]; if (!(c == 32 || (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122))) return 0 } + return 4 + l + (4 - l % 4) % 4 + } + END { + if (n < 64 || be(8) < 22) exit # format 22+: Unity 2020.1 and later + split("2 macOS 5 Windows32 9 iOS 13 Android 19 Windows 20 WebGL 21 UWP 24 Linux 25 XboxOne 31 PS4 37 tvOS 38 Switch 41 LinuxHeadlessSimulation 42 XboxSeries 43 GameCoreXboxOne 44 PS5 45 EmbeddedLinux 46 QNX 47 visionOS 48 Switch2", m, " ") + for (i = 1; i in m; i += 2) T[m[i]] = m[i+1] + for (i = 48; b[i] >= 48 && b[i] <= 57; i++) major = major * 10 + b[i] - 48 # "6000.4.0f1" + for (p = be(36) + 20; p + 8 < n; p += 4) { + s = name(p); if (!s || !name(p + s)) continue + if (le(p - 4) > 63 || le(p - 8) > 63 || !(le(p - 12) in T)) exit + t = le(major >= 6000 ? p - 32 : p - 12); if (t in T) print T[t] + exit + } + }' +} +# A path in git's form for --json: E:/x on Windows. +jpath() { if [ "$OS" = windows ]; then cygpath -m "$1"; else printf '%s' "$1"; fi; } + fs_type() { case "$OS" in windows) df -T "$1" 2>/dev/null | awk 'NR==2{print $2}' ;; # Git Bash df knows refs/ntfs; fsutil needs admin @@ -374,7 +415,14 @@ cow_probe() { # cow_probe head -c 8388608 /dev/urandom > "$p/a.bin" 2>/dev/null # 8 MiB, incompressible local rc=1 why case "$OS" in - darwin) if cp -c "$p/a.bin" "$p/b.bin" 2>/dev/null; then rc=0; why="APFS clonefile"; else why="cp -c rejected: not APFS"; fi ;; + # cp -c succeeding proves nothing: off APFS it falls back to a byte copy. + # Only APFS clones on macOS, so the volume type is the gate; cow_tree's + # cp -Rpc relies on every caller having probed the destination first. + darwin) local t; t="$(fs_type "$d")" + case "$t" in + apfs|APFS) if cp -c "$p/a.bin" "$p/b.bin" 2>/dev/null; then rc=0; why="APFS clonefile"; else why="cp -c failed on APFS"; fi ;; + *) why="volume is ${t:-of unknown type}, not APFS (cp -c would copy bytes there, not clone)" ;; + esac ;; linux) if cp --reflink=always "$p/a.bin" "$p/b.bin" 2>/dev/null; then rc=0; why="reflink ($(fs_type "$d"))" else why="reflink rejected by $(fs_type "$d") (ext4 cannot; XFS needs reflink=1)"; fi ;; windows) mkdir -p "$p/b" @@ -453,7 +501,7 @@ require_editor_closed() { # require_editor_closed fail "refusing to $2: a Unity Editor or Data Store is using $1" printf '%s\n' "$reasons" warn "Save and close the editor, then re-run. uwt never closes it for you." - json_fail $EXIT_REFUSED "refusing to $2: a Unity Editor or Data Store is using $1" + REASON=editor_busy json_fail $EXIT_REFUSED "refusing to $2: a Unity Editor or Data Store is using $1" exit $EXIT_REFUSED fi } @@ -500,7 +548,7 @@ cmd_doctor() { say ""; head_ "golden" if golden_ok "$gd"; then say " $gd" - say " unity=$(golden_field "$gd" unity_version) commit=$(golden_field "$gd" commit | cut -c1-10) created=$(golden_field "$gd" created) files=$(golden_field "$gd" files)" + say " unity=$(golden_field "$gd" unity_version) commit=$(golden_field "$gd" commit | cut -c1-10) created=$(golden_field "$gd" created) files=$(golden_field "$gd" files) build_target=$(build_target "$gd/Library")" [ "$(golden_field "$gd" unity_version)" = "$(unity_version "$proj")" ] || warn "golden was captured under a different editor version than this checkout declares; re-seed after upgrades" else say " none yet - run: uwt seed (with the editor closed)"; fi say ""; head_ "not offered, on purpose" @@ -539,10 +587,10 @@ cmd_seed() { [ -z "$pend" ] || { printf '%s\n' "$pend" | head -5 | sed 's/^/ /' die "$(printf '%s\n' "$pend" | wc -l | tr -d ' ') pending change(s) here (checkouts, adds, moves, deletes or a merge); check them in, shelve or undo them, then seed"; } local dirty; dirty="$(pl_local "$proj" | wc -l | tr -d ' ')" - [ "$dirty" = 0 ] || warn "$dirty local change(s) here (edited, deleted or moved without a checkout, hidden ones included); workspaces are reset to their branch, so these do not carry over" + [ "$dirty" = 0 ] || warn "$dirty local change(s) here (edited, deleted or moved without a checkout, hidden ones included); workspaces are reset to their branch, so these do not carry over, and the Library was imported from them, so each workspace reimports those assets on first open" else local dirty; dirty="$(git -C "$proj" status --porcelain --untracked-files=no 2>/dev/null | wc -l | tr -d ' ')" - [ "$dirty" = 0 ] || warn "$dirty uncommitted tracked change(s) here; worktrees are reset to their branch, so these do not carry over (ProjectVersion.txt bumps included)" + [ "$dirty" = 0 ] || warn "$dirty uncommitted tracked change(s) here; worktrees are reset to their branch, so these do not carry over (ProjectVersion.txt bumps included), and the Library was imported from them, so each worktree reimports those assets on first open" local flagged; flagged="$(git -C "$proj" ls-files -v 2>/dev/null | grep -c '^[a-zS] ')" [ "$flagged" = 0 ] || warn "$flagged file(s) marked assume-unchanged or skip-worktree; worktrees get the committed version of them, not your local one" fi @@ -597,38 +645,67 @@ cmd_seed() { [ $ok = 1 ] && mv "$stage.txt" "$gd.txt" || ok=0 [ $ok = 1 ] || { rm -rf "$stage" "$stage".* "$gd" "$gd".*; die "could not publish $gd"; } good "golden published in $(( $(now) - t0 ))s: $(golden_field "$gd" files) files, unity $(golden_field "$gd" unity_version), commit $(golden_field "$gd" commit | cut -c1-10)" + local bt; bt="$(build_target "$gd/Library")" + [ -z "$bt" ] || say " build target: $bt. Worktrees open on it; one that builds for another platform pays a platform switch, so seed from the platform you build for." say " re-run 'uwt seed --force' after an editor upgrade or when the snapshot feels stale (it is cheap)." exit $EXIT_OK } # ------------------------------------------------------------------ hydrate -hydrate_library() { # hydrate_library - local gd="$1" wt="$2" t0; t0=$(now) - cow_tree "$gd/Library" "$wt/Library" || { rm -rf "$wt/Library"; return 1; } - strip_never "$wt/Library" +hydrate_library() { # hydrate_library [] + local gd="$1" wt="$2" lib="${3:-$2/Library}" t0; t0=$(now) + cow_tree "$gd/Library" "$lib" || { rm -rf "$lib"; return 1; } + strip_never "$lib" local gv wv; gv="$(golden_field "$gd" unity_version)"; wv="$(unity_version "$wt")" [ "$gv" = "$wv" ] || warn "golden Library is from Unity '$gv', this worktree declares '$wv'; the editor will upgrade or reimport on first open (expect extra private disk). Re-seed under the new version to avoid it." { echo "golden_commit=$(golden_field "$gd" commit)"; echo "golden_created=$(golden_field "$gd" created)" - echo "golden_unity=$gv"; echo "hydrated=$(stamp)"; } > "$wt/Library/.uwt-hydration.txt" - good "Library hydrated in $(( $(now) - t0 ))s ($(file_count "$wt/Library") files)" + echo "golden_unity=$gv"; echo "hydrated=$(stamp)"; } > "$lib/.uwt-hydration.txt" + good "Library hydrated in $(( $(now) - t0 ))s ($(file_count "$lib") files)" + local bt; bt="$(build_target "$lib")" + [ -z "$bt" ] || say " build target: $bt (the golden's); building for another platform switches and reimports" +} + +# The end of every hydrate that leaves a Library in place. is +# "hydrated" or "kept"; library says who made what is there, as `list` does. +hydrate_done() { # hydrate_done + local wt="$1" from=other bt; [ -f "$wt/Library/.uwt-hydration.txt" ] && from=uwt + bt="$(build_target "$wt/Library")" + if [ "$2" = kept ]; then + good "Library/ already exists ($([ $from = uwt ] && echo "hydrated by uwt" || echo "not from uwt")); left as it is. Pass --force to replace it from the golden." + fi + json_ok hydrate "{\"project\":$(json_str "$(jpath "$wt")"),\"action\":\"$2\",\"library\":\"$from\",\"build_target\":$(json_str "$bt"),\"seconds\":$(( $(now) - $3 ))}" + exit $EXIT_OK } cmd_hydrate() { local wt; wt="$(project_root "${1:-$PWD}")" || exit $EXIT_FAIL local gd; gd="$(golden_dir "$wt")" + local lib="$wt/Library" new="$wt/Library.uwt-new" old="$wt/Library.uwt-old" t0; t0=$(now) say ""; head_ "uwt hydrate"; say "worktree $wt" - golden_ok "$gd" || die "no golden for this repo - run 'uwt seed' in a checkout with an imported Library" + # Idempotent, so a setup script can run it every time: a Library that is + # already there is left alone, whoever made it and whatever else is missing. + [ ! -d "$lib" ] || [ "${OPT_FORCE:-0}" = 1 ] || hydrate_done "$wt" kept "$t0" + golden_ok "$gd" || REASON=no_golden die "no golden for this repo - run 'uwt seed' in a checkout with an imported Library" local gpre wpre; gpre="$(golden_field "$gd" prefix)"; wpre="$(project_prefix "$wt")" - [ "$gpre" = "$wpre" ] || die "the golden is of the project at '${gpre:-}', not '${wpre:-}'" + [ "$gpre" = "$wpre" ] || REASON=other_project die "the golden is of the project at '${gpre:-}', not '${wpre:-}'" require_editor_closed "$wt" hydrate - if [ -d "$wt/Library" ]; then - [ "${OPT_FORCE:-0}" = 1 ] || die "Library/ already exists; pass --force to replace it" - rm -rf "$wt/Library" || die "could not remove the existing Library/" + # An existing Library is replaced by a staged swap, never deleted up front: + # it may be a full private import, and every check and the clone itself can + # still fail. A run interrupted mid-swap left it as Library.uwt-old. + if [ ! -e "$lib" ] && [ -d "$old" ]; then mv "$old" "$lib" || die "could not restore $old to Library/"; fi + rm -rf "$new" "$old" || die "could not clear a stale $new or $old" + [ ! -d "$lib" ] || [ "${OPT_FORCE:-0}" = 1 ] || hydrate_done "$wt" kept "$t0" + same_volume "$wt" "$gd" || REASON=different_volume die "worktree and golden are on different volumes; nothing would be shared" + local why; why="$(cow_probe "$wt")" || REASON=unsupported_volume die "this volume cannot block-clone ($why)" + if [ -d "$lib" ]; then + hydrate_library "$gd" "$wt" "$new" || die "hydrate failed; the existing Library/ is untouched" + mv "$lib" "$old" || { rm -rf "$new"; die "could not move the existing Library/ aside; it is untouched"; } + mv "$new" "$lib" || { mv "$old" "$lib"; rm -rf "$new"; die "could not put the new Library/ in place; the previous one is restored"; } + rm -rf "$old" || warn "the new Library/ is in place, but $old could not be deleted; delete it by hand" + else + hydrate_library "$gd" "$wt" || die "hydrate failed; Library/ removed" fi - same_volume "$wt" "$gd" || die "worktree and golden are on different volumes; nothing would be shared" - local why; why="$(cow_probe "$wt")" || die "this volume cannot block-clone ($why)" - hydrate_library "$gd" "$wt" || die "hydrate failed; Library/ removed" - exit $EXIT_OK + hydrate_done "$wt" hydrated "$t0" } # ------------------------------------------------------------------ new @@ -810,7 +887,7 @@ cmd_new() { local path="${OPT_PATH:-$(dirname "$m")/$(basename "$m")-$(printf '%s' "${branch#/}" | tr -c 'A-Za-z0-9._-' '-')}" mkdir -p "$(dirname "$path")" 2>/dev/null; path="$(abs_path "$path")" say ""; head_ "uwt new"; say "repo $m"; say "branch $branch"; say "path $path" - golden_ok "$gd" || die "no golden yet - run 'uwt seed' first (editor closed)" + golden_ok "$gd" || REASON=no_golden die "no golden yet - run 'uwt seed' first (editor closed)" [ -e "$path" ] && die "path already exists: $path" # MAX_PATH: the golden knows its longest relative path, so this is exact for @@ -824,9 +901,9 @@ cmd_new() { fi # Everything that can refuse does so BEFORE git registers anything. - same_volume "$gd" "$(dirname "$path")" || die "the golden ($gd) and the new worktree must be on one volume; set UWT_CACHE_ROOT or choose another --path" + same_volume "$gd" "$(dirname "$path")" || REASON=different_volume die "the golden ($gd) and the new worktree must be on one volume; set UWT_CACHE_ROOT or choose another --path" local why; why="$(cow_probe "$(dirname "$path")")" \ - || die "this volume cannot block-clone ($why); use plain $([ "$vcs" = plastic ] && echo "'cm workspace create'" || echo "'git worktree add'")" + || REASON=unsupported_volume die "this volume cannot block-clone ($why); use plain $([ "$vcs" = plastic ] && echo "'cm workspace create'" || echo "'git worktree add'")" [ "$vcs" = plastic ] && new_plastic local ref="${OPT_BASE:-HEAD}" exists=0 git -C "$m" rev-parse --verify --quiet "refs/heads/$branch" >/dev/null && { ref="refs/heads/$branch"; exists=1; } @@ -1159,7 +1236,8 @@ uwt - cheap Unity git worktrees (or Plastic workspaces) via copy-on-write uwt new BRANCH [--path DIR] [--base REF] [--inherit-untracked] [--init-submodules] [--verify] [--json] git worktree add + clone the snapshot + hydrate - uwt hydrate [PATH] [--force] give an existing worktree a Library from the golden + uwt hydrate [PATH] [--force] [--json] give an existing worktree a Library from the golden; + one that has a Library is left alone (exit 0) uwt list [PATH] [--json] the repo's worktrees and where each Library came from uwt remove PATH [--force] [--delete-branch] [--json] git worktree remove, behind the editor interlock; @@ -1173,7 +1251,9 @@ Plastic workspaces: BRANCH is a branch path (/main/task); a bare name is a Golden location: /.git/uwt/golden,
/.plastic/uwt/golden, or \$UWT_CACHE_ROOT/golden. --json: human output goes to stderr; stdout carries one record, - {"status":"ok","command":...,"value":...} or {"status":"error","exit":N,"message":...}. + {"status":"ok","command":...,"value":...} or {"status":"error","exit":N,"message":...}, + the latter with "reason" where there is one: no_golden, editor_busy, + unsupported_volume, different_volume, other_project. Exit codes: 0 ok, 2 failure, 3 refused (an editor is using the project). EOF } @@ -1202,7 +1282,7 @@ while [ $# -gt 0 ]; do shift done if [ "$OPT_JSON" = 1 ]; then - case "$CMD" in list|new|remove|measure) exec 3>&1 1>&2; JSON=1 ;; *) die "--json is supported by list, new, remove and measure" ;; esac + case "$CMD" in list|new|hydrate|remove|measure) exec 3>&1 1>&2; JSON=1 ;; *) die "--json is supported by list, new, hydrate, remove and measure" ;; esac fi case "$CMD" in doctor) cmd_doctor "${POS[@]:-}" ;; diff --git a/tests/run-tests.sh b/tests/run-tests.sh index ee3effb..35ff9c3 100644 --- a/tests/run-tests.sh +++ b/tests/run-tests.sh @@ -58,7 +58,9 @@ shares_blocks() { PROJ="$ROOT/proj" rm -rf "$ROOT"/proj "$ROOT"/proj-* "$ROOT"/wt-* "$ROOT"/plain mkdir -p "$PROJ" && cd "$PROJ" || exit 2 -git init -q && git config user.email t@t && git config user.name t && git config core.autocrlf false +# The branch is named, never left to init.defaultBranch: the groups below return to it by name. +git init -q -b main && git config user.email t@t && git config user.name t && git config core.autocrlf false \ + || { echo "fixture: git init failed"; exit 2; } mkdir -p ProjectSettings Assets/Scenes Packages Library/PackageCache/com.x@abc Library/DataStore/00 \ Library/Bee Library/BurstCache Library/ScriptAssemblies Library/Search Library/Pipeline Library/ShaderCache Temp obj printf 'm_EditorVersion: 6000.7.0b1\nm_EditorVersionWithRevision: 6000.7.0b1 (deadbeef)\n' > ProjectSettings/ProjectVersion.txt @@ -71,10 +73,15 @@ head -c 100000 /dev/urandom > Library/DataStore/UDSData_3.bin echo pkg > Library/PackageCache/com.x@abc/package.json echo dag > Library/Bee/x.dag; echo burst > Library/BurstCache/b.dll; echo dll > Library/ScriptAssemblies/a.dll echo idx > Library/Search/i.index; echo pipe > Library/Pipeline/p; echo sc > Library/ShaderCache/s.bin +# The smallest EditorUserBuildSettings.asset uwt reads the build target from: +# format 22, data at 64, a Unity 6 version string; then the active target +# (13, Android), a platform GUID, the selected target, both groups and names. +{ printf '\0\0\0\0\0\0\0\0\0\0\0\026'; head -c 20 /dev/zero; printf '\0\0\0\0\0\0\0\100\0\0\0\0\0\0\0\0' + printf '6000.7.0b1\0\0\0\0\0\0'; printf '\0\0\0\0\0\0\0\0\015\0\0\0'; head -c 16 /dev/zero + printf '\015\0\0\0\007\0\0\0\007\0\0\0\007\0\0\0Android\0\007\0\0\0Android\0\0\0\0\0'; } > Library/EditorUserBuildSettings.asset echo '{"process_id":999999}' > Library/EditorInstance.json; : > Library/ArtifactDB-lock; echo 123 > Library/burst.pid : > Temp/UnityLockfile; echo x > Foo.csproj -git add -A && git commit -qm init -git branch keep HEAD +git add -A && git commit -qm init && git branch keep HEAD || { echo "fixture: initial commit failed"; exit 2; } echo untracked > scratch.txt # untracked, not ignored: must not survive `new` # Can this ROOT clone? Decide once, from the tool's own probe. @@ -101,6 +108,7 @@ GD="$PROJ/.git/uwt/golden" out="$(uwt seed "$PROJ" 2>&1)"; rc=$? if [ $CLONES = 1 ]; then check "seed exits 0" [ $rc = 0 ] + check "seed names the build target worktrees open on" grep -q 'build target: Android' <<<"$out" check "golden.txt, golden.index and golden/Library exist" exists "$GD.txt" "$GD.index" "$GD/Library" check "golden records the Unity version" grep -q '^unity_version=6000.7.0b1' "$GD.txt" check "golden records HEAD" grep -q "^commit=$(git rev-parse HEAD)" "$GD.txt" @@ -197,8 +205,9 @@ if [ $CLONES = 1 ]; then else skip "MAX_PATH warning (Windows with LongPathsEnabled=0 only)" fi - # Cross-branch: the snapshot is at master; the target branch differs. - git -C "$PROJ" checkout -q -b other && echo changed > README.md && echo new > Assets/N.txt && git rm -q Packages/manifest.json && git add -A && git commit -qm other && git checkout -q master + # Cross-branch: the snapshot is at main; the target branch differs. + git -C "$PROJ" checkout -q -b other && echo changed > README.md && echo new > Assets/N.txt && git rm -q Packages/manifest.json && git add -A && git commit -qm other && git checkout -q main \ + || fail "cross-branch fixture setup" check "cross-branch: reset reconciles to the target branch exactly" \ bash -c "cd '$PROJ' && bash '$UWT' new other --path '$ROOT/wt-other' >/dev/null 2>&1 && [ \"\$(cat '$ROOT/wt-other/README.md')\" = changed ] && [ -f '$ROOT/wt-other/Assets/N.txt' ] && [ ! -e '$ROOT/wt-other/Packages/manifest.json' ] && [ -z \"\$(git -C '$ROOT/wt-other' status --porcelain)\" ]" # Local edits hidden behind index bits: git status cannot see them, so only @@ -227,11 +236,48 @@ group "5. hydrate" git -C "$PROJ" worktree add -q "$ROOT/plain" -b plain if [ $CLONES = 1 ]; then check "hydrate fills a plain git worktree" bash -c "bash '$UWT' hydrate '$ROOT/plain' >/dev/null 2>&1 && [ -s '$ROOT/plain/Library/ArtifactDB' ]" - check "hydrate refuses to overwrite without --force" bash -c "bash '$UWT' hydrate '$ROOT/plain' >/dev/null 2>&1; [ \$? = 2 ]" + out="$(uwt hydrate "$ROOT/plain" --force --json 2>/dev/null)"; rc=$? + check "hydrate --json is one ok record" bash -c "[ $rc = 0 ] && [ \$(wc -l <<<'$out') = 1 ]" + check "that says hydrated, from uwt, and the build target" \ + json_is "d['status']=='ok' and d['command']=='hydrate' and d['value']['action']=='hydrated' and d['value']['library']=='uwt' and d['value']['build_target']=='Android'" <<<"$out" + # Idempotent: a setup script runs it every time. + echo keep > "$ROOT/plain/Library/KEEP" + out="$(uwt hydrate "$ROOT/plain" --json 2>/dev/null)"; rc=$? + check "hydrate on a worktree that has a Library exits 0" [ $rc = 0 ] + check "and reports it kept" json_is "d['value']['action']=='kept' and d['value']['library']=='uwt'" <<<"$out" + check "and left it alone" exists "$ROOT/plain/Library/KEEP" + rm "$ROOT/plain/Library/.uwt-hydration.txt" + check "a Library uwt did not make is kept as 'other'" json_is "d['value']['action']=='kept' and d['value']['library']=='other'" <<<"$(uwt hydrate "$ROOT/plain" --json 2>/dev/null)" check "hydrate --force replaces" bash -c "echo junk > '$ROOT/plain/Library/JUNK'; bash '$UWT' hydrate '$ROOT/plain' --force >/dev/null 2>&1 && [ ! -e '$ROOT/plain/Library/JUNK' ]" + # A --force that cannot finish must leave the Library it was replacing. The + # failures are forced from PATH: a probe that finds no clone, then a clone + # that fails after the probe passed. + echo mine > "$ROOT/plain/Library/MINE"; cp "$ROOT/plain/Library/ArtifactDB" "$ROOT/plain-adb" + SH="$ROOT/shim"; rm -rf "$SH"; mkdir -p "$SH/probe" "$SH/clone" + if [ $WIN = 1 ]; then + printf '#!/usr/bin/env bash\nexit 0\n' > "$SH/probe/fsutil" + printf '#!/usr/bin/env bash\ncase "$*" in *.uwt-probe*) exec "%s" "$@" ;; esac\nexit 16\n' "$(command -v robocopy)" > "$SH/clone/robocopy" + else + printf '#!/usr/bin/env bash\nexit 1\n' > "$SH/probe/cp" + printf '#!/usr/bin/env bash\ncase "$*" in *.uwt-probe*) exec "%s" "$@" ;; esac\nexit 1\n' "$(command -v cp)" > "$SH/clone/cp" + fi + chmod +x "$SH"/*/* + for how in probe clone; do + PATH="$SH/$how:$PATH" uwt hydrate "$ROOT/plain" --force >/dev/null 2>&1; rc=$? + check "hydrate --force with a failing $how exits 2" [ $rc = 2 ] + check "and leaves the existing Library byte-identical" bash -c "cmp -s '$ROOT/plain/Library/ArtifactDB' '$ROOT/plain-adb' && [ -f '$ROOT/plain/Library/MINE' ]" + check "and leaves no staging directory" absent "$ROOT/plain/Library.uwt-new" "$ROOT/plain/Library.uwt-old" + done + out="$(PATH="$SH/probe:$PATH" uwt hydrate "$ROOT/plain" --force --json 2>/dev/null)" + check "a volume that cannot clone is reason unsupported_volume" json_is "d['status']=='error' and d['exit']==2 and d['reason']=='unsupported_volume'" <<<"$out" + mv "$ROOT/plain/Library" "$ROOT/plain/Library.uwt-old" && mkdir "$ROOT/plain/Library.uwt-new" + check "a Library an interrupted swap moved aside is restored" \ + bash -c "bash '$UWT' hydrate '$ROOT/plain' >/dev/null 2>&1; [ \$? = 0 ] && [ -f '$ROOT/plain/Library/MINE' ] && [ ! -e '$ROOT/plain/Library.uwt-new' ] && [ ! -e '$ROOT/plain/Library.uwt-old' ]" check "hydrate warns on a Unity version mismatch" bash -c "sed -i.bak 's/6000.7.0b1/6000.8.0a1/' '$ROOT/plain/ProjectSettings/ProjectVersion.txt'; bash '$UWT' hydrate '$ROOT/plain' --force 2>&1 | grep -q 'declares'" else - check "hydrate refuses with exit 2 when there is no golden" bash -c "bash '$UWT' hydrate '$ROOT/plain' >/dev/null 2>&1; [ \$? = 2 ]" + out="$(uwt hydrate "$ROOT/plain" --json 2>/dev/null)"; rc=$? + check "hydrate refuses with exit 2 when there is no golden" [ $rc = 2 ] + check "and --json gives the reason no_golden" json_is "d['status']=='error' and d['reason']=='no_golden'" <<<"$out" fi # ------------------------------------------------------------------ 6. cli @@ -700,6 +746,35 @@ else cd "$ROOT"; pl_sweep fi +# ------------------------------------------------------------------ 13. macOS, a volume that cannot clone +group "13. macOS: a volume that cannot clone" +# cp -c succeeds on FAT by copying bytes, so the probe must not take its word. +FATM="$ROOT/fatmnt" +if [ "$(uname -s)" = Darwin ] && command -v hdiutil >/dev/null 2>&1; then + hdiutil detach -quiet -force "$FATM" >/dev/null 2>&1; rm -rf "$ROOT/fat.dmg" "$FATM"; mkdir -p "$FATM" + if hdiutil create -quiet -size 64m -fs MS-DOS -volname UWTFAT "$ROOT/fat.dmg" \ + && hdiutil attach -quiet -nobrowse -mountpoint "$FATM" "$ROOT/fat.dmg"; then + FP="$FATM/proj" + ( mkdir -p "$FP/ProjectSettings" "$FP/Assets" "$FP/Library" && cd "$FP" \ + && git init -q -b main && git config user.email t@t && git config user.name t \ + && printf 'm_EditorVersion: 6000.7.0b1\n' > ProjectSettings/ProjectVersion.txt \ + && echo a > Assets/a.txt && printf '/Library/\n' > .gitignore \ + && git add -A && git commit -qm init && head -c 300000 /dev/urandom > Library/ArtifactDB ) \ + || fail "FAT fixture setup" + out="$(uwt doctor "$FP" 2>&1)"; rc=$? + check "doctor on FAT exits 2" [ $rc = 2 ] + check "and says the volume is not APFS" grep -q 'not APFS' <<<"$out" + uwt seed "$FP" >/dev/null 2>&1; rc=$? + check "seed on FAT exits 2" [ $rc = 2 ] + check "and publishes no golden" absent "$FP/.git/uwt/golden" "$FP/.git/uwt/golden.txt" + cd "$ROOT"; hdiutil detach -quiet -force "$FATM" >/dev/null 2>&1 + else + skip "could not create or mount a FAT image" + fi +else + skip "FAT refusal (macOS only)" +fi + # ------------------------------------------------------------------ done printf '\n%d passed, %d failed, %d skipped (fixtures in %s)\n' "$PASS" "$FAILN" "$SKIPN" "$ROOT" [ "$FAILN" = 0 ]