2024-07-13 11:38:56 +03:00
|
|
|
use gitbutler_branch::BranchCreateRequest;
|
2024-07-09 02:14:28 +03:00
|
|
|
use gitbutler_reference::Refname;
|
|
|
|
|
2024-03-29 12:04:26 +03:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn unapply_with_data() {
|
|
|
|
let Test {
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
controller,
|
|
|
|
repository,
|
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
std::fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.convert_to_real_branch(project, branches[0].id, Default::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(!repository.path().join("file.txt").exists());
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.unwrap();
|
2024-07-01 17:13:52 +03:00
|
|
|
assert_eq!(branches.len(), 0);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn conflicting() {
|
|
|
|
let Test {
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
controller,
|
|
|
|
repository,
|
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
|
|
|
// make sure we have an undiscovered commit in the remote branch
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file.txt"), "first").unwrap();
|
|
|
|
let first_commit_oid = repository.commit_all("first");
|
|
|
|
fs::write(repository.path().join("file.txt"), "second").unwrap();
|
|
|
|
repository.commit_all("second");
|
|
|
|
repository.push();
|
|
|
|
repository.reset_hard(Some(first_commit_oid));
|
|
|
|
}
|
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-01 17:13:52 +03:00
|
|
|
let unapplied_branch = {
|
2024-03-29 12:04:26 +03:00
|
|
|
// make a conflicting branch, and stash it
|
|
|
|
|
|
|
|
std::fs::write(repository.path().join("file.txt"), "conflict").unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
assert!(branches[0].base_current);
|
|
|
|
assert!(branches[0].active);
|
2024-04-23 12:07:20 +03:00
|
|
|
assert_eq!(
|
|
|
|
branches[0].files[0].hunks[0].diff,
|
|
|
|
"@@ -1 +1 @@\n-first\n\\ No newline at end of file\n+conflict\n\\ No newline at end of file\n"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-07-01 17:13:52 +03:00
|
|
|
let unapplied_branch = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.convert_to_real_branch(project, branches[0].id, Default::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-09 02:14:28 +03:00
|
|
|
Refname::from_str(&unapplied_branch).unwrap()
|
2024-03-29 12:04:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
// update base branch, causing conflict
|
2024-07-04 16:26:10 +03:00
|
|
|
controller.update_base_branch(project).await.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
std::fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"second"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:13:52 +03:00
|
|
|
let branch_id = {
|
2024-03-29 12:04:26 +03:00
|
|
|
// apply branch, it should conflict
|
2024-07-01 17:13:52 +03:00
|
|
|
let branch_id = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_virtual_branch_from_branch(project, &unapplied_branch)
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
std::fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"<<<<<<< ours\nconflict\n=======\nsecond\n>>>>>>> theirs\n"
|
|
|
|
);
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.unwrap();
|
2024-07-01 17:13:52 +03:00
|
|
|
|
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
let branch = &branches[0];
|
|
|
|
// assert!(!branch.base_current);
|
2024-03-29 12:04:26 +03:00
|
|
|
assert!(branch.conflicted);
|
2024-07-01 17:13:52 +03:00
|
|
|
assert_eq!(
|
|
|
|
branch.files[0].hunks[0].diff,
|
|
|
|
"@@ -1 +1,5 @@\n-first\n\\ No newline at end of file\n+<<<<<<< ours\n+conflict\n+=======\n+second\n+>>>>>>> theirs\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
branch_id
|
|
|
|
};
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
2024-07-01 17:13:52 +03:00
|
|
|
// Converting the branch to a real branch should put us back in an unconflicted state
|
2024-03-29 12:04:26 +03:00
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.convert_to_real_branch(project, branch_id, Default::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
std::fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"second"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn delete_if_empty() {
|
|
|
|
let Test {
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
controller,
|
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
controller
|
2024-07-13 11:38:56 +03:00
|
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.convert_to_real_branch(project, branches[0].id, Default::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(branches.len(), 0);
|
|
|
|
}
|