mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-21 08:31:40 +03:00
9e236e3469
The reason is that it's unclear if maybe it's there for a good reason, and I don't want to break the complex code in there.
251 lines
7.2 KiB
Rust
251 lines
7.2 KiB
Rust
use std::fs;
|
|
|
|
use gitbutler_branch::BranchCreateRequest;
|
|
|
|
use super::Test;
|
|
|
|
#[test]
|
|
fn to_head() {
|
|
let Test {
|
|
repository,
|
|
project,
|
|
controller,
|
|
..
|
|
} = &Test::default();
|
|
|
|
controller
|
|
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
|
|
.unwrap();
|
|
|
|
let branch1_id = controller
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
|
.unwrap();
|
|
|
|
let oid = {
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
// commit changes
|
|
let oid = controller
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
|
.unwrap();
|
|
|
|
let (branches, _) = controller.list_virtual_branches(project).unwrap();
|
|
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
|
|
.reset_virtual_branch(project, branch1_id, oid)
|
|
.unwrap();
|
|
|
|
let (branches, _) = controller.list_virtual_branches(project).unwrap();
|
|
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"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn to_target() {
|
|
let Test {
|
|
repository,
|
|
project,
|
|
controller,
|
|
..
|
|
} = &Test::default();
|
|
|
|
let base_branch = controller
|
|
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
|
|
.unwrap();
|
|
|
|
let branch1_id = controller
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
|
.unwrap();
|
|
|
|
{
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
// commit changes
|
|
let oid = controller
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
|
.unwrap();
|
|
|
|
let (branches, _) = controller.list_virtual_branches(project).unwrap();
|
|
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
|
|
.reset_virtual_branch(project, branch1_id, base_branch.base_sha)
|
|
.unwrap();
|
|
|
|
let (branches, _) = controller.list_virtual_branches(project).unwrap();
|
|
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"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn to_commit() {
|
|
let Test {
|
|
repository,
|
|
project,
|
|
controller,
|
|
..
|
|
} = &Test::default();
|
|
|
|
controller
|
|
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
|
|
.unwrap();
|
|
|
|
let branch1_id = controller
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
|
.unwrap();
|
|
|
|
let first_commit_oid = {
|
|
// commit some changes
|
|
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
let oid = controller
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
|
.unwrap();
|
|
|
|
let (branches, _) = controller.list_virtual_branches(project).unwrap();
|
|
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
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
|
.unwrap();
|
|
|
|
let (branches, _) = controller.list_virtual_branches(project).unwrap();
|
|
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
|
|
.reset_virtual_branch(project, branch1_id, first_commit_oid)
|
|
.unwrap();
|
|
|
|
let (branches, _) = controller.list_virtual_branches(project).unwrap();
|
|
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"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn to_non_existing() {
|
|
let Test {
|
|
repository,
|
|
project,
|
|
controller,
|
|
..
|
|
} = &Test::default();
|
|
|
|
controller
|
|
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
|
|
.unwrap();
|
|
|
|
let branch1_id = controller
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
|
.unwrap();
|
|
|
|
{
|
|
fs::write(repository.path().join("file.txt"), "content").unwrap();
|
|
|
|
// commit changes
|
|
let oid = controller
|
|
.create_commit(project, branch1_id, "commit", None, false)
|
|
.unwrap();
|
|
|
|
let (branches, _) = controller.list_virtual_branches(project).unwrap();
|
|
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
|
|
};
|
|
|
|
assert_eq!(
|
|
controller
|
|
.reset_virtual_branch(
|
|
project,
|
|
branch1_id,
|
|
"fe14df8c66b73c6276f7bb26102ad91da680afcb".parse().unwrap()
|
|
)
|
|
.unwrap_err()
|
|
.to_string(),
|
|
"commit fe14df8c66b73c6276f7bb26102ad91da680afcb not in the branch"
|
|
);
|
|
}
|