Module: GitRemoteBranch
- Includes:
- CaptureFu
- Defined in:
- lib/state.rb,
lib/version.rb,
lib/param_reader.rb,
lib/git_remote_branch.rb
Defined Under Namespace
Modules: VERSION Classes: InvalidBranchError, NotOnGitRepositoryError
Constant Summary collapse
- NAME =
'git_remote_branch'.freeze
- COMPLETE_NAME =
"#{NAME} #{VERSION::STRING}".freeze
- COMMAND_NAME =
'grb'.freeze
- SHORT_NAME =
COMMAND_NAME- 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}"' ] }, :publish => { :description => 'publish an exiting local branch', :aliases => %w{publish remotize share}, :commands => [ '"git push #{origin} #{branch_name}:refs/heads/#{branch_name}"', '"git fetch #{origin}"', '"git config branch.#{branch_name}.remote #{origin}"', '"git config branch.#{branch_name}.merge refs/heads/#{branch_name}"', '"git checkout #{branch_name}"' ] }, :rename => { :description => 'rename a remote branch and its local tracking branch', :aliases => %w{rename 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 => [ # This string programming thing is getting old. Not flexible enough anymore. '"git fetch #{origin}"', 'if local_branches.include?(branch_name) "git config branch.#{branch_name}.remote #{origin}\n" + "git config branch.#{branch_name}.merge refs/heads/#{branch_name}" else "git branch --track #{branch_name} #{origin}/#{branch_name}" end' ] } }
- ALIAS_REVERSE_MAP =
get_reverse_map(COMMANDS)
Class Method Summary collapse
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
- #get_usage ⇒ Object
- #get_welcome ⇒ Object
- #local_branches ⇒ Object
- #puts_cmd(*cmds) ⇒ Object
- #read_params(argv) ⇒ Object
- #silent!(argv) ⇒ Object
- #whisper(*msgs) ⇒ Object
Class Method Details
.get_reverse_map(commands) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/git_remote_branch.rb', line 95 def self.get_reverse_map(commands) h={} commands.each_pair do |cmd, params| params[:aliases].each do |alias_| unless h[alias_] h[alias_] = cmd else raise "Duplicate aliases: #{alias_.inspect} already defined for command #{h[alias_].inspect}" end end end h end |
Instance Method Details
#execute_action(action, branch_name, origin, current_branch) ⇒ Object
138 139 140 141 |
# File 'lib/git_remote_branch.rb', line 138 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
151 152 153 154 155 156 157 158 |
# File 'lib/git_remote_branch.rb', line 151 def execute_cmds(*cmds) silencer = $SILENT ? ' 2>&1' : '' cmds.flatten.each do |c| puts_cmd c `#{c}#{silencer}` whisper '' end end |
#explain_action(action, branch_name, origin, current_branch) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/git_remote_branch.rb', line 143 def explain_action(action, branch_name, origin, current_branch) cmds = COMMANDS[action][:commands].map{ |c| eval(c) }.compact whisper "List of operations to do to #{COMMANDS[action][:description]}:", '' puts_cmd cmds whisper '' end |
#explain_mode!(argv) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/param_reader.rb', line 39 def explain_mode!(argv) if argv[0].to_s.downcase == 'explain' argv.shift true else false end end |
#get_action(action) ⇒ Object
52 53 54 |
# File 'lib/param_reader.rb', line 52 def get_action(action) ALIAS_REVERSE_MAP[action.to_s.downcase] end |
#get_branch(branch) ⇒ Object
56 57 58 |
# File 'lib/param_reader.rb', line 56 def get_branch(branch) branch end |
#get_current_branch ⇒ Object
8 9 10 |
# File 'lib/state.rb', line 8 def get_current_branch local_branch_information[0] end |
#get_origin(origin) ⇒ Object
60 61 62 |
# File 'lib/param_reader.rb', line 60 def get_origin(origin) return origin || 'origin' end |
#get_usage ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/git_remote_branch.rb', line 114 def get_usage return "Usage:\n\n\#{[:create, :publish, :rename, :delete, :track].map{|action|\n \" grb \#{action} branch_name [origin_server] \\n\\n\"\n } \n}\n\nNotes:\n- If origin_server is not specified, the name 'origin' is assumed (git's default)\n- The rename functionality renames the current branch\n\nThe 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.\nExample: \n grb explain create\n grb explain create my_branch github\n\nAll commands also have aliases:\n\#{ COMMANDS.keys.map{|k| k.to_s}.sort.map {|cmd| \n \"\#{cmd}: \#{COMMANDS[cmd.to_sym][:aliases].join(', ')}\" }.join(\"\\n \") }\n" end |
#get_welcome ⇒ Object
110 111 112 |
# File 'lib/git_remote_branch.rb', line 110 def get_welcome "git_remote_branch version #{VERSION::STRING}\n\n" end |
#local_branches ⇒ Object
12 13 14 |
# File 'lib/state.rb', line 12 def local_branches local_branch_information[1] end |
#puts_cmd(*cmds) ⇒ Object
160 161 162 163 164 |
# File 'lib/git_remote_branch.rb', line 160 def puts_cmd(*cmds) cmds.flatten.each do |c| whisper "#{c}".red end end |
#read_params(argv) ⇒ Object
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 |
# File 'lib/param_reader.rb', line 7 def read_params(argv) #TODO Some validation on the params p={} p[:silent] = silent!(argv) p[:explain] = explain_mode!(argv) p[:action] = get_action(argv[0]) or return HELP_PARAMS return HELP_PARAMS if p[:action] == :help p[:branch] = get_branch(argv[1]) p[:origin] = get_origin(argv[2]) # If in explain mode, the user doesn't have to specify a branch or be on in # actual repo to get the explanation. # Of course if he is, the explanation will be made better by using contextual info. if p[:explain] p[:branch] ||= "branch_to_#{p[:action]}" p[:current_branch] = begin get_current_branch rescue NotOnGitRepositoryError, InvalidBranchError 'current_branch' end else return HELP_PARAMS unless p[:branch] p[:current_branch] = get_current_branch end return p end |
#silent!(argv) ⇒ Object
48 49 50 |
# File 'lib/param_reader.rb', line 48 def silent!(argv) !!argv.delete('--silent') end |
#whisper(*msgs) ⇒ Object
166 167 168 169 170 |
# File 'lib/git_remote_branch.rb', line 166 def whisper(*msgs) unless $SILENT msgs.flatten ? msgs.flatten.each{|m| puts m} : puts end end |