fix old way of moving

This commit is contained in:
Nikita Galaiko 2023-06-22 15:31:21 +02:00
parent c3248bfee4
commit fe44d9ae30
2 changed files with 91 additions and 5 deletions

View File

@ -69,17 +69,17 @@ impl Ownership {
return Some(self.clone());
}
if another.ranges.is_empty() {
// any ownership - full ownership = empty ownership
return None;
}
if self.ranges.is_empty() {
// full ownership - partial ownership = full ownership, since we don't know all the
// hunks.
return Some(self.clone());
}
if another.ranges.is_empty() {
// any ownership - full ownership = empty ownership
return None;
}
let mut ranges = self.ranges.clone();
for range in &another.ranges {
ranges = ranges
@ -303,6 +303,7 @@ mod ownership_tests {
("file.txt:1-10", "file.txt:11-15", Some("file.txt:1-10")),
("file.txt:1-10", "file.txt:1-10", None),
("file.txt:1-10", "file.txt", None),
("file.txt", "file.txt", None),
("file.txt", "file.txt:1-10", Some("file.txt")),
(
"file.txt:1-10,11-15",

View File

@ -915,6 +915,91 @@ mod tests {
Ok(())
}
#[test]
fn test_move_hunks_entire_file_single_source_full_file() -> Result<()> {
let repository = test_repository()?;
let project = projects::Project::try_from(&repository)?;
let gb_repo_path = tempdir()?.path().to_str().unwrap().to_string();
let storage = storage::Storage::from_path(tempdir()?.path());
let user_store = users::Storage::new(storage.clone());
let project_store = projects::Storage::new(storage);
project_store.add_project(&project)?;
let gb_repo = gb_repository::Repository::open(
gb_repo_path,
project.id.clone(),
project_store,
user_store,
)?;
let project_repository = project_repository::Repository::open(&project)?;
target::Writer::new(&gb_repo).write_default(&target::Target {
name: "origin".to_string(),
remote: "origin".to_string(),
sha: repository.head().unwrap().target().unwrap(),
})?;
let file_path = std::path::Path::new("test.txt");
std::fs::write(
std::path::Path::new(&project.path).join(file_path),
"line1\nline2\n",
)?;
let branch1_id = create_virtual_branch(&gb_repo, "test_branch")
.expect("failed to create virtual branch");
let branch2_id = create_virtual_branch(&gb_repo, "test_branch2")
.expect("failed to create virtual branch");
let branch_writer = branch::Writer::new(&gb_repo);
branch::Writer::new(&gb_repo).write_selected(&Some(branch1_id.clone()))?;
let current_session = gb_repo.get_or_create_current_session()?;
let current_session_reader = sessions::Reader::open(&gb_repo, &current_session)?;
let branch_reader = branch::Reader::new(&current_session_reader);
let branch1 = branch_reader.read(&branch1_id)?;
branch_writer.write(&branch::Branch {
ownership: vec!["test.txt".try_into()?],
..branch1
})?;
let statuses =
get_status_by_branch(&gb_repo, &project_repository).expect("failed to get status");
let files_by_branch_id = statuses
.iter()
.map(|(branch, files)| (branch.id.clone(), 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);
move_files(&gb_repo, &branch2_id, &vec!["test.txt".try_into()?])
.expect("failed to move hunks");
let statuses =
get_status_by_branch(&gb_repo, &project_repository).expect("failed to get status");
let files_by_branch_id = statuses
.iter()
.map(|(branch, files)| (branch.id.clone(), files))
.collect::<HashMap<_, _>>();
assert_eq!(files_by_branch_id.len(), 2);
assert_eq!(files_by_branch_id[&branch1_id].len(), 0);
assert_eq!(files_by_branch_id[&branch2_id].len(), 1);
let current_session = gb_repo.get_or_create_current_session()?;
let current_session_reader = sessions::Reader::open(&gb_repo, &current_session)?;
let branch_reader = branch::Reader::new(&current_session_reader);
assert_eq!(branch_reader.read(&branch1_id)?.ownership, vec![]);
assert_eq!(
branch_reader.read(&branch2_id)?.ownership,
vec!["test.txt".try_into()?]
);
Ok(())
}
#[test]
fn test_move_hunks_entire_file_single_source() -> Result<()> {
let repository = test_repository()?;