-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance by supporting repo.lstree with a specific path #457
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,9 +48,9 @@ def ref_to_sha(ref) | |
# ref - A String Git reference or Git SHA to a commit. | ||
# | ||
# Returns an Array of BlobEntry instances. | ||
def tree(ref) | ||
def tree(ref, path = '/', recursive = true) | ||
if (sha = ref_to_sha(ref)) | ||
get_cache(:tree, sha) { tree!(sha) } | ||
get_cache(:tree, "#{sha}#{path}") { tree!(sha, path, recursive) } | ||
else | ||
[] | ||
end | ||
|
@@ -156,16 +156,19 @@ def ref_to_sha!(ref) | |
# sha - String commit SHA. | ||
# | ||
# Returns an Array of BlobEntry instances. | ||
def tree!(sha) | ||
tree = @repo.lstree(sha, { :recursive => true }) | ||
items = [] | ||
tree.each do |entry| | ||
if entry[:type] == 'blob' | ||
next if @page_file_dir && !entry[:path].start_with?("#{@page_file_dir}/") | ||
items << BlobEntry.new(entry[:sha], entry[:path], entry[:size], entry[:mode]) | ||
def tree!(sha, path = '/', recursive = true) | ||
tree = @repo.lstree(sha, (Pathname.new(@page_file_dir.to_s) + path).to_s, recursive: recursive ) | ||
tree.reduce([]) do |accumulator, entry| | ||
if !@page_file_dir || entry[:path].start_with?("#{@page_file_dir}/") # guard against directory traversal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this line is needed, if we're already passing |
||
accumulator << case entry[:type] | ||
when 'blob' | ||
BlobEntry.new(entry[:sha], entry[:path], entry[:size], entry[:mode]) | ||
when 'tree' | ||
TreeEntry.new(entry[:sha], entry[:path]) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also opt to return blobs and trees in separate Arrays, so callers who need only the blobs can ignore the trees. Either way, I guess initializing the TreeEntries has minor performance impact when looping into the entire wiki. |
||
accumulator | ||
end | ||
items | ||
end | ||
|
||
# Reads the content from the Git db at the given SHA. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# ~*~ encoding: utf-8 ~*~ | ||
module Gollum | ||
# Represents a Tree (directory) that can be contained in another Tree. | ||
# We do not really care about any information other than the Tree's path and name: | ||
# If we want to know a Tree `foo`'s'contents, this can be achieved by e.g. calling wiki.path_list(foo.path) | ||
class TreeEntry | ||
attr_reader :sha | ||
attr_reader :path | ||
|
||
def initialize(sha, path) | ||
@sha = sha | ||
@path = path | ||
@name = ::File.basename(@path) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache key currently doesn't differentiate between
recursive
being true or false.