class Gcb::BranchChanger

Public Class Methods

new(git_repository_directory=Dir.pwd) click to toggle source
# File lib/gcb.rb, line 9
def initialize(git_repository_directory=Dir.pwd)
  @git_dir = "#{git_repository_directory}/.git"
end

Public Instance Methods

change!(branch) click to toggle source
# File lib/gcb.rb, line 13
def change!(branch)
  if branches.include?(branch)
    checkout_branch(branch)
  elsif partially_matching_branch = partially_matching_branch_from_branches_and_branch(branches, branch)
    checkout_branch(partially_matching_branch)
  else
    # don't change branch
  end
  current_branch
end

Private Instance Methods

branches() click to toggle source

Git Queries

# File lib/gcb.rb, line 37
def branches
  @branches ||= git('branch').split("\n").map { |branch| branch.gsub('*', '') }.map(&:strip)
end
checkout_branch(branch) click to toggle source
# File lib/gcb.rb, line 45
def checkout_branch(branch)
  git("checkout #{branch}")
end
current_branch() click to toggle source
# File lib/gcb.rb, line 41
def current_branch
  git('branch').match(/\* (.*)\n/)[1]
end
git(command) click to toggle source
# File lib/gcb.rb, line 49
def git(command)
  `git --git-dir=#{@git_dir} #{command}`
end
partially_matching_branch_from_branches_and_branch(branches, branch) click to toggle source

Matching

# File lib/gcb.rb, line 29
def partially_matching_branch_from_branches_and_branch(branches, branch)
  branches.find { |existing_branch| existing_branch.include?(branch) }
end