2024-05-28 19:54:50 +03:00
|
|
|
use std::path::PathBuf;
|
2024-03-29 12:04:26 +03:00
|
|
|
use std::{fs, path, str::FromStr};
|
|
|
|
|
2024-07-13 11:38:56 +03:00
|
|
|
use gitbutler_branch::BranchCreateRequest;
|
2024-07-10 15:49:48 +03:00
|
|
|
use gitbutler_branch_actions::VirtualBranchActions;
|
2024-07-09 12:13:39 +03:00
|
|
|
use gitbutler_error::error::Marker;
|
2024-07-08 16:33:26 +03:00
|
|
|
use gitbutler_project::{self as projects, Project, ProjectId};
|
2024-07-09 02:14:28 +03:00
|
|
|
use gitbutler_reference::Refname;
|
2024-04-06 11:08:50 +03:00
|
|
|
use gitbutler_testsupport::{paths, TestProject, VAR_NO_CLEANUP};
|
2024-07-13 11:38:56 +03:00
|
|
|
use tempfile::TempDir;
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
struct Test {
|
|
|
|
repository: TestProject,
|
|
|
|
project_id: ProjectId,
|
2024-05-17 16:11:09 +03:00
|
|
|
project: Project,
|
2024-03-29 12:04:26 +03:00
|
|
|
projects: projects::Controller,
|
2024-07-10 13:11:36 +03:00
|
|
|
controller: VirtualBranchActions,
|
2024-03-29 12:04:26 +03:00
|
|
|
data_dir: Option<TempDir>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Test {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if std::env::var_os(VAR_NO_CLEANUP).is_some() {
|
|
|
|
let _ = self.data_dir.take().unwrap().into_path();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Test {
|
|
|
|
fn default() -> Self {
|
|
|
|
let data_dir = paths::data_dir();
|
2024-04-24 10:20:55 +03:00
|
|
|
let projects = projects::Controller::from_path(data_dir.path());
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let test_project = TestProject::default();
|
|
|
|
let project = projects
|
|
|
|
.add(test_project.path())
|
|
|
|
.expect("failed to add project");
|
|
|
|
|
|
|
|
Self {
|
|
|
|
repository: test_project,
|
|
|
|
project_id: project.id,
|
2024-07-10 13:11:36 +03:00
|
|
|
controller: VirtualBranchActions {},
|
2024-03-29 12:04:26 +03:00
|
|
|
projects,
|
2024-05-17 16:11:09 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
data_dir: Some(data_dir),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:54:50 +03:00
|
|
|
impl Test {
|
|
|
|
/// Consume this instance and keep the temp directory that held the local repository, returning it.
|
|
|
|
/// Best used inside a `dbg!(test.debug_local_repo())`
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn debug_local_repo(mut self) -> PathBuf {
|
|
|
|
let repo = std::mem::take(&mut self.repository);
|
|
|
|
repo.debug_local_repo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 12:04:26 +03:00
|
|
|
mod amend;
|
|
|
|
mod apply_virtual_branch;
|
2024-07-01 17:13:52 +03:00
|
|
|
mod convert_to_real_branch;
|
2024-03-29 12:04:26 +03:00
|
|
|
mod create_commit;
|
|
|
|
mod create_virtual_branch_from_branch;
|
|
|
|
mod delete_virtual_branch;
|
|
|
|
mod init;
|
2024-04-29 16:03:01 +03:00
|
|
|
mod insert_blank_commit;
|
2024-07-27 16:37:28 +03:00
|
|
|
mod list;
|
2024-04-29 16:03:01 +03:00
|
|
|
mod move_commit_file;
|
2024-03-29 12:04:26 +03:00
|
|
|
mod move_commit_to_vbranch;
|
2024-05-17 16:11:09 +03:00
|
|
|
mod oplog;
|
2024-03-29 12:04:26 +03:00
|
|
|
mod references;
|
2024-04-29 16:03:01 +03:00
|
|
|
mod reorder_commit;
|
2024-03-29 12:04:26 +03:00
|
|
|
mod reset_virtual_branch;
|
|
|
|
mod selected_for_changes;
|
|
|
|
mod set_base_branch;
|
|
|
|
mod squash;
|
|
|
|
mod unapply_ownership;
|
2024-04-29 16:03:01 +03:00
|
|
|
mod undo_commit;
|
2024-03-29 12:04:26 +03:00
|
|
|
mod update_base_branch;
|
|
|
|
mod update_commit_message;
|
|
|
|
mod upstream;
|
2024-05-07 20:37:28 +03:00
|
|
|
mod verify_branch;
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn resolve_conflict_flow() {
|
|
|
|
let Test {
|
|
|
|
repository,
|
2024-07-04 16:26:10 +03:00
|
|
|
project,
|
2024-03-29 12:04:26 +03:00
|
|
|
controller,
|
|
|
|
..
|
|
|
|
} = &Test::default();
|
|
|
|
|
|
|
|
// make sure we have an undiscovered commit in the remote branch
|
|
|
|
{
|
|
|
|
fs::write(repository.path().join("file.txt"), "first").unwrap();
|
|
|
|
let first_commit_oid = repository.commit_all("first");
|
|
|
|
fs::write(repository.path().join("file.txt"), "second").unwrap();
|
|
|
|
repository.commit_all("second");
|
2024-05-30 12:33:13 +03:00
|
|
|
fs::write(repository.path().join("third.txt"), "three").unwrap();
|
|
|
|
repository.commit_all("third");
|
2024-03-29 12:04:26 +03:00
|
|
|
repository.push();
|
|
|
|
repository.reset_hard(Some(first_commit_oid));
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2024-07-01 17:13:52 +03:00
|
|
|
{
|
2024-03-29 12:04:26 +03:00
|
|
|
// make a branch that conflicts with the remote branch, but doesn't know about it yet
|
|
|
|
let branch1_id = controller
|
2024-07-13 11:38:56 +03:00
|
|
|
.create_virtual_branch(project, &BranchCreateRequest::default())
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
fs::write(repository.path().join("file.txt"), "conflict").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!(branches[0].active);
|
|
|
|
};
|
|
|
|
|
2024-07-01 17:13:52 +03:00
|
|
|
let unapplied_branch = {
|
|
|
|
// fetch remote. There is now a conflict, so the branch will be unapplied
|
2024-07-04 16:26:10 +03:00
|
|
|
let unapplied_branches = controller.update_base_branch(project).await.unwrap();
|
2024-07-01 17:13:52 +03:00
|
|
|
assert_eq!(unapplied_branches.len(), 1);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// there is a conflict now, so the branch should be inactive
|
2024-07-04 16:26:10 +03:00
|
|
|
let (branches, _) = controller.list_virtual_branches(project).await.unwrap();
|
2024-07-01 17:13:52 +03:00
|
|
|
assert_eq!(branches.len(), 0);
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-07-09 02:14:28 +03:00
|
|
|
Refname::from_str(&unapplied_branches[0]).unwrap()
|
2024-07-01 17:13:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let branch1_id = {
|
2024-03-29 12:04:26 +03:00
|
|
|
// when we apply conflicted branch, it has conflict
|
2024-07-01 17:13:52 +03:00
|
|
|
let branch1_id = controller
|
2024-07-25 16:11:07 +03:00
|
|
|
.create_virtual_branch_from_branch(project, &unapplied_branch, None)
|
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!(branches[0].active);
|
|
|
|
assert!(branches[0].conflicted);
|
2024-05-30 12:33:13 +03:00
|
|
|
assert_eq!(branches[0].files.len(), 2); // third.txt should be present during conflict
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
// and the conflict markers are in the file
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
|
|
|
|
"<<<<<<< ours\nconflict\n=======\nsecond\n>>>>>>> theirs\n"
|
|
|
|
);
|
2024-07-01 17:13:52 +03:00
|
|
|
|
|
|
|
branch1_id
|
|
|
|
};
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
// can't commit conflicts
|
|
|
|
assert!(matches!(
|
|
|
|
controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch1_id, "commit conflicts", None, false)
|
2024-03-31 22:52:56 +03:00
|
|
|
.await
|
|
|
|
.unwrap_err()
|
|
|
|
.downcast_ref(),
|
2024-05-31 16:46:04 +03:00
|
|
|
Some(Marker::ProjectConflict)
|
2024-03-29 12:04:26 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// fixing the conflict removes conflicted mark
|
|
|
|
fs::write(repository.path().join("file.txt"), "resolved").unwrap();
|
2024-07-04 16:26:10 +03:00
|
|
|
controller.list_virtual_branches(project).await.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
let commit_oid = controller
|
2024-07-04 16:26:10 +03:00
|
|
|
.create_commit(project, branch1_id, "resolution", None, false)
|
2024-03-29 12:04:26 +03:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let commit = repository.find_commit(commit_oid).unwrap();
|
|
|
|
assert_eq!(commit.parent_count(), 2);
|
|
|
|
|
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!(branches[0].active);
|
|
|
|
assert!(!branches[0].conflicted);
|
|
|
|
}
|
|
|
|
}
|