Created
October 25, 2016 06:01
-
-
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)
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 | |
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