Created
January 17, 2019 16:49
-
-
Save curtis1000/09f58b7ce1c021de4cc17043ab09e7cf to your computer and use it in GitHub Desktop.
Git Branch Switcher
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 'highline' | |
# Creates a numbered menu of your most recently committed branches to quickly jump between them | |
# Prerequisites: | |
# 1) ruby | |
# 2) gem install highline | |
LIMIT = 6 | |
def main | |
branches = `git branch --sort=-committerdate`.split("\n").map do |line| | |
line.gsub(/[* ]/,'') | |
end | |
if !is_clean | |
# list them but can't switch to any | |
puts "Working Directory is not clean." | |
return puts (branches.map {|branch| " - " + branch + "\n"}).join | |
end | |
# always include master in the list | |
branches.push('master') if !branches.include? 'master' | |
# make a menu | |
cli = HighLine.new | |
cli.choose do |menu| | |
menu.prompt = "Pick a branch to switch to: " | |
menu.choice('Nothing, nevermind...') {|result| return } | |
branches.each_with_index do |branch, i| | |
next if i > LIMIT | |
menu.choice(branch) {|result| checkout result} | |
end | |
end | |
end | |
def is_clean | |
`git status`.include? 'nothing to commit, working tree clean' | |
end | |
def checkout(branch) | |
`git checkout #{branch}` | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment