Created
February 12, 2020 18:38
-
-
Save mwlang/8badfc57b584519766599847fd526e3e to your computer and use it in GitHub Desktop.
Save jira-co.rb to a folder on your $PATH (for example ~/bin). Add git alias to your ~/.gitconfig
This file contains hidden or 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 | |
pattern = ARGV[0] | |
if pattern.to_s == "" | |
puts "Must supply a pattern for the branch. i.e. RUBY-2379" | |
exit 1 | |
end | |
branches = `git branch -a | grep "remotes" | grep -i #{pattern}`.split("\n").map(&:strip) | |
if branches.size == 0 | |
puts "No branch matched #{pattern.inspect}" | |
exit 1 | |
end | |
def shorten_branch_name branch_name | |
branch_name.split("/").last | |
end | |
if branches.size == 1 | |
branch_name = shorten_branch_name branches[0] | |
puts "checking out branch: #{branch_name.inspect}" | |
`git checkout #{branch_name}` | |
else | |
puts "Too many branches returned! Improve your pattern to match specific branch" | |
branches.each_with_index do |branch, index| | |
puts "#{index}: #{branch.inspect}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment