module GitRemoteBranch

Constants

ALIAS_REVERSE_MAP
COMMANDS
COMMAND_NAME
COMPLETE_NAME
GIT
HELP_PARAMS
LOCAL_BRANCH_LISTING_COMMAND
NAME
SHORT_NAME

Public Class Methods

get_reverse_map(commands) click to toggle source
# File lib/git_remote_branch.rb, line 76
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

Public Instance Methods

execute_action(action, branch_name, origin, current_branch) click to toggle source
# File lib/git_remote_branch.rb, line 119
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) click to toggle source
# File lib/git_remote_branch.rb, line 132
def execute_cmds(*cmds)
  silencer = $WHISPER ? ' 2>&1' : ''
  cmds.flatten.each do |c|
    puts_cmd c
    `#{c}#{silencer}`
    whisper ''
  end
end
explain_action(action, branch_name, origin, current_branch) click to toggle source
# File lib/git_remote_branch.rb, line 124
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) click to toggle source
# 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) click to toggle source
# File lib/param_reader.rb, line 52
def get_action(action)
  ALIAS_REVERSE_MAP[action.to_s.downcase]
end
get_branch(branch) click to toggle source
# File lib/param_reader.rb, line 56
def get_branch(branch)
  branch
end
get_current_branch() click to toggle source
# File lib/state.rb, line 5
def get_current_branch
  local_branch_information[0]
end
get_origin(origin) click to toggle source
# File lib/param_reader.rb, line 60
def get_origin(origin)
  return origin || 'origin'
end
get_usage() click to toggle source
# File lib/git_remote_branch.rb, line 95
  def get_usage
    return <<-HELP
  Usage:

#{[:create, :publish, :rename, :delete, :track].map{|action|
      "  grb #{action} branch_name [origin_server]"
    } * "\n"
  }

  Notes:
  - If origin_server is not specified, the name 'origin' is assumed (git's default)
  - The rename functionality renames the current branch

  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
get_welcome() click to toggle source
# File lib/git_remote_branch.rb, line 91
def get_welcome
  "git_remote_branch version #{VERSION::STRING}\n\n"
end
git_found?() click to toggle source
# File lib/state.rb, line 13
def git_found?
  ret, msg = capture_process_output "#{GIT} --version"
  user_version = msg.chomp.split[2]
  warn "Some grb commands will not work with git version < 1.7 (you have #{user_version})" unless user_version >= '1.7'
  ret == 0
end
local_branches() click to toggle source
# File lib/state.rb, line 9
def local_branches
  local_branch_information[1]
end
puts_cmd(*cmds) click to toggle source
# File lib/git_remote_branch.rb, line 141
def puts_cmd(*cmds)
  cmds.flatten.each do |c|
    whisper "#{c}".red
  end
end
read_params(argv) click to toggle source
# 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) click to toggle source
# File lib/param_reader.rb, line 48
def silent!(argv)
  !!argv.delete('--silent')
end

Private Instance Methods

local_branch_information() click to toggle source

Returns an array of 2 elements: [current_branch, [all local branches]]

# File lib/state.rb, line 22
def local_branch_information
  #This is sensitive to checkouts of branches specified with wrong case

  listing = capture_process_output("#{LOCAL_BRANCH_LISTING_COMMAND}")[1]

  raise(NotOnGitRepositoryError, listing.chomp) if listing =~ /Not a git repository/i
  if listing =~ /\(no branch\)/
    raise InvalidBranchError, ["Couldn't identify the current local branch. The branch listing was:",
      LOCAL_BRANCH_LISTING_COMMAND.red, listing].join("\n")
  end

  current_branch = nil
  branches = listing.split("\n").map do |line|
    current        = line.include? '*'
    clean_line     = line.gsub('*','').strip
    current_branch = clean_line if current
    clean_line
  end

  return current_branch, branches
end