Skip to content

Commit 8677179

Browse files
committed
cli: respect Git's core.excludesFile config (#87)
It probably doesn't make sense to respect Git's `core.excludesFile` config when not running in a Git-backed repo, but we also already respect `.gitignore` files in the working copy regardless of backend, so at least it's consistent with that. We can revisit it when the native backend becomes a reasonable choice. Closes #87.
1 parent 6e2971f commit 8677179

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

docs/git-compatibility.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ The following list describes which Git features Jujutsu is compatible with. For
1515
a comparison with Git, including how workflows are different, see the
1616
[Git-comparison doc](git-comparison.md).
1717

18-
* **Configuration: No.** The only configuration from Git (e.g. in
19-
`~/.gitconfig`) that's respected is the configuration of remotes. Feel free
20-
to file a bug if you miss any particular configuration options.
18+
* **Configuration: Partial.** The only configuration from Git (e.g. in
19+
`~/.gitconfig`) that's respected is the following. Feel free to file a bug if
20+
you miss any particular configuration options.
21+
* The configuration of remotes (`[remote "<name>"]`).
22+
* `core.exludesFile`
2123
* **Authentication: Partial.** Only `ssh-agent` or a password-less key file at
2224
`~/.ssh/id_rsa` (and only at exactly that path).
2325
* **Branches: Yes.** You can read more about

src/commands.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,21 @@ impl WorkspaceCommandHelper {
382382
self.working_copy_shared_with_git
383383
}
384384

385+
fn git_config(&self) -> Result<git2::Config, git2::Error> {
386+
if let Some(git_repo) = self.repo.store().git_repo() {
387+
git_repo.config()
388+
} else {
389+
git2::Config::open_default()
390+
}
391+
}
392+
385393
fn base_ignores(&self) -> Arc<GitIgnoreFile> {
386394
let mut git_ignores = GitIgnoreFile::empty();
387-
if let Ok(home_dir) = std::env::var("HOME") {
388-
let home_dir_path = PathBuf::from(home_dir);
389-
// TODO: Look up the name of the file in the core.excludesFile config instead of
390-
// hard-coding its name like this.
391-
git_ignores = git_ignores.chain_with_file("", home_dir_path.join(".gitignore"));
395+
if let Ok(excludes_file_str) = self
396+
.git_config()
397+
.and_then(|git_config| git_config.get_string("core.excludesFile"))
398+
{
399+
git_ignores = git_ignores.chain_with_file("", PathBuf::from(excludes_file_str));
392400
}
393401
if let Some(git_repo) = self.repo.store().git_repo() {
394402
git_ignores =

tests/test_gitignores.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,39 @@ fn test_gitignores() {
2626
.assert()
2727
.success();
2828

29-
// Say in .git/info/exclude that we don't want file1 and file2
29+
// Say in core.excludesFiles that we don't want file1, file2, or file3
30+
let mut file = std::fs::OpenOptions::new()
31+
.append(true)
32+
.open(workspace_root.join(".git").join("config"))
33+
.unwrap();
34+
let excludes_file_path = test_env
35+
.env_root()
36+
.join("my-ignores")
37+
.to_str()
38+
.unwrap()
39+
.to_string();
40+
file.write_all(format!("[core]\nexcludesFile={}", excludes_file_path).as_bytes())
41+
.unwrap();
42+
drop(file);
43+
std::fs::write(excludes_file_path, "file1\nfile2\nfile3").unwrap();
44+
45+
// Say in .git/info/exclude that we actually do want file2 and file3
3046
let mut file = std::fs::OpenOptions::new()
3147
.append(true)
3248
.open(workspace_root.join(".git").join("info").join("exclude"))
3349
.unwrap();
34-
file.write_all(b"file1\nfile2").unwrap();
50+
file.write_all(b"!file2\n!file3").unwrap();
3551
drop(file);
3652

37-
// Say in .gitignore (in the working copy) that we actually do want file2
38-
std::fs::write(workspace_root.join(".gitignore"), "!file2").unwrap();
53+
// Say in .gitignore (in the working copy) that we actually do not want file2
54+
// (again)
55+
std::fs::write(workspace_root.join(".gitignore"), "file2").unwrap();
3956

4057
// Writes some files to the working copy
4158
std::fs::write(workspace_root.join("file0"), "contents").unwrap();
4259
std::fs::write(workspace_root.join("file1"), "contents").unwrap();
4360
std::fs::write(workspace_root.join("file2"), "contents").unwrap();
61+
std::fs::write(workspace_root.join("file3"), "contents").unwrap();
4462

4563
let assert = test_env
4664
.jj_cmd(&workspace_root, &["diff", "-s"])
@@ -49,6 +67,6 @@ fn test_gitignores() {
4967
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
5068
A .gitignore
5169
A file0
52-
A file2
70+
A file3
5371
"###);
5472
}

0 commit comments

Comments
 (0)