2024-03-31 00:27:56 +03:00
|
|
|
use std::fs;
|
|
|
|
|
2024-10-08 16:44:59 +03:00
|
|
|
use gitbutler_branch::BranchCreateRequest;
|
2024-03-31 00:27:56 +03:00
|
|
|
|
2024-07-07 14:32:35 +03:00
|
|
|
use super::Test;
|
2024-03-30 21:41:45 +03:00
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn to_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 branch1_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let oid = {
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
|
|
|
// commit changes
|
2024-09-06 15:12:14 +03:00
|
|
|
let oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, branch1_id, "commit", None, false)
|
|
|
|
.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, branch1_id);
|
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
|
|
|
assert_eq!(branches[0].commits[0].id, oid);
|
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"content"
|
|
|
|
);
|
|
|
|
|
|
|
|
oid
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
// reset changes to head
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::reset_virtual_branch(project, branch1_id, oid).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, branch1_id);
|
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
|
|
|
assert_eq!(branches[0].commits[0].id, oid);
|
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"content"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn to_target() {
|
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
|
|
|
let base_branch = 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 branch1_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.txt"), "content").unwrap();
|
|
|
|
|
|
|
|
// commit changes
|
2024-09-06 15:12:14 +03:00
|
|
|
let oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, branch1_id, "commit", None, false)
|
|
|
|
.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, branch1_id);
|
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
|
|
|
assert_eq!(branches[0].commits[0].id, oid);
|
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"content"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// reset changes to head
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::reset_virtual_branch(project, branch1_id, base_branch.base_sha)
|
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_eq!(branches[0].id, branch1_id);
|
|
|
|
assert_eq!(branches[0].commits.len(), 0);
|
|
|
|
assert_eq!(branches[0].files.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"content"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn to_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
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let branch1_id =
|
|
|
|
gitbutler_branch_actions::create_virtual_branch(project, &BranchCreateRequest::default())
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let first_commit_oid = {
|
|
|
|
// commit some changes
|
|
|
|
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, branch1_id, "commit", None, false)
|
|
|
|
.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, branch1_id);
|
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
|
|
|
assert_eq!(branches[0].commits[0].id, oid);
|
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"content"
|
|
|
|
);
|
|
|
|
|
|
|
|
oid
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
// commit some more
|
|
|
|
fs::write(repository.path().join("file.txt"), "more content").unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let second_commit_oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, branch1_id, "commit", None, false)
|
|
|
|
.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, branch1_id);
|
|
|
|
assert_eq!(branches[0].commits.len(), 2);
|
|
|
|
assert_eq!(branches[0].commits[0].id, second_commit_oid);
|
|
|
|
assert_eq!(branches[0].commits[1].id, first_commit_oid);
|
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"more content"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// reset changes to the first commit
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::reset_virtual_branch(project, branch1_id, first_commit_oid)
|
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_eq!(branches[0].id, branch1_id);
|
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
|
|
|
assert_eq!(branches[0].commits[0].id, first_commit_oid);
|
|
|
|
assert_eq!(branches[0].files.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"more content"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn to_non_existing() {
|
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 branch1_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.txt"), "content").unwrap();
|
|
|
|
|
|
|
|
// commit changes
|
2024-09-06 15:12:14 +03:00
|
|
|
let oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, branch1_id, "commit", None, false)
|
|
|
|
.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, branch1_id);
|
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
|
|
|
assert_eq!(branches[0].commits[0].id, oid);
|
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"content"
|
|
|
|
);
|
|
|
|
|
|
|
|
oid
|
|
|
|
};
|
|
|
|
|
2024-05-31 11:03:22 +03:00
|
|
|
assert_eq!(
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::reset_virtual_branch(
|
|
|
|
project,
|
|
|
|
branch1_id,
|
|
|
|
"fe14df8c66b73c6276f7bb26102ad91da680afcb".parse().unwrap()
|
|
|
|
)
|
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
2024-05-31 11:03:22 +03:00
|
|
|
"commit fe14df8c66b73c6276f7bb26102ad91da680afcb not in the branch"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|