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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,12 @@ link.
|---|---|
| The two files are the same. | `wt` replaces the file with a link. |
| The two files are different. | `wt` keeps the file and prints a message. |
| A directory above the path is a link. | `wt` changes nothing and prints a message. |

Use the option `--force` to replace a file that is different.
Use the option `--force` to replace a file that is different. A path below a
link is not in the worktree: when `.auth` is a link to the store,
`.auth/token` is the file in the store. `wt` changes nothing there, also with
`--force`.

### Commands

Expand Down Expand Up @@ -294,6 +298,17 @@ The case of the key does not matter, because GitHub ignores it. A clone of
keys that differ only in case. They are the same repository, so merge their
lists into one entry. An entry with an empty list is not part of this rule.

Each path starts at the top of the worktree, for example `.env` or
`terraform/terraform.tfvars`. `wt` refuses a file with a path that is empty or
absolute, or that has the name `.` or `..` in it, for example `../.env`. `wt`
replaces the file at a shared path with a link, and such a path can give the
worktree itself or a file outside it. A `/` at the end is not part of the
path, so `.auth/` is `.auth`.

`wt` also refuses a file with a path inside a different one, for example
`.auth` and `.auth/token`. A link at `.auth` makes `.auth/token` the file in
the store. The same path two times is one path.

`wt` reads `$XDG_CONFIG_HOME/wt/config.toml` when that variable is set.

