I like descriptive, sometimes long, branch names. However, typing them is annoying. So, I've created this script that I can inject in place of typing a branch name. It shows me a numbered list of current branches, and I select one using the number from the list.
Example:
andrewculver:~/Sites/sample-project (master)$ git branch -d `gb`
1. master
2. some-bugfix
3. some-new-feature
4. test
> 3
Deleted branch some-new-feature (was 80b10ce).
andrewculver:~/Sites/sample-project (master)$ git checkout `gb`
1. master
2. some-bugfix
3. test
> 2
Switched to branch 'some-bugfix'
andrewculver:~/Sites/sample-project (some-bugfix)$
However, in order to accomplish this, all of the user interaction is output to $stderr
, and only the final result (e.g. the branch name) is output to $stdout
.
It works fine, but it seems wrong. Is there a better way to write tools like this?
Here's the code for the script incase you're interested:
#!/usr/bin/env ruby
# Get a list of branches.
branches = `git branch`
branches = branches.split("\n").map { |b| b.gsub(/[ \*] /, '') }
# Present choices.
branches.each_index do |i|
$stderr.puts "#{i + 1}. #{branches[i]}"
end
$stderr << "> "
# Gather input.
choice = gets.to_i
# Output branch selected, or an error.
if choice.is_a? Fixnum and branches[choice - 1].is_a? String
puts branches[choice - 1]
else
$stderr.puts "Invalid selection."
end
This doesn't answer the question posed, but I've also had this problem and have had a good experience with using zsh and git-completion to tab complete my git commands.
http://zsh.git.sourceforge.net/git/gitweb.cgi?p=zsh/zsh;a=blob_plain;f=Completion/Unix/Command/_git;hb=HEAD