Module: GitRemoteBranch
- Defined in:
- lib/param_reader.rb,
lib/git_remote_branch.rb
Constant Summary collapse
- VERSION =
'0.2.4'
- COMMANDS =
{ :create => { :description => 'create a new remote branch and track it locally', :aliases => %w{create new}, :commands => [ '"git push #{origin} #{current_branch}:refs/heads/#{branch_name}"', '"git fetch #{origin}"', '"git branch --track #{branch_name} #{origin}/#{branch_name}"', '"git checkout #{branch_name}"' ] }, :rename => { :description => 'rename a remote branch and its local tracking branch', :aliases => %w{ rn mv move }, :commands => [ '"git push #{origin} #{current_branch}:refs/heads/#{branch_name}"', '"git fetch #{origin}"', '"git branch --track #{branch_name} #{origin}/#{branch_name}"', '"git checkout #{branch_name}"', '"git push #{origin} :refs/heads/#{current_branch}"', '"git branch -d #{current_branch}"', ] }, :delete => { :description => 'delete a local and a remote branch', :aliases => %w{delete destroy kill remove rm}, :commands => [ '"git push #{origin} :refs/heads/#{branch_name}"', '"git checkout master" if current_branch == branch_name', '"git branch -d #{branch_name}"' ] }, :track => { :description => 'track an existing remote branch', :aliases => %w{track follow grab fetch}, :commands => [ '"git fetch #{origin}"', '"git checkout master" if current_branch == branch_name', '"git branch --track #{branch_name} #{origin}/#{branch_name}"' ] } }
Instance Method Summary collapse
- #execute_action(action, branch_name, origin, current_branch) ⇒ Object
- #execute_cmds(*cmds) ⇒ Object
- #explain_action(action, branch_name, origin, current_branch) ⇒ Object
- #explain_mode!(argv) ⇒ Object
- #get_action(action) ⇒ Object
- #get_branch(branch) ⇒ Object
- #get_current_branch ⇒ Object
- #get_origin(origin) ⇒ Object
- #print_usage ⇒ Object
- #print_welcome ⇒ Object
- #puts_cmd(*cmds) ⇒ Object
- #read_params(argv) ⇒ Object
Instance Method Details
#execute_action(action, branch_name, origin, current_branch) ⇒ Object
87 88 89 90 |
# File 'lib/git_remote_branch.rb', line 87 def execute_action(action, branch_name, origin, current_branch) cmds = COMMANDS[action][:commands].map{ |c| eval(c) }.compact execute_cmds(cmds) end |
#execute_cmds(*cmds) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/git_remote_branch.rb', line 100 def execute_cmds(*cmds) cmds.flatten.each do |c| puts_cmd c `#{c}` puts '' end end |
#explain_action(action, branch_name, origin, current_branch) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/git_remote_branch.rb', line 92 def explain_action(action, branch_name, origin, current_branch) cmds = COMMANDS[action][:commands].map{ |c| eval(c) }.compact puts "List of operations to do to #{COMMANDS[action][:description]}:", '' puts_cmd cmds puts '' end |
#explain_mode!(argv) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/param_reader.rb', line 18 def explain_mode!(argv) if argv[0].to_s.downcase == 'explain' argv.shift true else false end end |
#get_action(action) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/param_reader.rb', line 27 def get_action(action) a = action.to_s.downcase return :create if COMMANDS[:create][:aliases].include?(a) return :delete if COMMANDS[:delete][:aliases].include?(a) return :track if COMMANDS[:track][:aliases].include?(a) return :rename if COMMANDS[:rename][:aliases].include?(a) return nil end |
#get_branch(branch) ⇒ Object
36 37 38 |
# File 'lib/param_reader.rb', line 36 def get_branch(branch) branch end |
#get_current_branch ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/param_reader.rb', line 44 def get_current_branch #This is sensitive to checkouts of branches specified with wrong case x = `git branch -l` x.each_line do |l| return l.sub("*","").strip if l =~ /\A\*/ and not l =~ /\(no branch\)/ end raise "Couldn't identify the current local branch." end |
#get_origin(origin) ⇒ Object
40 41 42 |
# File 'lib/param_reader.rb', line 40 def get_origin(origin) return origin || 'origin' end |
#print_usage ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/git_remote_branch.rb', line 62 def print_usage puts <<-HELP Usage: grb create branch_name [origin_server] grb delete branch_name [origin_server] grb rename branch_name [origin_server] grb track branch_name [origin_server] If origin_server is not specified, the name 'origin' is assumed (git's default) The explain meta-command: you can also prepend any command with the keyword 'explain'. Instead of executing the command, git_remote_branch will simply output the list of commands you need to run to accomplish that goal. Example: grb explain create grb explain create my_branch github All commands also have aliases: #{ COMMANDS.keys.map{|k| k.to_s}.sort.map {|cmd| "#{cmd}: #{COMMANDS[cmd.to_sym][:aliases].join(', ')}" }.join("\n ") } HELP end |
#print_welcome ⇒ Object
58 59 60 |
# File 'lib/git_remote_branch.rb', line 58 def print_welcome puts "git_remote_branch version #{VERSION}", '' end |
#puts_cmd(*cmds) ⇒ Object
108 109 110 111 112 |
# File 'lib/git_remote_branch.rb', line 108 def puts_cmd(*cmds) cmds.flatten.each do |c| puts "#{c}".red end end |
#read_params(argv) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/param_reader.rb', line 2 def read_params(argv) p={} p[:explain] = explain_mode!(argv) p[:action] = get_action(argv[0]) || :help p[:branch] = get_branch(argv[1]) p[:origin] = get_origin(argv[2]) p[:current_branch] = get_current_branch #If in explain mode, the user doesn't have to specify a branch to get the explanation p[:branch] ||= "branch_to_#{p[:action]}" if p[:explain] #TODO Some validation on the params p end |