Debian Codegraph @

lib/tests/test_commit_builder.rs

raw
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
15 use assert_matches::assert_matches;
16 use futures::StreamExt as _;
17 use indoc::indoc;
18 use jj_lib::backend::BackendError;
19 use jj_lib::backend::ChangeId;
20 use jj_lib::backend::MillisSinceEpoch;
21 use jj_lib::backend::Signature;
22 use jj_lib::backend::Timestamp;
23 use jj_lib::config::ConfigLayer;
24 use jj_lib::config::ConfigSource;
25 use jj_lib::config::StackedConfig;
26 use jj_lib::matchers::EverythingMatcher;
27 use jj_lib::merged_tree::MergedTree;
28 use jj_lib::repo::Repo as _;
29 use jj_lib::repo_path::RepoPath;
30 use jj_lib::repo_path::RepoPathBuf;
31 use jj_lib::rewrite::RebaseOptions;
32 use jj_lib::settings::UserSettings;
33 use pollster::FutureExt as _;
34 use test_case::test_case;
35 use testutils::CommitBuilderExt as _;
36 use testutils::TestRepo;
37 use testutils::TestRepoBackend;
38 use testutils::TestResult;
39 use testutils::assert_rebased_onto;
40 use testutils::assert_tree_eq;
41 use testutils::create_tree;
42 use testutils::rebase_descendants_with_options_return_map;
43 use testutils::repo_path;
44 use testutils::write_random_commit;
45 use testutils::write_random_commit_with_parents;
46
47 fn config_with_commit_timestamp(timestamp: &str) -> StackedConfig {
48 let mut config = testutils::base_user_config();
49 let mut layer = ConfigLayer::empty(ConfigSource::User);
50 layer
51 .set_value("debug.commit-timestamp", timestamp)
52 .unwrap();
53 config.add_layer(layer);
54 config
55 }
56
57 fn diff_paths(from_tree: &MergedTree, to_tree: &MergedTree) -> Vec<RepoPathBuf> {
58 from_tree
59 .diff_stream(to_tree, &EverythingMatcher)
60 .map(|diff| {
61 diff.values.unwrap();
62 diff.path
63 })
64 .collect()
65 .block_on()
66 }
67
68 fn to_owned_path_vec(paths: &[&RepoPath]) -> Vec<RepoPathBuf> {
69 paths.iter().map(|&path| path.to_owned()).collect()
70 }
71
72 #[test_case(TestRepoBackend::Simple ; "simple backend")]
73 #[test_case(TestRepoBackend::Git ; "git backend")]
74 fn test_initial(backend: TestRepoBackend) -> TestResult {
75 let test_repo = TestRepo::init_with_backend(backend);
76 let repo = &test_repo.repo;
77 let store = repo.store();
78
79 let root_file_path = repo_path("file");
80 let dir_file_path = repo_path("dir/file");
81 let tree = create_tree(
82 repo,
83 &[
84 (root_file_path, "file contents"),
85 (dir_file_path, "dir/file contents"),
86 ],
87 );
88
89 let mut tx = repo.start_transaction();
90 let author_signature = Signature {
91 name: "author name".to_string(),
92 email: "author email".to_string(),
93 timestamp: Timestamp {
94 timestamp: MillisSinceEpoch(1000),
95 tz_offset: 60,
96 },
97 };
98 let committer_signature = Signature {
99 name: "committer name".to_string(),
100 email: "committer email".to_string(),
101 timestamp: Timestamp {
102 timestamp: MillisSinceEpoch(2000),
103 tz_offset: -60,
104 },
105 };
106 let change_id = ChangeId::new(vec![100u8; 16]);
107 let builder = tx
108 .repo_mut()
109 .new_commit(vec![store.root_commit_id().clone()], tree.clone())
110 .set_change_id(change_id.clone())
111 .set_description("description")
112 .set_author(author_signature.clone())
113 .set_committer(committer_signature.clone());
114 assert_eq!(builder.parents(), &[store.root_commit_id().clone()]);
115 assert_eq!(builder.predecessors(), &[]);
116 assert_tree_eq!(builder.tree(), tree);
117 assert_eq!(builder.change_id(), &change_id);
118 assert_eq!(builder.author(), &author_signature);
119 assert_eq!(builder.committer(), &committer_signature);
120 let commit = builder.write_unwrap();
121 let repo = tx.commit("test").block_on()?;
122
123 let parents = commit.parents().block_on()?;
124 assert_eq!(parents, vec![store.root_commit()]);
125 assert!(commit.store_commit().predecessors.is_empty());
126 assert_eq!(commit.description(), "description");
127 assert_eq!(commit.author(), &author_signature);
128 assert_eq!(commit.committer(), &committer_signature);
129 assert_eq!(
130 diff_paths(&store.root_commit().tree(), &commit.tree()),
131 to_owned_path_vec(&[dir_file_path, root_file_path]),
132 );
133 assert_matches!(
134 repo.operation().predecessors_for_commit(commit.id()),
135 Some([])
136 );
137 Ok(())
138 }
139
140 #[test_case(TestRepoBackend::Simple ; "simple backend")]
141 #[test_case(TestRepoBackend::Git ; "git backend")]
142 fn test_rewrite(backend: TestRepoBackend) -> TestResult {
143 let settings = testutils::user_settings();
144 let test_repo = TestRepo::init_with_backend_and_settings(backend, &settings);
145 let test_env = &test_repo.env;
146 let repo = &test_repo.repo;
147 let store = repo.store();
148
149 let root_file_path = repo_path("file");
150 let dir_file_path = repo_path("dir/file");
151 let initial_tree = create_tree(
152 repo,
153 &[
154 (root_file_path, "file contents"),
155 (dir_file_path, "dir/file contents"),
156 ],
157 );
158
159 let mut tx = repo.start_transaction();
160 let initial_commit = tx
161 .repo_mut()
162 .new_commit(vec![store.root_commit_id().clone()], initial_tree)
163 .write_unwrap();
164 let repo = tx.commit("test").block_on()?;
165
166 let rewritten_tree = create_tree(
167 &repo,
168 &[
169 (root_file_path, "file contents"),
170 (dir_file_path, "updated dir/file contents"),
171 ],
172 );
173
174 let mut config = StackedConfig::with_defaults();
175 config.add_layer(ConfigLayer::parse(
176 ConfigSource::User,
177 indoc! {"
178 user.name = 'Rewrite User'
179 user.email = '[email protected]'
180 "},
181 )?);
182 let rewrite_settings = UserSettings::from_config(config)?;
183 let repo = test_env.load_repo_at_head(&rewrite_settings, test_repo.repo_path());
184 let store = repo.store();
185 // We have a new store instance, so we need to associate the old tree with the
186 // new store instance.
187 let (tree_ids, labels) = rewritten_tree.into_tree_ids_and_labels();
188 let rewritten_tree = MergedTree::new(store.clone(), tree_ids, labels);
189 let initial_commit = store.get_commit(initial_commit.id())?;
190 let mut tx = repo.start_transaction();
191 let rewritten_commit = tx
192 .repo_mut()
193 .rewrite_commit(&initial_commit)
194 .set_tree(rewritten_tree)
195 .write_unwrap();
196 tx.repo_mut().rebase_descendants().block_on()?;
197 let repo = tx.commit("test").block_on()?;
198 let parents = rewritten_commit.parents().block_on()?;
199 assert_eq!(parents, vec![store.root_commit()]);
200 assert_eq!(
201 rewritten_commit.store_commit().predecessors,
202 [initial_commit.id().clone()]
203 );
204 assert_eq!(rewritten_commit.author().name, settings.user_name());
205 assert_eq!(rewritten_commit.author().email, settings.user_email());
206 assert_eq!(
207 rewritten_commit.committer().name,
208 rewrite_settings.user_name()
209 );
210 assert_eq!(
211 rewritten_commit.committer().email,
212 rewrite_settings.user_email()
213 );
214 assert_eq!(
215 diff_paths(&store.root_commit().tree(), &rewritten_commit.tree()),
216 to_owned_path_vec(&[dir_file_path, root_file_path]),
217 );
218 assert_eq!(
219 diff_paths(&initial_commit.tree(), &rewritten_commit.tree()),
220 to_owned_path_vec(&[dir_file_path]),
221 );
222 assert_matches!(
223 repo.operation().predecessors_for_commit(rewritten_commit.id()),
224 Some([id]) if id == initial_commit.id()
225 );
226 assert_matches!(
227 repo.operation()
228 .predecessors_for_commit(initial_commit.id()),
229 None
230 );
231 Ok(())
232 }
233
234 // An author field with an empty name/email should get filled in on rewrite
235 #[test_case(TestRepoBackend::Simple ; "simple backend")]
236 #[test_case(TestRepoBackend::Git ; "git backend")]
237 fn test_rewrite_update_missing_user(backend: TestRepoBackend) -> TestResult {
238 let missing_user_settings = UserSettings::from_config(StackedConfig::with_defaults())?;
239 let test_repo = TestRepo::init_with_backend_and_settings(backend, &missing_user_settings);
240 let test_env = &test_repo.env;
241 let repo = &test_repo.repo;
242
243 let mut tx = repo.start_transaction();
244 let initial_commit = tx
245 .repo_mut()
246 .new_commit(
247 vec![repo.store().root_commit_id().clone()],
248 repo.store().empty_merged_tree(),
249 )
250 .write_unwrap();
251 assert_eq!(initial_commit.author().name, "");
252 assert_eq!(initial_commit.author().email, "");
253 assert_eq!(initial_commit.committer().name, "");
254 assert_eq!(initial_commit.committer().email, "");
255 tx.commit("test").block_on()?;
256
257 let mut config = StackedConfig::with_defaults();
258 config.add_layer(ConfigLayer::parse(
259 ConfigSource::User,
260 indoc! {"
261 user.name = 'Configured User'
262 user.email = '[email protected]'
263 "},
264 )?);
265 let settings = UserSettings::from_config(config)?;
266 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
267 let initial_commit = repo.store().get_commit(initial_commit.id())?;
268 let mut tx = repo.start_transaction();
269 let rewritten_commit = tx.repo_mut().rewrite_commit(&initial_commit).write_unwrap();
270
271 assert_eq!(rewritten_commit.author().name, "Configured User");
272 assert_eq!(
273 rewritten_commit.author().email,
274 "[email protected]"
275 );
276 assert_eq!(rewritten_commit.committer().name, "Configured User");
277 assert_eq!(
278 rewritten_commit.committer().email,
279 "[email protected]"
280 );
281 Ok(())
282 }
283
284 #[test_case(TestRepoBackend::Simple ; "simple backend")]
285 #[test_case(TestRepoBackend::Git ; "git backend")]
286 fn test_rewrite_resets_author_timestamp(backend: TestRepoBackend) -> TestResult {
287 let test_repo = TestRepo::init_with_backend(backend);
288 let test_env = &test_repo.env;
289
290 // Create discardable commit
291 let initial_timestamp = "2001-02-03T04:05:06+07:00";
292 let settings = UserSettings::from_config(config_with_commit_timestamp(initial_timestamp))?;
293 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
294 let mut tx = repo.start_transaction();
295 let initial_commit = tx
296 .repo_mut()
297 .new_commit(
298 vec![repo.store().root_commit_id().clone()],
299 repo.store().empty_merged_tree(),
300 )
301 .write_unwrap();
302 tx.commit("test").block_on()?;
303
304 let initial_timestamp =
305 Timestamp::from_datetime(chrono::DateTime::parse_from_rfc3339(initial_timestamp)?);
306 assert_eq!(initial_commit.author().timestamp, initial_timestamp);
307 assert_eq!(initial_commit.committer().timestamp, initial_timestamp);
308
309 // Rewrite discardable commit to no longer be discardable
310 let new_timestamp_1 = "2002-03-04T05:06:07+08:00";
311 let settings = UserSettings::from_config(config_with_commit_timestamp(new_timestamp_1))?;
312 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
313 let initial_commit = repo.store().get_commit(initial_commit.id())?;
314 let mut tx = repo.start_transaction();
315 let rewritten_commit_1 = tx
316 .repo_mut()
317 .rewrite_commit(&initial_commit)
318 .set_description("No longer discardable")
319 .write_unwrap();
320 tx.repo_mut().rebase_descendants().block_on()?;
321 tx.commit("test").block_on()?;
322
323 let new_timestamp_1 =
324 Timestamp::from_datetime(chrono::DateTime::parse_from_rfc3339(new_timestamp_1)?);
325 assert_ne!(new_timestamp_1, initial_timestamp);
326
327 assert_eq!(rewritten_commit_1.author().timestamp, new_timestamp_1);
328 assert_eq!(rewritten_commit_1.committer().timestamp, new_timestamp_1);
329 assert_eq!(rewritten_commit_1.author(), rewritten_commit_1.committer());
330
331 // Rewrite non-discardable commit
332 let new_timestamp_2 = "2003-04-05T06:07:08+09:00";
333 let settings = UserSettings::from_config(config_with_commit_timestamp(new_timestamp_2))?;
334 let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
335 let rewritten_commit_1 = repo.store().get_commit(rewritten_commit_1.id())?;
336 let mut tx = repo.start_transaction();
337 let rewritten_commit_2 = tx
338 .repo_mut()
339 .rewrite_commit(&rewritten_commit_1)
340 .set_description("New description")
341 .write_unwrap();
342 tx.repo_mut().rebase_descendants().block_on()?;
343 tx.commit("test").block_on()?;
344
345 let new_timestamp_2 =
346 Timestamp::from_datetime(chrono::DateTime::parse_from_rfc3339(new_timestamp_2)?);
347 assert_ne!(new_timestamp_2, new_timestamp_1);
348
349 assert_eq!(rewritten_commit_2.author().timestamp, new_timestamp_1);
350 assert_eq!(rewritten_commit_2.committer().timestamp, new_timestamp_2);
351 Ok(())
352 }
353
354 #[test_case(TestRepoBackend::Simple ; "simple backend")]
355 #[test_case(TestRepoBackend::Git ; "git backend")]
356 fn test_rewrite_to_identical_commit(backend: TestRepoBackend) -> TestResult {
357 let timestamp = "2001-02-03T04:05:06+07:00";
358 let settings = UserSettings::from_config(config_with_commit_timestamp(timestamp))?;
359 let test_repo = TestRepo::init_with_backend_and_settings(backend, &settings);
360 let repo = test_repo.repo;
361 let store = repo.store();
362
363 let mut tx = repo.start_transaction();
364 let commit1 = tx
365 .repo_mut()
366 .new_commit(
367 vec![store.root_commit_id().clone()],
368 store.empty_merged_tree(),
369 )
370 .write_unwrap();
371 let repo = tx.commit("test").block_on()?;
372
373 // Create commit identical to the original
374 let mut tx = repo.start_transaction();
375 let mut builder = tx.repo_mut().rewrite_commit(&commit1).detach();
376 builder.set_predecessors(vec![]);
377 // Writing to the store should work
378 let commit2 = builder.write_hidden().block_on()?;
379 assert_eq!(commit1, commit2);
380 // Writing to the repo shouldn't work, which would create cycle in
381 // predecessors/parent mappings
382 let result = builder.write(tx.repo_mut()).block_on();
383 assert_matches!(result, Err(BackendError::Other(_)));
384 tx.repo_mut().rebase_descendants().block_on()?;
385 tx.commit("test").block_on()?;
386
387 // Create two rewritten commits of the same content and metadata
388 let mut tx = repo.start_transaction();
389 tx.repo_mut()
390 .rewrite_commit(&commit1)
391 .set_description("rewritten")
392 .write_unwrap();
393 let result = tx
394 .repo_mut()
395 .rewrite_commit(&commit1)
396 .set_description("rewritten")
397 .write()
398 .block_on();
399 assert_matches!(result, Err(BackendError::Other(_)));
400 tx.repo_mut().rebase_descendants().block_on()?;
401 tx.commit("test").block_on()?;
402 Ok(())
403 }
404
405 #[test_case(TestRepoBackend::Simple ; "simple backend")]
406 // #[test_case(TestRepoBackend::Git ; "git backend")]
407 fn test_commit_builder_descendants(backend: TestRepoBackend) -> TestResult {
408 let test_repo = TestRepo::init_with_backend(backend);
409 let repo = &test_repo.repo;
410 let store = repo.store().clone();
411
412 let mut tx = repo.start_transaction();
413 let commit1 = write_random_commit(tx.repo_mut());
414 let commit2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]);
415 let commit3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]);
416 let repo = tx.commit("test").block_on()?;
417
418 // Test with for_new_commit()
419 let mut tx = repo.start_transaction();
420 tx.repo_mut()
421 .new_commit(
422 vec![store.root_commit_id().clone()],
423 store.empty_merged_tree(),
424 )
425 .write_unwrap();
426 let rebase_map =
427 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
428 assert_eq!(rebase_map.len(), 0);
429
430 // Test with for_rewrite_from()
431 let mut tx = repo.start_transaction();
432 let commit4 = tx.repo_mut().rewrite_commit(&commit2).write_unwrap();
433 let rebase_map =
434 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
435 assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit3, &[commit4.id()]);
436 assert_eq!(rebase_map.len(), 1);
437
438 // Test with for_rewrite_from() but new change id
439 let mut tx = repo.start_transaction();
440 tx.repo_mut()
441 .rewrite_commit(&commit2)
442 .clear_rewrite_source()
443 .generate_new_change_id()
444 .write_unwrap();
445 let rebase_map =
446 rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default());
447 assert!(rebase_map.is_empty());
448 Ok(())
449 }