2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 08:00:42 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2023-06-28 14:12:40 +00:00
|
|
|
use jj_lib::backend::{ChangeId, MillisSinceEpoch, ObjectId, Signature, Timestamp};
|
|
|
|
use jj_lib::matchers::EverythingMatcher;
|
|
|
|
use jj_lib::repo::Repo;
|
|
|
|
use jj_lib::repo_path::RepoPath;
|
|
|
|
use jj_lib::settings::UserSettings;
|
|
|
|
use jj_lib::tree::DiffSummary;
|
2020-12-12 08:00:42 +00:00
|
|
|
use test_case::test_case;
|
2022-11-08 12:35:16 +00:00
|
|
|
use testutils::{assert_rebased, CommitGraphBuilder, TestRepo};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2020-12-12 08:00:42 +00:00
|
|
|
fn test_initial(use_git: bool) {
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 18:20:51 +00:00
|
|
|
let test_repo = TestRepo::init(use_git);
|
2022-02-05 23:37:08 +00:00
|
|
|
let repo = &test_repo.repo;
|
2020-12-12 08:00:42 +00:00
|
|
|
let store = repo.store();
|
|
|
|
|
2021-05-17 05:47:31 +00:00
|
|
|
let root_file_path = RepoPath::from_internal_string("file");
|
|
|
|
let dir_file_path = RepoPath::from_internal_string("dir/file");
|
2020-12-12 08:00:42 +00:00
|
|
|
let tree = testutils::create_tree(
|
2021-11-21 07:46:54 +00:00
|
|
|
repo,
|
2020-12-12 08:00:42 +00:00
|
|
|
&[
|
|
|
|
(&root_file_path, "file contents"),
|
|
|
|
(&dir_file_path, "dir/file contents"),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2022-11-13 06:29:06 +00:00
|
|
|
let mut tx = repo.start_transaction(&settings, "test");
|
2023-03-18 05:34:25 +00:00
|
|
|
let author_signature = Signature {
|
|
|
|
name: "author name".to_string(),
|
|
|
|
email: "author email".to_string(),
|
|
|
|
timestamp: Timestamp {
|
2023-05-11 23:01:09 +00:00
|
|
|
timestamp: MillisSinceEpoch(1000),
|
2023-03-18 05:34:25 +00:00
|
|
|
tz_offset: 60,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let committer_signature = Signature {
|
|
|
|
name: "committer name".to_string(),
|
|
|
|
email: "committer email".to_string(),
|
|
|
|
timestamp: Timestamp {
|
2023-05-11 23:01:09 +00:00
|
|
|
timestamp: MillisSinceEpoch(2000),
|
2023-03-18 05:34:25 +00:00
|
|
|
tz_offset: -60,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let change_id = ChangeId::new(vec![100u8; 16]);
|
|
|
|
let builder = tx
|
2022-12-25 16:36:13 +00:00
|
|
|
.mut_repo()
|
|
|
|
.new_commit(
|
|
|
|
&settings,
|
|
|
|
vec![store.root_commit_id().clone()],
|
|
|
|
tree.id().clone(),
|
|
|
|
)
|
2023-03-18 05:34:25 +00:00
|
|
|
.set_change_id(change_id.clone())
|
|
|
|
.set_description("description")
|
|
|
|
.set_author(author_signature.clone())
|
|
|
|
.set_committer(committer_signature.clone());
|
|
|
|
assert_eq!(builder.parents(), &[store.root_commit_id().clone()]);
|
|
|
|
assert_eq!(builder.predecessors(), &[]);
|
|
|
|
assert_eq!(builder.tree(), tree.id());
|
|
|
|
assert_eq!(builder.change_id(), &change_id);
|
|
|
|
assert_eq!(builder.author(), &author_signature);
|
|
|
|
assert_eq!(builder.committer(), &committer_signature);
|
|
|
|
let commit = builder.write().unwrap();
|
2021-09-11 16:45:06 +00:00
|
|
|
tx.commit();
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
assert_eq!(commit.parents(), vec![store.root_commit()]);
|
|
|
|
assert_eq!(commit.predecessors(), vec![]);
|
2023-03-18 05:34:25 +00:00
|
|
|
assert_eq!(commit.description(), "description");
|
|
|
|
assert_eq!(commit.author(), &author_signature);
|
|
|
|
assert_eq!(commit.committer(), &committer_signature);
|
2020-12-12 08:00:42 +00:00
|
|
|
assert_eq!(
|
2021-06-05 21:29:40 +00:00
|
|
|
store
|
|
|
|
.root_commit()
|
|
|
|
.tree()
|
|
|
|
.diff_summary(&commit.tree(), &EverythingMatcher),
|
2020-12-12 08:00:42 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
2021-05-17 06:40:39 +00:00
|
|
|
added: vec![dir_file_path, root_file_path],
|
2020-12-12 08:00:42 +00:00
|
|
|
removed: vec![]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2020-12-12 08:00:42 +00:00
|
|
|
fn test_rewrite(use_git: bool) {
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 18:20:51 +00:00
|
|
|
let test_repo = TestRepo::init(use_git);
|
2022-02-05 23:37:08 +00:00
|
|
|
let repo = &test_repo.repo;
|
2021-02-24 23:33:51 +00:00
|
|
|
let store = repo.store().clone();
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-05-17 05:47:31 +00:00
|
|
|
let root_file_path = RepoPath::from_internal_string("file");
|
|
|
|
let dir_file_path = RepoPath::from_internal_string("dir/file");
|
2020-12-12 08:00:42 +00:00
|
|
|
let initial_tree = testutils::create_tree(
|
2021-11-21 07:46:54 +00:00
|
|
|
repo,
|
2020-12-12 08:00:42 +00:00
|
|
|
&[
|
|
|
|
(&root_file_path, "file contents"),
|
|
|
|
(&dir_file_path, "dir/file contents"),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2022-11-13 06:29:06 +00:00
|
|
|
let mut tx = repo.start_transaction(&settings, "test");
|
2022-12-25 16:36:13 +00:00
|
|
|
let initial_commit = tx
|
|
|
|
.mut_repo()
|
|
|
|
.new_commit(
|
|
|
|
&settings,
|
|
|
|
vec![store.root_commit_id().clone()],
|
|
|
|
initial_tree.id().clone(),
|
|
|
|
)
|
|
|
|
.write()
|
|
|
|
.unwrap();
|
2021-09-11 16:45:06 +00:00
|
|
|
let repo = tx.commit();
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
let rewritten_tree = testutils::create_tree(
|
|
|
|
&repo,
|
|
|
|
&[
|
|
|
|
(&root_file_path, "file contents"),
|
|
|
|
(&dir_file_path, "updated dir/file contents"),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2022-03-10 02:04:19 +00:00
|
|
|
let config = config::Config::builder()
|
|
|
|
.set_override("user.name", "Rewrite User")
|
|
|
|
.unwrap()
|
|
|
|
.set_override("user.email", "rewrite.user@example.com")
|
|
|
|
.unwrap()
|
|
|
|
.build()
|
2020-12-12 08:00:42 +00:00
|
|
|
.unwrap();
|
|
|
|
let rewrite_settings = UserSettings::from_config(config);
|
2022-11-13 06:29:06 +00:00
|
|
|
let mut tx = repo.start_transaction(&settings, "test");
|
2022-12-25 20:58:08 +00:00
|
|
|
let rewritten_commit = tx
|
|
|
|
.mut_repo()
|
|
|
|
.rewrite_commit(&rewrite_settings, &initial_commit)
|
|
|
|
.set_tree(rewritten_tree.id().clone())
|
|
|
|
.write()
|
|
|
|
.unwrap();
|
2022-04-28 05:30:03 +00:00
|
|
|
tx.mut_repo().rebase_descendants(&settings).unwrap();
|
2021-09-11 16:45:06 +00:00
|
|
|
tx.commit();
|
2020-12-12 08:00:42 +00:00
|
|
|
assert_eq!(rewritten_commit.parents(), vec![store.root_commit()]);
|
|
|
|
assert_eq!(
|
|
|
|
rewritten_commit.predecessors(),
|
|
|
|
vec![initial_commit.clone()]
|
|
|
|
);
|
|
|
|
assert_eq!(rewritten_commit.author().name, settings.user_name());
|
|
|
|
assert_eq!(rewritten_commit.author().email, settings.user_email());
|
|
|
|
assert_eq!(
|
|
|
|
rewritten_commit.committer().name,
|
|
|
|
rewrite_settings.user_name()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
rewritten_commit.committer().email,
|
|
|
|
rewrite_settings.user_email()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
store
|
|
|
|
.root_commit()
|
|
|
|
.tree()
|
2021-06-05 21:29:40 +00:00
|
|
|
.diff_summary(&rewritten_commit.tree(), &EverythingMatcher),
|
2020-12-12 08:00:42 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
2021-05-17 06:40:39 +00:00
|
|
|
added: vec![dir_file_path.clone(), root_file_path],
|
2020-12-12 08:00:42 +00:00
|
|
|
removed: vec![]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2021-06-05 21:29:40 +00:00
|
|
|
initial_commit
|
|
|
|
.tree()
|
|
|
|
.diff_summary(&rewritten_commit.tree(), &EverythingMatcher),
|
2020-12-12 08:00:42 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![dir_file_path],
|
|
|
|
added: vec![],
|
|
|
|
removed: vec![]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-09-29 15:44:00 +00:00
|
|
|
|
2023-08-16 21:10:54 +00:00
|
|
|
// An author field with an empty name/email should get filled in on rewrite
|
2022-06-04 23:18:03 +00:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
|
|
|
fn test_rewrite_update_missing_user(use_git: bool) {
|
|
|
|
let missing_user_settings =
|
|
|
|
UserSettings::from_config(config::Config::builder().build().unwrap());
|
|
|
|
let test_repo = TestRepo::init(use_git);
|
|
|
|
let repo = &test_repo.repo;
|
|
|
|
|
2022-11-13 06:29:06 +00:00
|
|
|
let mut tx = repo.start_transaction(&missing_user_settings, "test");
|
2022-12-25 16:36:13 +00:00
|
|
|
let initial_commit = tx
|
|
|
|
.mut_repo()
|
|
|
|
.new_commit(
|
|
|
|
&missing_user_settings,
|
|
|
|
vec![repo.store().root_commit_id().clone()],
|
|
|
|
repo.store().empty_tree_id().clone(),
|
|
|
|
)
|
|
|
|
.write()
|
|
|
|
.unwrap();
|
2023-08-16 21:10:54 +00:00
|
|
|
assert_eq!(initial_commit.author().name, "");
|
|
|
|
assert_eq!(initial_commit.author().email, "");
|
|
|
|
assert_eq!(initial_commit.committer().name, "");
|
|
|
|
assert_eq!(initial_commit.committer().email, "");
|
2022-06-04 23:18:03 +00:00
|
|
|
|
|
|
|
let config = config::Config::builder()
|
|
|
|
.set_override("user.name", "Configured User")
|
|
|
|
.unwrap()
|
|
|
|
.set_override("user.email", "configured.user@example.com")
|
|
|
|
.unwrap()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let settings = UserSettings::from_config(config);
|
2022-12-25 20:58:08 +00:00
|
|
|
let rewritten_commit = tx
|
|
|
|
.mut_repo()
|
|
|
|
.rewrite_commit(&settings, &initial_commit)
|
|
|
|
.write()
|
|
|
|
.unwrap();
|
2022-06-04 23:18:03 +00:00
|
|
|
|
|
|
|
assert_eq!(rewritten_commit.author().name, "Configured User");
|
|
|
|
assert_eq!(
|
|
|
|
rewritten_commit.author().email,
|
|
|
|
"configured.user@example.com"
|
|
|
|
);
|
|
|
|
assert_eq!(rewritten_commit.committer().name, "Configured User");
|
|
|
|
assert_eq!(
|
|
|
|
rewritten_commit.committer().email,
|
|
|
|
"configured.user@example.com"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-29 15:44:00 +00:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
// #[test_case(true ; "git backend")]
|
|
|
|
fn test_commit_builder_descendants(use_git: bool) {
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 18:20:51 +00:00
|
|
|
let test_repo = TestRepo::init(use_git);
|
2022-02-05 23:37:08 +00:00
|
|
|
let repo = &test_repo.repo;
|
2021-09-29 15:44:00 +00:00
|
|
|
let store = repo.store().clone();
|
|
|
|
|
2022-11-13 06:29:06 +00:00
|
|
|
let mut tx = repo.start_transaction(&settings, "test");
|
2021-09-29 15:44:00 +00:00
|
|
|
let mut graph_builder = CommitGraphBuilder::new(&settings, tx.mut_repo());
|
|
|
|
let commit1 = graph_builder.initial_commit();
|
|
|
|
let commit2 = graph_builder.commit_with_parents(&[&commit1]);
|
|
|
|
let commit3 = graph_builder.commit_with_parents(&[&commit2]);
|
|
|
|
let repo = tx.commit();
|
|
|
|
|
|
|
|
// Test with for_new_commit()
|
2022-11-13 06:29:06 +00:00
|
|
|
let mut tx = repo.start_transaction(&settings, "test");
|
2022-12-25 16:36:13 +00:00
|
|
|
tx.mut_repo()
|
|
|
|
.new_commit(
|
|
|
|
&settings,
|
|
|
|
vec![store.root_commit_id().clone()],
|
|
|
|
store.empty_tree_id().clone(),
|
|
|
|
)
|
|
|
|
.write()
|
|
|
|
.unwrap();
|
2021-09-29 15:44:00 +00:00
|
|
|
let mut rebaser = tx.mut_repo().create_descendant_rebaser(&settings);
|
2022-04-28 05:30:03 +00:00
|
|
|
assert!(rebaser.rebase_next().unwrap().is_none());
|
2021-09-29 15:44:00 +00:00
|
|
|
|
|
|
|
// Test with for_rewrite_from()
|
2022-11-13 06:29:06 +00:00
|
|
|
let mut tx = repo.start_transaction(&settings, "test");
|
2022-12-25 20:58:08 +00:00
|
|
|
let commit4 = tx
|
|
|
|
.mut_repo()
|
|
|
|
.rewrite_commit(&settings, &commit2)
|
2022-12-24 05:09:19 +00:00
|
|
|
.write()
|
|
|
|
.unwrap();
|
2021-09-29 15:44:00 +00:00
|
|
|
let mut rebaser = tx.mut_repo().create_descendant_rebaser(&settings);
|
2022-04-28 05:30:03 +00:00
|
|
|
assert_rebased(rebaser.rebase_next().unwrap(), &commit3, &[&commit4]);
|
|
|
|
assert!(rebaser.rebase_next().unwrap().is_none());
|
2021-09-29 15:44:00 +00:00
|
|
|
|
|
|
|
// Test with for_rewrite_from() but new change id
|
2022-11-13 06:29:06 +00:00
|
|
|
let mut tx = repo.start_transaction(&settings, "test");
|
2022-12-25 20:58:08 +00:00
|
|
|
tx.mut_repo()
|
|
|
|
.rewrite_commit(&settings, &commit2)
|
2021-09-29 15:44:00 +00:00
|
|
|
.generate_new_change_id()
|
2022-12-24 05:09:19 +00:00
|
|
|
.write()
|
|
|
|
.unwrap();
|
2021-09-29 15:44:00 +00:00
|
|
|
let mut rebaser = tx.mut_repo().create_descendant_rebaser(&settings);
|
2022-04-28 05:30:03 +00:00
|
|
|
assert!(rebaser.rebase_next().unwrap().is_none());
|
2021-09-29 15:44:00 +00:00
|
|
|
}
|