Class: Fluffer

Inherits:
Object
  • Object
show all
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

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

.start(args) ⇒ Object



17
18
19
# File 'lib/fluffer.rb', line 17

def self.start(args)
  Fluffer.new(args).run
end

Instance Method Details

#browseObject



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_infoObject



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_forksObject



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_infoObject

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

#helpObject

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

#listObject



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

#lookObject



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

#meObject



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

Returns:

  • (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

#pullObject



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_infoObject



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

#runObject



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