Skip to content

Instantly share code, notes, and snippets.

@kaushikgopal
Created October 25, 2016 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaushikgopal/454365dd96464a72a89ec0a56bc9e598 to your computer and use it in GitHub Desktop.
Save kaushikgopal/454365dd96464a72a89ec0a56bc9e598 to your computer and use it in GitHub Desktop.
ruby script that makes merging up and pushing changes easy for versions ahead (if you follow semantic versioning)
#!/usr/bin/env ruby
require 'rubygems'
require 'set'
require 'highline/import'
# this assumes you follow proper semantic app versioning
# listen to this http://fragmentedpodcast.com/episodes/054/ for an explanation
def confirm(message)
confirmation = ask message + " (y/n)"
if confirmation != "y"
puts "Grr... exiting"
exit
end
end
def current_branch
current_branch = `git branch --no-color | grep '*'`.split.last
end
def upper_branch(last_3_releases, current_branch)
upper_branch = ""
return "" if current_branch == "master"
last_3_releases.each do |release|
upper_branch = release if release > current_branch
break if !upper_branch.empty?
end
if upper_branch.empty?
"master"
else
upper_branch
end
end
def last_3_releases
last_3_releases = SortedSet.new
all_branches = `git branch -a --no-color`
all_branches.split("\n").each do |branch_name|
branch_name = branch_name.strip
next if !branch_name.start_with? "remotes/origin/release/"
branch_name["remotes/origin/"] = ""
last_3_releases << branch_name
last_3_releases.delete(last_3_releases.first) if last_3_releases.size > 3
end
last_3_releases
end
def exit_if_merge_conflict
conflicting_files = `git ls-files -u`
return if conflicting_files.empty?
puts "merge conflict detected. Sorry you're on your own!\n#{conflicting_files}\n\nEXITING\n"
exit
end
def merge_up(current_branch)
puts "\n\n\n--- pulling latest changes for this branch"
system("git pull --no-ff")
exit_if_merge_conflict()
confirm "git pushing #{current_branch}"
system("git push")
return "" if current_branch == "master"
puts "\n\n--- current branch is #{current_branch}"
prev_branch = current_branch
next_branch = upper_branch(last_3_releases, prev_branch)
confirm "--- moving from #{prev_branch} -> #{next_branch}\nlooks good?"
system("git checkout #{next_branch}")
puts "\n\n\n--- pulling latest changes for this branch"
system("git pull --no-ff")
exit_if_merge_conflict()
confirm "merging branch #{prev_branch} onto #{next_branch}"
system("git merge --no-ff #{prev_branch}")
exit_if_merge_conflict()
confirm "git pushing #{next_branch}"
system("git push")
next_branch
end
def main
puts "--- fetching all latest branches from server"
system("git fetch origin")
last_3_releases = last_3_releases()
next_branch = current_branch()
while !next_branch.empty?
next_branch = merge_up(next_branch)
break if next_branch == "master"
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment