Repository navigation
fix: wt refuses a shared path that is not in the worktree - #24
Merged
Merged
Conversation
wt read each path in the config as a plain string, and a person can edit the file. The hook, wt sync and wt ls join each path to the store and to each worktree, and seed_entry removes the file that it finds there. An absolute path gave the same file two times, so the hook removed it and linked the path to itself. An empty path gave the worktree, so wt sync --force removed each file in it that git does not have. `.`, `..` and `../x` gave the worktree or a path outside it. A `/` at the end removed the store, also with no --force. The system follows a link before such a `/`, so a correct link to a directory in the store looked like a copy of the store. wt sync then removed the content of the store and failed. Config::load now parses each path into a SharedPath. It refuses a path that is empty or absolute, or that has the name `.` or `..`, and the error names the path. It keeps only the names, so `.auth/` is `.auth`. A config with such a path no longer loads, and the user corrects it. wt share keeps its own error for the top of the worktree: the error of the config says to give the path from the top of the worktree, and wt share reads a path from the current directory.
The names of a shared path are in the worktree, but its file can be outside it. seed_entry read the path with lstat, which does not follow the last name. The system follows each name above it. With `.auth` a link to the store, `.auth/token` was the file in the store: same_content compared the store with itself, and seed_entry removed the file and made a link from it to itself. A plain wt sync did it, with no --force, and the bytes were lost. A config gets such a path in three ways. The user narrows `.auth` to `.auth/token`, and the old links stay. The config in the dotfiles has `.auth/token` from a different machine, and wt share .auth adds `.auth`. A hand edit gives `terraform/vars.tfvars`, and `terraform` is a link out of the worktree: wt sync --force then replaced the file there. inspect now reads each directory above the path first, and it gives BelowLink when one is a link. seed_entry then changes nothing, also with --force, and wt ls, wt sync, wt share and the hook name the path. The hook matched the other outcomes with `_`, so it now names each one, and Outcome::is_change has no caller. wt makes no link in the store, so `find <common>/shared -type l` finds each file that an older wt lost this way.
The config kept the paths of a repository in a list, and RepoConfig::insert compared a new path only with the same path. wt share .auth therefore added `.auth` to a config that had `.auth/token`, for example from the dotfiles of a different machine. A link at `.auth` makes `.auth/token` the file in the store. Before the last commit, the hook then removed the file in the store at the next wt add. Now each worktree reports the path below a link, at each command. A hand edit also gave `.auth` and `.auth/` two times, and wt ls counted `2/2 links` for one path. RepoConfig::link is now a SharedPaths: a set that refuses a path inside a different one, at Config::load and at insert, and the error names both paths. The names decide, so `.auth-old` is not inside `.auth`. The same path two times loses nothing, so the set keeps it one time. wt share tests the list before it moves anything, and the completion of wt share no longer gives a path inside a shared one. A config with such two paths no longer loads, for each repository in it, and the user corrects it by hand.
With no -z, git ls-files puts a name that is not ASCII in quotes, for example `"\303\251.env"`. SharedPath accepted the quotes as part of the name, and the completion gave a path that is not present. The completion now reads the names with -z.
relative_entry changed a name that is not UTF-8 with to_string_lossy, so the path became a different path, and wt share then said that the path is not in the worktree. The config file holds only UTF-8, so wt share now says that. clap reads each argument as UTF-8, so such a name comes from the current directory.
Config::save wrote the file in place. A reader at the same time, for example the hook of a checkout in a different terminal, could read a part of the file and fail. save now writes a temporary file beside the config and renames it, so a reader gets the old file or the new file. The README asks for the file in the dotfiles, so it can be a link. A rename over the link would put a plain file in its place, and the dotfiles would no longer get the changes. save therefore writes the target of the link. Two wt share at the same time can still lose the path of one of them. wt share of that path again records it, because the store has it.
The last save renamed a temporary file over the result of canonicalize. canonicalize fails on a link to a file that the dotfiles do not have yet, so save put a plain file in place of the link. A rename onto a file that is a mount point, for example in a container, fails with EBUSY, and wt share then moved the path into the store but did not record it. A rename also gave the new text to one name of a hard link only, and the new file got the mode of the umask. link_target now follows each link, and the target does not need to exist. The temporary file gets the mode of the old file. For a hard link and for a mount point, save writes the file in place, as before the last commit.
below_link returned each error except NotFound. Below a plain file the system gives ENOTDIR, so a path such as a/b/x.env with a file at a stopped wt sync, the hook and wt share with an error, before the next path. Before below_link, wt reported the path as missing when the store did not have it. below_link now reads a file above the path as no link, and the next tests report the path as before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #21.
wt read each path in the config as a plain string. A person can edit the file, and the README asks for that. The hook,
wt syncandwt lsjoin each path to the store and to each worktree, andseed_entryremoves what it finds there. An absolute path,"",.,..and../xgive the same file two times, the worktree, or a path outside it.A
/at the end is worse, and the issue does not have it. Withlink = [".auth/"]and a correct link in the worktree, a plainwt sync(no--force) removed the content ofshared/.auth. The system follows a link before a/at the end, solstatsaw a directory,same_contentcompared the store with itself, andremove_dir_allemptied it. The check in the issue (components().all(Normal)) accepts.auth/and.auth/., bcPath::componentsdrops both.SharedPathinsrc/config.rs.Config::loadparses each path into one, so a config with a bad path does not load, and the error names the path:"../x" is not a path in the worktree. give it from the top of the worktree, with no `.` or `..`..or..anywhere in it, so also./.envanda/./b. It keeps only the names, with/between them, asRepoKey::from_urlkeeps one form of a URL..auth/and.auth//are.auth, and the next save writes.auth.RepoConfig::linkisVec<SharedPath>.seed_entry,inspect, the hook,wt lsandwt sharetake a&SharedPath.configis a private module, so luneta sees no change.relative_entryinsrc/cmd/share.rsreturns aSharedPath. It keeps its own check for the top of the worktree, which the issue moves into the constructor: the error of the constructor says to give the path from the top of the worktree, andwt share .would printerror: .: "" is not a path in the worktree.wt shareuses the same parse, which also drops the/that git gives after a directory.A link in a directory above a shared path gives the same failure, and the names do not show it. With
link = [".auth/token"]and.autha link toshared/.auth(the config had.authbefore, and the old links stay), a plainwt syncremovedshared/.auth/tokenand made a link from it to itself. The bytes are lost.wt share .authalso wrote[".auth", ".auth/token"]when the config from the dotfiles had.auth/tokenfrom a different machine, and the nextwt adddid the same. Withterraforma link out of the worktree,wt sync --forcereplaced the file there.inspectreads each directory above the path first, and it givesBelowLinkwhen one is a link.seed_entrythen changes nothing, also with--force, andwt ls,wt sync,wt shareand the hook name the path. The hook matched the other outcomes with_, so it names each one now, andOutcome::is_changeis gone.RepoConfig::linkis aSharedPaths: a set that refuses a path inside a different one, at load and at insert. The error names both paths:".auth/token" is inside ".auth". keep one of them. The names decide, so.auth-oldis not inside.auth. The same path two times is one path, and before[".auth", ".auth/"]gave2/2 links.wt sharetests the list before it moves anything.wt sharereadsgit ls-files -z. With no-z, git puts a name that is not ASCII in quotes, and the completion gave"\303\251.env".wt sharerefuses a path that is not UTF-8.to_string_lossygave a different path.Config::savewrites a temporary file and renames it, so the hook never reads a part of the config. A config that is a link into the dotfiles stays a link, bcsavewrites the target.wt makes no link in the store, so
find <common>/shared -type lfinds each file that an older wt lost this way.A config with a path that is not in the worktree, or with a path inside a different one, stops loading after the upgrade, for each repository in it, as with two keys that differ only in case. The user corrects it by hand.
Test
The four CI gates (
nix build .#wt .#wt-clippy .#wt-test .#wt-fmt) pass.cargo testruns 82 tests./home/you/project/main/.env,/,"",.,..,../.env,./.env,.auth/.anda/../.env. Each is refused, and the error names the path. With no check, each one loads. With the check in the issue,.auth/.loads..auth/andterraform//vars.tfvars. They are.authandterraform/vars.tfvars. With the check in the issue, they stay as they are.seed_entrywith--force. The store and the file outside keep their bytes. With no check, both fail.[".auth", ".auth/token"]in both orders, and each is refused. One loads.authwith.auth-old, and one loads a path two times.wt sharea current directory that is not UTF-8.Manual test: the binary from this branch, with
HOMEandXDG_CONFIG_HOMEin a temp dir, on awt cloneof a local origin with.envand.authshared..auth/,wt synceach worktree is correct, and the store keeps.auth/a.json. On main, the sameseed_entrycall empties the store and fails withwt cannot remove.wt syncandwt lsgit worktree add.envstays."",wt sync --force../x,wt share .envwt share .wt cannot share the top directory of the worktree, as before..authnarrowed to.auth/token, the old links stay,wt syncskipped, and the store keeps.auth/token. On main, the file in the store becomes a link to itself..auth/token,wt share .auth.auth/token. On main, the nextwt addloses the file in the store.terraforma link out of the worktree,wt sync --forceskipped, and the file outside stays.[".auth", ".auth/token"],wt syncé.envignored,wt share <TAB>é.env, with no quotes.