Skip to content

fix: worktree add gives the files git worktree add writes, in bytes, modes, names and extended attributes - #6553

Merged
janicduplessis merged 2 commits into
mainfrom
fix/6551-clone-equals-checkout
Nov 27, 2026
Merged

janicduplessis merged 2 commits into
mainfrom
fix/6551-clone-equals-checkout

Conversation

@janicduplessis

@janicduplessis janicduplessis commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Description

On the clone path, stim worktree add keeps the source checkout's files wherever git reports them unchanged. #6551 lists the ways that differs from git worktree add then stim worktree warm, each reproduced on main: an untracked .gitattributes applied to the files git writes, an LF file kept where the commit asks for CRLF, attr.tree, a name spelled another way on disk, permission bits beyond the owner's execute bit, and extended attributes.

This makes the tracked files of a cloned worktree equal what git worktree add writes, in bytes, permission bits, names and extended attributes. Three things a cloned file still keeps from the source checkout: its modification time, usually a BSD file flag such as hidden, and the com.apple.provenance attribute, which macOS sets and xattr cannot remove.

Solution

One step between the clone and the refresh (packages/stim-cli/src/workspace/clone-as-checkout.ts) walks the paths of the target commit and either makes a cloned file what a checkout writes or removes it, so git reset --hard writes it:

  • Removed, written by git: a file the commit's attributes convert (ident, filter, working-tree-encoding, an eol other than lf), a file whose size is not its blob's, a file spelled another way on disk in case or Unicode form, and a link or file standing where the commit has a folder.
  • Adjusted in place: permission bits (0666 or 0777 for an executable, 0777 for a folder or a link, less the umask, as git's entry.c does) and extended attributes (xattr -c -s). When a uchg flag stops a change, that flag is cleared from the paths of that xattr batch, or from the whole new worktree for a chmod or a removal (ignored files included), and the step runs again. clonefile(2) as Stim calls it does not copy access control lists; the test checks that.
  • Plain path instead: core.autocrlf=true in any spelling git reads as true, core.eol=crlf, core.symlinks=false, attr.tree or GIT_ATTR_SOURCE, a core.attributesFile inside the worktree (read in the new worktree, whose configuration can differ from the source's), a git older than 2.41, which lacks --attr-source (checked before anything is registered; I could not run an older git, so this is untested), a path that names .git or .. (git refuses the commit), two tracked paths that differ only in case or Unicode form, and a tracked folder spelled another way on disk, which holds ignored files a removal would take. core.autocrlf=input forces nothing.

The refresh and the reset now run with --attr-source=HEAD, so they read .gitattributes from the target commit and not from the working tree, where the clone put the source's files, untracked ones included. That replaces removing those files. They also run with core.checkStat=default and core.fsmonitor=false: under minimal a clone matches its source's entry without being read, and the copied index's fsmonitor bitmap makes git skip files. Both are wrong-bytes cases on main. Blob sizes are read with GIT_NO_LAZY_FETCH=1, so a partial clone does not fetch blobs one at a time; a file whose blob is missing is left to the reset.

Why this is enough for content: the refresh proves that a file cleans to its blob. Once the paths a checkout converts are left to git, the only clean conversion left is CRLF to LF, which changes the size, so a file of its blob's size that the refresh accepts holds the blob's bytes. This holds because the step, the refresh and the reset read the same attributes.

Every removal, chmod and xattr call reaches a path only through folders that are real folders in the new worktree, since a cloned link can lead into the source checkout. A test with an absolute link into the source checks that the source is untouched.

Cost: files a filter, ident, an encoding or eol=crlf applies to are always written by git and share no disk blocks with the source, as on the plain path. Time, medians of 4 alternating runs after a discarded round, load average 10 to 24:

Source checkout Target git worktree add + warm main This PR
expo, 19,021 tracked files, no dependency folders HEAD 2.58 s 5.43 s 6.00 s
expo HEAD~200 3.25 s 6.27 s 6.92 s
stim, 3,811 tracked files, 18 node_modules folders HEAD~50 5.76 s 7.43 s 7.01 s

The step adds about 0.6 s on expo, half of it xattr (0.74 s for 19,021 paths in sequence, 0.28 s four calls at a time, which is what ships), and about 0.2 s on stim by the minimums (6.76 s against 6.54 s; the stim medians are within noise). At the source's commit git wrote 0 of 19,020 files on expo and 1 of 3,811 on stim. The clone path is still slower than the plain path on a large checkout without dependency folders, as on main: the full refresh is unchanged.

The docs state the equality again and list the plain-path cases; this supersedes #6552.

Test plan

worktree-add.integration.test.ts (macOS only, needs clonefile; PR CI does not run it, I ran it locally: 46 pass). The comparison helper now records, for every entry outside .git, the on-disk name, permission bits, extended attribute names and raw bytes or link target, and every existing case that compares with git worktree add + warm uses it. One new case per class runs both commands from the same source, compares the two worktrees and git status, and checks that the source checkout is unchanged:

  • untracked .gitattributes asking for CRLF, at an older commit; LF where the commit asks for CRLF; a CRLF file left from core.autocrlf=true; a CRLF file clean under core.autocrlf=input; ident with another blob's identifier; a smudge filter whose output the source does not hold; the output of a filter the commit no longer names; the output of a filter only an untracked .gitattributes names; working-tree-encoding;
  • other permission bits on a file and a folder, and a run under umask 027;
  • a file name in another case and one decomposed on disk; extended attributes and an ACL; a folder replaced by an absolute link into the source;
  • core.checkStat=minimal with an edit that kept size and time; an fsmonitor hook that hides an edit; a link created under another umask; an immutable file with an extended attribute and one with other permission bits;
  • plain path: a folder in another case, two paths that differ only in case, core.autocrlf=true, the same as a bare key, core.eol=crlf, core.symlinks=false, attr.tree, core.autocrlf=1 included only for the new branch, a core.attributesFile naming an untracked file;
  • a commit whose tree names .git, added inside the source checkout: git refuses it and the source checkout, with a staged file, is unchanged.

Each guard was removed in turn in a copy of the worktree and the suite rerun; every removal fails at least one case:

Guard removed Failing case
--attr-source on the reset untracked .gitattributes; filter the commit no longer names
--attr-source on the refresh filter only an untracked .gitattributes names
eol attribute LF where the commit asks for CRLF
ident / filter / encoding attributes smudge filter
size equals the blob's CRLF file clean under core.autocrlf=input
file mode, folder mode permission bits; umask 027
file name both spelling cases
folder name, collision, core.autocrlf/core.eol, attr.tree the matching plain-path case
xattr extended attributes
link standing where a folder is tracked absolute link into the source
core.checkStat=default, core.fsmonitor=false, core.symlinks, link mode, chflags retry, .git path the matching case each

Real tool calls exercised by these tests: git ls-tree -r -l -z, git check-attr -z --stdin --source=HEAD, git config --type=bool-or-str, git --attr-source=HEAD update-index/reset, /usr/bin/xattr -c -s, /usr/bin/chflags -h nouchg and -R nouchg (Apple Git 2.54, macOS 27).

Fixes #6551

@janicduplessis janicduplessis added area: cli packages/stim-cli, including its README and guide area: website website/ area: docs docs/ and root documents ci-full run the macOS, Windows, Desktop and license jobs on this pull request labels Nov 27, 2026
@janicduplessis
janicduplessis force-pushed the fix/6551-clone-equals-checkout branch from 9098571 to b523d05 Compare November 27, 2026 04:31
@janicduplessis
janicduplessis force-pushed the fix/6551-clone-equals-checkout branch from b523d05 to 23b78d1 Compare November 27, 2026 04:49
@janicduplessis janicduplessis added the review: ai-approved A fresh AI review found nothing left to fix label Nov 27, 2026
@janicduplessis
janicduplessis marked this pull request as ready for review November 27, 2026 04:56
@janicduplessis
janicduplessis enabled auto-merge (squash) November 27, 2026 04:56
@janicduplessis
janicduplessis merged commit 290cf9e into main Nov 27, 2026
67 of 68 checks passed
@janicduplessis
janicduplessis deleted the fix/6551-clone-equals-checkout branch November 27, 2026 05:22
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 area: docs docs/ and root documents area: website website/ 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.

worktree add: ways the clone path differs from git worktree add then worktree warm

1 participant