class GitPcheckout::Base

Public Class Methods

new(arg) click to toggle source
# File lib/git-pcheckout.rb, line 5
def initialize(arg)
  self.arg = arg
end

Public Instance Methods

perform() click to toggle source
# File lib/git-pcheckout.rb, line 9
def perform
  if pull_request_url?(arg)
    handle_pull_request_url(arg)
  else
    handle_branch_name(arg)
  end
rescue
  puts 'aborted'
end

Private Instance Methods

checkout_pull_request_branch(url) click to toggle source
# File lib/git-pcheckout.rb, line 33
def checkout_pull_request_branch(url)
  puts "handling Pull Request URL..."
  out = `hub checkout #{url}`
  return false if out == ''
  out.scan(/Branch (.+) set/).flatten.first
end
delete_branch(branch_name) click to toggle source
# File lib/git-pcheckout.rb, line 62
def delete_branch(branch_name)
  system("git branch -D #{branch_name}")
end
errors() click to toggle source
# File lib/git-pcheckout.rb, line 70
def errors
  { dirty_branch: "Please, commit your changes or stash them before you can switch branches"}
end
handle_branch_name(branch) click to toggle source
# File lib/git-pcheckout.rb, line 66
def handle_branch_name(branch)
  HandleBranch.new(branch).perform
end
handle_pull_request_url(url) click to toggle source
# File lib/git-pcheckout.rb, line 25
def handle_pull_request_url(url)
  branch_name_with_prefix = checkout_pull_request_branch(url)

  if source_repository_branch?(branch_name_with_prefix)
    handle_source_branch(branch_name_with_prefix)
  end
end
handle_source_branch(branch_name_with_prefix) click to toggle source
# File lib/git-pcheckout.rb, line 56
def handle_source_branch(branch_name_with_prefix)
  branch_name = substitute_prefix(branch_name_with_prefix)
  handle_branch_name(branch_name)
  delete_branch(branch_name_with_prefix)
end
origin_url() click to toggle source
# File lib/git-pcheckout.rb, line 52
def origin_url
  `git config --get remote.origin.url`
end
origin_user_name() click to toggle source
# File lib/git-pcheckout.rb, line 48
def origin_user_name
  origin_url.scan(/([\w\-_]+)\/[\w\-_]+.git/).flatten[0]
end
pull_request_url?(arg) click to toggle source
# File lib/git-pcheckout.rb, line 21
def pull_request_url?(arg)
  arg.match /https:\/\/github.com\//
end
source_repository_branch?(branch_name) click to toggle source
# File lib/git-pcheckout.rb, line 40
def source_repository_branch?(branch_name)
  branch_name.start_with?("#{origin_user_name}-")
end
substitute_prefix(branch_name) click to toggle source
# File lib/git-pcheckout.rb, line 44
def substitute_prefix(branch_name)
  branch_name.gsub("#{origin_user_name}-",'')
end