#!/usr/bin/env ruby require "open3" def run_command(command) output, status = Open3.capture2e(command) raise "Command failed: #{ command }\n#{ output }" unless status.success? output end def run_bundle_update run_command("bundle update") if File.exist?("Gemfile.lock") end def run_yarn_upgrade run_command("yarn upgrade") if File.exist?("yarn.lock") end def run_db_migrate run_command("rails db:migrate") if Dir.glob("db/migrate/*.rb").any? end def add_files_to_git run_command("git add -A") end def rebase_continue run_command("git rebase --continue") end def restore_git_state run_command("git reset --hard ORIG_HEAD") end def process_commit run_bundle_update run_yarn_upgrade run_db_migrate add_files_to_git rebase_continue end def main Dir.chdir(File.expand_path("..", __dir__)) do commits = `git rev-list --reverse HEAD`.split("\n") commits.each do |commit| begin run_command("git checkout #{ commit }") process_commit rescue StandardError => e puts "Error processing commit #{ commit }: #{ e.message }" restore_git_state break end end end end main