integrate Gollum::GitAccess

This commit is contained in:
rick
2010-10-11 12:06:35 -07:00
parent 62c4b795be
commit ebf7855c09
6 changed files with 151 additions and 114 deletions
+7 -3
View File
@@ -6,10 +6,13 @@ module Gollum
# Gets the String full path for this blob.
attr_reader :path
def initialize(sha, path)
attr_reader :size
def initialize(sha, path, size = nil)
@sha = sha
@path = path
@dir = @name = @blob = nil
@size = size
@dir = @name = @blob = nil
end
# Gets the normalized directory path for this blob.
@@ -28,7 +31,8 @@ module Gollum
#
# Returns an unbaked Grit::Blob instance.
def blob(repo)
@blob ||= Grit::Blob.create(repo, :id => @sha, :name => @name)
@blob ||= Grit::Blob.create(repo,
:id => @sha, :name => name, :size => @size)
end
# Gets a Page instance for this blob.
+2 -3
View File
@@ -53,11 +53,10 @@ module Gollum
def find(name, version)
checked = name.downcase
map = @wiki.tree_map_for(version)
sha = @wiki.ref_map[version] || version
if entry = map.detect { |entry| entry.path.downcase == checked }
@path = name
@blob = Grit::Blob.create(@wiki.repo, :id => entry.sha, :name => entry.name)
@version = Grit::Commit.create(@wiki.repo, :id => sha)
@blob = entry.blob(@wiki.repo)
@version = @wiki.commit_for(version)
self
end
end
+94 -4
View File
@@ -2,18 +2,108 @@ module Gollum
class GitAccess
attr_reader :path
attr_reader :repo
# Gets a Hash cache of refs to commit SHAs.
#
# {"master" => "abc123", ...}
#
# Returns the Hash cache.
attr_reader :ref_map
# Gets a Hash cache of commit SHAs to a recursive tree of blobs.
#
# {"abc123" => [["lib/foo.rb", "blob-sha"], [file, sha], ...], ...}
#
# Returns the Hash cache.
attr_reader :tree_map
attr_reader :commit_map
def initialize(path)
@path = path
@repo = Grit::Repo.new(path)
@ref_map = {}
@tree_map = {}
@path = path
@repo = Grit::Repo.new(path)
clear
end
def clear
@ref_map = {}
@tree_map = {}
@commit_map = {}
end
def refresh
@ref_map.clear
end
def exist?
@repo.git.exist?
end
def ref_to_sha(ref)
@ref_map[ref] ||= ref_to_sha!(ref)
end
def tree(ref)
sha = ref_to_sha(ref)
@tree_map[sha] ||= tree!(sha)
end
def blob(sha)
cat_file!(sha)
end
def commit(ref)
if sha = @ref_map[ref]
@commit_map[sha] ||= commit!(sha)
else
cm = commit!(ref)
@ref_map[ref] = cm.id
@commit_map[cm.id] = cm
end
end
def ref_to_sha!(ref)
@repo.git.rev_list({:max_count=>1}, ref)
end
def tree!(sha)
tree = @repo.git.native(:ls_tree,
{:r => true, :l => true, :z => true}, sha)
tree.split("\0").inject([]) do |items, line|
items << parse_tree_line(line)
end
end
def cat_file!(sha)
@repo.git.cat_file({:p => true}, sha)
end
def commit!(sha)
@repo.commit(sha)
end
# Parses a line of output from the `ls-tree` command.
#
# line - A String line of output:
# "100644 blob 839c2291b30495b9a882c17d08254d3c90d8fb53 Home.md"
#
# Returns an Array of BlobEntry instances.
def parse_tree_line(line)
mode, type, sha, size, *name = line.split(/\s+/)
BlobEntry.new(sha, name.to_s, size.to_i)
end
# Decode octal sequences (\NNN) in tree path names.
#
# path - String path name.
#
# Returns a decoded String.
def decode_git_path(path)
if path[0] == ?" && path[-1] == ?"
path = path[1...-1]
path.gsub!(/\\\d{3}/) { |m| m[1..-1].to_i(8).chr }
end
path.gsub!(/\\[rn"\\]/) { |m| eval(%("#{m.to_s}")) }
path
end
end
end
+2 -3
View File
@@ -272,9 +272,8 @@ module Gollum
def find(name, version)
map = @wiki.tree_map_for(version)
if page = find_page_in_tree(map, name)
sha = @wiki.ref_map[version] || version
page.version = Grit::Commit.create(@wiki.repo, :id => sha)
page.historical = sha == version
page.version = @wiki.commit_for(version)
page.historical = page.version.id == version
page
end
rescue Grit::GitRuby::Repository::NoSuchShaFound
+21 -77
View File
@@ -58,13 +58,16 @@ module Gollum
#
# Returns a fresh Gollum::Repo.
def initialize(path, options = {})
if path.is_a?(GitAccess)
options[:access] = path
path = path.path
end
@path = path
@access = options[:access] || GitAccess.new(path)
@repo = @access.repo
@access = options[:access] || GitAccess.new(path)
@base_path = options[:base_path] || "/"
@page_class = options[:page_class] || self.class.page_class
@file_class = options[:file_class] || self.class.file_class
clear_cache
@repo = @access.repo
end
# Public: check whether the wiki's git repo exists on the filesystem.
@@ -109,7 +112,7 @@ module Gollum
path = @page_class.cname(name) + '.' + ext
blob = OpenStruct.new(:name => path, :data => data)
page.populate(blob, path)
page.version = self.repo.commit("HEAD")
page.version = @access.commit('HEAD')
page
end
@@ -138,7 +141,7 @@ module Gollum
actor = Grit::Actor.new(commit[:name], commit[:email])
sha1 = index.commit(commit[:message], parents, actor)
@ref_map.clear
@access.refresh
update_working_dir(index, '', name, format)
sha1
@@ -181,7 +184,7 @@ module Gollum
actor = Grit::Actor.new(commit[:name], commit[:email])
sha1 = index.commit(commit[:message], [pcommit], actor)
@ref_map.clear
@access.refresh
update_working_dir(index, dir, page.name, page.format)
update_working_dir(index, dir, name, format)
@@ -210,7 +213,7 @@ module Gollum
actor = Grit::Actor.new(commit[:name], commit[:email])
sha1 = index.commit(commit[:message], [pcommit], actor)
@ref_map.clear
@access.refresh
update_working_dir(index, dir, page.name, page.format)
sha1
@@ -267,6 +270,10 @@ module Gollum
@repo.log('master', nil, log_pagination_options(options))
end
def clear_cache
@access.refresh
end
#########################################################################
#
# Internal Methods
@@ -283,20 +290,6 @@ module Gollum
# Returns the String path.
attr_reader :path
# Gets a Hash cache of refs to commit SHAs.
#
# {"master" => "abc123", ...}
#
# Returns the Hash cache.
attr_reader :ref_map
# Gets a Hash cache of commit SHAs to a recursive tree of blobs.
#
# {"abc123" => [["lib/foo.rb", "blob-sha"], [file, sha], ...], ...}
#
# Returns the Hash cache.
attr_reader :tree_map
# Gets the page class used by all instances of this Wiki.
attr_reader :page_class
@@ -359,8 +352,7 @@ module Gollum
def tree_list(ref)
tree_map_for(ref).inject([]) do |list, entry|
next list unless @page_class.valid_page_name?(entry.name)
sha = ref_map[ref]
list << entry.page(self, @repo.commit(sha))
list << entry.page(self, @access.commit(ref))
end
end
@@ -479,6 +471,11 @@ module Gollum
@repo.config['user.email'] || self.class.default_committer_email
end
def commit_for(ref)
@access.commit(ref)
rescue Grit::GitRuby::Repository::NoSuchShaFound
end
# Finds a full listing of files and their blob SHA for a given ref. Each
# listing is cached based on its actual commit SHA.
#
@@ -486,62 +483,9 @@ module Gollum
#
# Returns an Array of BlobEntry instances.
def tree_map_for(ref)
sha = @ref_map[ref] || ref
@tree_map[sha] || begin
real_sha = @repo.git.rev_list({:max_count=>1}, ref)
@ref_map[ref] = real_sha if real_sha != ref
@tree_map[real_sha] ||= parse_tree_for(real_sha)
end
@access.tree(ref)
rescue Grit::GitRuby::Repository::NoSuchShaFound
[]
end
# Finds the full listing of files and their blob SHA for a given commit
# SHA. No caching or ref lookups are performed.
#
# sha - String commit SHA.
#
# Returns an Array of BlobEntry instances.
def parse_tree_for(sha)
tree = @repo.git.native(:ls_tree, {:r => true, :z => true}, sha)
items = []
tree.split("\0").each do |line|
items << parse_tree_line(line)
end
items
end
# Parses a line of output from the `ls-tree` command.
#
# line - A String line of output:
# "100644 blob 839c2291b30495b9a882c17d08254d3c90d8fb53 Home.md"
#
# 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)
BlobEntry.new(sha, name)
end
# Decode octal sequences (\NNN) in tree path names.
#
# path - String path name.
#
# Returns a decoded String.
def decode_git_path(path)
if path[0] == ?" && path[-1] == ?"
path = path[1...-1]
path.gsub!(/\\\d{3}/) { |m| m[1..-1].to_i(8).chr }
end
path.gsub!(/\\[rn"\\]/) { |m| eval(%("#{m.to_s}")) }
path
end
# Resets the ref and tree caches for this wiki.
def clear_cache
@ref_map = {}
@tree_map = {}
end
end
end
+25 -24
View File
@@ -2,7 +2,8 @@ require File.join(File.dirname(__FILE__), *%w[helper])
context "Wiki" do
setup do
@wiki = Gollum::Wiki.new(testpath("examples/lotr.git"))
@access = Gollum::GitAccess.new(testpath("examples/lotr.git"))
@wiki = Gollum::Wiki.new(@access)
end
test "repo path" do
@@ -60,29 +61,29 @@ context "Wiki" do
@wiki.normalize_commit(commit.dup))
end
test "#tree_map_for caches ref and tree" do
assert @wiki.ref_map.empty?
assert @wiki.tree_map.empty?
@wiki.tree_map_for 'master'
assert_equal({"master"=>"60f12f4254f58801b9ee7db7bca5fa8aeefaa56b"}, @wiki.ref_map)
map = @wiki.tree_map['60f12f4254f58801b9ee7db7bca5fa8aeefaa56b']
assert_equal 'Bilbo-Baggins.md', map[0].path
assert_equal '', map[0].dir
assert_equal map[0].path, map[0].name
assert_equal 'Mordor/Eye-Of-Sauron.md', map[3].path
assert_equal '/Mordor', map[3].dir
assert_equal 'Eye-Of-Sauron.md', map[3].name
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?
entry = @wiki.tree_map['60f12f4254f58801b9ee7db7bca5fa8aeefaa56b'][0]
assert_equal 'Bilbo-Baggins.md', entry.path
end
#test "#tree_map_for caches ref and tree" do
# assert @wiki.ref_map.empty?
# assert @wiki.tree_map.empty?
# @wiki.tree_map_for 'master'
# assert_equal({"master"=>"60f12f4254f58801b9ee7db7bca5fa8aeefaa56b"}, @wiki.ref_map)
#
# map = @wiki.tree_map['60f12f4254f58801b9ee7db7bca5fa8aeefaa56b']
# assert_equal 'Bilbo-Baggins.md', map[0].path
# assert_equal '', map[0].dir
# assert_equal map[0].path, map[0].name
# assert_equal 'Mordor/Eye-Of-Sauron.md', map[3].path
# assert_equal '/Mordor', map[3].dir
# assert_equal 'Eye-Of-Sauron.md', map[3].name
#end
#
#test "#tree_map_for only caches tree for commit" do
# assert @access.tree_map.empty?
# @access.tree '60f12f4254f58801b9ee7db7bca5fa8aeefaa56b'
# assert @access.ref_map.empty?
#
# entry = @access.tree_map['60f12f4254f58801b9ee7db7bca5fa8aeefaa56b'][0]
# assert_equal 'Bilbo-Baggins.md', entry.path
#end
end
context "Wiki page previewing" do