2024-07-07 18:30:18 +03:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
io::Write,
|
|
|
|
path::{Path, PathBuf},
|
2024-07-09 17:43:58 +03:00
|
|
|
str::FromStr,
|
2024-07-07 18:30:18 +03:00
|
|
|
};
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
use std::{
|
|
|
|
fs::Permissions,
|
|
|
|
os::unix::{fs::symlink, prelude::*},
|
|
|
|
};
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
use git2::TreeEntry;
|
2024-07-09 01:10:55 +03:00
|
|
|
use gitbutler_branch::{
|
2024-07-28 21:48:13 +03:00
|
|
|
BranchCreateRequest, BranchOwnershipClaims, BranchUpdateRequest, Target, VirtualBranchesHandle,
|
2024-07-07 18:30:18 +03:00
|
|
|
};
|
2024-07-12 17:17:15 +03:00
|
|
|
use gitbutler_branch_actions::{
|
2024-07-20 23:01:46 +03:00
|
|
|
commit, get_applied_status, integrate_upstream_commits, is_remote_branch_mergeable,
|
2024-07-12 17:17:15 +03:00
|
|
|
list_virtual_branches, unapply_ownership, update_branch, update_gitbutler_integration,
|
2024-07-28 21:48:13 +03:00
|
|
|
verify_branch, BranchManagerExt, Get,
|
2024-07-10 18:12:26 +03:00
|
|
|
};
|
2024-07-09 14:19:49 +03:00
|
|
|
use gitbutler_commit::{commit_ext::CommitExt, commit_headers::CommitHeadersV2};
|
2024-07-09 02:14:28 +03:00
|
|
|
use gitbutler_reference::{Refname, RemoteRefname};
|
2024-07-08 13:21:01 +03:00
|
|
|
use gitbutler_repo::RepositoryExt;
|
2024-07-07 18:30:18 +03:00
|
|
|
use gitbutler_testsupport::{commit_all, virtual_branches::set_test_target, Case, Suite};
|
2024-07-28 21:48:13 +03:00
|
|
|
use pretty_assertions::assert_eq;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn commit_on_branch_then_change_file_then_get_status() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { project, ctx, .. } = &suite.new_case_with_files(HashMap::from([
|
2024-07-07 18:30:18 +03:00
|
|
|
(PathBuf::from("test.txt"), "line1\nline2\nline3\nline4\n"),
|
|
|
|
(PathBuf::from("test2.txt"), "line5\nline6\nline7\nline8\n"),
|
|
|
|
]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch1_id = ctx
|
2024-07-09 17:43:58 +03:00
|
|
|
.branch_manager()
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line0\nline1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches[0];
|
|
|
|
assert_eq!(branch.files.len(), 1);
|
|
|
|
assert_eq!(branch.commits.len(), 0);
|
|
|
|
|
|
|
|
// commit
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "test commit", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// status (no files)
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches[0];
|
|
|
|
assert_eq!(branch.files.len(), 0);
|
|
|
|
assert_eq!(branch.commits.len(), 1);
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test2.txt"),
|
|
|
|
"line5\nline6\nlineBLAH\nline7\nline8\n",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// should have just the last change now, the other line is committed
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches[0];
|
|
|
|
assert_eq!(branch.files.len(), 1);
|
|
|
|
assert_eq!(branch.commits.len(), 1);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn track_binary_files() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
|
|
|
let file_path2 = Path::new("test2.txt");
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path2),
|
|
|
|
"line5\nline6\nline7\nline8\n",
|
|
|
|
)?;
|
|
|
|
// add a binary file
|
|
|
|
let image_data: [u8; 12] = [
|
|
|
|
255, 0, 0, // Red pixel
|
|
|
|
0, 0, 255, // Blue pixel
|
|
|
|
255, 255, 0, // Yellow pixel
|
|
|
|
0, 255, 0, // Green pixel
|
|
|
|
];
|
|
|
|
let mut file = std::fs::File::create(Path::new(&project.path).join("image.bin"))?;
|
|
|
|
file.write_all(&image_data)?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch1_id = ctx
|
2024-07-09 17:43:58 +03:00
|
|
|
.branch_manager()
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
// test file change
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path2),
|
|
|
|
"line5\nline6\nline7\nline8\nline9\n",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// add a binary file
|
|
|
|
let image_data: [u8; 12] = [
|
|
|
|
255, 0, 0, // Red pixel
|
|
|
|
0, 255, 0, // Green pixel
|
|
|
|
0, 0, 255, // Blue pixel
|
|
|
|
255, 255, 0, // Yellow pixel
|
|
|
|
];
|
|
|
|
let mut file = std::fs::File::create(Path::new(&project.path).join("image.bin"))?;
|
|
|
|
file.write_all(&image_data)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches[0];
|
|
|
|
assert_eq!(branch.files.len(), 2);
|
|
|
|
let img_file = &branch
|
|
|
|
.files
|
|
|
|
.iter()
|
|
|
|
.find(|b| b.path.as_os_str() == "image.bin")
|
|
|
|
.unwrap();
|
|
|
|
assert!(img_file.binary);
|
|
|
|
let img_oid_hex = "944996dd82015a616247c72b251e41661e528ae1";
|
|
|
|
assert_eq!(
|
|
|
|
img_file.hunks[0].diff, img_oid_hex,
|
|
|
|
"the binary file was stored in the ODB as otherwise we wouldn't have its contents. \
|
|
|
|
It cannot easily be reconstructed from the diff-lines, or we don't attempt it."
|
|
|
|
);
|
|
|
|
|
|
|
|
// commit
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "test commit", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// status (no files)
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission()).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
let commit_id = &branches[0].commits[0].id;
|
2024-07-29 12:13:25 +03:00
|
|
|
let commit_obj = ctx.repository().find_commit(commit_id.to_owned())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let tree = commit_obj.tree()?;
|
2024-07-29 12:13:25 +03:00
|
|
|
let files = tree_to_entry_list(ctx.repository(), &tree);
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(files[0].0, "image.bin");
|
|
|
|
assert_eq!(
|
|
|
|
files[0].3, img_oid_hex,
|
|
|
|
"our vbranch commit tree references the binary object we previously stored"
|
|
|
|
);
|
|
|
|
|
|
|
|
let image_data: [u8; 12] = [
|
|
|
|
0, 255, 0, // Green pixel
|
|
|
|
255, 0, 0, // Red pixel
|
|
|
|
255, 255, 0, // Yellow pixel
|
|
|
|
0, 0, 255, // Blue pixel
|
|
|
|
];
|
|
|
|
let mut file = std::fs::File::create(Path::new(&project.path).join("image.bin"))?;
|
|
|
|
file.write_all(&image_data)?;
|
|
|
|
|
|
|
|
// commit
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "test commit", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission()).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
let commit_id = &branches[0].commits[0].id;
|
|
|
|
// get tree from commit_id
|
2024-07-29 12:13:25 +03:00
|
|
|
let commit_obj = ctx.repository().find_commit(commit_id.to_owned())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let tree = commit_obj.tree()?;
|
2024-07-29 12:13:25 +03:00
|
|
|
let files = tree_to_entry_list(ctx.repository(), &tree);
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
assert_eq!(files[0].0, "image.bin");
|
|
|
|
assert_eq!(files[0].3, "ea6901a04d1eed6ebf6822f4360bda9f008fa317");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_branch_with_ownership() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { project, ctx, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path), "line1\nline2\n").unwrap();
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 23:22:55 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch0 = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch");
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
get_applied_status(ctx, None).expect("failed to get status");
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let vb_state = VirtualBranchesHandle::new(ctx.project().gb_dir());
|
2024-07-08 15:52:00 +03:00
|
|
|
let branch0 = vb_state.get_branch_in_workspace(branch0.id).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1 = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(
|
|
|
|
&BranchCreateRequest {
|
|
|
|
ownership: Some(branch0.ownership),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2024-07-15 23:22:55 +03:00
|
|
|
guard.write_permission(),
|
2024-07-15 15:22:27 +03:00
|
|
|
)
|
2024-07-09 17:43:58 +03:00
|
|
|
.expect("failed to create virtual branch");
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let files_by_branch_id = statuses
|
|
|
|
.iter()
|
|
|
|
.map(|(branch, files)| (branch.id, files))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
assert_eq!(files_by_branch_id.len(), 2);
|
|
|
|
assert_eq!(files_by_branch_id[&branch0.id].len(), 0);
|
|
|
|
assert_eq!(files_by_branch_id[&branch1.id].len(), 1);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_branch_in_the_middle() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-09 17:43:58 +03:00
|
|
|
branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(
|
|
|
|
&BranchCreateRequest::default(),
|
|
|
|
project.exclusive_worktree_access().write_permission(),
|
|
|
|
)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch");
|
2024-07-09 17:43:58 +03:00
|
|
|
branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(
|
|
|
|
&BranchCreateRequest::default(),
|
|
|
|
project.exclusive_worktree_access().write_permission(),
|
|
|
|
)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch");
|
2024-07-09 17:43:58 +03:00
|
|
|
branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(
|
|
|
|
&BranchCreateRequest {
|
|
|
|
order: Some(1),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
project.exclusive_worktree_access().write_permission(),
|
|
|
|
)
|
2024-07-09 17:43:58 +03:00
|
|
|
.expect("failed to create virtual branch");
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let vb_state = VirtualBranchesHandle::new(ctx.project().gb_dir());
|
2024-07-05 16:58:35 +03:00
|
|
|
let mut branches = vb_state
|
|
|
|
.list_branches_in_workspace()
|
|
|
|
.expect("failed to read branches");
|
2024-07-07 18:30:18 +03:00
|
|
|
branches.sort_by_key(|b| b.order);
|
|
|
|
assert_eq!(branches.len(), 3);
|
|
|
|
assert_eq!(branches[0].name, "Virtual branch");
|
|
|
|
assert_eq!(branches[1].name, "Virtual branch 2");
|
|
|
|
assert_eq!(branches[2].name, "Virtual branch 1");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_branch_no_arguments() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-09 17:43:58 +03:00
|
|
|
branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(
|
|
|
|
&BranchCreateRequest::default(),
|
|
|
|
project.exclusive_worktree_access().write_permission(),
|
|
|
|
)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch");
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let vb_state = VirtualBranchesHandle::new(ctx.project().gb_dir());
|
2024-07-05 16:58:35 +03:00
|
|
|
let branches = vb_state
|
|
|
|
.list_branches_in_workspace()
|
|
|
|
.expect("failed to read branches");
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
assert_eq!(branches[0].name, "Virtual branch");
|
|
|
|
assert_eq!(branches[0].ownership, BranchOwnershipClaims::default());
|
|
|
|
assert_eq!(branches[0].order, 0);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hunk_expantion() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path), "line1\nline2\n")?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 23:22:55 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch2_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let files_by_branch_id = statuses
|
|
|
|
.iter()
|
|
|
|
.map(|(branch, files)| (branch.id, files))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
assert_eq!(files_by_branch_id.len(), 2);
|
|
|
|
assert_eq!(files_by_branch_id[&branch1_id].len(), 1);
|
|
|
|
assert_eq!(files_by_branch_id[&branch2_id].len(), 0);
|
|
|
|
|
|
|
|
// even though selected branch has changed
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch1_id,
|
|
|
|
order: Some(1),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch2_id,
|
|
|
|
order: Some(0),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// a slightly different hunk should still go to the same branch
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
let files_by_branch_id = statuses
|
|
|
|
.iter()
|
|
|
|
.map(|(branch, files)| (branch.id, files))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
assert_eq!(files_by_branch_id.len(), 2);
|
|
|
|
assert_eq!(files_by_branch_id[&branch1_id].len(), 1);
|
|
|
|
assert_eq!(files_by_branch_id[&branch2_id].len(), 0);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_status_files_by_branch_no_hunks_no_branches() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-16 17:55:49 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
assert_eq!(statuses.len(), 0);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_status_files_by_branch() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path), "line1\nline2\n")?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 23:22:55 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch2_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
let files_by_branch_id = statuses
|
|
|
|
.iter()
|
|
|
|
.map(|(branch, files)| (branch.id, files))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
assert_eq!(files_by_branch_id.len(), 2);
|
|
|
|
assert_eq!(files_by_branch_id[&branch1_id].len(), 1);
|
|
|
|
assert_eq!(files_by_branch_id[&branch2_id].len(), 0);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_hunks_multiple_sources() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case_with_files(HashMap::from([(
|
2024-07-07 18:30:18 +03:00
|
|
|
PathBuf::from("test.txt"),
|
|
|
|
"line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\n",
|
|
|
|
)]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 23:22:55 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch2_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch3_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line0\nline1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let vb_state = VirtualBranchesHandle::new(ctx.project().gb_dir());
|
2024-07-08 15:52:00 +03:00
|
|
|
let mut branch2 = vb_state.get_branch_in_workspace(branch2_id)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
branch2.ownership = BranchOwnershipClaims {
|
|
|
|
claims: vec!["test.txt:1-5".parse()?],
|
|
|
|
};
|
|
|
|
vb_state.set_branch(branch2.clone())?;
|
2024-07-08 15:52:00 +03:00
|
|
|
let mut branch1 = vb_state.get_branch_in_workspace(branch1_id)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
branch1.ownership = BranchOwnershipClaims {
|
|
|
|
claims: vec!["test.txt:11-15".parse()?],
|
|
|
|
};
|
|
|
|
vb_state.set_branch(branch1.clone())?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let files_by_branch_id = statuses
|
|
|
|
.iter()
|
|
|
|
.map(|(branch, files)| (branch.id, files))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
assert_eq!(files_by_branch_id.len(), 3);
|
|
|
|
assert_eq!(files_by_branch_id[&branch1_id].len(), 1);
|
|
|
|
// assert_eq!(files_by_branch_id[&branch1_id][0].hunks.len(), 1);
|
|
|
|
assert_eq!(files_by_branch_id[&branch2_id].len(), 1);
|
|
|
|
// assert_eq!(files_by_branch_id[&branch2_id][0].hunks.len(), 1);
|
|
|
|
assert_eq!(files_by_branch_id[&branch3_id].len(), 0);
|
|
|
|
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch3_id,
|
|
|
|
ownership: Some("test.txt:1-5,11-15".parse()?),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let files_by_branch_id = statuses
|
|
|
|
.iter()
|
|
|
|
.map(|(branch, files)| (branch.id, files))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
assert_eq!(files_by_branch_id.len(), 3);
|
|
|
|
assert_eq!(files_by_branch_id[&branch1_id].len(), 0);
|
|
|
|
assert_eq!(files_by_branch_id[&branch2_id].len(), 0);
|
|
|
|
assert_eq!(files_by_branch_id[&branch3_id].len(), 1);
|
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
files_by_branch_id[&branch3_id]
|
|
|
|
.get(Path::new("test.txt"))
|
|
|
|
.unwrap()
|
|
|
|
.hunks
|
|
|
|
.len(),
|
2024-07-07 18:30:18 +03:00
|
|
|
2
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
files_by_branch_id[&branch3_id]
|
|
|
|
.get(Path::new("test.txt"))
|
|
|
|
.unwrap()
|
|
|
|
.hunks[0]
|
|
|
|
.diff,
|
2024-07-07 18:30:18 +03:00
|
|
|
"@@ -1,3 +1,4 @@\n+line0\n line1\n line2\n line3\n"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
files_by_branch_id[&branch3_id]
|
|
|
|
.get(Path::new("test.txt"))
|
|
|
|
.unwrap()
|
|
|
|
.hunks[1]
|
|
|
|
.diff,
|
2024-07-07 18:30:18 +03:00
|
|
|
"@@ -10,3 +11,4 @@ line9\n line10\n line11\n line12\n+line13\n"
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_hunks_partial_explicitly() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
|
|
|
let Case {
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
project,
|
|
|
|
..
|
|
|
|
} = &suite.new_case_with_files(HashMap::from([(
|
|
|
|
PathBuf::from("test.txt"),
|
|
|
|
"line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\n",
|
|
|
|
)]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line0\nline1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\nline14\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 23:22:55 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch2_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
let files_by_branch_id = statuses
|
|
|
|
.iter()
|
|
|
|
.map(|(branch, files)| (branch.id, files))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
assert_eq!(files_by_branch_id.len(), 2);
|
|
|
|
assert_eq!(files_by_branch_id[&branch1_id].len(), 1);
|
|
|
|
// assert_eq!(files_by_branch_id[&branch1_id][0].hunks.len(), 2);
|
|
|
|
assert_eq!(files_by_branch_id[&branch2_id].len(), 0);
|
|
|
|
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch2_id,
|
|
|
|
ownership: Some("test.txt:1-5".parse()?),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let files_by_branch_id = statuses
|
|
|
|
.iter()
|
|
|
|
.map(|(branch, files)| (branch.id, files))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
assert_eq!(files_by_branch_id.len(), 2);
|
|
|
|
assert_eq!(files_by_branch_id[&branch1_id].len(), 1);
|
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
files_by_branch_id[&branch1_id]
|
|
|
|
.get(Path::new("test.txt"))
|
|
|
|
.unwrap()
|
|
|
|
.hunks
|
|
|
|
.len(),
|
2024-07-07 18:30:18 +03:00
|
|
|
1
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
files_by_branch_id[&branch1_id]
|
|
|
|
.get(Path::new("test.txt"))
|
|
|
|
.unwrap()
|
|
|
|
.hunks[0]
|
|
|
|
.diff,
|
2024-07-07 18:30:18 +03:00
|
|
|
"@@ -11,3 +12,4 @@ line10\n line11\n line12\n line13\n+line14\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(files_by_branch_id[&branch2_id].len(), 1);
|
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
files_by_branch_id[&branch2_id]
|
|
|
|
.get(Path::new("test.txt"))
|
|
|
|
.unwrap()
|
|
|
|
.hunks
|
|
|
|
.len(),
|
2024-07-07 18:30:18 +03:00
|
|
|
1
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
files_by_branch_id[&branch2_id]
|
|
|
|
.get(Path::new("test.txt"))
|
|
|
|
.unwrap()
|
|
|
|
.hunks[0]
|
|
|
|
.diff,
|
2024-07-07 18:30:18 +03:00
|
|
|
"@@ -1,3 +1,4 @@\n+line0\n line1\n line2\n line3\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_new_hunk_to_the_end() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
|
|
|
let Case {
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
project,
|
|
|
|
..
|
|
|
|
} = &suite.new_case_with_files(HashMap::from([(
|
|
|
|
PathBuf::from("test.txt"),
|
|
|
|
"line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\nline13\nline14\n",
|
|
|
|
)]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\nline14\nline15\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 23:22:55 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch");
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
statuses[0].1.get(Path::new("test.txt")).unwrap().hunks[0].diff,
|
2024-07-07 18:30:18 +03:00
|
|
|
"@@ -11,5 +11,5 @@ line10\n line11\n line12\n line13\n-line13\n line14\n+line15\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line0\nline1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nline11\nline12\nline13\nline14\nline15\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let statuses = get_applied_status(ctx, None)
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to get status")
|
2024-07-21 00:00:06 +03:00
|
|
|
.branches;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
statuses[0].1.get(Path::new("test.txt")).unwrap().hunks[0].diff,
|
2024-07-07 18:30:18 +03:00
|
|
|
"@@ -11,5 +12,5 @@ line10\n line11\n line12\n line13\n-line13\n line14\n+line15\n"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-07-21 21:12:54 +03:00
|
|
|
statuses[0].1.get(Path::new("test.txt")).unwrap().hunks[1].diff,
|
2024-07-07 18:30:18 +03:00
|
|
|
"@@ -1,3 +1,4 @@\n+line0\n line1\n line2\n line3\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn commit_id_can_be_generated_or_specified() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// lets make sure a change id is generated
|
2024-07-29 12:13:25 +03:00
|
|
|
let target_oid = ctx.repository().head().unwrap().target().unwrap();
|
|
|
|
let target = ctx.repository().find_commit(target_oid).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
let change_id = target.change_id();
|
|
|
|
|
|
|
|
// make sure we created a change-id
|
|
|
|
assert!(change_id.is_some());
|
|
|
|
|
|
|
|
// ok, make another change and specify a change-id
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nline5\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
let repository = ctx.repository();
|
2024-07-07 18:30:18 +03:00
|
|
|
let mut index = repository.index().expect("failed to get index");
|
|
|
|
index
|
|
|
|
.add_all(["."], git2::IndexAddOption::DEFAULT, None)
|
|
|
|
.expect("failed to add all");
|
|
|
|
index.write().expect("failed to write index");
|
|
|
|
let oid = index.write_tree().expect("failed to write tree");
|
|
|
|
let signature = git2::Signature::now("test", "test@email.com").unwrap();
|
|
|
|
let head = repository.head().expect("failed to get head");
|
2024-07-09 02:14:28 +03:00
|
|
|
let refname: Refname = head.name().unwrap().parse().unwrap();
|
2024-07-29 12:13:25 +03:00
|
|
|
ctx.repository()
|
2024-07-07 18:30:18 +03:00
|
|
|
.commit_with_signature(
|
|
|
|
Some(&refname),
|
|
|
|
&signature,
|
|
|
|
&signature,
|
|
|
|
"some commit",
|
|
|
|
&repository.find_tree(oid).expect("failed to find tree"),
|
|
|
|
&[&repository
|
|
|
|
.find_commit(
|
|
|
|
repository
|
|
|
|
.refname_to_id("HEAD")
|
|
|
|
.expect("failed to get head"),
|
|
|
|
)
|
|
|
|
.expect("failed to find commit")],
|
|
|
|
// The change ID should always be generated by calling CommitHeadersV2::new
|
2024-07-09 14:19:49 +03:00
|
|
|
Some(CommitHeadersV2 {
|
2024-07-07 18:30:18 +03:00
|
|
|
change_id: "my-change-id".to_string(),
|
2024-08-19 19:10:28 +03:00
|
|
|
conflicted: None,
|
2024-07-07 18:30:18 +03:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.expect("failed to commit");
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
let target_oid = ctx.repository().head().unwrap().target().unwrap();
|
|
|
|
let target = ctx.repository().find_commit(target_oid).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
let change_id = target.change_id();
|
|
|
|
|
|
|
|
// the change id should be what we specified, rather than randomly generated
|
|
|
|
assert_eq!(change_id, Some("my-change-id".to_string()));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_vbranch_upstream_clean_rebase() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-08-28 08:41:53 +03:00
|
|
|
let Case { ctx, project, .. } = &mut suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// create a commit and set the target
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
|
|
|
let target_oid = ctx.repository().head().unwrap().target().unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\n",
|
|
|
|
)?;
|
|
|
|
// add a commit to the target branch it's pointing to so there is something "upstream"
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
|
|
|
let last_push = ctx.repository().head().unwrap().target().unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// coworker adds some work
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\ncoworker work\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
|
|
|
let coworker_work = ctx.repository().head().unwrap().target().unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
//update repo ref refs/remotes/origin/master to up_target oid
|
2024-07-29 12:13:25 +03:00
|
|
|
ctx.repository().reference(
|
2024-07-07 18:30:18 +03:00
|
|
|
"refs/remotes/origin/master",
|
|
|
|
coworker_work,
|
|
|
|
true,
|
|
|
|
"update target",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// revert to our file
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
|
|
|
let vb_state = VirtualBranchesHandle::new(ctx.project().gb_dir());
|
2024-07-09 01:40:10 +03:00
|
|
|
vb_state.set_default_target(Target {
|
2024-07-07 18:30:18 +03:00
|
|
|
branch: "refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
remote_url: "origin".to_string(),
|
|
|
|
sha: target_oid,
|
|
|
|
push_remote_name: None,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// add some uncommitted work
|
|
|
|
let file_path2 = Path::new("test2.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path2), "file2\n")?;
|
|
|
|
|
|
|
|
// Update integration commit
|
2024-07-29 11:46:17 +03:00
|
|
|
update_gitbutler_integration(&vb_state, ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-09 02:14:28 +03:00
|
|
|
let remote_branch: RemoteRefname = "refs/remotes/origin/master".parse().unwrap();
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let mut branch = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch");
|
|
|
|
branch.upstream = Some(remote_branch.clone());
|
|
|
|
branch.head = last_push;
|
|
|
|
vb_state.set_branch(branch.clone())?;
|
|
|
|
|
|
|
|
// create the branch
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-08-28 08:41:53 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch1 = &branches[0];
|
2024-08-28 08:41:53 +03:00
|
|
|
assert_eq!(
|
|
|
|
branch1.files.len(),
|
|
|
|
1 + 1,
|
|
|
|
"'test' (modified compared to index) and 'test2' (untracked).\
|
|
|
|
This is actually correct when looking at the git repository"
|
|
|
|
);
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(branch1.commits.len(), 1);
|
|
|
|
// assert_eq!(branch1.upstream.as_ref().unwrap().commits.len(), 1);
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
integrate_upstream_commits(ctx, branch1.id)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch1 = &branches[0];
|
|
|
|
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path))?;
|
|
|
|
assert_eq!(
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\ncoworker work\n",
|
|
|
|
String::from_utf8(contents)?
|
|
|
|
);
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path2))?;
|
|
|
|
assert_eq!("file2\n", String::from_utf8(contents)?);
|
|
|
|
assert_eq!(branch1.files.len(), 1);
|
|
|
|
assert_eq!(branch1.commits.len(), 2);
|
|
|
|
// assert_eq!(branch1.upstream.as_ref().unwrap().commits.len(), 0);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-07-28 21:36:55 +03:00
|
|
|
#[test]
|
|
|
|
fn merge_vbranch_upstream_conflict() -> Result<()> {
|
2024-07-07 18:30:18 +03:00
|
|
|
let suite = Suite::default();
|
|
|
|
let mut case = suite.new_case();
|
|
|
|
|
|
|
|
case = case.refresh(&suite);
|
2024-07-29 11:46:17 +03:00
|
|
|
let ctx = &case.ctx;
|
2024-07-07 18:30:18 +03:00
|
|
|
let project = &case.project;
|
|
|
|
|
|
|
|
// create a commit and set the target
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
|
|
|
let target_oid = ctx.repository().head().unwrap().target().unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\n",
|
|
|
|
)?;
|
|
|
|
// add a commit to the target branch it's pointing to so there is something "upstream"
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
|
|
|
let last_push = ctx.repository().head().unwrap().target().unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// coworker adds some work
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\ncoworker work\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
|
|
|
let coworker_work = ctx.repository().head().unwrap().target().unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
//update repo ref refs/remotes/origin/master to up_target oid
|
2024-07-29 12:13:25 +03:00
|
|
|
ctx.repository().reference(
|
2024-07-07 18:30:18 +03:00
|
|
|
"refs/remotes/origin/master",
|
|
|
|
coworker_work,
|
|
|
|
true,
|
|
|
|
"update target",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// revert to our file
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-10 17:18:34 +03:00
|
|
|
let vb_state = VirtualBranchesHandle::new(project.gb_dir());
|
2024-07-09 01:40:10 +03:00
|
|
|
vb_state.set_default_target(Target {
|
2024-07-07 18:30:18 +03:00
|
|
|
branch: "refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
remote_url: "origin".to_string(),
|
|
|
|
sha: target_oid,
|
|
|
|
push_remote_name: None,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// add some uncommitted work
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\nother side\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-09 02:14:28 +03:00
|
|
|
let remote_branch: RemoteRefname = "refs/remotes/origin/master".parse().unwrap();
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let mut branch = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch");
|
|
|
|
branch.upstream = Some(remote_branch.clone());
|
|
|
|
branch.head = last_push;
|
|
|
|
vb_state.set_branch(branch.clone())?;
|
|
|
|
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
&BranchUpdateRequest {
|
|
|
|
id: branch.id,
|
|
|
|
allow_rebasing: Some(false),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// create the branch
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch1 = &branches[0];
|
|
|
|
|
|
|
|
assert_eq!(branch1.files.len(), 1);
|
|
|
|
assert_eq!(branch1.commits.len(), 1);
|
|
|
|
// assert_eq!(branch1.upstream.as_ref().unwrap().commits.len(), 1);
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
integrate_upstream_commits(ctx, branch1.id)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch1 = &branches[0];
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path))?;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\n<<<<<<< ours\nother side\n=======\ncoworker work\n>>>>>>> theirs\n",
|
|
|
|
String::from_utf8(contents)?
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(branch1.files.len(), 1);
|
|
|
|
assert_eq!(branch1.commits.len(), 1);
|
|
|
|
assert!(branch1.conflicted);
|
|
|
|
|
|
|
|
// fix the conflict
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\nother side\ncoworker work\n",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// make gb see the conflict resolution
|
2024-08-28 16:32:28 +03:00
|
|
|
gitbutler_branch_actions::update_gitbutler_integration(&vb_state, ctx)?;
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
assert!(branches[0].conflicted);
|
|
|
|
|
|
|
|
// commit the merge resolution
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1.id, "fix merge conflict", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch1 = &branches[0];
|
|
|
|
assert!(!branch1.conflicted);
|
|
|
|
assert_eq!(branch1.files.len(), 0);
|
|
|
|
assert_eq!(branch1.commits.len(), 3);
|
|
|
|
|
|
|
|
// make sure the last commit was a merge commit (2 parents)
|
|
|
|
let last_id = &branch1.commits[0].id;
|
2024-07-29 12:13:25 +03:00
|
|
|
let last_commit = ctx.repository().find_commit(last_id.to_owned())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(last_commit.parent_count(), 2);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unapply_ownership_partial() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case_with_files(HashMap::from([(
|
2024-07-07 18:30:18 +03:00
|
|
|
PathBuf::from("test.txt"),
|
|
|
|
"line1\nline2\nline3\nline4\n",
|
|
|
|
)]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line1\nline2\nline3\nline4\nbranch1\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch");
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
assert_eq!(branches[0].files.len(), 1);
|
|
|
|
assert_eq!(branches[0].ownership.claims.len(), 1);
|
|
|
|
assert_eq!(branches[0].files[0].hunks.len(), 1);
|
|
|
|
assert_eq!(branches[0].ownership.claims[0].hunks.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
std::fs::read_to_string(Path::new(&project.path).join("test.txt"))?,
|
|
|
|
"line1\nline2\nline3\nline4\nbranch1\n"
|
|
|
|
);
|
|
|
|
|
2024-07-15 15:22:27 +03:00
|
|
|
unapply_ownership(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-15 15:22:27 +03:00
|
|
|
&"test.txt:2-6".parse().unwrap(),
|
|
|
|
guard.write_permission(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(branches.len(), 1);
|
|
|
|
assert_eq!(branches[0].files.len(), 0);
|
|
|
|
assert_eq!(branches[0].ownership.claims.len(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
std::fs::read_to_string(Path::new(&project.path).join("test.txt"))?,
|
|
|
|
"line1\nline2\nline3\nline4\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unapply_branch() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { project, ctx, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// create a commit and set the target
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nbranch1\n",
|
|
|
|
)?;
|
|
|
|
let file_path2 = Path::new("test2.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path2), "line5\nline6\n")?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch2_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch2_id,
|
|
|
|
ownership: Some("test2.txt:1-3".parse()?),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path))?;
|
|
|
|
assert_eq!(
|
|
|
|
"line1\nline2\nline3\nline4\nbranch1\n",
|
|
|
|
String::from_utf8(contents)?
|
|
|
|
);
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path2))?;
|
|
|
|
assert_eq!("line5\nline6\n", String::from_utf8(contents)?);
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
assert_eq!(branch.files.len(), 1);
|
|
|
|
assert!(branch.active);
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-25 16:47:10 +03:00
|
|
|
let real_branch =
|
|
|
|
branch_manager.convert_to_real_branch(branch1_id, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path))?;
|
|
|
|
assert_eq!("line1\nline2\nline3\nline4\n", String::from_utf8(contents)?);
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path2))?;
|
|
|
|
assert_eq!("line5\nline6\n", String::from_utf8(contents)?);
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
assert!(!branches.iter().any(|b| b.id == branch1_id));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let branch1_id = branch_manager.create_virtual_branch_from_branch(
|
|
|
|
&Refname::from_str(&real_branch)?,
|
2024-07-25 16:11:07 +03:00
|
|
|
None,
|
2024-07-15 15:22:27 +03:00
|
|
|
guard.write_permission(),
|
|
|
|
)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path))?;
|
|
|
|
assert_eq!(
|
|
|
|
"line1\nline2\nline3\nline4\nbranch1\n",
|
|
|
|
String::from_utf8(contents)?
|
|
|
|
);
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path2))?;
|
|
|
|
assert_eq!("line5\nline6\n", String::from_utf8(contents)?);
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
2024-07-08 18:09:01 +03:00
|
|
|
// TODO: expect there to be 0 branches
|
|
|
|
assert_eq!(branch.files.len(), 0);
|
2024-07-07 18:30:18 +03:00
|
|
|
assert!(branch.active);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn apply_unapply_added_deleted_files() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { project, ctx, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// create a commit and set the target
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path), "file1\n")?;
|
|
|
|
let file_path2 = Path::new("test2.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path2), "file2\n")?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// rm file_path2, add file3
|
|
|
|
std::fs::remove_file(Path::new(&project.path).join(file_path2))?;
|
|
|
|
let file_path3 = Path::new("test3.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path3), "file3\n")?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch2_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch3_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch2_id,
|
|
|
|
ownership: Some("test2.txt:0-0".parse()?),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch3_id,
|
|
|
|
ownership: Some("test3.txt:1-2".parse()?),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
list_virtual_branches(ctx, guard.write_permission()).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-25 16:47:10 +03:00
|
|
|
let real_branch_2 =
|
|
|
|
branch_manager.convert_to_real_branch(branch2_id, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// check that file2 is back
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path2))?;
|
|
|
|
assert_eq!("file2\n", String::from_utf8(contents)?);
|
|
|
|
|
2024-07-25 16:47:10 +03:00
|
|
|
let real_branch_3 =
|
|
|
|
branch_manager.convert_to_real_branch(branch3_id, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
// check that file3 is gone
|
|
|
|
assert!(!Path::new(&project.path).join(file_path3).exists());
|
|
|
|
|
2024-07-09 17:43:58 +03:00
|
|
|
branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch_from_branch(
|
|
|
|
&Refname::from_str(&real_branch_2).unwrap(),
|
2024-07-25 16:11:07 +03:00
|
|
|
None,
|
2024-07-15 15:22:27 +03:00
|
|
|
guard.write_permission(),
|
|
|
|
)
|
2024-07-09 17:43:58 +03:00
|
|
|
.unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// check that file2 is gone
|
|
|
|
assert!(!Path::new(&project.path).join(file_path2).exists());
|
|
|
|
|
2024-07-09 17:43:58 +03:00
|
|
|
branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch_from_branch(
|
|
|
|
&Refname::from_str(&real_branch_3).unwrap(),
|
2024-07-25 16:11:07 +03:00
|
|
|
None,
|
2024-07-15 15:22:27 +03:00
|
|
|
guard.write_permission(),
|
|
|
|
)
|
2024-07-09 17:43:58 +03:00
|
|
|
.unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// check that file3 is back
|
|
|
|
let contents = std::fs::read(Path::new(&project.path).join(file_path3))?;
|
|
|
|
assert_eq!("file3\n", String::from_utf8(contents)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verifies that we are able to detect when a remote branch is conflicting with the current applied branches.
|
|
|
|
#[test]
|
|
|
|
fn detect_mergeable_branch() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { project, ctx, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// create a commit and set the target
|
|
|
|
let file_path = Path::new("test.txt");
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nbranch1\n",
|
|
|
|
)?;
|
|
|
|
let file_path4 = Path::new("test4.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path4), "line5\nline6\n")?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch2_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch2_id,
|
|
|
|
ownership: Some("test4.txt:1-3".parse()?),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("failed to update branch");
|
|
|
|
|
|
|
|
// unapply both branches and create some conflicting ones
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-25 16:47:10 +03:00
|
|
|
branch_manager.convert_to_real_branch(branch1_id, guard.write_permission())?;
|
|
|
|
branch_manager.convert_to_real_branch(branch2_id, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
ctx.repository().set_head("refs/heads/master")?;
|
|
|
|
ctx.repository()
|
2024-07-07 18:30:18 +03:00
|
|
|
.checkout_head(Some(&mut git2::build::CheckoutBuilder::default().force()))?;
|
|
|
|
|
|
|
|
// create an upstream remote conflicting commit
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nupstream\n",
|
|
|
|
)?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
|
|
|
let up_target = ctx.repository().head().unwrap().target().unwrap();
|
|
|
|
ctx.repository().reference(
|
2024-07-07 18:30:18 +03:00
|
|
|
"refs/remotes/origin/remote_branch",
|
|
|
|
up_target,
|
|
|
|
true,
|
|
|
|
"update target",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// revert content and write a mergeable branch
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
|
|
|
let file_path3 = Path::new("test3.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path3), "file3\n")?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
|
|
|
let up_target = ctx.repository().head().unwrap().target().unwrap();
|
|
|
|
ctx.repository().reference(
|
2024-07-07 18:30:18 +03:00
|
|
|
"refs/remotes/origin/remote_branch2",
|
|
|
|
up_target,
|
|
|
|
true,
|
|
|
|
"update target",
|
|
|
|
)?;
|
|
|
|
// remove file_path3
|
|
|
|
std::fs::remove_file(Path::new(&project.path).join(file_path3))?;
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
ctx.repository()
|
|
|
|
.set_head("refs/heads/gitbutler/integration")?;
|
|
|
|
ctx.repository()
|
2024-07-07 18:30:18 +03:00
|
|
|
.checkout_head(Some(&mut git2::build::CheckoutBuilder::default().force()))?;
|
|
|
|
|
|
|
|
// create branches that conflict with our earlier branches
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-09 17:43:58 +03:00
|
|
|
branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch");
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch4_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
// branch3 conflicts with branch1 and remote_branch
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path),
|
|
|
|
"line1\nline2\nline3\nline4\nbranch3\n",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// branch4 conflicts with branch2
|
|
|
|
let file_path2 = Path::new("test2.txt");
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join(file_path2),
|
|
|
|
"line1\nline2\nline3\nline4\nbranch4\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-10 17:18:34 +03:00
|
|
|
let vb_state = VirtualBranchesHandle::new(project.gb_dir());
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-08 15:52:00 +03:00
|
|
|
let mut branch4 = vb_state.get_branch_in_workspace(branch4_id)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
branch4.ownership = BranchOwnershipClaims {
|
|
|
|
claims: vec!["test2.txt:1-6".parse()?],
|
|
|
|
};
|
|
|
|
vb_state.set_branch(branch4.clone())?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let remotes =
|
|
|
|
gitbutler_branch_actions::list_remote_branches(ctx).expect("failed to list remotes");
|
2024-07-07 18:30:18 +03:00
|
|
|
let _remote1 = &remotes
|
|
|
|
.iter()
|
|
|
|
.find(|b| b.name.to_string() == "refs/remotes/origin/remote_branch")
|
|
|
|
.unwrap();
|
|
|
|
assert!(!is_remote_branch_mergeable(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
&"refs/remotes/origin/remote_branch".parse().unwrap()
|
|
|
|
)
|
|
|
|
.unwrap());
|
|
|
|
// assert_eq!(remote1.commits.len(), 1);
|
|
|
|
|
|
|
|
let _remote2 = &remotes
|
|
|
|
.iter()
|
|
|
|
.find(|b| b.name.to_string() == "refs/remotes/origin/remote_branch2")
|
|
|
|
.unwrap();
|
|
|
|
assert!(is_remote_branch_mergeable(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
&"refs/remotes/origin/remote_branch2".parse().unwrap()
|
|
|
|
)
|
|
|
|
.unwrap());
|
|
|
|
// assert_eq!(remote2.commits.len(), 2);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn upstream_integrated_vbranch() -> Result<()> {
|
|
|
|
// ok, we need a vbranch with some work and an upstream target that also includes that work, but the base is behind
|
|
|
|
// plus a branch with work not in upstream so we can see that it is not included in the vbranch
|
|
|
|
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case_with_files(HashMap::from([
|
2024-07-07 18:30:18 +03:00
|
|
|
(PathBuf::from("test.txt"), "file1\n"),
|
|
|
|
(PathBuf::from("test2.txt"), "file2\n"),
|
|
|
|
(PathBuf::from("test3.txt"), "file3\n"),
|
|
|
|
]));
|
|
|
|
|
2024-07-10 17:18:34 +03:00
|
|
|
let vb_state = VirtualBranchesHandle::new(project.gb_dir());
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
let base_commit = ctx.repository().head().unwrap().target().unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"file1\nversion2\n",
|
|
|
|
)?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
let upstream_commit = ctx.repository().head().unwrap().target().unwrap();
|
|
|
|
ctx.repository().reference(
|
2024-07-07 18:30:18 +03:00
|
|
|
"refs/remotes/origin/master",
|
|
|
|
upstream_commit,
|
|
|
|
true,
|
|
|
|
"update target",
|
|
|
|
)?;
|
|
|
|
|
2024-07-09 01:40:10 +03:00
|
|
|
vb_state.set_default_target(Target {
|
2024-07-07 18:30:18 +03:00
|
|
|
branch: "refs/remotes/origin/master".parse().unwrap(),
|
|
|
|
remote_url: "http://origin.com/project".to_string(),
|
|
|
|
sha: base_commit,
|
|
|
|
push_remote_name: None,
|
|
|
|
})?;
|
2024-07-29 12:13:25 +03:00
|
|
|
ctx.repository()
|
|
|
|
.remote("origin", "http://origin.com/project")?;
|
2024-07-29 11:46:17 +03:00
|
|
|
update_gitbutler_integration(&vb_state, ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// create vbranches, one integrated, one not
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch2_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch3_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test2.txt"),
|
|
|
|
"file2\nversion2\n",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test3.txt"),
|
|
|
|
"file3\nversion2\n",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch1_id,
|
|
|
|
name: Some("integrated".to_string()),
|
|
|
|
ownership: Some("test.txt:1-2".parse()?),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch2_id,
|
|
|
|
name: Some("not integrated".to_string()),
|
|
|
|
ownership: Some("test2.txt:1-2".parse()?),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
update_branch(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-09 01:10:55 +03:00
|
|
|
&BranchUpdateRequest {
|
2024-07-07 18:30:18 +03:00
|
|
|
id: branch3_id,
|
|
|
|
name: Some("not committed".to_string()),
|
|
|
|
ownership: Some("test3.txt:1-2".parse()?),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// create a new virtual branch from the remote branch
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "integrated commit", None, false)?;
|
|
|
|
commit(ctx, branch2_id, "non-integrated commit", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let branch1 = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
assert!(branch1.commits.iter().any(|c| c.is_integrated));
|
|
|
|
assert_eq!(branch1.files.len(), 0);
|
|
|
|
assert_eq!(branch1.commits.len(), 1);
|
|
|
|
|
|
|
|
let branch2 = &branches.iter().find(|b| b.id == branch2_id).unwrap();
|
|
|
|
assert!(!branch2.commits.iter().any(|c| c.is_integrated));
|
|
|
|
assert_eq!(branch2.files.len(), 0);
|
|
|
|
assert_eq!(branch2.commits.len(), 1);
|
|
|
|
|
|
|
|
let branch3 = &branches.iter().find(|b| b.id == branch3_id).unwrap();
|
|
|
|
assert!(!branch3.commits.iter().any(|c| c.is_integrated));
|
|
|
|
assert_eq!(branch3.files.len(), 1);
|
|
|
|
assert_eq!(branch3.commits.len(), 0);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn commit_same_hunk_twice() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
|
|
|
let Case {
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
project,
|
|
|
|
..
|
|
|
|
} = &suite.new_case_with_files(HashMap::from([(
|
|
|
|
PathBuf::from("test.txt"),
|
|
|
|
"line1\nline2\nline3\nline4\nline5\nmiddle\nmiddle\nmiddle\nmiddle\nline6\nline7\nline8\nline9\nline10\nmiddle\nmiddle\nmiddle\nline11\nline12\n",
|
|
|
|
)]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line1\npatch1\nline2\nline3\nline4\nline5\nmiddle\nmiddle\nmiddle\nmiddle\nline6\nline7\nline8\nline9\nline10\nmiddle\nmiddle\nmiddle\nline11\nline12\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.files.len(), 1);
|
|
|
|
assert_eq!(branch.files[0].hunks.len(), 1);
|
|
|
|
assert_eq!(branch.commits.len(), 0);
|
|
|
|
|
|
|
|
// commit
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "first commit to test.txt", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.files.len(), 0, "no files expected");
|
|
|
|
|
|
|
|
assert_eq!(branch.commits.len(), 1, "file should have been commited");
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1, "hunks expected");
|
|
|
|
assert_eq!(
|
|
|
|
branch.commits[0].files[0].hunks.len(),
|
|
|
|
1,
|
|
|
|
"one hunk should have been commited"
|
|
|
|
);
|
|
|
|
|
|
|
|
// update same lines
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line1\nPATCH1\nline2\nline3\nline4\nline5\nmiddle\nmiddle\nmiddle\nmiddle\nline6\nline7\nline8\nline9\nline10\nmiddle\nmiddle\nmiddle\nline11\nline12\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.files.len(), 1, "one file should be changed");
|
|
|
|
assert_eq!(branch.commits.len(), 1, "commit is still there");
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "second commit to test.txt", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
branch.files.len(),
|
|
|
|
0,
|
|
|
|
"all changes should have been commited"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(branch.commits.len(), 2, "two commits expected");
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1);
|
|
|
|
assert_eq!(branch.commits[0].files[0].hunks.len(), 1);
|
|
|
|
assert_eq!(branch.commits[1].files.len(), 1);
|
|
|
|
assert_eq!(branch.commits[1].files[0].hunks.len(), 1);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn commit_same_file_twice() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
|
|
|
let Case {
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
project,
|
|
|
|
..
|
|
|
|
} = &suite.new_case_with_files(HashMap::from([(
|
|
|
|
PathBuf::from("test.txt"),
|
|
|
|
"line1\nline2\nline3\nline4\nline5\nmiddle\nmiddle\nmiddle\nmiddle\nline6\nline7\nline8\nline9\nline10\nmiddle\nmiddle\nmiddle\nline11\nline12\n",
|
|
|
|
)]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line1\npatch1\nline2\nline3\nline4\nline5\nmiddle\nmiddle\nmiddle\nmiddle\nline6\nline7\nline8\nline9\nline10\nmiddle\nmiddle\nmiddle\nline11\nline12\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.files.len(), 1);
|
|
|
|
assert_eq!(branch.files[0].hunks.len(), 1);
|
|
|
|
assert_eq!(branch.commits.len(), 0);
|
|
|
|
|
|
|
|
// commit
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "first commit to test.txt", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.files.len(), 0, "no files expected");
|
|
|
|
|
|
|
|
assert_eq!(branch.commits.len(), 1, "file should have been commited");
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1, "hunks expected");
|
|
|
|
assert_eq!(
|
|
|
|
branch.commits[0].files[0].hunks.len(),
|
|
|
|
1,
|
|
|
|
"one hunk should have been commited"
|
|
|
|
);
|
|
|
|
|
|
|
|
// add second patch
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("file.txt"),
|
|
|
|
"line1\npatch1\nline2\nline3\nline4\nline5\nmiddle\nmiddle\nmiddle\nmiddle\nline6\nline7\nline8\nline9\nline10\nmiddle\nmiddle\nmiddle\npatch2\nline11\nline12\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.files.len(), 1, "one file should be changed");
|
|
|
|
assert_eq!(branch.commits.len(), 1, "commit is still there");
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "second commit to test.txt", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
branch.files.len(),
|
|
|
|
0,
|
|
|
|
"all changes should have been commited"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(branch.commits.len(), 2, "two commits expected");
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1);
|
|
|
|
assert_eq!(branch.commits[0].files[0].hunks.len(), 1);
|
|
|
|
assert_eq!(branch.commits[1].files.len(), 1);
|
|
|
|
assert_eq!(branch.commits[1].files[0].hunks.len(), 1);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn commit_partial_by_hunk() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
|
|
|
let Case {
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
project,
|
|
|
|
..
|
|
|
|
} = &suite.new_case_with_files(HashMap::from([(
|
|
|
|
PathBuf::from("test.txt"),
|
|
|
|
"line1\nline2\nline3\nline4\nline5\nmiddle\nmiddle\nmiddle\nmiddle\nline6\nline7\nline8\nline9\nline10\nmiddle\nmiddle\nmiddle\nline11\nline12\n",
|
|
|
|
)]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line1\npatch1\nline2\nline3\nline4\nline5\nmiddle\nmiddle\nmiddle\nmiddle\nline6\nline7\nline8\nline9\nline10\nmiddle\nmiddle\nmiddle\npatch2\nline11\nline12\n",
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.files.len(), 1);
|
|
|
|
assert_eq!(branch.files[0].hunks.len(), 2);
|
|
|
|
assert_eq!(branch.commits.len(), 0);
|
|
|
|
|
|
|
|
// commit
|
|
|
|
commit(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
branch1_id,
|
|
|
|
"first commit to test.txt",
|
|
|
|
Some(&"test.txt:1-6".parse::<BranchOwnershipClaims>().unwrap()),
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.files.len(), 1);
|
|
|
|
assert_eq!(branch.files[0].hunks.len(), 1);
|
|
|
|
assert_eq!(branch.commits.len(), 1);
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1);
|
|
|
|
assert_eq!(branch.commits[0].files[0].hunks.len(), 1);
|
|
|
|
|
|
|
|
commit(
|
2024-07-29 11:46:17 +03:00
|
|
|
ctx,
|
2024-07-07 18:30:18 +03:00
|
|
|
branch1_id,
|
|
|
|
"second commit to test.txt",
|
|
|
|
Some(&"test.txt:16-22".parse::<BranchOwnershipClaims>().unwrap()),
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(branch.files.len(), 0);
|
|
|
|
assert_eq!(branch.commits.len(), 2);
|
|
|
|
assert_eq!(branch.commits[0].files.len(), 1);
|
|
|
|
assert_eq!(branch.commits[0].files[0].hunks.len(), 1);
|
|
|
|
assert_eq!(branch.commits[1].files.len(), 1);
|
|
|
|
assert_eq!(branch.commits[1].files[0].hunks.len(), 1);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn commit_partial_by_file() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case_with_files(HashMap::from([
|
2024-07-07 18:30:18 +03:00
|
|
|
(PathBuf::from("test.txt"), "file1\n"),
|
|
|
|
(PathBuf::from("test2.txt"), "file2\n"),
|
|
|
|
]));
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
let commit1_oid = ctx.repository().head().unwrap().target().unwrap();
|
|
|
|
let commit1 = ctx.repository().find_commit(commit1_oid).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// remove file
|
|
|
|
std::fs::remove_file(Path::new(&project.path).join("test2.txt"))?;
|
|
|
|
// add new file
|
|
|
|
let file_path3 = Path::new("test3.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path3), "file3\n")?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
// commit
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "branch1 commit", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch1 = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
// branch one test.txt has just the 1st and 3rd hunks applied
|
|
|
|
let commit2 = &branch1.commits[0].id;
|
2024-07-29 11:46:17 +03:00
|
|
|
let commit2 = ctx
|
2024-07-29 12:13:25 +03:00
|
|
|
.repository()
|
2024-07-07 18:30:18 +03:00
|
|
|
.find_commit(commit2.to_owned())
|
|
|
|
.expect("failed to get commit object");
|
|
|
|
|
|
|
|
let tree = commit1.tree().expect("failed to get tree");
|
2024-07-29 12:13:25 +03:00
|
|
|
let file_list = tree_to_file_list(ctx.repository(), &tree);
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(file_list, vec!["test.txt", "test2.txt"]);
|
|
|
|
|
|
|
|
// get the tree
|
|
|
|
let tree = commit2.tree().expect("failed to get tree");
|
2024-07-29 12:13:25 +03:00
|
|
|
let file_list = tree_to_file_list(ctx.repository(), &tree);
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(file_list, vec!["test.txt", "test3.txt"]);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn commit_add_and_delete_files() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case_with_files(HashMap::from([
|
2024-07-07 18:30:18 +03:00
|
|
|
(PathBuf::from("test.txt"), "file1\n"),
|
|
|
|
(PathBuf::from("test2.txt"), "file2\n"),
|
|
|
|
]));
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
let commit1_oid = ctx.repository().head().unwrap().target().unwrap();
|
|
|
|
let commit1 = ctx.repository().find_commit(commit1_oid).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// remove file
|
|
|
|
std::fs::remove_file(Path::new(&project.path).join("test2.txt"))?;
|
|
|
|
// add new file
|
|
|
|
let file_path3 = Path::new("test3.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path3), "file3\n")?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
// commit
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "branch1 commit", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch1 = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
// branch one test.txt has just the 1st and 3rd hunks applied
|
|
|
|
let commit2 = &branch1.commits[0].id;
|
2024-07-29 11:46:17 +03:00
|
|
|
let commit2 = ctx
|
2024-07-29 12:13:25 +03:00
|
|
|
.repository()
|
2024-07-07 18:30:18 +03:00
|
|
|
.find_commit(commit2.to_owned())
|
|
|
|
.expect("failed to get commit object");
|
|
|
|
|
|
|
|
let tree = commit1.tree().expect("failed to get tree");
|
2024-07-29 12:13:25 +03:00
|
|
|
let file_list = tree_to_file_list(ctx.repository(), &tree);
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(file_list, vec!["test.txt", "test2.txt"]);
|
|
|
|
|
|
|
|
// get the tree
|
|
|
|
let tree = commit2.tree().expect("failed to get tree");
|
2024-07-29 12:13:25 +03:00
|
|
|
let file_list = tree_to_file_list(ctx.repository(), &tree);
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(file_list, vec!["test.txt", "test3.txt"]);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
fn commit_executable_and_symlinks() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case_with_files(HashMap::from([
|
2024-07-07 18:30:18 +03:00
|
|
|
(PathBuf::from("test.txt"), "file1\n"),
|
|
|
|
(PathBuf::from("test2.txt"), "file2\n"),
|
|
|
|
]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// add symlinked file
|
|
|
|
let file_path3 = Path::new("test3.txt");
|
|
|
|
let src = Path::new(&project.path).join("test2.txt");
|
|
|
|
let dst = Path::new(&project.path).join(file_path3);
|
|
|
|
symlink(src, dst)?;
|
|
|
|
|
|
|
|
// add executable
|
|
|
|
let file_path4 = Path::new("test4.bin");
|
|
|
|
let exec = Path::new(&project.path).join(file_path4);
|
|
|
|
std::fs::write(&exec, "exec\n")?;
|
|
|
|
let permissions = std::fs::metadata(&exec)?.permissions();
|
|
|
|
let new_permissions = Permissions::from_mode(permissions.mode() | 0o111); // Add execute permission
|
|
|
|
std::fs::set_permissions(&exec, new_permissions)?;
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 15:22:27 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
// commit
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "branch1 commit", None, false)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let (branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
let branch1 = &branches.iter().find(|b| b.id == branch1_id).unwrap();
|
|
|
|
|
|
|
|
let commit = &branch1.commits[0].id;
|
2024-07-29 11:46:17 +03:00
|
|
|
let commit = ctx
|
2024-07-29 12:13:25 +03:00
|
|
|
.repository()
|
2024-07-07 18:30:18 +03:00
|
|
|
.find_commit(commit.to_owned())
|
|
|
|
.expect("failed to get commit object");
|
|
|
|
|
|
|
|
let tree = commit.tree().expect("failed to get tree");
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
let list = tree_to_entry_list(ctx.repository(), &tree);
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(list[0].0, "test.txt");
|
|
|
|
assert_eq!(list[0].1, "100644");
|
|
|
|
assert_eq!(list[1].0, "test2.txt");
|
|
|
|
assert_eq!(list[1].1, "100644");
|
|
|
|
assert_eq!(list[2].0, "test3.txt");
|
|
|
|
assert_eq!(list[2].1, "120000");
|
|
|
|
assert_eq!(list[2].2, "test2.txt");
|
|
|
|
assert_eq!(list[3].0, "test4.bin");
|
|
|
|
assert_eq!(list[3].1, "100755");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tree_to_file_list(repository: &git2::Repository, tree: &git2::Tree) -> Vec<String> {
|
|
|
|
let mut file_list = Vec::new();
|
|
|
|
walk(tree, |_, entry| {
|
|
|
|
let path = entry.name().unwrap();
|
|
|
|
let entry = tree.get_path(Path::new(path)).unwrap();
|
|
|
|
let object = entry.to_object(repository).unwrap();
|
|
|
|
if object.kind() == Some(git2::ObjectType::Blob) {
|
|
|
|
file_list.push(path.to_string());
|
|
|
|
}
|
|
|
|
TreeWalkResult::Continue
|
|
|
|
})
|
|
|
|
.expect("failed to walk tree");
|
|
|
|
file_list
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tree_to_entry_list(
|
|
|
|
repository: &git2::Repository,
|
|
|
|
tree: &git2::Tree,
|
|
|
|
) -> Vec<(String, String, String, String)> {
|
|
|
|
let mut file_list = Vec::new();
|
|
|
|
walk(tree, |_root, entry| {
|
|
|
|
let path = entry.name().unwrap();
|
|
|
|
let entry = tree.get_path(Path::new(path)).unwrap();
|
|
|
|
let object = entry.to_object(repository).unwrap();
|
|
|
|
let blob = object.as_blob().expect("failed to get blob");
|
|
|
|
// convert content to string
|
|
|
|
let octal_mode = format!("{:o}", entry.filemode());
|
|
|
|
if let Ok(content) =
|
|
|
|
std::str::from_utf8(blob.content()).context("failed to convert content to string")
|
|
|
|
{
|
|
|
|
file_list.push((
|
|
|
|
path.to_string(),
|
|
|
|
octal_mode,
|
|
|
|
content.to_string(),
|
|
|
|
blob.id().to_string(),
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
file_list.push((
|
|
|
|
path.to_string(),
|
|
|
|
octal_mode,
|
|
|
|
"BINARY".to_string(),
|
|
|
|
blob.id().to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
TreeWalkResult::Continue
|
|
|
|
})
|
|
|
|
.expect("failed to walk tree");
|
|
|
|
file_list
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_branch_commits_to_integration() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-29 11:46:17 +03:00
|
|
|
verify_branch(ctx, guard.write_permission()).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// write two commits
|
|
|
|
let file_path2 = Path::new("test2.txt");
|
|
|
|
std::fs::write(Path::new(&project.path).join(file_path2), "file")?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
2024-07-07 18:30:18 +03:00
|
|
|
std::fs::write(Path::new(&project.path).join(file_path2), "update")?;
|
2024-07-29 12:13:25 +03:00
|
|
|
commit_all(ctx.repository());
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// verify puts commits onto the virtual branch
|
2024-07-29 11:46:17 +03:00
|
|
|
verify_branch(ctx, guard.write_permission()).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
// one virtual branch with two commits was created
|
2024-07-29 11:46:17 +03:00
|
|
|
let (virtual_branches, _) = list_virtual_branches(ctx, guard.write_permission())?;
|
2024-07-07 18:30:18 +03:00
|
|
|
assert_eq!(virtual_branches.len(), 1);
|
|
|
|
|
|
|
|
let branch = &virtual_branches.first().unwrap();
|
|
|
|
assert_eq!(branch.commits.len(), 2);
|
|
|
|
assert_eq!(branch.commits.len(), 2);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_branch_not_integration() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { ctx, project, .. } = &suite.new_case();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-15 15:22:27 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-29 11:46:17 +03:00
|
|
|
verify_branch(ctx, guard.write_permission()).unwrap();
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
ctx.repository().set_head("refs/heads/master")?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let verify_result = verify_branch(ctx, guard.write_permission());
|
2024-07-07 18:30:18 +03:00
|
|
|
assert!(verify_result.is_err());
|
|
|
|
assert_eq!(
|
|
|
|
format!("{:#}", verify_result.unwrap_err()),
|
|
|
|
"<verification-failed>: project is on refs/heads/master. Please checkout gitbutler/integration to continue"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pre_commit_hook_rejection() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { project, ctx, .. } = &suite.new_case_with_files(HashMap::from([
|
2024-07-07 18:30:18 +03:00
|
|
|
(PathBuf::from("test.txt"), "line1\nline2\nline3\nline4\n"),
|
|
|
|
(PathBuf::from("test2.txt"), "line5\nline6\nline7\nline8\n"),
|
|
|
|
]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 23:22:55 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line0\nline1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let hook = b"#!/bin/sh
|
|
|
|
echo 'rejected'
|
|
|
|
exit 1
|
|
|
|
";
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
git2_hooks::create_hook(ctx.repository(), git2_hooks::HOOK_PRE_COMMIT, hook);
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let res = commit(ctx, branch1_id, "test commit", None, true);
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let err = res.unwrap_err();
|
2024-07-20 13:54:48 +03:00
|
|
|
assert_eq!(
|
|
|
|
err.source().unwrap().to_string(),
|
|
|
|
"commit hook rejected: rejected"
|
|
|
|
);
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn post_commit_hook() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { project, ctx, .. } = &suite.new_case_with_files(HashMap::from([
|
2024-07-07 18:30:18 +03:00
|
|
|
(PathBuf::from("test.txt"), "line1\nline2\nline3\nline4\n"),
|
|
|
|
(PathBuf::from("test2.txt"), "line5\nline6\nline7\nline8\n"),
|
|
|
|
]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 23:22:55 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line0\nline1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let hook = b"#!/bin/sh
|
|
|
|
touch hook_ran
|
|
|
|
";
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
git2_hooks::create_hook(ctx.repository(), git2_hooks::HOOK_POST_COMMIT, hook);
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
let hook_ran_proof = ctx.repository().path().parent().unwrap().join("hook_ran");
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
assert!(!hook_ran_proof.exists());
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
commit(ctx, branch1_id, "test commit", None, true)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
assert!(hook_ran_proof.exists());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn commit_msg_hook_rejection() -> Result<()> {
|
|
|
|
let suite = Suite::default();
|
2024-07-29 11:46:17 +03:00
|
|
|
let Case { project, ctx, .. } = &suite.new_case_with_files(HashMap::from([
|
2024-07-07 18:30:18 +03:00
|
|
|
(PathBuf::from("test.txt"), "line1\nline2\nline3\nline4\n"),
|
|
|
|
(PathBuf::from("test2.txt"), "line5\nline6\nline7\nline8\n"),
|
|
|
|
]));
|
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
set_test_target(ctx)?;
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let branch_manager = ctx.branch_manager();
|
2024-07-15 23:22:55 +03:00
|
|
|
let mut guard = project.exclusive_worktree_access();
|
2024-07-09 17:43:58 +03:00
|
|
|
let branch1_id = branch_manager
|
2024-07-15 23:22:55 +03:00
|
|
|
.create_virtual_branch(&BranchCreateRequest::default(), guard.write_permission())
|
2024-07-07 18:30:18 +03:00
|
|
|
.expect("failed to create virtual branch")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
Path::new(&project.path).join("test.txt"),
|
|
|
|
"line0\nline1\nline2\nline3\nline4\n",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let hook = b"#!/bin/sh
|
|
|
|
echo 'rejected'
|
|
|
|
exit 1
|
|
|
|
";
|
|
|
|
|
2024-07-29 12:13:25 +03:00
|
|
|
git2_hooks::create_hook(ctx.repository(), git2_hooks::HOOK_COMMIT_MSG, hook);
|
2024-07-07 18:30:18 +03:00
|
|
|
|
2024-07-29 11:46:17 +03:00
|
|
|
let res = commit(ctx, branch1_id, "test commit", None, true);
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
let err = res.unwrap_err();
|
2024-07-20 13:54:48 +03:00
|
|
|
assert_eq!(
|
|
|
|
err.source().unwrap().to_string(),
|
|
|
|
"commit-msg hook rejected: rejected"
|
|
|
|
);
|
2024-07-07 18:30:18 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn walk<C>(tree: &git2::Tree, mut callback: C) -> Result<()>
|
|
|
|
where
|
|
|
|
C: FnMut(&str, &TreeEntry) -> TreeWalkResult,
|
|
|
|
{
|
|
|
|
tree.walk(git2::TreeWalkMode::PreOrder, |root, entry| {
|
|
|
|
match callback(root, &entry.clone()) {
|
|
|
|
TreeWalkResult::Continue => git2::TreeWalkResult::Ok,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err(Into::into)
|
|
|
|
}
|
|
|
|
|
|
|
|
enum TreeWalkResult {
|
|
|
|
Continue,
|
|
|
|
}
|