Class: Fluffer
- Inherits:
-
Object
- Object
- Fluffer
- Defined in:
- lib/fluffer.rb
Constant Summary collapse
- FLUFF_CACHE_FILE =
'.git/fluff_cache.json'
- FLUFF_DATA_FILE =
'.git/fluff_data.json'
Class Method Summary collapse
Instance Method Summary collapse
- #browse ⇒ Object
- #cache_pull_info ⇒ Object
- #clean(info) ⇒ Object
- #fetch_stale_forks ⇒ Object
- #get_data(file) ⇒ Object
-
#get_pull_info ⇒ Object
API/DATA HELPER FUNCTIONS #.
- #git(command) ⇒ Object
- #has_sha(sha) ⇒ Object
-
#help ⇒ Object
COMMANDS ##.
-
#initialize(args) ⇒ Fluffer
constructor
A new instance of Fluffer.
-
#l(info, size) ⇒ Object
DISPLAY HELPER FUNCTIONS #.
- #list ⇒ Object
- #look ⇒ Object
- #me ⇒ Object
- #not_merged?(sha) ⇒ Boolean
- #pull ⇒ Object
- #pull_num(num) ⇒ Object
- #r(info, size) ⇒ Object
- #repo_info ⇒ Object
- #run ⇒ Object
- #save_data(data, file) ⇒ Object
Constructor Details
#initialize(args) ⇒ Fluffer
Returns a new instance of Fluffer.
11 12 13 14 15 |
# File 'lib/fluffer.rb', line 11 def initialize(args) @command = args.shift @user, @repo = repo_info @args = args end |
Class Method Details
Instance Method Details
#browse ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/fluffer.rb', line 77 def browse num = @args.shift if p = pull_num(num) `open #{p['html_url']}` else puts "No such number" end end |
#cache_pull_info ⇒ Object
162 163 164 165 166 |
# File 'lib/fluffer.rb', line 162 def cache_pull_info path = "/pulls/#{@user}/#{@repo}/open" response = HTTParty.get('https://github.com/api/v2/json' << path) save_data(response, FLUFF_CACHE_FILE) end |
#clean(info) ⇒ Object
152 153 154 |
# File 'lib/fluffer.rb', line 152 def clean(info) info.to_s.gsub("\n", ' ') end |
#fetch_stale_forks ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/fluffer.rb', line 112 def fetch_stale_forks puts "Checking for forks in need of fluffing" pulls = get_pull_info repos = {} pulls.each do |pull| o = pull['head']['repository']['owner'] r = pull['head']['repository']['name'] s = pull['head']['sha'] if !has_sha(s) repo = "#{o}/#{r}" repos[repo] = true end end repos.each do |repo, bool| puts " fetching #{repo}" git("fetch git://github.com/#{repo}.git refs/heads/*:refs/pr/#{repo}/*") end end |
#get_data(file) ⇒ Object
168 169 170 |
# File 'lib/fluffer.rb', line 168 def get_data(file) data = JSON.parse(File.read(file)) end |
#get_pull_info ⇒ Object
API/DATA HELPER FUNCTIONS #
158 159 160 |
# File 'lib/fluffer.rb', line 158 def get_pull_info get_data(FLUFF_CACHE_FILE)['pulls'] end |
#git(command) ⇒ Object
199 200 201 |
# File 'lib/fluffer.rb', line 199 def git(command) `git #{command}`.chomp end |
#has_sha(sha) ⇒ Object
131 132 133 134 |
# File 'lib/fluffer.rb', line 131 def has_sha(sha) git("show #{sha} 2>&1") $?.exitstatus == 0 end |
#help ⇒ Object
COMMANDS ##
31 32 33 34 |
# File 'lib/fluffer.rb', line 31 def help puts "No command: #{@command}" puts "Try: me, list, look, pull, browse" end |
#l(info, size) ⇒ Object
DISPLAY HELPER FUNCTIONS #
144 145 146 |
# File 'lib/fluffer.rb', line 144 def l(info, size) clean(info)[0, size].ljust(size) end |
#list ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/fluffer.rb', line 86 def list option = @args.shift puts "Open Pull Requests for #{@user}/#{@repo}" pulls = get_pull_info pulls.reverse! if option == '--reverse' pulls.each do |pull| line = [] line << l(pull['number'], 4) line << l(Date.parse(pull['created_at']).strftime("%m/%d"), 5) line << l(pull['comments'], 2) line << l(pull['title'], 35) line << l(pull['head']['label'], 20) sha = pull['head']['sha'] if not_merged?(sha) puts line.join ' ' end end end |
#look ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/fluffer.rb', line 50 def look num = @args.shift option = @args.shift if p = pull_num(num) puts "Number : #{p['number']}" puts "Label : #{p['head']['label']}" puts "Created : #{p['created_at']}" puts "Votes : #{p['votes']}" puts "Comments : #{p['comments']}" puts puts "Title : #{p['title']}" puts "Body :" puts puts p['body'] puts puts '------------' puts if option == '--full' exec "git diff --color=always HEAD...#{p['head']['sha']}" else puts git("diff --stat --color=always HEAD...#{p['head']['sha']}") end else puts "No such number" end end |
#me ⇒ Object
105 106 107 108 109 110 |
# File 'lib/fluffer.rb', line 105 def me puts "Fluffing #{@user}/#{@repo}" cache_pull_info fetch_stale_forks list end |
#not_merged?(sha) ⇒ Boolean
136 137 138 139 |
# File 'lib/fluffer.rb', line 136 def not_merged?(sha) commits = git("rev-list #{sha} ^HEAD 2>&1") commits.split("\n").size > 0 end |
#pull ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fluffer.rb', line 36 def pull num = @args.shift if p = pull_num(num) o = p['head']['repository']['owner'] r = p['head']['repository']['name'] s = p['head']['sha'] msg = "Merge pull request ##{num} from #{o}/#{r}" puts cmd = "git merge --no-ff -m '#{msg}' #{s}" exec(cmd) else puts "No such number" end end |
#pull_num(num) ⇒ Object
178 179 180 181 |
# File 'lib/fluffer.rb', line 178 def pull_num(num) data = get_pull_info data.select { |p| p['number'].to_s == num.to_s }.first end |
#r(info, size) ⇒ Object
148 149 150 |
# File 'lib/fluffer.rb', line 148 def r(info, size) clean(info)[0, size].rjust(size) end |
#repo_info ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/fluffer.rb', line 184 def repo_info c = {} config = git('config --list') config.split("\n").each do |line| k, v = line.split('=') c[k] = v end u = c['remote.origin.url'] if m = /github\.com.(.*?)\/(.*?)\.git/.match(u) user = m[1] proj = m[2] end [user, proj] end |
#run ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/fluffer.rb', line 21 def run if self.respond_to? @command self.send @command else help end end |
#save_data(data, file) ⇒ Object
172 173 174 175 176 |
# File 'lib/fluffer.rb', line 172 def save_data(data, file) File.open(file, "w+") do |f| f.puts data.to_json end end |