2024-03-31 00:27:56 +03:00
|
|
|
use std::fs;
|
|
|
|
|
2024-07-13 11:38:56 +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
|
|
|
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 branch1_id = controller
|
2024-07-09 01:10:55 +03:00
|
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let oid = {
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
|
|
|
// commit changes
|
|
|
|
let oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
let (branches, _) = controller.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
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.reset_virtual_branch(project, branch1_id, oid)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
let (branches, _) = controller.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
|
|
|
controller,
|
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
|
|
|
let base_branch = 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 branch1_id = controller
|
2024-07-09 01:10:55 +03:00
|
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
|
|
|
// commit changes
|
|
|
|
let oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
let (branches, _) = controller.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
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.reset_virtual_branch(project, branch1_id, base_branch.base_sha)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
let (branches, _) = controller.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
|
|
|
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 branch1_id = controller
|
2024-07-09 01:10:55 +03:00
|
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let first_commit_oid = {
|
|
|
|
// commit some changes
|
|
|
|
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
|
|
|
let oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
let (branches, _) = controller.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();
|
|
|
|
|
|
|
|
let second_commit_oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
let (branches, _) = controller.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
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.reset_virtual_branch(project, branch1_id, first_commit_oid)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
let (branches, _) = controller.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
|
|
|
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 branch1_id = controller
|
2024-07-09 01:10:55 +03:00
|
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
|
|
|
|
// commit changes
|
|
|
|
let oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
let (branches, _) = controller.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-03-29 12:04:26 +03:00
|
|
|
controller
|
|
|
|
.reset_virtual_branch(
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-05-29 12:07:36 +03:00
|
|
|
branch1_id,
|
2024-03-29 12:04:26 +03:00
|
|
|
"fe14df8c66b73c6276f7bb26102ad91da680afcb".parse().unwrap()
|
|
|
|
)
|
2024-03-31 22:52:56 +03:00
|
|
|
.unwrap_err()
|
2024-05-31 11:03:22 +03:00
|
|
|
.to_string(),
|
|
|
|
"commit fe14df8c66b73c6276f7bb26102ad91da680afcb not in the branch"
|
|
|
|
);
|
2024-03-29 12:04:26 +03:00
|
|
|
}
|