From 7e2b1fdbc62c74ef47b33fcff67a8789273127b0 Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 30 Aug 2010 17:30:45 -0700 Subject: [PATCH] Wiki#tree_map_for returns an array of BlobEntry instances --- lib/gollum.rb | 1 + lib/gollum/blob_entry.rb | 51 ++++++++++++++++++++++++++++++++++++++++ lib/gollum/file.rb | 4 ++-- lib/gollum/page.rb | 15 ++++-------- lib/gollum/wiki.rb | 27 ++++----------------- test/test_wiki.rb | 8 +++++-- 6 files changed, 69 insertions(+), 37 deletions(-) create mode 100644 lib/gollum/blob_entry.rb diff --git a/lib/gollum.rb b/lib/gollum.rb index 21a0c8ea..68e96fff 100644 --- a/lib/gollum.rb +++ b/lib/gollum.rb @@ -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' diff --git a/lib/gollum/blob_entry.rb b/lib/gollum/blob_entry.rb new file mode 100644 index 00000000..23c92084 --- /dev/null +++ b/lib/gollum/blob_entry.rb @@ -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 \ No newline at end of file diff --git a/lib/gollum/file.rb b/lib/gollum/file.rb index 8dff7e47..0f1f58fd 100644 --- a/lib/gollum/file.rb +++ b/lib/gollum/file.rb @@ -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 diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index f0948987..7ac45bdb 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -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 diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index eb8f1bef..af92cbba 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -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 diff --git a/test/test_wiki.rb b/test/test_wiki.rb index 7b45ea53..a7d065f1 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -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