Last active
December 17, 2020 22:59
-
-
Save amree/352da40578762072035ac55e245eb5fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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