some change-id based tests

This commit is contained in:
Scott Chacon 2024-05-15 15:21:19 +02:00
parent 2ce4e5bfd0
commit b0c5c07a57
No known key found for this signature in database
4 changed files with 105 additions and 2 deletions

View File

@ -278,10 +278,13 @@ impl Repository {
Ok(oid.into())
}
/// takes raw commit data and commits it to the repository
/// - if the git config commit.gpgSign is set, it will sign the commit
/// returns an oid of the new commit object
pub fn commit_buffer(&self, buffer: String) -> Result<git2::Oid> {
// check git config for gpg.signingkey
let should_sign = self.0.config()?.get_string("commit.gpgSign");
if let Ok(_should_sign) = should_sign {
if should_sign.unwrap_or("false".to_string()) != "false" {
// TODO: support gpg.ssh.defaultKeyCommand to get the signing key if this value doesn't exist
let signing_key = self.0.config()?.get_string("user.signingkey");
if let Ok(signing_key) = signing_key {
@ -320,7 +323,6 @@ impl Repository {
}
let key_file_path = key_storage.into_temp_path();
let key_storage = std::fs::read_to_string(&key_file_path)?;
cmd.arg(&key_file_path);
cmd.arg("-U");

View File

@ -25,6 +25,7 @@ async fn move_file_down() {
.create_commit(project_id, &branch_id, "commit one", None, false)
.await
.unwrap();
let commit1 = repository.find_commit(commit1_id).unwrap();
// create commit
fs::write(repository.path().join("file2.txt"), "content2").unwrap();
@ -33,6 +34,7 @@ async fn move_file_down() {
.create_commit(project_id, &branch_id, "commit two", None, false)
.await
.unwrap();
let commit2 = repository.find_commit(commit2_id).unwrap();
// amend another hunk
let to_amend: branch::BranchOwnershipClaims = "file2.txt:1-2".parse().unwrap();
@ -50,6 +52,13 @@ async fn move_file_down() {
.find(|b| b.id == branch_id)
.unwrap();
// shas changed but change_id is the same
assert_eq!(&commit1.change_id(), &branch.commits[1].change_id);
assert_ne!(&commit1.id(), &branch.commits[1].id);
assert_eq!(&commit2.change_id(), &branch.commits[0].change_id);
assert_ne!(&commit2.id(), &branch.commits[0].id);
assert_eq!(branch.commits[0].files.len(), 1);
assert_eq!(branch.commits.len(), 2);
assert_eq!(branch.commits[0].files.len(), 1);
assert_eq!(branch.commits[1].files.len(), 2); // this now has both file changes

View File

@ -42,6 +42,8 @@ async fn head() {
.await
.unwrap()
};
let commit_three = repository.find_commit(commit_three_oid).unwrap();
let before_change_id = &commit_three.change_id();
controller
.update_commit_message(
@ -68,6 +70,13 @@ async fn head() {
.map(|c| c.description.clone())
.collect::<Vec<_>>();
// get the last commit
let commit = repository.find_commit(branch.head).unwrap();
// make sure the SHA changed, but the change ID did not
assert_ne!(&commit_three.id(), &commit.id());
assert_eq!(before_change_id, &commit.change_id());
assert_eq!(
descriptions,
vec!["commit three updated", "commit two", "commit one"]

View File

@ -666,6 +666,89 @@ fn add_new_hunk_to_the_end() -> Result<()> {
Ok(())
}
#[test]
fn commit_id_can_be_generated_or_specified() -> Result<()> {
let suite = Suite::default();
let Case {
project_repository,
project,
..
} = &suite.new_case();
let file_path = Path::new("test.txt");
std::fs::write(
Path::new(&project.path).join(file_path),
"line1\nline2\nline3\nline4\n",
)?;
commit_all(&project_repository.git_repository);
// lets make sure a change id is generated
let target_oid = project_repository
.git_repository
.head()
.unwrap()
.target()
.unwrap();
let target = project_repository
.git_repository
.find_commit(target_oid)
.unwrap();
let change_id = target.change_id();
// make sure we created a change-id
assert!(change_id.is_some());
// ok, make another change and specify a change-id
let file_path = Path::new("test.txt");
std::fs::write(
Path::new(&project.path).join(file_path),
"line1\nline2\nline3\nline4\nline5\n",
)?;
let repository = &project_repository.git_repository;
let mut index = repository.index().expect("failed to get index");
index
.add_all(["."], git2::IndexAddOption::DEFAULT, None)
.expect("failed to add all");
index.write().expect("failed to write index");
let oid = index.write_tree().expect("failed to write tree");
let signature = gitbutler_core::git::Signature::now("test", "test@email.com").unwrap();
let head = repository.head().expect("failed to get head");
repository
.commit(
Some(&head.name().unwrap()),
&signature,
&signature,
"some commit",
&repository.find_tree(oid).expect("failed to find tree"),
&[&repository
.find_commit(
repository
.refname_to_id("HEAD")
.expect("failed to get head"),
)
.expect("failed to find commit")],
Some("my-change-id"),
)
.expect("failed to commit");
let target_oid = project_repository
.git_repository
.head()
.unwrap()
.target()
.unwrap();
let target = project_repository
.git_repository
.find_commit(target_oid)
.unwrap();
let change_id = target.change_id();
// the change id should be what we specified, rather than randomly generated
assert_eq!(change_id, Some("my-change-id".to_string()));
Ok(())
}
#[test]
fn merge_vbranch_upstream_clean_rebase() -> Result<()> {
let suite = Suite::default();