just playing with tangled
1// Copyright 2024 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 crate::common::create_commit_with_files;
16use crate::common::CommandOutput;
17use crate::common::TestEnvironment;
18use crate::common::TestWorkDir;
19
20#[test]
21fn test_backout() {
22 let test_env = TestEnvironment::default();
23 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
24 let work_dir = test_env.work_dir("repo");
25
26 create_commit_with_files(&work_dir, "a", &[], &[("a", "a\n")]);
27 // Test the setup
28 insta::assert_snapshot!(get_log_output(&work_dir), @r"
29 @ 7d980be7a1d4 a
30 ◆ 000000000000
31 [EOF]
32 ");
33 let output = work_dir.run_jj(["diff", "-s"]);
34 insta::assert_snapshot!(output, @r"
35 A a
36 [EOF]
37 ");
38
39 // Backout the commit
40 let output = work_dir.run_jj(["backout", "-r", "@"]);
41 insta::assert_snapshot!(output, @r"
42 ------- stderr -------
43 Warning: `jj backout` is deprecated; use `jj revert` instead
44 Warning: `jj backout` will be removed in a future version, and this will be a hard error
45 [EOF]
46 ");
47 insta::assert_snapshot!(get_log_output(&work_dir), @r#"
48 ○ b8c8e82a19bc Back out "a"
49 │
50 │ This backs out commit 7d980be7a1d499e4d316ab4c01242885032f7eaf.
51 @ 7d980be7a1d4 a
52 ◆ 000000000000
53 [EOF]
54 "#);
55 let output = work_dir.run_jj(["diff", "-s", "-r", "@+"]);
56 insta::assert_snapshot!(output, @r"
57 D a
58 [EOF]
59 ");
60
61 // Backout the new backed-out commit
62 work_dir.run_jj(["edit", "@+"]).success();
63 let output = work_dir.run_jj(["backout", "-r", "@"]);
64 insta::assert_snapshot!(output, @r"
65 ------- stderr -------
66 Warning: `jj backout` is deprecated; use `jj revert` instead
67 Warning: `jj backout` will be removed in a future version, and this will be a hard error
68 [EOF]
69 ");
70 insta::assert_snapshot!(get_log_output(&work_dir), @r#"
71 ○ 812d823be175 Back out "Back out "a""
72 │
73 │ This backs out commit b8c8e82a19bcf6217e065d1aff9a5f0ba807b565.
74 @ b8c8e82a19bc Back out "a"
75 │
76 │ This backs out commit 7d980be7a1d499e4d316ab4c01242885032f7eaf.
77 ○ 7d980be7a1d4 a
78 ◆ 000000000000
79 [EOF]
80 "#);
81 let output = work_dir.run_jj(["diff", "-s", "-r", "@+"]);
82 insta::assert_snapshot!(output, @r"
83 A a
84 [EOF]
85 ");
86}
87
88#[test]
89fn test_backout_multiple() {
90 let test_env = TestEnvironment::default();
91 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
92 let work_dir = test_env.work_dir("repo");
93
94 create_commit_with_files(&work_dir, "a", &[], &[("a", "a\n")]);
95 create_commit_with_files(&work_dir, "b", &["a"], &[("a", "a\nb\n")]);
96 create_commit_with_files(&work_dir, "c", &["b"], &[("a", "a\nb\n"), ("b", "b\n")]);
97 create_commit_with_files(&work_dir, "d", &["c"], &[]);
98 create_commit_with_files(&work_dir, "e", &["d"], &[("a", "a\nb\nc\n")]);
99
100 // Test the setup
101 insta::assert_snapshot!(get_log_output(&work_dir), @r"
102 @ 51a01d6d8cc4 e
103 ○ 4b9d123d3b33 d
104 ○ 05e1f540476f c
105 ○ f93a910dbdf0 b
106 ○ 7d980be7a1d4 a
107 ◆ 000000000000
108 [EOF]
109 ");
110
111 // Backout multiple commits
112 let output = work_dir.run_jj(["backout", "-r", "b", "-r", "c", "-r", "e"]);
113 insta::assert_snapshot!(output, @r"
114 ------- stderr -------
115 Warning: `jj backout` is deprecated; use `jj revert` instead
116 Warning: `jj backout` will be removed in a future version, and this will be a hard error
117 [EOF]
118 ");
119 insta::assert_snapshot!(get_log_output(&work_dir), @r#"
120 ○ 036128aa5f6e Back out "b"
121 │
122 │ This backs out commit f93a910dbdf0f841e6cf2bc0ab0ba4c336d6f436.
123 ○ 156974608bed Back out "c"
124 │
125 │ This backs out commit 05e1f540476f8c4207ff44febbe2ce6e6696dc4b.
126 ○ 3f72017241e0 Back out "e"
127 │
128 │ This backs out commit 51a01d6d8cc48a296cb87f8383b34ade3c050363.
129 @ 51a01d6d8cc4 e
130 ○ 4b9d123d3b33 d
131 ○ 05e1f540476f c
132 ○ f93a910dbdf0 b
133 ○ 7d980be7a1d4 a
134 ◆ 000000000000
135 [EOF]
136 "#);
137 // View the output of each backed out commit
138 let output = work_dir.run_jj(["show", "@+"]);
139 insta::assert_snapshot!(output, @r#"
140 Commit ID: 3f72017241e0a32ab837ae929061cdc05ff04f5b
141 Change ID: wqnwkozpkustnxypnnntnykwrqrkrpvv
142 Author : Test User <test.user@example.com> (2001-02-03 08:05:19)
143 Committer: Test User <test.user@example.com> (2001-02-03 08:05:19)
144
145 Back out "e"
146
147 This backs out commit 51a01d6d8cc48a296cb87f8383b34ade3c050363.
148
149 Modified regular file a:
150 1 1: a
151 2 2: b
152 3 : c
153 [EOF]
154 "#);
155 let output = work_dir.run_jj(["show", "@++"]);
156 insta::assert_snapshot!(output, @r#"
157 Commit ID: 156974608bed539fb98d89c1f6995d962123cdbd
158 Change ID: mouksmquosnpvwqrpsvvxtxpywpnxlss
159 Author : Test User <test.user@example.com> (2001-02-03 08:05:19)
160 Committer: Test User <test.user@example.com> (2001-02-03 08:05:19)
161
162 Back out "c"
163
164 This backs out commit 05e1f540476f8c4207ff44febbe2ce6e6696dc4b.
165
166 Removed regular file b:
167 1 : b
168 [EOF]
169 "#);
170 let output = work_dir.run_jj(["show", "@+++"]);
171 insta::assert_snapshot!(output, @r#"
172 Commit ID: 036128aa5f6eb3770cc8284c0dbe198b2c9a5f62
173 Change ID: tqvpomtpwrqsylrpsxknultrymmqxmxv
174 Author : Test User <test.user@example.com> (2001-02-03 08:05:19)
175 Committer: Test User <test.user@example.com> (2001-02-03 08:05:19)
176
177 Back out "b"
178
179 This backs out commit f93a910dbdf0f841e6cf2bc0ab0ba4c336d6f436.
180
181 Modified regular file a:
182 1 1: a
183 2 : b
184 [EOF]
185 "#);
186}
187
188#[test]
189fn test_backout_description_template() {
190 let test_env = TestEnvironment::default();
191 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
192 test_env.add_config(
193 r#"
194 [templates]
195 backout_description = '''
196 separate(" ",
197 "Revert commit",
198 commit_id.short(),
199 '"' ++ description.first_line() ++ '"',
200 )
201 '''
202 "#,
203 );
204 let work_dir = test_env.work_dir("repo");
205 create_commit_with_files(&work_dir, "a", &[], &[("a", "a\n")]);
206
207 // Test the setup
208 insta::assert_snapshot!(get_log_output(&work_dir), @r"
209 @ 7d980be7a1d4 a
210 ◆ 000000000000
211 [EOF]
212 ");
213 let output = work_dir.run_jj(["diff", "-s"]);
214 insta::assert_snapshot!(output, @r"
215 A a
216 [EOF]
217 ");
218
219 // Verify that message of backed out commit follows the template
220 let output = work_dir.run_jj(["backout", "-r", "a"]);
221 insta::assert_snapshot!(output, @r"
222 ------- stderr -------
223 Warning: `jj backout` is deprecated; use `jj revert` instead
224 Warning: `jj backout` will be removed in a future version, and this will be a hard error
225 [EOF]
226 ");
227 insta::assert_snapshot!(get_log_output(&work_dir), @r#"
228 ○ 6bfb98a33f58 Revert commit 7d980be7a1d4 "a"
229 @ 7d980be7a1d4 a
230 ◆ 000000000000
231 [EOF]
232 "#);
233}
234
235#[must_use]
236fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
237 let template = r#"commit_id.short() ++ " " ++ description"#;
238 work_dir.run_jj(["log", "-T", template])
239}