2024-07-13 11:38:56 +03:00
|
|
|
use gitbutler_branch::BranchOwnershipClaims;
|
|
|
|
use gitbutler_branch::{BranchCreateRequest, BranchUpdateRequest};
|
2024-07-09 01:10:55 +03:00
|
|
|
|
2024-03-29 12:04:26 +03:00
|
|
|
use super::*;
|
|
|
|
|
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
|
|
|
controller,
|
|
|
|
projects,
|
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
|
|
|
projects
|
|
|
|
.update(&projects::UpdateRequest {
|
|
|
|
id: *project_id,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
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
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
projects
|
|
|
|
.update(&projects::UpdateRequest {
|
|
|
|
id: *project_id,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.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
|
|
|
.unwrap();
|
|
|
|
|
2024-04-29 16:03:01 +03:00
|
|
|
// create commit
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
let commit_id = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch_id, "commit one", None, false)
|
2024-04-29 16:03:01 +03:00
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.push_virtual_branch(project, branch_id, false, None)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
// amend another hunk
|
|
|
|
fs::write(repository.path().join("file2.txt"), "content2").unwrap();
|
2024-07-09 01:10:55 +03:00
|
|
|
let to_amend: BranchOwnershipClaims = "file2.txt:1-2".parse().unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.amend(project, branch_id, commit_id, &to_amend)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let branch = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(branch.requires_force);
|
|
|
|
assert_eq!(branch.commits.len(), 1);
|
|
|
|
assert_eq!(branch.files.len(), 0);
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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
|
|
|
.unwrap();
|
|
|
|
|
2024-06-26 11:23:31 +03:00
|
|
|
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
|
|
|
.unwrap();
|
|
|
|
|
2024-06-26 11:23:31 +03:00
|
|
|
controller
|
|
|
|
.update_virtual_branch(
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-07-13 11:38:56 +03:00
|
|
|
BranchUpdateRequest {
|
2024-06-26 11:23:31 +03:00
|
|
|
id: branch_id,
|
|
|
|
allow_rebasing: Some(false),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-04-29 16:03:01 +03:00
|
|
|
// create commit
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
let commit_oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch_id, "commit one", None, false)
|
2024-04-29 16:03:01 +03:00
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.push_virtual_branch(project, branch_id, false, None)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file2.txt"), "content2").unwrap();
|
2024-07-09 01:10:55 +03:00
|
|
|
let to_amend: BranchOwnershipClaims = "file2.txt:1-2".parse().unwrap();
|
2024-05-31 10:39:29 +03:00
|
|
|
assert_eq!(
|
2024-03-29 12:04:26 +03:00
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.amend(project, branch_id, commit_oid, &to_amend)
|
2024-03-31 22:52:56 +03:00
|
|
|
.unwrap_err()
|
2024-05-31 10:39:29 +03:00
|
|
|
.to_string(),
|
|
|
|
"force-push is not allowed"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn non_locked_hunk() {
|
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
|
|
|
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
|
|
|
.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
|
|
|
.unwrap();
|
|
|
|
|
2024-04-29 16:03:01 +03:00
|
|
|
// create commit
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
let commit_oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch_id, "commit one", None, false)
|
2024-04-29 16:03:01 +03:00
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-04-29 16:03:01 +03:00
|
|
|
let branch = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.list_virtual_branches(project)
|
2024-04-29 16:03:01 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(branch.commits.len(), 1);
|
|
|
|
assert_eq!(branch.files.len(), 0);
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
// amend another hunk
|
|
|
|
fs::write(repository.path().join("file2.txt"), "content2").unwrap();
|
2024-07-09 01:10:55 +03:00
|
|
|
let to_amend: BranchOwnershipClaims = "file2.txt:1-2".parse().unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.amend(project, branch_id, commit_oid, &to_amend)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let branch = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(branch.commits.len(), 1);
|
|
|
|
assert_eq!(branch.files.len(), 0);
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn locked_hunk() {
|
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
|
|
|
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
|
|
|
.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
|
|
|
.unwrap();
|
|
|
|
|
2024-04-29 16:03:01 +03:00
|
|
|
// create commit
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
let commit_oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch_id, "commit one", None, false)
|
2024-04-29 16:03:01 +03:00
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-04-29 16:03:01 +03:00
|
|
|
let branch = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.list_virtual_branches(project)
|
2024-04-29 16:03:01 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(branch.commits.len(), 1);
|
|
|
|
assert_eq!(branch.files.len(), 0);
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
branch.commits[0].files[0].hunks[0].diff,
|
|
|
|
"@@ -0,0 +1 @@\n+content\n\\ No newline at end of file\n"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
// amend another hunk
|
|
|
|
fs::write(repository.path().join("file.txt"), "more content").unwrap();
|
2024-07-09 01:10:55 +03:00
|
|
|
let to_amend: BranchOwnershipClaims = "file.txt:1-2".parse().unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.amend(project, branch_id, commit_oid, &to_amend)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let branch = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.list_virtual_branches(project)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.commits.len(), 1);
|
|
|
|
assert_eq!(branch.files.len(), 0);
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
branch.commits[0].files[0].hunks[0].diff,
|
|
|
|
"@@ -0,0 +1 @@\n+more content\n\\ No newline at end of file\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn non_existing_ownership() {
|
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
|
|
|
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
|
|
|
.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
|
|
|
.unwrap();
|
|
|
|
|
2024-04-29 16:03:01 +03:00
|
|
|
// create commit
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
let commit_oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch_id, "commit one", None, false)
|
2024-04-29 16:03:01 +03:00
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-04-29 16:03:01 +03:00
|
|
|
let branch = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.list_virtual_branches(project)
|
2024-04-29 16:03:01 +03:00
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.into_iter()
|
|
|
|
.find(|b| b.id == branch_id)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(branch.commits.len(), 1);
|
|
|
|
assert_eq!(branch.files.len(), 0);
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
// amend non existing hunk
|
2024-07-09 01:10:55 +03:00
|
|
|
let to_amend: BranchOwnershipClaims = "file2.txt:1-2".parse().unwrap();
|
2024-05-31 10:39:29 +03:00
|
|
|
assert_eq!(
|
2024-03-29 12:04:26 +03:00
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.amend(project, branch_id, commit_oid, &to_amend)
|
2024-03-31 22:52:56 +03:00
|
|
|
.unwrap_err()
|
2024-05-31 10:39:29 +03:00
|
|
|
.to_string(),
|
|
|
|
"target ownership not found"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|
|
|
|
}
|