2024-07-13 11:38:56 +03:00
|
|
|
use gitbutler_branch::{Branch, BranchCreateRequest, BranchUpdateRequest};
|
2024-07-10 15:49:48 +03:00
|
|
|
use gitbutler_branch_actions::VirtualBranch;
|
2024-07-09 13:39:18 +03:00
|
|
|
use gitbutler_id::id::Id;
|
2024-04-25 16:27:43 +03:00
|
|
|
|
2024-03-29 12:04:26 +03:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn should_lock_updated_hunks() {
|
|
|
|
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();
|
|
|
|
|
|
|
|
let branch_id = 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();
|
|
|
|
|
|
|
|
{
|
|
|
|
// by default, hunks are not locked
|
2024-06-25 12:03:27 +03:00
|
|
|
repository.write_file("file.txt", &["content".to_string()]);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let branch = get_virtual_branch(controller, project, branch_id).await;
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(branch.files.len(), 1);
|
|
|
|
assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
|
|
|
|
assert_eq!(branch.files[0].hunks.len(), 1);
|
|
|
|
assert!(!branch.files[0].hunks[0].locked);
|
|
|
|
}
|
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch_id, "test", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
// change in the committed hunks leads to hunk locking
|
2024-06-25 12:03:27 +03:00
|
|
|
repository.write_file("file.txt", &["updated content".to_string()]);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let branch = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(branch.files.len(), 1);
|
|
|
|
assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
|
|
|
|
assert_eq!(branch.files[0].hunks.len(), 1);
|
|
|
|
assert!(branch.files[0].hunks[0].locked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-25 17:08:38 +03:00
|
|
|
#[tokio::test]
|
|
|
|
async fn should_reset_into_same_branch() {
|
|
|
|
let Test {
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-04-25 17:08:38 +03:00
|
|
|
controller,
|
|
|
|
repository,
|
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
2024-06-25 12:03:27 +03:00
|
|
|
let mut lines = repository.gen_file("file.txt", 7);
|
2024-04-25 17:08:38 +03:00
|
|
|
commit_and_push_initial(repository);
|
|
|
|
|
|
|
|
let base_branch = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
|
2024-04-25 17:08:38 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
controller
|
2024-07-13 11:38:56 +03:00
|
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
2024-04-25 17:08:38 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let branch_2_id = controller
|
|
|
|
.create_virtual_branch(
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-07-13 11:38:56 +03:00
|
|
|
&BranchCreateRequest {
|
2024-04-25 17:08:38 +03:00
|
|
|
selected_for_changes: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
lines[0] = "change 1".to_string();
|
2024-06-25 12:03:27 +03:00
|
|
|
repository.write_file("file.txt", &lines);
|
2024-04-25 17:08:38 +03:00
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch_2_id, "commit to branch 2", None, false)
|
2024-04-25 17:08:38 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let files = get_virtual_branch(controller, project, branch_2_id)
|
2024-04-25 17:08:38 +03:00
|
|
|
.await
|
|
|
|
.files;
|
|
|
|
assert_eq!(files.len(), 0);
|
|
|
|
|
|
|
|
// Set target to branch 1 and verify the file resets into branch 2.
|
|
|
|
controller
|
|
|
|
.update_virtual_branch(
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-07-13 11:38:56 +03:00
|
|
|
BranchUpdateRequest {
|
2024-04-25 17:08:38 +03:00
|
|
|
id: branch_2_id,
|
|
|
|
selected_for_changes: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.reset_virtual_branch(project, branch_2_id, base_branch.base_sha)
|
2024-04-25 17:08:38 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let files = get_virtual_branch(controller, project, branch_2_id)
|
2024-04-25 17:08:38 +03:00
|
|
|
.await
|
|
|
|
.files;
|
|
|
|
assert_eq!(files.len(), 1);
|
|
|
|
}
|
|
|
|
|
2024-04-25 16:27:43 +03:00
|
|
|
fn commit_and_push_initial(repository: &TestProject) {
|
|
|
|
repository.commit_all("initial commit");
|
|
|
|
repository.push();
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_virtual_branch(
|
2024-07-10 13:11:36 +03:00
|
|
|
controller: &VirtualBranchActions,
|
2024-07-04 16:26:10 +03:00
|
|
|
project: &Project,
|
2024-04-25 16:27:43 +03:00
|
|
|
branch_id: Id<Branch>,
|
|
|
|
) -> VirtualBranch {
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.list_virtual_branches(project)
|
2024-04-25 16:27:43 +03:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == branch_id)
|
|
|
|
.unwrap()
|
2024-04-24 01:09:51 +03:00
|
|
|
}
|