Class: Autostager::PullRequest

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/autostager/pull_request.rb

Overview

Convenience class to represent a PR in staging directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

log, safe

Constructor Details

#initialize(branch, clone_url, base_dir, name, upstream) ⇒ PullRequest



15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



11
12
13
# File 'lib/autostager/pull_request.rb', line 11

def base_dir
  @base_dir
end

#branchObject (readonly)

Returns the value of attribute branch.



7
8
9
# File 'lib/autostager/pull_request.rb', line 7

def branch
  @branch
end

#clone_urlObject (readonly)

Returns the value of attribute clone_url.



9
10
11
# File 'lib/autostager/pull_request.rb', line 9

def clone_url
  @clone_url
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/autostager/pull_request.rb', line 4

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



6
7
8
# File 'lib/autostager/pull_request.rb', line 6

def number
  @number
end

#shaObject (readonly)

Returns the value of attribute sha.



8
9
10
# File 'lib/autostager/pull_request.rb', line 8

def sha
  @sha
end

#upstream_urlObject (readonly)

Returns the value of attribute upstream_url.



10
11
12
# File 'lib/autostager/pull_request.rb', line 10

def upstream_url
  @upstream_url
end

#web_urlObject (readonly)

Returns the value of attribute web_url.



5
6
7
# File 'lib/autostager/pull_request.rb', line 5

def web_url
  @web_url
end

Instance Method Details

#add_upstream_remoteObject



105
106
107
108
109
110
# 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) ⇒ Fixnum

How many commits is this branch behind?



39
40
41
42
# File 'lib/autostager/pull_request.rb', line 39

def behind(treeish)
  Dir.chdir @staging_dir
  `git log --oneline ..#{treeish}`.lines.count
end

#behind_thresholdFixnum

Note:

This number is based on the value of same name in

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

spec/spec_helper.rb, so please update the value both here and in spec_helper.rb if you need to change the threshold.



51
52
53
# File 'lib/autostager/pull_request.rb', line 51

def behind_threshold
  10
end

#cloneObject



112
113
114
115
116
117
118
119
# 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

#fetchObject



85
86
87
88
89
90
# 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_shaObject



25
26
27
28
# File 'lib/autostager/pull_request.rb', line 25

def local_sha
  Dir.chdir @staging_dir
  `git log --pretty='%H' HEAD^1..`.chomp
end

#rebaseFixnum?

Note:

This fails if the branch cannot be fast-forwarded.

Fast-forward this branch to origin.



69
70
71
72
73
74
75
76
77
# 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) ⇒ Boolean



98
99
100
101
102
103
# 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_hardObject



79
80
81
82
83
# 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?Boolean



30
31
32
# File 'lib/autostager/pull_request.rb', line 30

def staged?
  File.exist? @staging_dir
end

#up2date?(treeish) ⇒ Boolean

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



60
61
62
# File 'lib/autostager/pull_request.rb', line 60

def up2date?(treeish)
  behind(treeish) <= behind_threshold
end

#update_submodulesObject



92
93
94
95
96
# 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