The file holds no secret. It holds only path names. The content stays in the
Expand Down
11 changes: 8 additions & 3 deletions src/cmd/completions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::SharedPath;
use crate::git;
use crate::repo::Repo;
use anyhow::Result;
Expand Down Expand Up @@ -53,12 +54,16 @@ pub fn shareable(repo: &Repo) -> Result<()> {
"--ignored",
"--exclude-standard",
"--directory",
// With no -z, git puts a name that is not ASCII in quotes.
"-z",
],
)?;
let already = repo.manifest().unwrap_or_default();
for line in text.lines() {
let path = line.trim_end_matches('/');
if !path.is_empty() && !already.contains(path) {
for name in text.split('\0') {
// Git gives a directory with `/` at the end. The shared path has none.
if let Ok(path) = SharedPath::try_from(name.to_string())
&& already.link.overlap(&path).is_none()
{
println!("{path}");
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/cmd/ls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::SharedPaths;
use crate::hook;
use crate::repo::Repo;
use crate::seed::{State, inspect};
Expand Down Expand Up @@ -54,7 +55,7 @@ pub fn run(repo: &Repo) -> Result<()> {
/// Count the links that are correct, and name the paths that are not.
///
/// This function only reports. `inspect` changes nothing.
fn links(shared: &std::path::Path, wt: &crate::repo::Worktree, entries: &[String]) -> String {
fn links(shared: &std::path::Path, wt: &crate::repo::Worktree, entries: &SharedPaths) -> String {
let mut good = 0usize;
let mut problems = Vec::new();

Expand All @@ -66,6 +67,7 @@ fn links(shared: &std::path::Path, wt: &crate::repo::Worktree, entries: &[String
Ok(State::SameFile) => problems.push(format!("{entry} is a copy")),
Ok(State::DifferentFile) => problems.push(format!("{entry} differs")),
Ok(State::MissingInStore) => problems.push(format!("{entry} not in store")),
Ok(State::BelowLink) => problems.push(format!("{entry} below a link")),
Err(e) => problems.push(format!("{entry}: {e}")),
}
}
Expand Down
61 changes: 51 additions & 10 deletions src/cmd/share.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::Config;
use crate::config::{Config, SharedPath};
use crate::git;
use crate::repo::Repo;
use crate::seed::{Outcome, seed_entry};
Expand Down Expand Up @@ -38,14 +38,22 @@ fn share_one(
let source = toplevel.join(&entry);
let stored = repo.shared().join(&entry);

if manifest.contains(&entry) {
bail!("the config file contains this path");
// Before wt moves anything: the list refuses a path inside a different
// one.
if let Some(other) = manifest.link.overlap(&entry) {
if *other == entry {
bail!("the config file contains this path");
}
bail!(
"the config file contains {other}, and one of the two paths is inside the other\n \
remove {other} from the config first"
);
}

// Git supplies each tracked path. A link over a tracked path hides the
// checkout. This test also refuses `wt share terraform` when git ignores
// only terraform/terraform.tfvars.
let tracked = git::out(toplevel, &["ls-files", "--", &entry])?;
let tracked = git::out(toplevel, &["ls-files", "--", entry.as_str()])?;
if !tracked.is_empty() {
let first = tracked.lines().next().unwrap_or_default();
bail!(
Expand All @@ -54,7 +62,7 @@ fn share_one(
);
}

if !git::ok(toplevel, &["check-ignore", "-q", "--", &entry]) {
if !git::ok(toplevel, &["check-ignore", "-q", "--", entry.as_str()]) {
bail!("git does not ignore this path. wt controls only the ignored paths");
}

Expand All @@ -68,7 +76,10 @@ fn share_one(
&& fs::read_link(&source).ok().as_deref() == Some(stored.as_path());

if link_is_correct || !source.exists() {
manifest.insert(&entry);
manifest
.link
.insert(entry.clone())
.map_err(anyhow::Error::msg)?;
eprintln!(" recorded {entry} (the store already has it)");
backfill(repo, &entry, force)?;
return Ok(());
Expand Down Expand Up @@ -98,13 +109,16 @@ fn share_one(
entry
);

manifest.insert(&entry);
manifest
.link
.insert(entry.clone())
.map_err(anyhow::Error::msg)?;
backfill(repo, &entry, force)?;
Ok(())
}

/// Make the link in each worktree. This worktree is one of them.
fn backfill(repo: &Repo, entry: &str, force: bool) -> Result<()> {
fn backfill(repo: &Repo, entry: &SharedPath, force: bool) -> Result<()> {
let shared = repo.shared();
let mut linked = Vec::new();
for wt in repo.worktrees()? {
Expand All @@ -114,6 +128,10 @@ fn backfill(repo: &Repo, entry: &str, force: bool) -> Result<()> {
" skipped {} (a different file is present. use --force to replace it)",
wt.name()
),
Outcome::SkippedBelowLink => eprintln!(
" skipped {} (a directory above the path is a link)",
wt.name()
),
Outcome::MissingInStore | Outcome::AlreadyLinked => {}
}
}
Expand All @@ -124,7 +142,7 @@ fn backfill(repo: &Repo, entry: &str, force: bool) -> Result<()> {
}

/// Change a path from the user into a path that is relative to the worktree.
fn relative_entry(toplevel: &Path, cwd: &Path, raw: &str) -> Result<String> {
fn relative_entry(toplevel: &Path, cwd: &Path, raw: &str) -> Result<SharedPath> {
let joined = if Path::new(raw).is_absolute() {
PathBuf::from(raw)
} else {
Expand All @@ -147,7 +165,12 @@ fn relative_entry(toplevel: &Path, cwd: &Path, raw: &str) -> Result<String> {
if rel.as_os_str().is_empty() {
bail!("wt cannot share the top directory of the worktree");
}
Ok(rel.to_string_lossy().into_owned())
// The config file holds only UTF-8. A name that is not UTF-8 would
// become a different name.
let rel = rel
.to_str()
.context("the config file holds only UTF-8, and this path is not UTF-8")?;
SharedPath::try_from(rel.to_string()).map_err(anyhow::Error::msg)
}

fn display_rel(base: &Path, p: &Path) -> String {
Expand All @@ -156,3 +179,21 @@ fn display_rel(base: &Path, p: &Path) -> String {
.to_string_lossy()
.into_owned()
}

#[cfg(test)]
mod tests {
use super::*;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;

/// The config file holds only UTF-8. A name that is not UTF-8 would
/// become a different path.
#[test]
fn a_path_that_is_not_utf8_is_refused() {
let cwd = Path::new("/top").join(OsStr::from_bytes(b"sub\xff"));

let error = relative_entry(Path::new("/top"), &cwd, ".env").unwrap_err();

assert!(error.to_string().contains("not UTF-8"), "{error}");
}
}
17 changes: 16 additions & 1 deletion src/cmd/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn run(repo: &Repo, force: bool) -> Result<()> {
let shared = repo.shared();
let mut changes = 0usize;
let mut skipped = 0usize;
let mut below_link = 0usize;

for wt in repo.worktrees()? {
for entry in &manifest.link {
Expand All @@ -38,17 +39,31 @@ pub fn run(repo: &Repo, force: bool) -> Result<()> {
);
skipped += 1;
}
Outcome::SkippedBelowLink => {
eprintln!(
" skipped {}/{} (a directory above it is a link)",
wt.name(),
entry
);
below_link += 1;
}
Outcome::AlreadyLinked => {}
}
}
}

if changes == 0 && skipped == 0 {
if changes == 0 && skipped == 0 && below_link == 0 {
eprintln!("each worktree is correct");
} else if skipped > 0 {
eprintln!(
"{changes} link(s) made. {skipped} path(s) need your attention. use --force to replace them"
);
}
// --force does not change a path below a link.
if below_link > 0 {
eprintln!(
"{below_link} path(s) are below a link, and wt changes nothing there. remove the link, or remove the path from the config"
);
}
Ok(())
}
Loading
Loading