to_same_branch working

This commit is contained in:
Nikita Galaiko 2023-11-01 14:44:47 +01:00 committed by GitButler
parent 693e92119d
commit b221254257
2 changed files with 175 additions and 3 deletions

View File

@ -2334,6 +2334,16 @@ pub fn amend(
.find(|(b, _)| b.id == *branch_id)
.context("branch not found")?;
if project_repository
.l(
target_branch.head,
project_repository::LogUntil::Commit(default_target.sha),
)?
.is_empty()
{
bail!("branch doesn't have any commits");
}
let diffs_to_consider = calculate_non_commited_diffs(
project_repository,
target_branch,
@ -2404,6 +2414,12 @@ pub fn amend(
}
.context("failed to create commit")?;
let branch_writer = branch::Writer::new(gb_repository);
branch_writer.write(&branch::Branch {
head: commit_oid,
..target_branch.clone()
})?;
super::integration::update_gitbutler_integration(gb_repository, project_repository)?;
Ok(commit_oid)

View File

@ -1778,16 +1778,173 @@ mod amend {
use super::*;
mod to_same_branch {
use gblib::virtual_branches::branch::Ownership;
use super::*;
#[tokio::test]
async fn to_default_target() {
let Test {
repository,
project_id,
controller,
..
} = Test::default();
controller
.set_base_branch(
&project_id,
&git::RemoteBranchName::from_str("refs/remotes/origin/master").unwrap(),
)
.unwrap();
let branch_id = controller
.create_virtual_branch(&project_id, &branch::BranchCreateRequest::default())
.await
.unwrap();
// amend without head commit
fs::write(repository.path().join("file2.txt"), "content").unwrap();
let to_amend: Ownership = "file2.txt:1-2".parse().unwrap();
controller
.amend(&project_id, &branch_id, &to_amend)
.await
.unwrap_err();
}
#[tokio::test]
async fn non_locked_hunk() {
todo!()
let Test {
repository,
project_id,
controller,
..
} = Test::default();
controller
.set_base_branch(
&project_id,
&git::RemoteBranchName::from_str("refs/remotes/origin/master").unwrap(),
)
.unwrap();
let branch_id = controller
.create_virtual_branch(&project_id, &branch::BranchCreateRequest::default())
.await
.unwrap();
{
// create commit
fs::write(repository.path().join("file.txt"), "content").unwrap();
controller
.create_commit(&project_id, &branch_id, "commit one", None)
.await
.unwrap();
let branch = controller
.list_virtual_branches(&project_id)
.await
.unwrap()
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.commits.len(), 1);
assert_eq!(branch.files.len(), 0);
assert_eq!(branch.commits[0].files.len(), 1);
};
{
// amend another hunk
fs::write(repository.path().join("file2.txt"), "content2").unwrap();
let to_amend: Ownership = "file2.txt:1-2".parse().unwrap();
controller
.amend(&project_id, &branch_id, &to_amend)
.await
.unwrap();
let branch = controller
.list_virtual_branches(&project_id)
.await
.unwrap()
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.commits.len(), 1);
assert_eq!(branch.files.len(), 0);
assert_eq!(branch.commits[0].files.len(), 2);
}
}
#[tokio::test]
async fn locked_hunk() {
todo!()
let Test {
repository,
project_id,
controller,
..
} = Test::default();
controller
.set_base_branch(
&project_id,
&git::RemoteBranchName::from_str("refs/remotes/origin/master").unwrap(),
)
.unwrap();
let branch_id = controller
.create_virtual_branch(&project_id, &branch::BranchCreateRequest::default())
.await
.unwrap();
{
// create commit
fs::write(repository.path().join("file.txt"), "content").unwrap();
controller
.create_commit(&project_id, &branch_id, "commit one", None)
.await
.unwrap();
let branch = controller
.list_virtual_branches(&project_id)
.await
.unwrap()
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.commits.len(), 1);
assert_eq!(branch.files.len(), 0);
assert_eq!(branch.commits[0].files.len(), 1);
assert_eq!(
branch.commits[0].files[0].hunks[0].diff,
"@@ -0,0 +1 @@\n+content\n\\ No newline at end of file\n"
);
};
{
// amend another hunk
fs::write(repository.path().join("file.txt"), "more content").unwrap();
let to_amend: Ownership = "file.txt:1-2".parse().unwrap();
controller
.amend(&project_id, &branch_id, &to_amend)
.await
.unwrap();
let branch = controller
.list_virtual_branches(&project_id)
.await
.unwrap()
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.commits.len(), 1);
assert_eq!(branch.files.len(), 0);
assert_eq!(branch.commits[0].files.len(), 1);
assert_eq!(
branch.commits[0].files[0].hunks[0].diff,
"@@ -0,0 +1 @@\n+more content\n\\ No newline at end of file\n"
);
}
}
}
@ -1804,5 +1961,4 @@ mod amend {
todo!()
}
}
}