Skip to content

Instantly share code, notes, and snippets.

@amree
Last active December 17, 2020 22:59
Show Gist options
  • Save amree/352da40578762072035ac55e245eb5fa to your computer and use it in GitHub Desktop.
Save amree/352da40578762072035ac55e245eb5fa to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# frozen_string_literal: true
# README
# - gem install git
# - Update the ticket prefix
# - Update the branch name
# - Put this in /usr/local/bin and chmod +x it
require 'git'
def log(msg)
puts "#{Time.now} - #{msg}"
end
print 'πŸ€” Proceed? (yes): '
result = gets.chomp
exit unless result == 'yes'
TICKET_PREFIX = /^\[GH-[0-9]{3,4}\]/
branches = [
'branch-name-1',
'branch-name-2',
'branch-name-3',
'branch-name-4',
]
g = Git.open('./')
log "πŸ“¦ DIRECTORY: #{g.dir}"
local_branches = g.branches.local.map(&:name)
branches.each_with_index do |branch_name, index|
next if index.zero?
log("πŸ“¦ BRANCH: #{branch_name}")
unless local_branches.include? branch_name
log("🚨 ERROR: Specified branch doesn't exist in local")
break
end
g.branch(branch_name).checkout
unless g.current_branch == branch_name
log('🚨 ERROR: Checkout failed')
break
end
log('βœ…')
log_titles = g.log.map { |m| m.message.split("\n").first }
commit_count = log_titles.find_index { |m| m.match TICKET_PREFIX }
command = "git rebase --onto #{branches[index - 1]} HEAD~#{commit_count + 1}"
log "⚑️ #{command}"
puts nil
system command
puts nil
log('πŸš€ Done')
puts nil
end
puts '-----------------'
print '🚨 PUSH FORCE? (yes): '
result = gets.chomp
exit unless result == 'yes'
branches.each do |branch_name|
command = "git push -f --quiet origin #{branch_name}:#{branch_name}"
log "⚑️ #{command}"
puts nil
system command
puts nil
log('πŸš€ Done')
end
log('πŸš€ πŸš€ πŸš€')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment