Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: tests
# GitHub-hosted runners in seconds. ubuntu (ext4) and windows (NTFS) cannot
# block-clone, so they exercise the REFUSAL paths; macOS (APFS) can. The
# linux-cow job runs the clone paths on Linux, on loopback btrfs, XFS and ZFS
# volumes. Harnesses that need Unity are instruments, not gates, and are
# deliberately not here.
# volumes; windows-refs runs them on a ReFS VHDX. Harnesses that need Unity are
# instruments, not gates, and are deliberately not here.

on:
pull_request:
Expand Down Expand Up @@ -75,3 +75,44 @@ jobs:
- name: Run the suite
shell: bash
run: bash tests/run-tests.sh /mnt/cow

# robocopy block-clones on ReFS from Windows 11 24H2 / Server 2025, so 2025
# runs the clone paths and 2022 the refusal paths on the same filesystem.
# Hosted runners are elevated and users usually are not, so a clone path
# that only works as admin would still pass here.
windows-refs:
name: bash (${{ matrix.os }}, ReFS)
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: windows-2025
clones: "yes"
- os: windows-2022
clones: "no"
steps:
- uses: actions/checkout@v7
- name: Make a ReFS VHDX
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
New-VHD -Path "$env:RUNNER_TEMP\cow.vhdx" -SizeBytes 8GB -Dynamic |
Mount-VHD -Passthru |
Initialize-Disk -PartitionStyle GPT -Passthru |
New-Partition -DriveLetter R -UseMaximumSize |
Format-Volume -FileSystem ReFS -NewFileSystemLabel cow -Confirm:$false -Force
New-Item -ItemType Directory R:\cow | Out-Null
[Environment]::OSVersion.Version
fsutil fsinfo refsinfo R:
- name: Run the suite
shell: bash
run: |
set -o pipefail
bash tests/run-tests.sh R:/cow 2>&1 | tee "$RUNNER_TEMP/suite.log"
# A suite that passes on the wrong side of cloning has not tested what this job is for.
clones="$(sed -n 's/^fixture:.*block-cloning: \(yes\|no\).*/\1/p' "$RUNNER_TEMP/suite.log" | head -n 1)"
if [ "$clones" != "${{ matrix.clones }}" ]; then
echo "::error::block-cloning was '$clones', expected '${{ matrix.clones }}' on ${{ matrix.os }}"
exit 1
fi
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ an editor is using the project.
A filesystem that block-clones, with the repo and the worktrees on the same
volume:

- **Windows**: ReFS (Dev Drive) on Windows 11 24H2 or later. NTFS cannot.
- **Windows**: ReFS (Dev Drive) on Windows 11 24H2 or later, with ReFS dedup
off on that volume. NTFS cannot.
- **macOS**: APFS.
- **Linux**: btrfs, XFS with `reflink=1`, or ZFS ≥ 2.2. ext4 cannot.

Expand Down
93 changes: 79 additions & 14 deletions skills/unity-worktree-cow/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ outcomes apart:
| 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` |
| ReFS copied files instead of cloning them (Troubleshooting) | `error`, exit 2, `reason` = `clone_copied` |

### A `Library/` copied in by hand

Expand All @@ -151,9 +152,9 @@ The fix is to delete `Library/Bee`, `BurstCache`, `BuildCache` and

| command | what it does |
|---|---|
| `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, and when the `Library/` holds build state made in another checkout. |
| `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, and when the `Library/` holds build state made in another checkout. On Windows, estimates how many more worktrees fit under ReFS's per-block reference limit, and fails at none. |
| `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. |
| `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 except those `.uwt-keep` lists, 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] [--json]` | Gives an existing worktree a `Library/` cloned from the golden. One that already has a `Library/` is left alone (exit 0), with a warning if it holds build state made in another checkout; `--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`), and a `foreign-build-state` tag where it applies. |
| `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". |
Expand All @@ -173,11 +174,13 @@ stderr and stdout carries exactly one record, either
`{"status":"ok","command":"new","value":{...}}` or
`{"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`.
`editor_busy`, `unsupported_volume`, `different_volume`, `other_project`,
`clone_copied`.
`hydrate`'s value has `project`, `action`, `library`, `build_target`,
`foreign_build_state` 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
`verified`, `submodules_uninitialized` and `kept` (the `.uwt-keep` paths it
kept); `remove`'s has `path`, `branch` and
`branch_deleted`.

A project in a subdirectory of its repo (a monorepo) works: `seed` records
Expand All @@ -190,7 +193,18 @@ small. One golden per repo, so one Unity project per repo.
A filesystem that block-clones, with golden and worktrees on **one volume**:

- **Windows**: ReFS (Dev Drive) on Windows 11 24H2 or later. Runs under Git
Bash and clones with robocopy. NTFS cannot clone.
Bash and clones with robocopy. NTFS cannot clone. Keep ReFS dedup off on
that volume (`Get-ReFSDedupStatus`, as administrator). Dedup folds repeated
blocks inside a file, so one block can already carry a thousand references
before any clone, and each clone of the file adds as many again. ReFS stops
cloning a block at 8,176 references, and on a deduplicated volume that came
at the seventh worktree of a mid-size project. `doctor` estimates how many
more worktrees fit, and `seed`, `new` and `hydrate` fail with
`clone_copied` once none do (see Troubleshooting). Turning dedup off does
not undo the blocks it already folded or compressed. `measure` cannot
locate compressed extents, which dedup and ReFS compression both make, so
it reports them as `compressed` and leaves them out of `allocated`,
`private` and `shared`.
- **macOS**: APFS. `new` re-reads every tracked byte once there, so on a
large LFS project it takes about a minute (see Troubleshooting).
- **Linux**: btrfs, XFS with `reflink=1`, ZFS ≥ 2.2 (`measure` excepted). ext4
Expand Down Expand Up @@ -252,8 +266,8 @@ and ~10 s, and opened with 0 assets imported.
`cm workspace create` into an empty directory (its own name and GUID), clone
the snapshot, place the snapshot's wktree and selector (never
`plastic.workspace`), `cm undo -r` to the snapshot's changeset, `cm switch`,
require zero pending or local changes, delete private and ignored files,
hydrate. The workspace is named after its directory, which must be unused
require zero pending or local changes, delete private and ignored files
except those a checked-in `.uwt-keep` lists, hydrate. The workspace is named after its directory, which must be unused
as a workspace name on the machine. Rolls back on failure, branch included.
- **`seed` refuses pending changes** (checkouts, adds, moves, deletes,
merges): the wktree would carry them, and any exclusive lock a checkout
Expand Down Expand Up @@ -314,7 +328,7 @@ switches branches) but the reimport lands as private disk. `hydrate` warns
when the golden's version differs from the worktree's.

**Never clone worktree-to-worktree, only from the golden.** ReFS caps a
region at 8175 references; chaining multiplies them. uwt only ever clones the
block at 8,176 references; chaining multiplies them. uwt only ever clones the
golden.

**Keep Windows worktree roots short** unless `LongPathsEnabled=1`:
Expand Down Expand Up @@ -390,6 +404,20 @@ checkout, full of absolute paths) are left out of the snapshot. Untracked files
that are in the snapshot are removed from a new worktree unless you pass
`--inherit-untracked`.

To keep some of them, such as build output a first open or a CI warmup would
otherwise rebuild, commit a `.uwt-keep` at the root of the repo or workspace:

```
# one path per line, relative to the root; no globs
AssetBundles
Assets/StreamingAssets/aa
```

`new` reads the copy committed on the branch it checks out, so a branch can
keep less, and names what it kept (`kept` in `--json`). Kept files were built
from the snapshot's commit; on a branch that changes what they are built from
they are stale, and the project has to rebuild them.

The deny list is measured, not argued. Grafting a whole `Bee` into a worktree and building a player there (Unity 6000.7.0b1,
ReFS): the build succeeded but took 33 s *longer* than without the graft, kept
the donor's player DAG and stripper response file, read the donor's link
Expand All @@ -402,6 +430,9 @@ artifacts by absolute path.

1. `git worktree add --no-checkout` registers the worktree and writes no files.
2. The snapshot (minus `Library`) is cloned in, one directory-level clone.
robocopy copies a file ReFS will not clone and still reports success, so
on Windows this and every other clone then reads the reference counts of
the 32 largest files it wrote and rolls back if any was copied.
3. The snapshot's git index is copied into the worktree's git dir. git decides
"unchanged" from the stat data cached in the index, and the clone
preserves mtimes, so every file that matches is skipped.
Expand All @@ -424,7 +455,8 @@ artifacts by absolute path.
tracked byte once. On macOS and Linux the status has already done exactly
that, so `--verify` says so and skips a second pass (about a minute on the
23.5 GB LFS project).
6. `git clean -xdf`, then `Library/` is cloned from the golden.
6. `git clean -xdf`, sparing what `.uwt-keep` lists, then `Library/` is
cloned from the golden.

## Cost model

Expand All @@ -439,6 +471,8 @@ there is to save, not what a worktree costs.
Two corrections when predicting from a histogram: identical trees can store a
different number of files inline (a golden 50,370, a fresh worktree of it
57,111), and every inline file is a private cluster, so allow a few percent.
On btrfs, files up to 2 KiB (the default `max_inline`) live inside its
metadata instead: a clone copies them, and `measure` counts each at its size.

