2024-07-28 21:48:13 +03:00
|
|
|
use std::{fs, path, path::PathBuf, str::FromStr};
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-08-28 16:32:28 +03:00
|
|
|
use gitbutler_branch::{BranchCreateRequest, VirtualBranchesHandle};
|
2024-09-06 15:12:14 +03:00
|
|
|
use gitbutler_branch_actions::update_workspace_commit;
|
2024-08-21 16:00:01 +03:00
|
|
|
use gitbutler_branch_actions::GITBUTLER_WORKSPACE_COMMIT_TITLE;
|
2024-08-28 16:32:28 +03:00
|
|
|
use gitbutler_command_context::CommandContext;
|
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,
|
|
|
|
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,
|
|
|
|
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)]
|
2024-08-28 08:41:53 +03:00
|
|
|
pub fn debug_local_repo(&mut self) -> Option<PathBuf> {
|
|
|
|
self.repository.debug_local_repo()
|
2024-05-28 19:54:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 12:04:26 +03:00
|
|
|
mod amend;
|
|
|
|
mod apply_virtual_branch;
|
|
|
|
mod create_commit;
|
|
|
|
mod create_virtual_branch_from_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-08-10 22:04:09 +03:00
|
|
|
mod list_details;
|
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;
|
2024-09-13 17:58:42 +03:00
|
|
|
mod save_and_unapply_virtual_branch;
|
2024-03-29 12:04:26 +03:00
|
|
|
mod selected_for_changes;
|
|
|
|
mod set_base_branch;
|
|
|
|
mod squash;
|
|
|
|
mod unapply_ownership;
|
2024-09-13 17:58:42 +03:00
|
|
|
mod unapply_without_saving_virtual_branch;
|
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-08-30 10:56:32 +03:00
|
|
|
mod workspace_migration;
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn resolve_conflict_flow() {
|
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"), "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));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2024-03-29 12:04:26 +03:00
|
|
|
// make a branch that conflicts with the remote branch, but doesn't know about it yet
|
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("file.txt"), "conflict").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);
|
|
|
|
};
|
|
|
|
|
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-09-06 15:12:14 +03:00
|
|
|
let unapplied_branches = gitbutler_branch_actions::update_base_branch(project).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-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
|
|
|
|
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-09-06 15:12:14 +03:00
|
|
|
let branch1_id = gitbutler_branch_actions::create_virtual_branch_from_branch(
|
|
|
|
project,
|
|
|
|
&unapplied_branch,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
2024-08-28 16:32:28 +03:00
|
|
|
let vb_state = VirtualBranchesHandle::new(project.gb_dir());
|
|
|
|
let ctx = CommandContext::open(project).unwrap();
|
2024-08-28 14:06:28 +03:00
|
|
|
update_workspace_commit(&vb_state, &ctx).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!(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!(
|
2024-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::create_commit(
|
|
|
|
project,
|
|
|
|
branch1_id,
|
|
|
|
"commit conflicts",
|
|
|
|
None,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
.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-09-06 15:12:14 +03:00
|
|
|
gitbutler_branch_actions::list_virtual_branches(project).unwrap();
|
|
|
|
let commit_oid =
|
|
|
|
gitbutler_branch_actions::create_commit(project, branch1_id, "resolution", None, false)
|
|
|
|
.unwrap();
|
2024-03-29 12:04:26 +03:00
|
|
|
|
|
|
|
let commit = repository.find_commit(commit_oid).unwrap();
|
|
|
|
assert_eq!(commit.parent_count(), 2);
|
|
|
|
|
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!(!branches[0].conflicted);
|
|
|
|
}
|
|
|
|
}
|