git-forks 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/git-forks.rb +73 -32
- metadata +1 -1
data/lib/git-forks.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
1
3
|
# JSON is used to cache GitHub API response data.
|
2
4
|
require 'json'
|
3
5
|
# Launchy is used in 'browse' to open a browser.
|
@@ -15,7 +17,7 @@ class GitForks
|
|
15
17
|
@command = args.shift
|
16
18
|
@user, @repo = repo_info
|
17
19
|
@args = args
|
18
|
-
@
|
20
|
+
@updated = false
|
19
21
|
end
|
20
22
|
|
21
23
|
def self.start(args)
|
@@ -38,26 +40,47 @@ class GitForks
|
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
43
|
+
#-----------------------------------------------------------------------------
|
44
|
+
# Logging (stolen from Grit)
|
45
|
+
#-----------------------------------------------------------------------------
|
46
|
+
|
47
|
+
class << self
|
48
|
+
# Set +debug+ to true to log everything
|
49
|
+
attr_accessor :debug
|
50
|
+
|
51
|
+
# The standard +logger+ for debugging - this defaults to a plain STDOUT logger
|
52
|
+
attr_accessor :logger
|
53
|
+
def log(str)
|
54
|
+
logger.debug { str }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
self.debug = false # TODO: add --verbose switch
|
58
|
+
|
59
|
+
@logger ||= ::Logger.new(STDOUT)
|
60
|
+
|
41
61
|
#-----------------------------------------------------------------------------
|
42
62
|
# Commands
|
43
63
|
#-----------------------------------------------------------------------------
|
44
64
|
|
45
65
|
# Get the latest GitHub data.
|
46
66
|
def update
|
67
|
+
puts 'Retrieving the latest GitHub data...'
|
68
|
+
|
47
69
|
cache('forks', fetch_fork_info)
|
48
70
|
update_branches
|
71
|
+
|
72
|
+
@updated = true
|
49
73
|
end
|
50
74
|
|
51
75
|
# Fetch and cache all branches for each fork.
|
52
|
-
def update_branches
|
53
|
-
pattern ||= @branch_pattern
|
76
|
+
def update_branches
|
54
77
|
forks = get_cached_data('forks')
|
55
78
|
|
56
79
|
forks.each do |fork|
|
57
80
|
fork_user = fork['owner']['login']
|
58
|
-
|
81
|
+
GitForks.log "Fetching branches in '#{fork_user}/#{@repo}'" if GitForks.debug
|
59
82
|
|
60
|
-
branches = fetch_fork_branches(fork_user
|
83
|
+
branches = fetch_fork_branches(fork_user)
|
61
84
|
|
62
85
|
fork['branches'] = branches
|
63
86
|
end
|
@@ -65,6 +88,22 @@ class GitForks
|
|
65
88
|
cache('forks', forks)
|
66
89
|
end
|
67
90
|
|
91
|
+
# git-fetch from the fork's GitHub repository. (Forces cache update.)
|
92
|
+
def fetch
|
93
|
+
target_owners = @args
|
94
|
+
|
95
|
+
update if not @updated
|
96
|
+
get_cached_data('forks').each do |fork|
|
97
|
+
owner = fork['owner']['login']
|
98
|
+
if target_owners.empty? or target_owners.include?(owner)
|
99
|
+
puts '-' * 80
|
100
|
+
puts "Fething Git data from fork '#{owner}/#{@repo}'"
|
101
|
+
git("fetch #{github_endpoint}/#{owner}/#{@repo}.git " +
|
102
|
+
"+refs/heads/*:refs/forks/#{owner}/*")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
68
107
|
# List all forks.
|
69
108
|
#
|
70
109
|
# Example::
|
@@ -170,20 +209,26 @@ class GitForks
|
|
170
209
|
end
|
171
210
|
end
|
172
211
|
|
212
|
+
def help
|
213
|
+
puts "No command: #{@command}"
|
214
|
+
puts "Try: browse, fetch, list, show, update"
|
215
|
+
puts "or call with '-h' for usage information"
|
216
|
+
end
|
217
|
+
|
173
218
|
# Show a quick reference of available commands.
|
174
219
|
def usage
|
175
220
|
puts 'Usage: git forks <command>'
|
176
221
|
puts 'Get GitHub project forks information.'
|
177
222
|
puts
|
178
223
|
puts 'Available commands:'
|
224
|
+
puts ' browse <owner>[:<ref>] Show fork in web browser.'
|
225
|
+
puts ' <ref> denotes a Git Tree or a Git Commit.'
|
226
|
+
puts ' fetch <owners> git-fetch fork data from GitHub.'
|
227
|
+
puts ' <owners> is a space separate list.'
|
228
|
+
puts ' (Forces cache update.)'
|
179
229
|
puts ' list [--reverse] List all forks.'
|
180
230
|
puts ' show <owner> Show details for a single fork.'
|
181
231
|
puts ' update Retrieve fork info from GitHub API v3.'
|
182
|
-
puts ' browse <owner>[:<ref>] Show fork in web browser.'
|
183
|
-
puts ' <ref> denotes a Git Tree or a Git Commit.'
|
184
|
-
puts
|
185
|
-
puts 'Git configurations:'
|
186
|
-
puts ' github.forks.branchpattern Only grab branches matching this Ruby Regexp.'
|
187
232
|
end
|
188
233
|
|
189
234
|
#-----------------------------------------------------------------------------
|
@@ -224,10 +269,25 @@ class GitForks
|
|
224
269
|
forks = Octokit.forks("#{@user}/#{@repo}")
|
225
270
|
end
|
226
271
|
|
227
|
-
def fetch_fork_branches(fork_user
|
228
|
-
pattern ||= @branch_pattern
|
272
|
+
def fetch_fork_branches(fork_user)
|
229
273
|
branches = Octokit.branches("#{fork_user}/#{@repo}")
|
230
|
-
|
274
|
+
end
|
275
|
+
|
276
|
+
#-----------------------------------------------------------------------------
|
277
|
+
# Git
|
278
|
+
#-----------------------------------------------------------------------------
|
279
|
+
|
280
|
+
def git(command)
|
281
|
+
`git #{command}`.chomp
|
282
|
+
end
|
283
|
+
|
284
|
+
def github_endpoint
|
285
|
+
host = git("config --get-all github.host")
|
286
|
+
if host.size > 0
|
287
|
+
host
|
288
|
+
else
|
289
|
+
'https://github.com'
|
290
|
+
end
|
231
291
|
end
|
232
292
|
|
233
293
|
#-----------------------------------------------------------------------------
|
@@ -254,12 +314,6 @@ class GitForks
|
|
254
314
|
private
|
255
315
|
#-----------------------------------------------------------------------------
|
256
316
|
|
257
|
-
def help
|
258
|
-
puts "No command: #{@command}"
|
259
|
-
puts "Try: browse, list, show, update"
|
260
|
-
puts "or call with '-h' for usage information"
|
261
|
-
end
|
262
|
-
|
263
317
|
def configure
|
264
318
|
Octokit.configure do |config|
|
265
319
|
#config.login = github_login
|
@@ -270,15 +324,6 @@ class GitForks
|
|
270
324
|
# git("config --get-all github.user")
|
271
325
|
#end
|
272
326
|
|
273
|
-
def branch_pattern
|
274
|
-
patterns = git("config --get-all github.forks.branchpattern")
|
275
|
-
if patterns.empty?
|
276
|
-
Regexp.new(/.+/) # match anything
|
277
|
-
else
|
278
|
-
Regexp.new(patterns.gsub("\n", "|"))
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
327
|
def repo_info
|
283
328
|
c = {}
|
284
329
|
config = git('config --list')
|
@@ -317,8 +362,4 @@ class GitForks
|
|
317
362
|
return nil, nil
|
318
363
|
end
|
319
364
|
|
320
|
-
def git(command)
|
321
|
-
`git #{command}`.chomp
|
322
|
-
end
|
323
|
-
|
324
365
|
end # GitForks
|