jj/lib/tests/test_commit_builder.rs
Martin von Zweigbergk 0108673087 backend: let each backend handle root commit on write
This moves the logic for handling the root commit when writing commits
from `CommitBuilder` into the individual backends. It always bothered
me a bit that the `commit::Commit` wrapper had a different idea of the
number of parents than the wrapped `backend::Commit` had.

With this change, the `LocalBackend` will now write the root commit in
the list of parents if it's there in the argument to
`write_commit()`. Note that root commit itself won't be written. The
main argument for not writing it is that we can then keep the fake
all-zeros hash for it. One argument for writing it, if we were to do
so, is that it would make the set of written objects consistent, so
any future processing of them (such as GC) doesn't have to know to
ignore the root commit in the list of parents.

We still treat the two backends the same, so the user won't be allowed
to create merges including the root commit even when using the
`LocalBackend`.
2022-09-20 21:20:57 -07:00

257 lines
9 KiB
Rust

// Copyright 2020 Google LLC
//
// 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.
use jujutsu_lib::commit_builder::CommitBuilder;
use jujutsu_lib::matchers::EverythingMatcher;
use jujutsu_lib::repo_path::RepoPath;
use jujutsu_lib::settings::UserSettings;
use jujutsu_lib::testutils;
use jujutsu_lib::testutils::{assert_rebased, CommitGraphBuilder, TestRepo};
use jujutsu_lib::tree::DiffSummary;
use test_case::test_case;
#[test_case(false ; "local backend")]
#[test_case(true ; "git backend")]
fn test_initial(use_git: bool) {
let settings = testutils::user_settings();
let test_repo = TestRepo::init(use_git);
let repo = &test_repo.repo;
let store = repo.store();
let root_file_path = RepoPath::from_internal_string("file");
let dir_file_path = RepoPath::from_internal_string("dir/file");
let tree = testutils::create_tree(
repo,
&[
(&root_file_path, "file contents"),
(&dir_file_path, "dir/file contents"),
],
);
let mut tx = repo.start_transaction("test");
let commit = CommitBuilder::for_new_commit(
&settings,
vec![store.root_commit_id().clone()],
tree.id().clone(),
)
.write_to_repo(tx.mut_repo());
tx.commit();
assert_eq!(commit.parents(), vec![store.root_commit()]);
assert_eq!(commit.predecessors(), vec![]);
assert!(!commit.is_open());
assert_eq!(commit.description(), "");
assert_eq!(commit.author().name, settings.user_name());
assert_eq!(commit.author().email, settings.user_email());
assert_eq!(commit.committer().name, settings.user_name());
assert_eq!(commit.committer().email, settings.user_email());
assert_eq!(
store
.root_commit()
.tree()
.diff_summary(&commit.tree(), &EverythingMatcher),
DiffSummary {
modified: vec![],
added: vec![dir_file_path, root_file_path],
removed: vec![]
}
);
}
#[test_case(false ; "local backend")]
#[test_case(true ; "git backend")]
fn test_rewrite(use_git: bool) {
let settings = testutils::user_settings();
let test_repo = TestRepo::init(use_git);
let repo = &test_repo.repo;
let store = repo.store().clone();
let root_file_path = RepoPath::from_internal_string("file");
let dir_file_path = RepoPath::from_internal_string("dir/file");
let initial_tree = testutils::create_tree(
repo,
&[
(&root_file_path, "file contents"),
(&dir_file_path, "dir/file contents"),
],
);
let mut tx = repo.start_transaction("test");
let initial_commit = CommitBuilder::for_new_commit(
&settings,
vec![store.root_commit_id().clone()],
initial_tree.id().clone(),
)
.write_to_repo(tx.mut_repo());
let repo = tx.commit();
let rewritten_tree = testutils::create_tree(
&repo,
&[
(&root_file_path, "file contents"),
(&dir_file_path, "updated dir/file contents"),
],
);
let config = config::Config::builder()
.set_override("user.name", "Rewrite User")
.unwrap()
.set_override("user.email", "rewrite.user@example.com")
.unwrap()
.build()
.unwrap();
let rewrite_settings = UserSettings::from_config(config);
let mut tx = repo.start_transaction("test");
let rewritten_commit = CommitBuilder::for_rewrite_from(&rewrite_settings, &initial_commit)
.set_tree(rewritten_tree.id().clone())
.write_to_repo(tx.mut_repo());
tx.mut_repo().rebase_descendants(&settings).unwrap();
tx.commit();
assert_eq!(rewritten_commit.parents(), vec![store.root_commit()]);
assert_eq!(
rewritten_commit.predecessors(),
vec![initial_commit.clone()]
);
assert!(!rewritten_commit.is_open());
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()
.diff_summary(&rewritten_commit.tree(), &EverythingMatcher),
DiffSummary {
modified: vec![],
added: vec![dir_file_path.clone(), root_file_path],
removed: vec![]
}
);
assert_eq!(
initial_commit
.tree()
.diff_summary(&rewritten_commit.tree(), &EverythingMatcher),
DiffSummary {
modified: vec![dir_file_path],
added: vec![],
removed: vec![]
}
);
}
// An author field with the placeholder name/email should get filled in on
// rewrite
#[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;
let mut tx = repo.start_transaction("test");
let initial_commit = CommitBuilder::for_new_commit(
&missing_user_settings,
vec![repo.store().root_commit_id().clone()],
repo.store().empty_tree_id().clone(),
)
.write_to_repo(tx.mut_repo());
assert_eq!(initial_commit.author().name, "(no name configured)");
assert_eq!(initial_commit.author().email, "(no email configured)");
assert_eq!(initial_commit.committer().name, "(no name configured)");
assert_eq!(initial_commit.committer().email, "(no email configured)");
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);
let rewritten_commit =
CommitBuilder::for_rewrite_from(&settings, &initial_commit).write_to_repo(tx.mut_repo());
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"
);
}
#[test_case(false ; "local backend")]
// #[test_case(true ; "git backend")]
fn test_commit_builder_descendants(use_git: bool) {
let settings = testutils::user_settings();
let test_repo = TestRepo::init(use_git);
let repo = &test_repo.repo;
let store = repo.store().clone();
let mut tx = repo.start_transaction("test");
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()
let mut tx = repo.start_transaction("test");
CommitBuilder::for_new_commit(
&settings,
vec![store.root_commit_id().clone()],
store.empty_tree_id().clone(),
)
.write_to_repo(tx.mut_repo());
let mut rebaser = tx.mut_repo().create_descendant_rebaser(&settings);
assert!(rebaser.rebase_next().unwrap().is_none());
// Test with for_open_commit()
let mut tx = repo.start_transaction("test");
CommitBuilder::for_open_commit(
&settings,
commit2.id().clone(),
store.empty_tree_id().clone(),
)
.write_to_repo(tx.mut_repo());
let mut rebaser = tx.mut_repo().create_descendant_rebaser(&settings);
assert!(rebaser.rebase_next().unwrap().is_none());
// Test with for_rewrite_from()
let mut tx = repo.start_transaction("test");
let commit4 = CommitBuilder::for_rewrite_from(&settings, &commit2).write_to_repo(tx.mut_repo());
let mut rebaser = tx.mut_repo().create_descendant_rebaser(&settings);
assert_rebased(rebaser.rebase_next().unwrap(), &commit3, &[&commit4]);
assert!(rebaser.rebase_next().unwrap().is_none());
// Test with for_rewrite_from() but new change id
let mut tx = repo.start_transaction("test");
CommitBuilder::for_rewrite_from(&settings, &commit2)
.generate_new_change_id()
.write_to_repo(tx.mut_repo());
let mut rebaser = tx.mut_repo().create_descendant_rebaser(&settings);
assert!(rebaser.rebase_next().unwrap().is_none());
}