2024-07-13 11:38:56 +03:00
|
|
|
use gitbutler_branch::{BranchCreateRequest, BranchId};
|
2024-03-31 00:27:56 +03:00
|
|
|
|
2024-07-07 14:32:35 +03:00
|
|
|
use super::Test;
|
2024-03-31 00:27:56 +03:00
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn no_diffs() {
|
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);
|
|
|
|
|
|
|
|
let source_branch_id = branches[0].id;
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let commit_oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, source_branch_id, "commit", None, false)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let target_branch_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
|
|
|
gitbutler_branch_actions::move_commit(project, target_branch_id, commit_oid).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let destination_branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == target_branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let source_branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == source_branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(destination_branch.commits.len(), 1);
|
|
|
|
assert_eq!(destination_branch.files.len(), 0);
|
|
|
|
assert_eq!(source_branch.commits.len(), 0);
|
|
|
|
assert_eq!(source_branch.files.len(), 0);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn diffs_on_source_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);
|
|
|
|
|
|
|
|
let source_branch_id = branches[0].id;
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let commit_oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, source_branch_id, "commit", None, false)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
repository.path().join("another file.txt"),
|
|
|
|
"another content",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let target_branch_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
|
|
|
gitbutler_branch_actions::move_commit(project, target_branch_id, commit_oid).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let destination_branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == target_branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let source_branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == source_branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(destination_branch.commits.len(), 1);
|
|
|
|
assert_eq!(destination_branch.files.len(), 0);
|
|
|
|
assert_eq!(source_branch.commits.len(), 0);
|
|
|
|
assert_eq!(source_branch.files.len(), 1);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn diffs_on_target_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);
|
|
|
|
|
|
|
|
let source_branch_id = branches[0].id;
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let commit_oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, source_branch_id, "commit", None, false)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let target_branch_id = 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"),
|
|
|
|
"another content",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::move_commit(project, target_branch_id, commit_oid).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let destination_branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == target_branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let source_branch = gitbutler_branch_actions::list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == source_branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(destination_branch.commits.len(), 1);
|
|
|
|
assert_eq!(destination_branch.files.len(), 1);
|
|
|
|
assert_eq!(source_branch.commits.len(), 0);
|
|
|
|
assert_eq!(source_branch.files.len(), 0);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn locked_hunks_on_source_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);
|
|
|
|
|
|
|
|
let source_branch_id = branches[0].id;
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let commit_oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, source_branch_id, "commit", None, false)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
std::fs::write(repository.path().join("file.txt"), "locked content").unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let target_branch_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-05-30 21:53:17 +03:00
|
|
|
assert_eq!(
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::move_commit(project, target_branch_id, commit_oid)
|
2024-03-31 22:52:56 +03:00
|
|
|
.unwrap_err()
|
2024-05-30 21:53:17 +03:00
|
|
|
.to_string(),
|
|
|
|
"the source branch contains hunks locked to the target commit"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn no_commit() {
|
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);
|
|
|
|
|
|
|
|
let source_branch_id = branches[0].id;
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, source_branch_id, "commit", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let target_branch_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-05-30 21:53:17 +03:00
|
|
|
let commit_id_hex = "a99c95cca7a60f1a2180c2f86fb18af97333c192";
|
|
|
|
assert_eq!(
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::move_commit(
|
|
|
|
project,
|
|
|
|
target_branch_id,
|
|
|
|
git2::Oid::from_str(commit_id_hex).unwrap()
|
|
|
|
)
|
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
2024-05-30 21:53:17 +03:00
|
|
|
format!("commit {commit_id_hex} to be moved could not be found")
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn no_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);
|
|
|
|
|
|
|
|
let source_branch_id = branches[0].id;
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let commit_oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, source_branch_id, "commit", None, false)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-05-30 21:53:17 +03:00
|
|
|
let id = BranchId::generate();
|
|
|
|
assert_eq!(
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::move_commit(project, id, commit_oid)
|
2024-03-31 22:52:56 +03:00
|
|
|
.unwrap_err()
|
2024-05-30 21:53:17 +03:00
|
|
|
.to_string(),
|
|
|
|
format!("branch {id} is not among applied branches")
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|