forked from dstarh/git-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbs
executable file
·58 lines (50 loc) · 1.14 KB
/
gbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env ruby
#
# search git branches by pattern (grep) and switch (checkout)
#
# borrowed from https://github.com/dstarh/git-utils/blob/master/branch
#
class String
# colorization
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def yellow
colorize(33)
end
end
def colorize (line, search)
split = line.split(search)
colorized = ""
size = split.size
split.each_with_index do |section, index|
colorized << section
colorized << search.yellow if index + 1 < size
end
colorized
end
search = ARGV[0].dup << ".*"
search_raw = ARGV[0]
lines = if ARGV[1] == "--remote"
`git branch -a | grep -e "#{search}" -i --color`.split("\n")
else
`git branch | grep -e "#{search}" -i --color`.split("\n")
end
unless lines.size > 0
puts "No matching branches!"
exit
end
lines.each do |line|
line.sub! "*", ""
line.strip!
end
lines.each_with_index do |line, index|
puts index.to_s.rjust(3) + " - " + colorize(line, search_raw)
end
puts "q to quit"
print "Which branch: "
the_index = STDIN.gets.chomp
exit if the_index == 'q'
the_branch = lines[the_index.to_i]
the_branch.gsub!("remotes/origin/", "")
`git checkout #{the_branch}`