class Git::Whistles::PullRequest::Bare

Constants

BROWSERS
SAFE_QUERY_STRING_SIZE

Public Class Methods

new() click to toggle source
Calls superclass method Git::Whistles::App::new
# File lib/git-whistles/pull_request/bare.rb, line 8
def initialize
  super
end

Public Instance Methods

main(args) click to toggle source
Calls superclass method Git::Whistles::App#main
# File lib/git-whistles/pull_request/bare.rb, line 12
def main(args)
  super

  parse_args!(args)

  if args.count > 0
    die 'Too many arguments', :usage => true
  end

  if options.from == options.to
    die "You cannot issue a pull request to the same branch (#{options.from})."
  end

  query = {}

  # guess team name
  if options.from =~ %r{^(\w+)/.*}
    team = $1.capitalize
  else
    team = nil
  end

  # guess title.
  title = options.from.split('/').last.split(/[_-]/).delete_if { |word| word =~ /^\d+$/ }.join(' ').capitalize
  query[:"pull_request[title]"] = team ? "#{team}: #{title}" : title

  query.merge!(tracker_related_params(team))

  query_string = query.map { |key,value|
    "#{CGI.escape key.to_s}=#{CGI.escape value}"
  }.join('&')
  url = "https://github.com/#{repo}/compare/#{options.to}...#{options.from}?#{query_string}"

  puts "Preparing a pull request for branch #{options.from}"

  unless launch_browser(url)
    log.warn "Sorry, I don't know how to launch a web browser on your system. You can open it yourself and paste this URL:\n#{url}"
  end
end

Private Instance Methods

defaults() click to toggle source
# File lib/git-whistles/pull_request/bare.rb, line 82
def defaults
  {
    from:   run!('git symbolic-ref HEAD').strip.gsub(%r(^refs/heads/), ''),
    to:     'master',
    remote: 'origin'
  }
end
launch_browser(url) click to toggle source
# File lib/git-whistles/pull_request/bare.rb, line 74
def launch_browser(url)
  BROWSERS.each do |command|
    next if run("which #{command}").strip.empty?
    system(command, url) and return true
  end
  false
end
option_parser() click to toggle source
# File lib/git-whistles/pull_request/bare.rb, line 90
def option_parser
  @option_parser ||= OptionParser.new do |op|
    op.banner = "Usage: git #{pr_command} [options]"

    op.on("-f", "--from YOUR_BRANCH", "Branch to issue pull request for [head]") do |v|
      options.from = v
    end

    op.on("-to", "--to UPSTREAM_BRANCH", "Branch into which you want your code merged [master]") do |v|
      options.to = v
    end

    op.on("-r", "--remote NAME", "The remote you're sending this to [origin]") do |v|
      options.to = v
    end
  end
end
origin_url() click to toggle source
# File lib/git-whistles/pull_request/bare.rb, line 62
def origin_url
  @origin_url ||= begin
    run!("git config --get remote.#{options.remote}.url").strip.tap do |url|
      url =~ /github\.com/ or die 'origin does not have a Github URL !'
    end
  end
end
pr_command() click to toggle source
# File lib/git-whistles/pull_request/bare.rb, line 54
def pr_command # to be overridden by subclass
  'pr'
end
repo() click to toggle source
# File lib/git-whistles/pull_request/bare.rb, line 70
def repo
  @repo ||= origin_url.sub(/.*github\.com[\/:]/, '').sub(/\.git$/, '')
end