From c14e138698da4ccb9e623aa5c05698432d7621fa Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 13 Mar 2022 08:14:01 -0700 Subject: [PATCH] cli: expand `~` in `core.excludesFile`, don't crash when missing I thought that `std::fs::canonicalize()` expanded "~", but it doesn't seem to do that, which caused #131. Git seems to do the expansion itself, so we probably also should. More importantly `std::fs::canonicalize()` crashes when the file doesn't exist. The manual expansion we do now does not. Closes #131. --- CHANGELOG.md | 5 +++++ src/commands.rs | 13 +++++++++++-- src/testutils.rs | 1 + tests/test_gitignores.rs | 23 +++++++---------------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce47332a2..270fb252a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed bugs + + - (#131) Fixed crash when `core.excludesFile` pointed to non-existent file, and + made leading `~/` in that config expand to `$HOME/` + ## [0.3.0] - 2022-03-12 Last release before this changelog started. diff --git a/src/commands.rs b/src/commands.rs index 4eb71f9d2..16b0861c6 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -396,8 +396,7 @@ impl WorkspaceCommandHelper { .git_config() .and_then(|git_config| git_config.get_string("core.excludesFile")) { - let excludes_file_path = - std::fs::canonicalize(PathBuf::from(excludes_file_str)).unwrap(); + let excludes_file_path = expand_git_path(excludes_file_str); git_ignores = git_ignores.chain_with_file("", excludes_file_path); } if let Some(git_repo) = self.repo.store().git_repo() { @@ -708,6 +707,16 @@ impl WorkspaceCommandHelper { } } +/// Expands "~/" to "$HOME/" as Git seems to do for e.g. core.excludesFile. +fn expand_git_path(path_str: String) -> PathBuf { + if let Some(remainder) = path_str.strip_prefix("~/") { + if let Ok(home_dir_str) = std::env::var("HOME") { + return PathBuf::from(home_dir_str).join(remainder); + } + } + PathBuf::from(path_str) +} + fn rev_arg<'help>() -> Arg<'help> { Arg::new("revision") .long("revision") diff --git a/src/testutils.rs b/src/testutils.rs index b3d070cc0..363de5b19 100644 --- a/src/testutils.rs +++ b/src/testutils.rs @@ -31,6 +31,7 @@ impl Default for TestEnvironment { let tmp_dir = TempDir::new().unwrap(); let env_root = tmp_dir.path().canonicalize().unwrap(); let home_dir = env_root.join("home"); + std::fs::create_dir(&home_dir).unwrap(); Self { _temp_dir: tmp_dir, env_root, diff --git a/tests/test_gitignores.rs b/tests/test_gitignores.rs index 8042ad189..b822a241c 100644 --- a/tests/test_gitignores.rs +++ b/tests/test_gitignores.rs @@ -31,24 +31,15 @@ fn test_gitignores() { .append(true) .open(workspace_root.join(".git").join("config")) .unwrap(); - let excludes_file_path = test_env - .env_root() - .join("my-ignores") - .to_str() - .unwrap() - .to_string(); - file.write_all( - format!( - "[core]\nexcludesFile=\"{}\"", - excludes_file_path - .replace('\\', "\\\\") - .replace('\"', "\\\"") - ) - .as_bytes(), + // Put the file in "~/my-ignores" so we also test that "~" expands to "$HOME" + file.write_all(b"[core]\nexcludesFile=~/my-ignores\n") + .unwrap(); + drop(file); + std::fs::write( + test_env.home_dir().join("my-ignores"), + "file1\nfile2\nfile3", ) .unwrap(); - drop(file); - std::fs::write(excludes_file_path, "file1\nfile2\nfile3").unwrap(); // Say in .git/info/exclude that we actually do want file2 and file3 let mut file = std::fs::OpenOptions::new()