2024-07-13 11:38:56 +03:00
|
|
|
use gitbutler_branch::{BranchCreateRequest, BranchUpdateRequest};
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-07-28 21:48:13 +03:00
|
|
|
use super::*;
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn unapplying_selected_branch_selects_anther() {
|
2024-03-29 12:04:26 +03:00
|
|
|
let Test {
|
|
|
|
repository,
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::set_base_branch(
|
|
|
|
project,
|
|
|
|
&"refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
std::fs::write(repository.path().join("file one.txt"), "").unwrap();
|
|
|
|
|
|
|
|
// first branch should be created as default
|
2024-09-06 15:12:14 +03:00
|
|
|
let b_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// if default branch exists, new branch should not be created as default
|
2024-09-06 15:12:14 +03:00
|
|
|
let b2_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let b = branches.iter().find(|b| b.id == b_id).unwrap();
|
|
|
|
|
|
|
|
let b2 = branches.iter().find(|b| b.id == b2_id).unwrap();
|
|
|
|
|
|
|
|
assert!(b.selected_for_changes);
|
|
|
|
assert!(!b2.selected_for_changes);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::convert_to_real_branch(project, b_id).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-07-01 17:13:52 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
assert_eq!(branches[0].id, b2.id);
|
|
|
|
assert!(branches[0].selected_for_changes);
|
|
|
|
assert!(branches[0].active);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn deleting_selected_branch_selects_anther() {
|
2024-09-06 15:12:14 +03:00
|
|
|
let Test { project, .. } = &Test::default();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::set_base_branch(
|
|
|
|
project,
|
|
|
|
&"refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// first branch should be created as default
|
2024-09-06 15:12:14 +03:00
|
|
|
let b_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// if default branch exists, new branch should not be created as default
|
2024-09-06 15:12:14 +03:00
|
|
|
let b2_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let b = branches.iter().find(|b| b.id == b_id).unwrap();
|
|
|
|
|
|
|
|
let b2 = branches.iter().find(|b| b.id == b2_id).unwrap();
|
|
|
|
|
|
|
|
assert!(b.selected_for_changes);
|
|
|
|
assert!(!b2.selected_for_changes);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::delete_virtual_branch(project, b_id).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
assert_eq!(branches[0].id, b2.id);
|
|
|
|
assert!(branches[0].selected_for_changes);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn create_virtual_branch_should_set_selected_for_changes() {
|
2024-09-06 15:12:14 +03:00
|
|
|
let Test { project, .. } = &Test::default();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::set_base_branch(
|
|
|
|
project,
|
|
|
|
&"refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// first branch should be created as default
|
2024-09-06 15:12:14 +03:00
|
|
|
let b_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
|
|
|
let branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == b_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(branch.selected_for_changes);
|
|
|
|
|
|
|
|
// if default branch exists, new branch should not be created as default
|
2024-09-06 15:12:14 +03:00
|
|
|
let b_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
|
|
|
let branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == b_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(!branch.selected_for_changes);
|
|
|
|
|
|
|
|
// explicitly don't make this one default
|
2024-09-06 15:12:14 +03:00
|
|
|
let b_id = gitbutler_branch_actions::create_virtual_branch(
|
|
|
|
project,
|
|
|
|
&BranchCreateRequest {
|
|
|
|
selected_for_changes: Some(false),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == b_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(!branch.selected_for_changes);
|
|
|
|
|
|
|
|
// explicitly make this one default
|
2024-09-06 15:12:14 +03:00
|
|
|
let b_id = gitbutler_branch_actions::create_virtual_branch(
|
|
|
|
project,
|
|
|
|
&BranchCreateRequest {
|
|
|
|
selected_for_changes: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == b_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(branch.selected_for_changes);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn update_virtual_branch_should_reset_selected_for_changes() {
|
2024-09-06 15:12:14 +03:00
|
|
|
let Test { project, .. } = &Test::default();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::set_base_branch(
|
|
|
|
project,
|
|
|
|
&"refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let b1_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
|
|
|
let b1 = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == b1_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(b1.selected_for_changes);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let b2_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
|
|
|
let b2 = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == b2_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(!b2.selected_for_changes);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::update_virtual_branch(
|
|
|
|
project,
|
|
|
|
BranchUpdateRequest {
|
|
|
|
id: b2_id,
|
|
|
|
selected_for_changes: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let b1 = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == b1_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(!b1.selected_for_changes);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let b2 = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == b2_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(b2.selected_for_changes);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn unapply_virtual_branch_should_reset_selected_for_changes() {
|
2024-03-29 12:04:26 +03:00
|
|
|
let Test {
|
|
|
|
repository,
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::set_base_branch(
|
|
|
|
project,
|
|
|
|
&"refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let b1_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
std::fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let b1 = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == b1_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(b1.selected_for_changes);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let b2_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let b2 = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
2024-07-01 17:13:52 +03:00
|
|
|
.find(|b| b.id == b2_id)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
2024-07-01 17:13:52 +03:00
|
|
|
assert!(!b2.selected_for_changes);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::convert_to_real_branch(project, b1_id).unwrap();
|
2024-07-01 17:13:52 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
assert!(gitbutler_branch_actions::list_virtual_branches(project)
|
2024-07-01 17:13:52 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.any(|b| b.selected_for_changes && b.id != b1_id))
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn hunks_distribution() {
|
2024-03-29 12:04:26 +03:00
|
|
|
let Test {
|
|
|
|
repository,
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::set_base_branch(
|
|
|
|
project,
|
|
|
|
&"refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
std::fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(branches[0].files.len(), 1);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_virtual_branch(
|
|
|
|
project,
|
|
|
|
&BranchCreateRequest {
|
|
|
|
selected_for_changes: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
std::fs::write(repository.path().join("another_file.txt"), "content").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(branches[0].files.len(), 1);
|
|
|
|
assert_eq!(branches[1].files.len(), 1);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn applying_first_branch() {
|
2024-03-29 12:04:26 +03:00
|
|
|
let Test {
|
|
|
|
repository,
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::set_base_branch(
|
|
|
|
project,
|
|
|
|
&"refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
std::fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let unapplied_branch =
|
|
|
|
gitbutler_branch_actions::convert_to_real_branch(project, branches[0].id).unwrap();
|
2024-07-09 02:14:28 +03:00
|
|
|
let unapplied_branch = Refname::from_str(&unapplied_branch).unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_virtual_branch_from_branch(project, &unapplied_branch, None)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
assert!(branches[0].active);
|
|
|
|
assert!(branches[0].selected_for_changes);
|
|
|
|
}
|
2024-06-25 12:56:26 +03:00
|
|
|
|
|
|
|
// This test was written in response to issue #4148, to ensure the appearence
|
|
|
|
// of a locked hunk doesn't drag along unrelated hunks to its branch.
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn new_locked_hunk_without_modifying_existing() {
|
2024-06-25 12:56:26 +03:00
|
|
|
let Test {
|
|
|
|
repository,
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-06-25 12:56:26 +03:00
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
|
|
|
let mut lines = repository.gen_file("file.txt", 9);
|
|
|
|
repository.commit_all("first commit");
|
|
|
|
repository.push();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::set_base_branch(
|
|
|
|
project,
|
|
|
|
&"refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-06-25 12:56:26 +03:00
|
|
|
|
|
|
|
lines[0] = "modification 1".to_string();
|
|
|
|
repository.write_file("file.txt", &lines);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-06-25 12:56:26 +03:00
|
|
|
assert_eq!(branches[0].files.len(), 1);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branches[0].id, "second commit", None, false)
|
2024-06-25 12:56:26 +03:00
|
|
|
.expect("failed to create commit");
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-06-25 12:56:26 +03:00
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_virtual_branch(
|
|
|
|
project,
|
|
|
|
&BranchCreateRequest {
|
|
|
|
selected_for_changes: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-06-25 12:56:26 +03:00
|
|
|
|
|
|
|
lines[8] = "modification 2".to_string();
|
|
|
|
repository.write_file("file.txt", &lines);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-06-25 12:56:26 +03:00
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(branches[1].files.len(), 1);
|
|
|
|
|
|
|
|
lines[0] = "modification 3".to_string();
|
|
|
|
repository.write_file("file.txt", &lines);
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-06-25 12:56:26 +03:00
|
|
|
assert_eq!(branches[0].files.len(), 1);
|
|
|
|
assert_eq!(branches[1].files.len(), 1);
|
|
|
|
}
|