class Autostager::PullRequest

Convenience class to represent a PR in staging directory.

Attributes

base_dir[R]
branch[R]
clone_url[R]
name[R]
number[R]
sha[R]
upstream_url[R]
web_url[R]

Public Class Methods

new(branch, clone_url, base_dir, name, upstream) click to toggle source
# File lib/autostager/pull_request.rb, line 15
def initialize(branch, clone_url, base_dir, name, upstream)
  @branch = branch
  @clone_url = clone_url
  @base_dir = base_dir
  @name = name
  @upstream_url = upstream

  @staging_dir = File.join base_dir, @name
end

Public Instance Methods

add_upstream_remote() click to toggle source
# File lib/autostager/pull_request.rb, line 105
def add_upstream_remote
  Dir.chdir @staging_dir
  log 'add upstream remote'
  `git remote add upstream #{@upstream_url} &> /dev/null`
  `git fetch --prune upstream &> /dev/null`
end
behind(treeish) click to toggle source

How many commits is this branch behind?

@param treeish [String] example: 'upstream/production'

@return [Fixnum]

# File lib/autostager/pull_request.rb, line 39
def behind(treeish)
  Dir.chdir @staging_dir
  `git log --oneline ..#{treeish}`.lines.count
end
behind_threshold() click to toggle source

A threshold for how many commits a branch can be behind upstream.

@note This number is based on the value of same name in spec/spec_helper.rb, so please update the value both here and in spec_helper.rb if you need to change the threshold.

@return [Fixnum]

# File lib/autostager/pull_request.rb, line 51
def behind_threshold
  10
end
clone() click to toggle source
# File lib/autostager/pull_request.rb, line 112
def clone
  log "clone to #{@staging_dir}"
  FileUtils.mkdir_p @base_dir unless File.exist?(@base_dir)
  `git clone -b #{@branch} #{@clone_url} #{@staging_dir} &> /dev/null`
  Dir.chdir @staging_dir
  add_upstream_remote
  update_submodules
end
fetch() click to toggle source
# File lib/autostager/pull_request.rb, line 85
def fetch
  log 'git fetch'
  Dir.chdir @staging_dir
  add_upstream_remote
  `git fetch --all --prune &> /dev/null`
end
local_sha() click to toggle source
# File lib/autostager/pull_request.rb, line 25
def local_sha
  Dir.chdir @staging_dir
  `git log --pretty='%H' HEAD^1..`.chomp
end
rebase() click to toggle source

Fast-forward this branch to origin. @note This fails if the branch cannot be fast-forwarded.

@return [Fixnum] exit status of `git rebase` @return [nil] if `git rebase` fails to exit.

# File lib/autostager/pull_request.rb, line 69
def rebase
  log "rebase origin/#{@branch}"
  Dir.chdir @staging_dir
  `git rebase origin/#{@branch} &> /dev/null`
  status = $CHILD_STATUS.exitstatus
  update_submodules
  log "#{@branch} is at revision #{local_sha}"
  status
end
remote?(s) click to toggle source
# File lib/autostager/pull_request.rb, line 98
def remote?(s)
  Dir.chdir @staging_dir
  remotes = `git remote -v`.split("\n")
  urls = remotes.map { |r| r.split("\t").last.split(' ').first }.uniq
  urls.include?(s)
end
reset_hard() click to toggle source
# File lib/autostager/pull_request.rb, line 79
def reset_hard
  Dir.chdir @staging_dir
  `git reset --hard origin/#{@branch} &> /dev/null`
  update_submodules
end
staged?() click to toggle source
# File lib/autostager/pull_request.rb, line 30
def staged?
  File.exist? @staging_dir
end
up2date?(treeish) click to toggle source

Is this branch within a reasonable number of commits with upstream?

@param treeish [String] example: 'upstream/production'

@return [Boolean]

# File lib/autostager/pull_request.rb, line 60
def up2date?(treeish)
  behind(treeish) <= behind_threshold
end
update_submodules() click to toggle source
# File lib/autostager/pull_request.rb, line 92
def update_submodules
  log "update submodules in #{@staging_dir}"
  `git submodule sync &> /dev/null`
  `git submodule update --init &> /dev/null`
end