6 def get_paths_by_type(dir, file_re, recursive = true)
7 get_file_paths(dir, recursive).select { |path| path =~ file_re }
10 def get_file_paths(dir, recursive = true)
11 get_tree(get_tree_sha(dir), recursive)['tree'].select { |f| f['type'] == 'blob' }.map { |f| f['path'] }
14 def read_files(path, files)
15 full_path = "https://raw.githubusercontent.com/#@repo/#@branch_sha/#{path}"
16 contents = read_cached_files(path, files)
17 log_http_get_files contents.keys, full_path, true if contents.keys
18 files -= contents.keys
19 log_http_get_files files, full_path, false
22 contents[name] = open("#{full_path}/#{name}").read
23 Thread.exclusive { write_cached_files path, name => contents[name] }
29 def read_cached_files(path, files)
30 full_path = "#@cache_path/#@branch_sha/#{path}"
32 if File.directory?(full_path)
34 path = "#{full_path}/#{name}"
35 contents[name] = File.read(path, mode: 'rb') if File.exists?(path)
41 def write_cached_files(path, files)
42 full_path = "./#@cache_path/#@branch_sha/#{path}"
43 files.each do |name, content|
44 FileUtils.mkdir_p File.dirname(File.join(full_path, name))
45 File.open("#{full_path}/#{name}", 'wb') { |f| f.write content }
52 cache_path = "./#@cache_path#{uri.path}#{uri.query.tr('?&=', '-') if uri.query}"
53 FileUtils.mkdir_p File.dirname(cache_path)
54 if File.exists?(cache_path)
55 log_http_get_file url, true
56 File.read(cache_path, mode: 'rb')
58 log_http_get_file url, false
59 content = open(url).read
60 File.open(cache_path, 'wb') { |f| f.write content }
65 # get sha of the branch (= the latest commit)
68 if @branch + "\n" == %x[git rev-parse #@branch]
71 cmd = "git ls-remote #{Shellwords.escape "https://github.com/#@repo"} #@branch"
74 raise 'Could not get branch sha!' unless $?.success? && !result.empty?
75 result.split(/\s+/).first
80 # Get the sha of a dir
81 def get_tree_sha(dir, tree = get_trees)
82 tree['tree'].find { |t| t['path'] == dir }['sha']
86 @trees ||= get_tree(@branch_sha)
89 def get_tree(sha, recursive = true)
90 get_json("https://api.github.com/repos/#@repo/git/trees/#{sha}#{'?recursive=1' if recursive}")
94 JSON.parse get_file(url)