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 head() {
|
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 branch_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file one.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit one", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file two.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit two", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file three.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit three", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let commit_four_oid = {
|
|
|
|
fs::write(repository.path().join("file four.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit four", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::squash(project, branch_id, commit_four_oid).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
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 == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let descriptions = branch
|
|
|
|
.commits
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.description.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(
|
|
|
|
descriptions,
|
|
|
|
vec!["commit three\ncommit four", "commit two", "commit one"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn middle() {
|
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 branch_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file one.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit one", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let commit_two_oid = {
|
|
|
|
fs::write(repository.path().join("file two.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit two", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file three.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit three", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file four.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit four", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::squash(project, branch_id, commit_two_oid).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
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 == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let descriptions = branch
|
|
|
|
.commits
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.description.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(
|
|
|
|
descriptions,
|
|
|
|
vec!["commit four", "commit three", "commit one\ncommit two"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn forcepush_allowed() {
|
2024-03-29 12:04:26 +03:00
|
|
|
let Test {
|
|
|
|
repository,
|
|
|
|
project_id,
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
projects,
|
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
|
|
|
projects
|
|
|
|
.update(&projects::UpdateRequest {
|
|
|
|
id: *project_id,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
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 branch_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file one.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit one", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let commit_two_oid = {
|
|
|
|
fs::write(repository.path().join("file two.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit two", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file three.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit three", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file four.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit four", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::squash(project, branch_id, commit_two_oid).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
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 == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let descriptions = branch
|
|
|
|
.commits
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.description.clone())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(
|
|
|
|
descriptions,
|
|
|
|
vec!["commit four", "commit three", "commit one\ncommit two"]
|
|
|
|
);
|
|
|
|
assert!(branch.requires_force);
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn forcepush_forbidden() {
|
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 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::update_virtual_branch(
|
|
|
|
project,
|
|
|
|
BranchUpdateRequest {
|
|
|
|
id: branch_id,
|
|
|
|
allow_rebasing: Some(false),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-06-26 11:23:31 +03:00
|
|
|
|
2024-03-29 12:04:26 +03:00
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file one.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit one", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::push_virtual_branch(project, branch_id, false, None).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let commit_two_oid = {
|
|
|
|
fs::write(repository.path().join("file two.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit two", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file three.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit three", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file four.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit four", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
2024-05-31 11:03:22 +03:00
|
|
|
assert_eq!(
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::squash(project, branch_id, commit_two_oid)
|
2024-03-31 22:52:56 +03:00
|
|
|
.unwrap_err()
|
2024-05-31 11:03:22 +03:00
|
|
|
.to_string(),
|
|
|
|
"force push not allowed"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn root_forbidden() {
|
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 branch_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let commit_one_oid = {
|
|
|
|
fs::write(repository.path().join("file one.txt"), "").unwrap();
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch_id, "commit one", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
2024-05-31 11:03:22 +03:00
|
|
|
assert_eq!(
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::squash(project, branch_id, commit_one_oid)
|
2024-03-31 22:52:56 +03:00
|
|
|
.unwrap_err()
|
2024-05-31 11:03:22 +03:00
|
|
|
.to_string(),
|
|
|
|
"can not squash root commit"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|