diff --git a/docs/git-compatibility.md b/docs/git-compatibility.md index b49383026f..08ba2ecdf5 100644 --- a/docs/git-compatibility.md +++ b/docs/git-compatibility.md @@ -16,8 +16,10 @@ a comparison with Git, including how workflows are different, see the [Git-comparison doc](git-comparison.md). * Configuration: No, the only configuration from Git (e.g. in `~/.gitconfig`) - that's respected is the configuration of remotes. Feel free to file a bug - if you miss any particular configuration options. + that's respected is the following. Feel free to file a bug if you miss any + particular configuration options. + * The configuration of remotes (`[remote ""]`). + * `core.exludesFile` * Authentication: Partial. Only `ssh-agent` or a password-less key file at `~/.ssh/id_rsa` (and only at exactly that path). * Branches: Yes. You can read more about diff --git a/src/commands.rs b/src/commands.rs index b8776a926e..df92a63bc6 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -382,13 +382,21 @@ impl WorkspaceCommandHelper { self.working_copy_shared_with_git } + fn git_config(&self) -> Result { + if let Some(git_repo) = self.repo.store().git_repo() { + git_repo.config() + } else { + git2::Config::open_default() + } + } + fn base_ignores(&self) -> Arc { let mut git_ignores = GitIgnoreFile::empty(); - if let Ok(home_dir) = std::env::var("HOME") { - let home_dir_path = PathBuf::from(home_dir); - // TODO: Look up the name of the file in the core.excludesFile config instead of - // hard-coding its name like this. - git_ignores = git_ignores.chain_with_file("", home_dir_path.join(".gitignore")); + if let Ok(excludes_file_str) = self + .git_config() + .and_then(|git_config| git_config.get_string("core.excludesFile")) + { + git_ignores = git_ignores.chain_with_file("", PathBuf::from(excludes_file_str)); } if let Some(git_repo) = self.repo.store().git_repo() { git_ignores = diff --git a/tests/test_gitignores.rs b/tests/test_gitignores.rs index 8d6d37dc36..6a286a66c0 100644 --- a/tests/test_gitignores.rs +++ b/tests/test_gitignores.rs @@ -26,21 +26,39 @@ fn test_gitignores() { .assert() .success(); - // Say in .git/info/exclude that we don't want file1 and file2 + // Say in core.excludesFiles that we don't want file1, file2, or file3 + let mut file = std::fs::OpenOptions::new() + .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).as_bytes()) + .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() .append(true) .open(workspace_root.join(".git").join("info").join("exclude")) .unwrap(); - file.write_all(b"file1\nfile2").unwrap(); + file.write_all(b"!file2\n!file3").unwrap(); drop(file); - // Say in .gitignore (in the working copy) that we actually do want file2 - std::fs::write(workspace_root.join(".gitignore"), "!file2").unwrap(); + // Say in .gitignore (in the working copy) that we actually do not want file2 + // (again) + std::fs::write(workspace_root.join(".gitignore"), "file2").unwrap(); // Writes some files to the working copy std::fs::write(workspace_root.join("file0"), "contents").unwrap(); std::fs::write(workspace_root.join("file1"), "contents").unwrap(); std::fs::write(workspace_root.join("file2"), "contents").unwrap(); + std::fs::write(workspace_root.join("file3"), "contents").unwrap(); let assert = test_env .jj_cmd(&workspace_root, &["diff", "-s"]) @@ -49,6 +67,6 @@ fn test_gitignores() { insta::assert_snapshot!(get_stdout_string(&assert), @r###" A .gitignore A file0 - A file2 + A file3 "###); }