Last active
December 2, 2016 16:16
-
-
Save DuckOfDoom/f5e309d6d720ca842597 to your computer and use it in GitHub Desktop.
Checking out git branches with grep
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 | |
command = 'git branch -a' | |
unless ARGV[0].nil? | |
command += ' | grep ' + ARGV[0] | |
end | |
lines = IO.popen(command).readlines | |
if lines.count == 0 | |
puts "No branch found containing \"#{ARGV[0]}\"." | |
abort | |
end | |
if lines[0].include?('fatal') && lines.include?('.git') | |
puts lines[0] | |
abort | |
end | |
puts "Found branches:\n" | |
counter = 1; | |
branches = Hash.new | |
lines.each do |l| | |
next if l.include?("origin/HEAD") | |
name = l.sub('remotes/','').sub("\n", "") | |
puts "#{counter.to_s.rjust(3)}: #{name}" | |
branches[counter] = name | |
counter += 1 | |
end | |
puts "\nEnter branch number:" | |
input = STDIN.gets.chomp.strip | |
branch = branches[input.to_i] | |
unless branch.nil? | |
name = branch.sub('remotes/', '').sub('origin/', '').sub('*','').strip | |
puts "Checking out '#{name}'..." | |
IO.popen("git checkout #{name}") { |io| io.each {||} } | |
else | |
puts "Invalid input: #{input}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment