Skip to content

feat: clone with cow-clone on macOS, Linux and Windows instead of a compiled helper - #6550

Merged
janicduplessis merged 4 commits into
mainfrom
feat/6548-cow-clone
Nov 27, 2026
Merged

janicduplessis merged 4 commits into
mainfrom
feat/6548-cow-clone

Conversation

@janicduplessis

@janicduplessis janicduplessis commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Stim cloned directories with stim-clonefile, a C helper compiled with xcrun clang on first use. It existed only on macOS and needed the Xcode Command Line Tools, so stim worktree add took its one-pass clone path on macOS only and stim worktree warm copied on Linux and Windows even on a file system that clones.

This replaces the helper with cow-clone, a package with prebuilt binaries for macOS, Linux and Windows (x64 and arm64): clonefile(2) on APFS, ioctl(FICLONE) file by file on btrfs and XFS, block cloning file by file on ReFS and a Dev Drive. A clone that is not possible reports unsupported, cross-device or failed with an errno name.

The dependency is "cow-clone": "0.1.1" from npm, pinned like real-size, with cow-clone@0.1.1 in minimumReleaseAgeExclude.

Solution

One clone layer, packages/stim-cli/src/clone.ts: clonePairs(operation, pairs) runs the cow-clone binary through Stim's executor, as private-size.ts runs real-size, so the child is declared for the operation and a cancel kills it. Pairs go on stdin, so no pair count reaches a command-line limit and the 256-pair batches are gone. It returns one result per pair. worktree warm, worktree add and the engine store's cloneTree use it. The C source, the compile step, the xcode-select probe and the macOS gate are removed.

worktree add clones the checkout worktree warm clones ignored entries
macOS, APFS yes, as before yes, files too (they went through cp -Rc)
Linux, btrfs or XFS yes, new yes, new
Linux, ext4 and others no: plain path, unsupported no: cp -R
Windows, ReFS or Dev Drive no: plain path yes, new
Windows, NTFS no: plain path no: fs.cp as before

worktree add on macOS and Linux:

  • Before git registers anything, it clones one tracked, non-empty file of the source checkout to a temporary name in the nearest existing directory above the new path, and removes it. This replaces the platform gate and the st_dev comparison (two btrfs subvolumes have different device numbers and clone into each other). Without it, every run on ext4 would register a worktree and a branch, fail at the first file, remove both and add them again.
  • The plain path's line names cow-clone's reason: because the file system cannot clone (unsupported, ENOTSUP), because the source and the destination are on different volumes (cross-device, EXDEV), or because cloning failed (<message>: <path> (failed, <errno>)).
  • cow-clone continues after a failed pair where the helper stopped; cloneCheckout throws on the first failed result and the existing rollback removes the worktree.
  • The clone-as-checkout step of fix: worktree add gives the files git worktree add writes, in bytes, modes, names and extended attributes #6553 now also runs after a clone on Linux. There it sets file and folder modes as on macOS, since cow-clone keeps the source's permission bits, and skips what is macOS-only: xattr and chflags (cow-clone copies no extended attribute or flag on Linux) and lchmod (a Linux link's mode is always 0777). Before setting modes it makes a folder and a file in the new worktree and takes the plain path when their modes are not the umask's, which is the case under a set-group-ID folder or a default ACL, where a checkout writes other bits.
  • On macOS the probe is removed even when it was cloned from a file with the uchg flag.

worktree warm:

  • Each entry is cloned, file or directory; when the clone does not happen, for any reason, the entry is copied as before: cp -R, or on Windows fs.cp with its link handling. The intermediate cp -Rc attempt is gone.
  • Windows: cow-clone copies a symbolic link or junction as its reparse data, and a junction's target is always absolute, so a cloned pnpm node_modules would resolve into the source checkout (Windows: worktree warm cannot carry a pnpm node_modules (cp -R fails on its links) #6464 fixed this for the copy). After cloning an entry the warm walks it and points links that lead into the source checkout at the new worktree, with the code the copy uses. The walk does not enter a junction that Node reads as a directory (a volume mount point).

On Windows worktree add does not clone the checkout (maintainer-approved; #6554 tracks what it would need). Git for Windows takes a junction for a directory (compat/mingw.c file_attr_to_st_mode; entry.c remove_subtree has no mount-point check), and a clone keeps every junction's target. A git reset --hard in a cloned tree can therefore delete or overwrite through a junction into the source checkout or wherever it leads, and a ReFS clone is stat-identical to its source for Git for Windows (no inode, creation time as change time), so a copied index hides an edit that kept size and modification time. So on Windows git checks out into an empty directory and the warm, which runs no git write, clones the ignored folders. The cost is the disk of the tracked files only. LLP 0029 records it under "On Windows only the warm clones".

Result equality between the clone path and git worktree add plus warm (macOS, Linux): the tracked files are what #6553 guarantees, now checked by its tree() comparison (bytes, modes, names, link targets) on btrfs and XFS too. A cloned ignored folder keeps each file's mode and modification time, where the warm's copy applies the umask and the current time, as on macOS before.

Limits:

Test plan

  • macOS (APFS), run locally: worktree-add.integration, worktree-warm.integration, api-worktree-warm.integration, the engine store tests, cli-parity, guide, guide-budget, test/e2e/storage-volumes.e2e.js, lint, typecheck, knip, format, and one run of the built CLI on a scratch repository.
  • New clone (<fs>) jobs in ci.yml under full, for ext4, btrfs, XFS, NTFS and ReFS. The btrfs and XFS loopback mounts and the ReFS virtual disk are set up as cow-clone's CI does, with the tests' temporary directory on them.
    • btrfs, XFS: worktree add asserts as a clone of and the clone-only cases run; worktree warm asserts cloned.
    • ext4: the plain path with unsupported.
    • NTFS, ReFS: worktree add asserts the plain path with the Windows reason; on ReFS the warm tests assert cloned and that pnpm junctions resolve inside the new worktree.
  • Tests: the clone mocks match the cow-clone binary and read pairs from stdin; a failing clone is simulated with cow-clone's exit 1 and JSON; a new case covers a cross-device probe, another a cloned folder with a link into the source checkout where the commit has a file.
  • The Windows failure modes above were reasoned from the Git for Windows, libuv and cow-clone sources during review; the one observed in CI was the copied index hiding an edit on ReFS, in a clone (refs) run of an earlier state of this branch that cloned the checkout there.
  • test (node 22) and test (node 24), which pack stim and install it elsewhere in engine-store.e2e.js, pass with the registry dependency.

Fixes #6548

@janicduplessis janicduplessis added area: cli packages/stim-cli, including its README and guide ci-full run the macOS, Windows, Desktop and license jobs on this pull request labels Nov 27, 2026
…ompiled helper

worktree warm, worktree add and the engine store clone through one layer that runs the prebuilt binary of cow-clone 0.1.0. The C helper, its compile step and the xcode-select probe are gone. worktree add tests a clone of one tracked file before git registers anything and names cow-clone's reason when it takes the plain path. It now clones on btrfs and XFS. On Windows git still checks out the tracked files, since it writes through the junctions a clone keeps, and the warm clones the ignored folders on ReFS and a Dev Drive, pointing links that led into the source checkout at the new worktree.

Adds clone lanes on ext4, btrfs, XFS, NTFS and ReFS to the full suite.

Closes #6548
…not give new files' modes, and removes an immutable probe

Under a set-group-ID folder or a default ACL a checkout writes other permission bits than the umask's, which the clone step set. On macOS a probe cloned from a file with the uchg flag could not be removed.
@janicduplessis
janicduplessis marked this pull request as ready for review November 27, 2026 07:03
@janicduplessis janicduplessis added the review: ai-approved A fresh AI review found nothing left to fix label Nov 27, 2026
@janicduplessis
janicduplessis enabled auto-merge (squash) November 27, 2026 07:04
@janicduplessis
janicduplessis merged commit d5cebb0 into main Nov 27, 2026
79 of 80 checks passed
@janicduplessis
janicduplessis deleted the feat/6548-cow-clone branch November 27, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cli packages/stim-cli, including its README and guide ci-full run the macOS, Windows, Desktop and license jobs on this pull request review: ai-approved A fresh AI review found nothing left to fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clone with cow-clone instead of the compiled stim-clonefile helper, on Linux and Windows too

1 participant