Wiki#tree_map_for returns an array of BlobEntry instances

This commit is contained in:
rick
2010-08-30 17:30:45 -07:00
parent 6f077702e1
commit 7e2b1fdbc6
6 changed files with 69 additions and 37 deletions
+1
View File
@@ -12,6 +12,7 @@ require 'gollum/ruby1.8'
# internal
require 'gollum/pagination'
require 'gollum/blob_entry'
require 'gollum/wiki'
require 'gollum/page'
require 'gollum/file'
+51
View File
@@ -0,0 +1,51 @@
module Gollum
class BlobEntry
# Gets the String SHA for this blob.
attr_reader :sha
# Gets the String full path for this blob.
attr_reader :path
def initialize(sha, path)
@sha = sha
@path = path
@dir = @name = @blob = nil
end
# Gets the normalized directory path for this blob.
def dir
@dir ||= self.class.normalize_dir(::File.dirname(@path))
end
# Gets the String file base name for this blob.
def name
@name ||= ::File.basename(@path)
end
# Gets the Grit::Blob instance for this blob.
def blob(repo)
@blob ||= Grit::Blob.create(repo, :id => @sha, :name => @name)
end
# Normalizes a given directory name for searching through tree paths.
# Ensures that a directory begins with a slash, or
#
# normalize_dir("") # => ""
# normalize_dir(".") # => ""
# normalize_dir("foo") # => "/foo"
# normalize_dir("/foo/") # => "/foo"
# normalize_dir("/") # => ""
#
# dir - String directory name.
#
# Returns a normalized String directory name, or nil if no directory
# is given.
def self.normalize_dir(dir)
if dir
dir = ::File.expand_path(dir, '/')
dir = '' if dir == '/'
end
dir
end
end
end
+2 -2
View File
@@ -49,9 +49,9 @@ module Gollum
checked = name.downcase
map = @wiki.tree_map_for(version)
sha = @wiki.ref_map[version] || version
if pair = map.detect { |(path, _)| path.downcase == checked }
if entry = map.detect { |entry| entry.path.downcase == checked }
@path = name
@blob = Grit::Blob.create(@wiki.repo, :id => pair.last, :name => ::File.basename(@path))
@blob = Grit::Blob.create(@wiki.repo, :id => entry.sha, :name => entry.name)
@version = Grit::Commit.create(@wiki.repo, :id => sha)
self
end
+5 -10
View File
@@ -264,19 +264,14 @@ module Gollum
#
# Returns a Gollum::Page or nil if the page could not be found.
def find_page_in_tree(map, name, checked_dir = nil)
if checked_dir = Wiki.normalize_directory(checked_dir)
if checked_dir = BlobEntry.normalize_dir(checked_dir)
checked_dir.downcase!
end
map.each do |(full_name, blob_sha)|
blob_name = ::File.basename(full_name)
dir = Wiki.normalize_directory(::File.dirname(full_name))
blob_in_dir = checked_dir.nil? || dir.downcase == checked_dir
if blob_in_dir && page_match(name, blob_name)
blob = Grit::Blob.create(@wiki.repo, :id => blob_sha, :name => blob_name)
return self.class.new(@wiki).populate(blob, dir)
end
map.each do |entry|
next unless checked_dir.nil? || entry.dir.downcase == checked_dir
next unless page_match(name, entry.name)
return self.class.new(@wiki).populate(entry.blob(@wiki.repo), entry.dir)
end
return nil # nothing was found
+4 -23
View File
@@ -375,7 +375,7 @@ module Gollum
#
# ref - A String ref that is either a commit SHA or references one.
#
# Returns an Array of [filename, sha] Arrays.
# Returns an Array of BlobEntry instances.
def tree_map_for(ref)
sha = @ref_map[ref] || ref
@tree_map[sha] || begin
@@ -390,7 +390,7 @@ module Gollum
#
# sha - String commit SHA.
#
# Returns an Array of [filename, sha] Arrays.
# Returns an Array of BlobEntry instances.
def parse_tree_for(sha)
tree = @repo.git.native(:ls_tree, {:r => true, :z => true}, sha)
items = []
@@ -405,12 +405,12 @@ module Gollum
# line - A String line of output:
# "100644 blob 839c2291b30495b9a882c17d08254d3c90d8fb53 Home.md"
#
# Returns an Array of [filename, sha].
# Returns an Array of BlobEntry instances.
def parse_tree_line(line)
data, name = line.split("\t")
mode, type, sha = data.split(' ')
name = decode_git_path(name)
[name, sha]
BlobEntry.new sha, name
end
# Decode octal sequences (\NNN) in tree path names.
@@ -431,25 +431,6 @@ module Gollum
def clear_cache
@ref_map = {}
@tree_map = {}
end # Normalizes a given directory name for searching through tree paths.
# Ensures that a directory begins with a slash, or
#
# normalize_directory("") # => ""
# normalize_directory(".") # => ""
# normalize_directory("foo") # => "/foo"
# normalize_directory("/foo/") # => "/foo"
# normalize_directory("/") # => ""
#
# dir - String directory name.
#
# Returns a normalized String directory name, or nil if no directory
# is given.
def self.normalize_directory(dir)
if dir
dir = ::File.expand_path(dir, '/')
dir = '' if dir == '/'
end
dir
end
end
end
+6 -2
View File
@@ -59,14 +59,18 @@ context "Wiki" do
assert @wiki.tree_map.empty?
@wiki.tree_map_for 'master'
assert_equal({"master"=>"60f12f4254f58801b9ee7db7bca5fa8aeefaa56b"}, @wiki.ref_map)
assert_equal 'Bilbo-Baggins.md', @wiki.tree_map['60f12f4254f58801b9ee7db7bca5fa8aeefaa56b'][0][0]
entry = @wiki.tree_map['60f12f4254f58801b9ee7db7bca5fa8aeefaa56b'][0]
assert_equal 'Bilbo-Baggins.md', entry.path
end
test "#tree_map_for only caches tree for commit" do
assert @wiki.tree_map.empty?
@wiki.tree_map_for '60f12f4254f58801b9ee7db7bca5fa8aeefaa56b'
assert @wiki.ref_map.empty?
assert_equal 'Bilbo-Baggins.md', @wiki.tree_map['60f12f4254f58801b9ee7db7bca5fa8aeefaa56b'][0][0]
entry = @wiki.tree_map['60f12f4254f58801b9ee7db7bca5fa8aeefaa56b'][0]
assert_equal 'Bilbo-Baggins.md', entry.path
end
end