Skip to content

Commit

Permalink
merge.rb: Refactor.
Browse files Browse the repository at this point in the history
Remove code duplication.
  • Loading branch information
byohay committed Jun 14, 2022
1 parent 56f6df0 commit 07acddb
Showing 1 changed file with 15 additions and 34 deletions.
49 changes: 15 additions & 34 deletions lib/kintsugi/merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ class << self
def resolve_conflicts(project_file_path, changes_output_path)
validate_project(project_file_path)

project_in_temp_directory =
open_project_of_current_commit_in_temporary_directory(project_file_path)
base_project = create_project_from_stage_number_in_temporary_directory(project_file_path, 1)
ours_project = create_project_from_stage_number_in_temporary_directory(project_file_path, 2)
theirs_project = create_project_from_stage_number_in_temporary_directory(project_file_path, 3)

change = change_of_conflicting_commit_with_parent(project_file_path)
change = Xcodeproj::Differ.project_diff(theirs_project, base_project, :added, :removed)

if changes_output_path
File.write(changes_output_path, JSON.pretty_generate(change))
end

apply_change_and_copy_to_original_path(project_in_temp_directory, change, project_file_path)
apply_change_and_copy_to_original_path(ours_project, change, project_file_path)
end

# Merges the changes done between `theirs_project_path` and `base_project_path` to the file at
Expand Down Expand Up @@ -111,22 +112,21 @@ def validate_project(project_file_path)
end
end

def copy_project_to_temporary_path_in_directory_with_name(project_file_path, directory_name)
temp_directory_name = File.join(Dir.mktmpdir, directory_name)
Dir.mkdir(temp_directory_name)
temp_project_file_path = File.join(temp_directory_name, PROJECT_FILE_NAME)
FileUtils.cp(project_file_path, temp_project_file_path)
def create_project_from_stage_number_in_temporary_directory(project_file_path, stage_number)
project_directory_name = File.basename(File.dirname(project_file_path))
temp_project_file_path = File.join(Dir.mktmpdir, project_directory_name, PROJECT_FILE_NAME)
Dir.mkdir(File.dirname(temp_project_file_path))
Dir.chdir(File.dirname(project_file_path)) do
`git show :#{stage_number}:./#{PROJECT_FILE_NAME} > "#{temp_project_file_path}"`
end
Xcodeproj::Project.open(File.dirname(temp_project_file_path))
end

def open_project_of_current_commit_in_temporary_directory(project_file_path)
project_directory_name = File.basename(File.dirname(project_file_path))
temp_directory_name = File.join(Dir.mktmpdir, project_directory_name)
def copy_project_to_temporary_path_in_directory_with_name(project_file_path, directory_name)
temp_directory_name = File.join(Dir.mktmpdir, directory_name)
Dir.mkdir(temp_directory_name)
temp_project_file_path = File.join(temp_directory_name, PROJECT_FILE_NAME)
Dir.chdir(File.dirname(project_file_path)) do
`git show HEAD:./project.pbxproj > "#{temp_project_file_path}"`
end
FileUtils.cp(project_file_path, temp_project_file_path)
Xcodeproj::Project.open(File.dirname(temp_project_file_path))
end

Expand All @@ -144,24 +144,5 @@ def file_has_version_in_stage_numbers?(file_path, stage_numbers)
end
(stage_numbers - actual_stage_numbers.map(&:to_i)).empty?
end

def change_of_conflicting_commit_with_parent(project_file_path)
Dir.chdir(File.dirname(project_file_path)) do
conflicting_commit_project_file_path = File.join(Dir.mktmpdir, PROJECT_FILE_NAME)
`git show :3:./#{PROJECT_FILE_NAME} > #{conflicting_commit_project_file_path}`

conflicting_commit_parent_project_file_path = File.join(Dir.mktmpdir, PROJECT_FILE_NAME)
`git show :1:./#{PROJECT_FILE_NAME} > #{conflicting_commit_parent_project_file_path}`

conflicting_commit_project = Xcodeproj::Project.open(
File.dirname(conflicting_commit_project_file_path)
)
conflicting_commit_parent_project =
Xcodeproj::Project.open(File.dirname(conflicting_commit_parent_project_file_path))

Xcodeproj::Differ.project_diff(conflicting_commit_project,
conflicting_commit_parent_project, :added, :removed)
end
end
end
end

0 comments on commit 07acddb

Please sign in to comment.