2024-03-31 00:27:56 +03:00
|
|
|
use std::fs;
|
|
|
|
|
2024-05-31 11:03:22 +03:00
|
|
|
use gitbutler_core::virtual_branches::branch;
|
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-03-29 12:04:26 +03:00
|
|
|
#[tokio::test]
|
|
|
|
async fn to_head() {
|
|
|
|
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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let branch1_id = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn to_target() {
|
|
|
|
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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let branch1_id = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn to_commit() {
|
|
|
|
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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let branch1_id = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn to_non_existing() {
|
|
|
|
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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let branch1_id = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.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
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.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
|
|
|
.await
|
|
|
|
.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
|
|
|
}
|