2024-07-13 11:38:56 +03:00
|
|
|
use gitbutler_branch::BranchCreateRequest;
|
2024-07-09 02:14:28 +03:00
|
|
|
use gitbutler_reference::Refname;
|
|
|
|
|
2024-03-29 12:04:26 +03:00
|
|
|
use super::*;
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn rebase_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();
|
|
|
|
|
|
|
|
// make sure we have an undiscovered commit in the remote branch
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file.txt"), "one").unwrap();
|
|
|
|
fs::write(repository.path().join("another_file.txt"), "").unwrap();
|
|
|
|
let first_commit_oid = repository.commit_all("first");
|
|
|
|
fs::write(repository.path().join("file.txt"), "two").unwrap();
|
|
|
|
repository.commit_all("second");
|
|
|
|
repository.push();
|
|
|
|
repository.reset_hard(Some(first_commit_oid));
|
|
|
|
}
|
|
|
|
|
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-07-01 17:13:52 +03:00
|
|
|
let mut branch1_id = {
|
2024-03-29 12:04:26 +03:00
|
|
|
// create a branch with some commited work
|
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("another_file.txt"), "virtual").unwrap();
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(project, branch1_id, "virtual commit", None, false)
|
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!(branches[0].active);
|
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
|
|
|
|
|
|
|
branch1_id
|
|
|
|
};
|
|
|
|
|
2024-07-01 17:13:52 +03:00
|
|
|
let unapplied_branch = {
|
2024-03-29 12:04:26 +03:00
|
|
|
// unapply first vbranch
|
2024-09-06 15:12:14 +03:00
|
|
|
let unapplied_branch =
|
2024-09-13 17:58:42 +03:00
|
|
|
gitbutler_branch_actions::save_and_unapply_virutal_branch(project, branch1_id).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("another_file.txt")).unwrap(),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"one"
|
|
|
|
);
|
|
|
|
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-07-01 17:13:52 +03:00
|
|
|
assert_eq!(branches.len(), 0);
|
|
|
|
|
2024-07-09 02:14:28 +03:00
|
|
|
Refname::from_str(&unapplied_branch).unwrap()
|
2024-07-01 17:13:52 +03:00
|
|
|
};
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
// fetch remote
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::update_base_branch(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// branch is stil unapplied
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-07-01 17:13:52 +03:00
|
|
|
assert_eq!(branches.len(), 0);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("another_file.txt")).unwrap(),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"two"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// apply first vbranch again
|
2024-09-06 15:12:14 +03:00
|
|
|
branch1_id = gitbutler_branch_actions::create_virtual_branch_from_branch(
|
|
|
|
project,
|
|
|
|
&unapplied_branch,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// it should be rebased
|
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].files.len(), 0);
|
2024-07-25 16:25:51 +03:00
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
2024-03-29 12:04:26 +03:00
|
|
|
assert!(branches[0].active);
|
|
|
|
assert!(!branches[0].conflicted);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("another_file.txt")).unwrap(),
|
|
|
|
"virtual"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"two"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn rebase_work() {
|
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();
|
|
|
|
|
|
|
|
// make sure we have an undiscovered commit in the remote branch
|
|
|
|
{
|
|
|
|
let first_commit_oid = repository.commit_all("first");
|
|
|
|
fs::write(repository.path().join("file.txt"), "").unwrap();
|
|
|
|
repository.commit_all("second");
|
|
|
|
repository.push();
|
|
|
|
repository.reset_hard(Some(first_commit_oid));
|
|
|
|
}
|
|
|
|
|
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-07-01 17:13:52 +03:00
|
|
|
let mut branch1_id = {
|
2024-03-29 12:04:26 +03:00
|
|
|
// make a branch with some work
|
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("another_file.txt"), "").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!(branches[0].active);
|
|
|
|
assert_eq!(branches[0].files.len(), 1);
|
|
|
|
assert_eq!(branches[0].commits.len(), 0);
|
|
|
|
|
|
|
|
branch1_id
|
|
|
|
};
|
|
|
|
|
2024-07-01 17:13:52 +03:00
|
|
|
let unapplied_branch = {
|
2024-03-29 12:04:26 +03:00
|
|
|
// unapply first vbranch
|
2024-09-06 15:12:14 +03:00
|
|
|
let unapplied_branch =
|
2024-09-13 17:58:42 +03:00
|
|
|
gitbutler_branch_actions::save_and_unapply_virutal_branch(project, branch1_id).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-07-01 17:13:52 +03:00
|
|
|
assert_eq!(branches.len(), 0);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
assert!(!repository.path().join("another_file.txt").exists());
|
|
|
|
assert!(!repository.path().join("file.txt").exists());
|
2024-07-01 17:13:52 +03:00
|
|
|
|
2024-07-09 02:14:28 +03:00
|
|
|
Refname::from_str(&unapplied_branch).unwrap()
|
2024-07-01 17:13:52 +03:00
|
|
|
};
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
// fetch remote
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::update_base_branch(project).unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// first branch is stil unapplied
|
2024-09-06 15:12:14 +03:00
|
|
|
let (branches, _) = gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
2024-07-01 17:13:52 +03:00
|
|
|
assert_eq!(branches.len(), 0);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
assert!(!repository.path().join("another_file.txt").exists());
|
|
|
|
assert!(repository.path().join("file.txt").exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// apply first vbranch again
|
2024-09-06 15:12:14 +03:00
|
|
|
branch1_id = gitbutler_branch_actions::create_virtual_branch_from_branch(
|
|
|
|
project,
|
|
|
|
&unapplied_branch,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// workdir should be rebased, and work should be restored
|
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);
|
2024-07-08 18:09:01 +03:00
|
|
|
// TODO: Should be 1
|
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
// TODO: Should be 0
|
|
|
|
assert_eq!(branches[0].commits.len(), 1);
|
2024-03-29 12:04:26 +03:00
|
|
|
assert!(branches[0].active);
|
|
|
|
assert!(!branches[0].conflicted);
|
|
|
|
|
|
|
|
assert!(repository.path().join("another_file.txt").exists());
|
|
|
|
assert!(repository.path().join("file.txt").exists());
|
|
|
|
}
|
|
|
|
}
|