fix shelve-and-goto
[git-knacks.git] / git-shelve-to
blob62bc1ea388c9e969d8a063c3d68870dcf9ddd982
1 #!/usr/bin/env ruby
3 #this will shelve your current changes to a new branch
4 require 'optparse'
8 class ShelveTo
9 attr_accessor :options
10 attr_accessor :destination_branch
12 def initialize
13 options = {}
14 branch = OptionParser.new do |opts|
15 opts.on("-v", "--[no-]verbose", "Verbose output (echo individual git commands)") do |v|
16 options[:verbose] = v
17 end
18 end.parse!
19 self.options = {:verbose => false}.merge options
20 self.destination_branch = branch.join('-')
21 end
23 def run
24 previous_branch = current_branch
25 git("git checkout #{"-b" unless branch_exists?(destination_branch)} #{destination_branch}") &&
26 git("git add .") &&
27 git("git ci -a -m 'SHELVE SHELVE SHELVE'") &&
28 git("git checkout #{previous_branch}")
29 end
32 private
33 def current_branch
34 output = get('git branch')
35 output.match(/^\* (.+)$/)[1]
36 end
38 def branch_exists?(branch)
39 output = get('git branch')
40 !output.match("^(\\*| ) #{branch}$").nil?
41 end
43 def git(cmd)
44 puts cmd if options[:verbose]
45 system cmd
46 end
48 def get(cmd)
49 puts cmd if options[:verbose]
50 `#{cmd}`
51 end
52 end
54 ShelveTo.new.run