just playing with tangled
1// Copyright 2020 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::io::Write as _;
16
17use testutils::git;
18
19use crate::common::TestEnvironment;
20
21#[test]
22fn test_gitignores() {
23 let test_env = TestEnvironment::default();
24 let work_dir = test_env.work_dir("repo");
25 git::init(work_dir.root());
26 work_dir
27 .run_jj(["git", "init", "--git-repo", "."])
28 .success();
29
30 // Say in core.excludesFiles that we don't want file1, file2, or file3
31 let mut file = std::fs::OpenOptions::new()
32 .append(true)
33 .open(work_dir.root().join(".git").join("config"))
34 .unwrap();
35 // Put the file in "~/my-ignores" so we also test that "~" expands to "$HOME"
36 file.write_all(b"[core]\nexcludesFile=~/my-ignores\n")
37 .unwrap();
38 drop(file);
39 std::fs::write(
40 test_env.home_dir().join("my-ignores"),
41 "file1\nfile2\nfile3",
42 )
43 .unwrap();
44
45 // Say in .git/info/exclude that we actually do want file2 and file3
46 let mut file = std::fs::OpenOptions::new()
47 .append(true)
48 .open(work_dir.root().join(".git").join("info").join("exclude"))
49 .unwrap();
50 file.write_all(b"!file2\n!file3").unwrap();
51 drop(file);
52
53 // Say in .gitignore (in the working copy) that we actually do not want file2
54 // (again)
55 work_dir.write_file(".gitignore", "file2");
56
57 // Writes some files to the working copy
58 work_dir.write_file("file0", "contents");
59 work_dir.write_file("file1", "contents");
60 work_dir.write_file("file2", "contents");
61 work_dir.write_file("file3", "contents");
62
63 let output = work_dir.run_jj(["diff", "-s"]);
64 insta::assert_snapshot!(output, @r"
65 A .gitignore
66 A file0
67 A file3
68 [EOF]
69 ");
70}
71
72#[test]
73fn test_gitignores_relative_excludes_file_path() {
74 let test_env = TestEnvironment::default();
75 let work_dir = test_env.work_dir("repo");
76 test_env
77 .run_jj_in(".", ["git", "init", "--colocate", "repo"])
78 .success();
79
80 let mut file = std::fs::OpenOptions::new()
81 .append(true)
82 .open(work_dir.root().join(".git").join("config"))
83 .unwrap();
84 file.write_all(b"[core]\nexcludesFile=../my-ignores\n")
85 .unwrap();
86 drop(file);
87 std::fs::write(test_env.env_root().join("my-ignores"), "ignored\n").unwrap();
88
89 work_dir.write_file("ignored", "");
90 work_dir.write_file("not-ignored", "");
91
92 // core.excludesFile should be resolved relative to the workspace root, not
93 // to the cwd.
94 let sub_dir = work_dir.create_dir("sub");
95 let output = sub_dir.run_jj(["diff", "-s"]);
96 insta::assert_snapshot!(output.normalize_backslash(), @r"
97 A ../not-ignored
98 [EOF]
99 ");
100 let output = test_env.run_jj_in(".", ["-Rrepo", "diff", "-s"]);
101 insta::assert_snapshot!(output.normalize_backslash(), @r"
102 A repo/not-ignored
103 [EOF]
104 ");
105}
106
107#[test]
108fn test_gitignores_ignored_file_in_target_commit() {
109 let test_env = TestEnvironment::default();
110 let work_dir = test_env.work_dir("repo");
111 git::init(work_dir.root());
112 work_dir
113 .run_jj(["git", "init", "--git-repo", "."])
114 .success();
115
116 // Create a commit with file "ignored" in it
117 work_dir.write_file("ignored", "committed contents\n");
118 work_dir
119 .run_jj(["bookmark", "create", "-r@", "with-file"])
120 .success();
121 let target_commit_id = work_dir
122 .run_jj(["log", "--no-graph", "-T=commit_id", "-r=@"])
123 .success()
124 .stdout
125 .into_raw();
126
127 // Create another commit where we ignore that path
128 work_dir.run_jj(["new", "root()"]).success();
129 work_dir.write_file("ignored", "contents in working copy\n");
130 work_dir.write_file(".gitignore", ".gitignore\nignored\n");
131
132 // Update to the commit with the "ignored" file
133 let output = work_dir.run_jj(["edit", "with-file"]);
134 insta::assert_snapshot!(output, @r"
135 ------- stderr -------
136 Working copy (@) now at: qpvuntsm 5ada929e with-file | (no description set)
137 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
138 Added 1 files, modified 0 files, removed 0 files
139 Warning: 1 of those updates were skipped because there were conflicting changes in the working copy.
140 Hint: Inspect the changes compared to the intended target with `jj diff --from 5ada929e5d2e`.
141 Discard the conflicting changes with `jj restore --from 5ada929e5d2e`.
142 [EOF]
143 ");
144 let output = work_dir.run_jj(["diff", "--git", "--from", &target_commit_id]);
145 insta::assert_snapshot!(output, @r"
146 diff --git a/ignored b/ignored
147 index 8a69467466..4d9be5127b 100644
148 --- a/ignored
149 +++ b/ignored
150 @@ -1,1 +1,1 @@
151 -committed contents
152 +contents in working copy
153 [EOF]
154 ");
155}