## Measuring it yourself

Expand All @@ -452,15 +486,37 @@ A block is private to a worktree when no other tree (the main checkout and its
`.git`, the golden, sibling worktrees) maps the same physical block. `measure`
reads physical extents directly: `FSCTL_GET_RETRIEVAL_POINTERS` on Windows
(through PowerShell and inline C#), FIEMAP on Linux and `F_LOG2PHYS_EXT` on
macOS (through `python3`). Files stored inline count as one private cluster
each. Data still in the write cache has no blocks yet, and a cached overwrite
macOS (through `python3`). Each block counts once per tree, so the groups add
up to the tree. Files stored resident on ReFS count as one private cluster
each; files stored inline on btrfs count their size, which is data in btrfs
metadata (stored twice on a single-device volume, which `measure` does not
count, as it does not count RAID copies of data). Data still in the write cache has no blocks yet, and a cached overwrite
of a clone still maps to the shared ones, so `measure` flushes every file
written in the last 30 minutes before reading it; it is safe to run right
after work, with the editor closed. On ReFS it takes 30 s for a 41.5k-file project with four worktrees and
the golden, ~50k files each, and gives the same answer every run. ZFS clones
blocks but does not report physical extents, so `measure` refuses there
(exit 2).

On compressed btrfs (`compress`, `compress-force`, `chattr +c`) the kernel
reports where a compressed extent starts but not how long it is on disk, only
its uncompressed length. `measure` still tells shared from private exactly,
since every reference to an extent reports the same start, but it estimates
each one's size from the gap to the next extent, capped at 128 KiB, and says
how many it estimated. The estimate is an upper bound, close when the data was
written in one pass: on a `compress-force=zstd:3` volume holding a 41k-file
project, two worktrees and the golden, `allocated` came within 1.4% of
`compsize` for every tree. Inline files are counted uncompressed, which there
overstated each tree's private by about 28 MiB. `compsize` (run as root) reads
the exact sizes.

On ReFS, dedup and compression leave compressed extents with no physical
address at all, so neither their size on disk nor who shares them can be
read. `measure` lists them per tree as `compressed` (logical size) and leaves
them out of `allocated`, `private` and `shared%`. Where they are a large part
of a tree, its figures say little: on a deduplicated volume, the
`Library/DataStore` of a project with 39k tracked files held 4.7 GiB of them.

What not to use:

- **Free space and `du`.** Free space updates lazily on ReFS and APFS, and
Expand Down Expand Up @@ -509,10 +565,18 @@ not the bare project name: Unity bakes `productName` into `app.info` and
`Get-Volume`).
- `doctor` says cloning does not work on Linux: ext4 cannot; XFS must have
been created with `-m reflink=1` (`xfs_info <mount> | grep reflink`).
- ReFS error 347 / `0xC000048C` (too many clones): a region hit the 8175
reference cap. Re-seed to reset the count.
- The disk fills up after a successful `new`: cloned blocks diverge lazily,
when Unity writes. Budget as above.
- `seed`, `new` or `hydrate` fails with "ReFS copied N of the largest files
instead of cloning them" (`clone_copied`): ReFS refused to clone those
files (error 347, a block at its 8,176-reference limit) and robocopy copied
them instead, reporting success; uwt checks and rolls back. The usual cause
is ReFS dedup on the volume (see Requirements): `Get-ReFSDedupStatus` as
administrator. `doctor` names the file that fills first and estimates how
many more worktrees fit. Re-seeding does not help, since the golden shares the
source's blocks. Removing worktrees does, about 15 s after the delete, when
ReFS releases their references. Turn dedup off for that volume so it stops
creating more.
- `new` reports tracked differences after reset and rolls back: the snapshot's
index did not match its files (mtimes not preserved). Re-seed and retry.
- Every fresh worktree spends minutes importing: the golden is from another
Expand All @@ -526,7 +590,8 @@ not the bare project name: Unity bakes `productName` into `app.info` and
`git status` re-hashing the tracked content, LFS included, because every
cloned file has a new inode. It scales with tracked bytes (URP, 0.95 GB:
8 s for the whole `new`). Measured on macOS: 65.5 s of a 78 s `new`
for 23.5 GB of LFS content; the reset itself took 0.4 s. It is paid once per
for 23.5 GB of LFS content; the reset itself took 0.4 s. On Linux btrfs:
12 s of a 13 s `new` for 41k tracked files, 5.3 GB with LFS. It is paid once per
worktree. Skipping it would only move it to the first `git status` anyone
runs there. `GIT_TRACE2_PERF=/tmp/t.txt` on the `new` shows it as the
`refresh` region.
Loading
Loading