use std::io::Write as _;
use testutils::git;
use crate::common::TestEnvironment;
#[test]
fn test_gitignores() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
git::init(work_dir.root());
work_dir
.run_jj(["git", "init", "--git-repo", "."])
.success();
let mut file = std::fs::OpenOptions::new()
.append(true)
.open(work_dir.root().join(".git").join("config"))
.unwrap();
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();
let mut file = std::fs::OpenOptions::new()
.append(true)
.open(work_dir.root().join(".git").join("info").join("exclude"))
.unwrap();
file.write_all(b"!file2\n!file3").unwrap();
drop(file);
work_dir.write_file(".gitignore", "file2");
work_dir.write_file("file0", "contents");
work_dir.write_file("file1", "contents");
work_dir.write_file("file2", "contents");
work_dir.write_file("file3", "contents");
let output = work_dir.run_jj(["diff", "-s"]);
insta::assert_snapshot!(output, @r"
A .gitignore
A file0
A file3
[EOF]
");
}
#[test]
fn test_gitignores_relative_excludes_file_path() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
test_env
.run_jj_in(".", ["git", "init", "--colocate", "repo"])
.success();
let mut file = std::fs::OpenOptions::new()
.append(true)
.open(work_dir.root().join(".git").join("config"))
.unwrap();
file.write_all(b"[core]\nexcludesFile=../my-ignores\n")
.unwrap();
drop(file);
std::fs::write(test_env.env_root().join("my-ignores"), "ignored\n").unwrap();
work_dir.write_file("ignored", "");
work_dir.write_file("not-ignored", "");
let sub_dir = work_dir.create_dir("sub");
let output = sub_dir.run_jj(["diff", "-s"]);
insta::assert_snapshot!(output.normalize_backslash(), @r"
A ../not-ignored
[EOF]
");
let output = test_env.run_jj_in(".", ["-Rrepo", "diff", "-s"]);
insta::assert_snapshot!(output.normalize_backslash(), @r"
A repo/not-ignored
[EOF]
");
}
#[test]
fn test_gitignores_ignored_file_in_target_commit() {
let test_env = TestEnvironment::default();
let work_dir = test_env.work_dir("repo");
git::init(work_dir.root());
work_dir
.run_jj(["git", "init", "--git-repo", "."])
.success();
work_dir.write_file("ignored", "committed contents\n");
work_dir
.run_jj(["bookmark", "create", "-r@", "with-file"])
.success();
let target_commit_id = work_dir
.run_jj(["log", "--no-graph", "-T=commit_id", "-r=@"])
.success()
.stdout
.into_raw();
work_dir.run_jj(["new", "root()"]).success();
work_dir.write_file("ignored", "contents in working copy\n");
work_dir.write_file(".gitignore", ".gitignore\nignored\n");
let output = work_dir.run_jj(["edit", "with-file"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Working copy (@) now at: qpvuntsm 3cf51c1a with-file | (no description set)
Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
Added 1 files, modified 0 files, removed 0 files
Warning: 1 of those updates were skipped because there were conflicting changes in the working copy.
Hint: Inspect the changes compared to the intended target with `jj diff --from 3cf51c1acf24`.
Discard the conflicting changes with `jj restore --from 3cf51c1acf24`.
[EOF]
");
let output = work_dir.run_jj(["diff", "--git", "--from", &target_commit_id]);
insta::assert_snapshot!(output, @r"
diff --git a/ignored b/ignored
index 8a69467466..4d9be5127b 100644
--- a/ignored
+++ b/ignored
@@ -1,1 +1,1 @@
-committed contents
+contents in working copy
[EOF]
");
}