From f5622c543843b892948ca72c9dae19ff78f5960b Mon Sep 17 00:00:00 2001 From: Eshed Shaham Date: Thu, 1 Sep 2022 18:33:49 +0300 Subject: [PATCH] apply_change_to_project: fix group moves. --- lib/kintsugi/apply_change_to_project.rb | 9 +++- spec/kintsugi_apply_change_to_project_spec.rb | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/kintsugi/apply_change_to_project.rb b/lib/kintsugi/apply_change_to_project.rb index 6405ca8..4e036a7 100644 --- a/lib/kintsugi/apply_change_to_project.rb +++ b/lib/kintsugi/apply_change_to_project.rb @@ -195,12 +195,17 @@ def apply_group_and_file_diffs(project, diffs) end def apply_group_removals(project, removals) - removals.each do |change, path| + removals.sort_by(&:last).reverse.each do |change, path| next unless %w[PBXGroup PBXVariantGroup].include?(change["isa"]) group_path = join_path(path, change["displayName"]) - remove_component(project[group_path], change) + # by now we've deleted all of this group's children in the project, so we need to adapt the + # change to the expected current state of the group, that is, without any children. + change_without_children = change.dup + change_without_children["children"] = [] + + remove_component(project[group_path], change_without_children) end end diff --git a/spec/kintsugi_apply_change_to_project_spec.rb b/spec/kintsugi_apply_change_to_project_spec.rb index 81e90a3..b7f8edb 100644 --- a/spec/kintsugi_apply_change_to_project_spec.rb +++ b/spec/kintsugi_apply_change_to_project_spec.rb @@ -203,6 +203,55 @@ expect(base_project).to be_equivalent_to_project(theirs_project) end + it "moves a group with files in it" do + new_group = base_project.main_group.find_subpath("new_group", true) + new_group.new_reference("new_file") + base_project.save + + theirs_project = create_copy_of_project(base_project.path, "theirs") + new_group2 = theirs_project.main_group.find_subpath("new_group2", true) + theirs_project["new_group"].move(new_group2) + + changes_to_apply = get_diff(theirs_project, base_project) + + described_class.apply_change_to_project(base_project, changes_to_apply) + + expect(base_project).to be_equivalent_to_project(theirs_project) + end + + it "moves a group with a group in it" do + new_group = base_project.main_group.find_subpath("new_group", true) + new_group.find_subpath("sub_group", true) + base_project.save + + theirs_project = create_copy_of_project(base_project.path, "theirs") + new_group2 = theirs_project.main_group.find_subpath("new_group2", true) + theirs_project["new_group"].move(new_group2) + + changes_to_apply = get_diff(theirs_project, base_project) + + described_class.apply_change_to_project(base_project, changes_to_apply) + + expect(base_project).to be_equivalent_to_project(theirs_project) + end + + it "moves a group with a group with a file in it" do + new_group = base_project.main_group.find_subpath("new_group", true) + sub_group = new_group.find_subpath("sub_group", true) + sub_group.new_reference("new_file") + base_project.save + + theirs_project = create_copy_of_project(base_project.path, "theirs") + new_group2 = theirs_project.main_group.find_subpath("new_group2", true) + theirs_project["new_group"].move(new_group2) + + changes_to_apply = get_diff(theirs_project, base_project) + + described_class.apply_change_to_project(base_project, changes_to_apply) + + expect(base_project).to be_equivalent_to_project(theirs_project) + end + it "adds file with include in index and last known file type as nil" do theirs_project = create_copy_of_project(base_project.path, "theirs") file_reference = theirs_project.main_group.new_reference("#{filepath}.h")