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.
This commit is contained in:
Martin von Zweigbergk 2022-03-13 08:14:01 -07:00 committed by Martin von Zweigbergk
parent 43e1f42c63
commit c14e138698
4 changed files with 24 additions and 18 deletions

View file

@ -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.

View file

@ -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")

View file

@ -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,

View file

@ -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()