From 8336c489fa1a28bddf1aa695b91c875a36265f8d Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Fri, 11 Mar 2022 15:46:20 -0800 Subject: [PATCH] cli: respect .git/info/exclude if in Git-backed repo (#65) Closes #65. --- src/commands.rs | 6 +++-- tests/test_gitignores.rs | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tests/test_gitignores.rs diff --git a/src/commands.rs b/src/commands.rs index b702a443d..e7a814e64 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -390,8 +390,10 @@ impl WorkspaceCommandHelper { // hard-coding its name like this. git_ignores = git_ignores.chain_with_file("", home_dir_path.join(".gitignore")); } - // TODO: Chain with the .jj/git/info/exclude file if we're inside a git-backed - // repo. + if let Some(git_repo) = self.repo.store().git_repo() { + git_ignores = + git_ignores.chain_with_file("", git_repo.path().join("info").join("exclude")); + } git_ignores } diff --git a/tests/test_gitignores.rs b/tests/test_gitignores.rs new file mode 100644 index 000000000..8d6d37dc3 --- /dev/null +++ b/tests/test_gitignores.rs @@ -0,0 +1,54 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::io::Write; + +use jujutsu::testutils::{get_stdout_string, TestEnvironment}; + +#[test] +fn test_gitignores() { + let test_env = TestEnvironment::default(); + let workspace_root = test_env.env_root().join("repo"); + git2::Repository::init(&workspace_root).unwrap(); + test_env + .jj_cmd(&workspace_root, &["init", "--git-repo", "."]) + .assert() + .success(); + + // Say in .git/info/exclude that we don't want file1 and file2 + 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(); + drop(file); + + // Say in .gitignore (in the working copy) that we actually do want file2 + 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(); + + let assert = test_env + .jj_cmd(&workspace_root, &["diff", "-s"]) + .assert() + .success(); + insta::assert_snapshot!(get_stdout_string(&assert), @r###" + A .gitignore + A file0 + A file2 + "###); +}