fix index clobbering and tighten some stuff up

This commit is contained in:
Tom Preston-Werner
2010-04-19 10:50:40 -07:00
parent 2b20054d5c
commit 5f037e5e47
6 changed files with 107 additions and 16 deletions
+8 -1
View File
@@ -10,11 +10,18 @@ module Gollum
@blob = nil
end
# Public: The on-disk filename of the file.
#
# Returns the String name.
def name
@blob && @blob.name
end
# Public: The raw contents of the page.
#
# Returns the String data.
def raw_data
@blob.data rescue nil
@blob && @blob.data
end
# Public: The Grit::Commit version of the file.
+11 -1
View File
@@ -8,8 +8,10 @@ module Gollum
#
# Returns a new Gollum::Markup object, ready for rendering.
def initialize(page)
@wiki = page.wiki
@name = page.name
@data = page.raw_data
@version = page.version.id
@tagmap = {}
end
@@ -74,7 +76,15 @@ module Gollum
parts = tag.split('|')
name = parts[0].strip
nil
if name =~ /^\//
if file = @wiki.file(name[1..-1], @version)
%{<img src="#{file.name}" />}
else
nil
end
else
nil
end
end
# Attempt to process the tag as a file link tag.
+8 -3
View File
@@ -16,7 +16,7 @@ module Gollum
#
# Returns the String name.
def name
@blob.name rescue nil
@blob && @blob.name
end
# Public: The path of the page within the repo.
@@ -28,14 +28,14 @@ module Gollum
#
# Returns the String data.
def raw_data
@blob.data rescue nil
@blob && @blob.data
end
# Public: The formatted contents of the page.
#
# Returns the String data.
def formatted_data
Gollum::Markup.new(self).render rescue nil
@blob && Gollum::Markup.new(self).render
end
# Public: The format of the page.
@@ -107,6 +107,11 @@ module Gollum
#
#########################################################################
# The underlying wiki repo.
#
# Returns the Gollum::Wiki containing the page.
attr_reader :wiki
# Set the Grit::Commit version of the page.
#
# Returns nothing.
+57 -7
View File
@@ -43,11 +43,17 @@ module Gollum
def write_page(name, format, data, commit = {})
ext = Page.format_to_ext(format)
path = Gollum.canonical_name(name) + '.' + ext
index = @repo.index
index.add(path, data)
map = {}
if pcommit = @repo.commit('master')
map = tree_map(pcommit.tree)
end
map[path] = data
index = tree_map_to_index(map)
parents = pcommit ? [pcommit] : []
actor = Grit::Actor.new(commit[:name], commit[:email])
parent = @repo.commit('master')
index.commit(commit[:message], [parent], actor)
index.commit(commit[:message], parents, actor)
end
# Public: Update an existing page with new content. The location of the
@@ -62,11 +68,13 @@ module Gollum
#
# Returns the String SHA1 of the newly written version.
def update_page(page, data, commit = {})
index = @repo.index
pcommit = @repo.commit('master')
map = tree_map(pcommit.tree)
index = tree_map_to_index(map)
index.add(page.path, data)
actor = Grit::Actor.new(commit[:name], commit[:email])
parent = @repo.commit('master')
index.commit(commit[:message], [parent], actor)
index.commit(commit[:message], [pcommit], actor)
end
#########################################################################
@@ -84,5 +92,47 @@ module Gollum
#
# Returns the String path.
attr_reader :path
# Fill an index with the existing state of the repository.
#
# tree - The Grit::Tree to start with.
#
# Returns a nested Hash of filename to content mappings.
def tree_map(tree)
map = {}
tree.contents.each do |item|
case item
when Grit::Blob
map[item.name] = item.data
when Grit::Tree
map[item.name] = tree_map(item)
end
end
map
end
# Use a treemap to fill in the index.
#
# map - The Hash map:
# key - The String directory or filename.
# val - The Hash submap or the String contents of the file.
# index - The Grit::Index to use. Leave blank when calling from outside
# this method (default: nil).
#
# Returns the Grit::Index.
def tree_map_to_index(map, prefix = '', index = nil)
index ||= @repo.index
map.each do |k, v|
case k
when String
name = [prefix, k].join('/')[1..-1]
index.add(k, v)
when Hash
new_prefix = [prefix, k].join('/')[1..-1]
tree_map_to_index(v, new_prefix, index)
end
end
index
end
end
end
+17 -4
View File
@@ -7,10 +7,9 @@ context "Markup" do
Grit::Repo.init_bare(@path)
@wiki = Gollum::Wiki.new(@path)
commit = { :message => "Bilbo page",
:name => "Tom Preston-Werner",
:email => "tom@github.com" }
@wiki.write_page("Bilbo Baggins", :markdown, "a [[Bilbo Baggins]] b", commit)
@commit = { :message => "Add stuff",
:name => "Tom Preston-Werner",
:email => "tom@github.com" }
end
teardown do
@@ -18,8 +17,22 @@ context "Markup" do
end
test "page link" do
@wiki.write_page("Bilbo Baggins", :markdown, "a [[Bilbo Baggins]] b", @commit)
page = @wiki.page("Bilbo Baggins")
output = Gollum::Markup.new(page).render
assert_equal %{<p>a <a href="Bilbo-Baggins">Bilbo Baggins</a> b</p>\n}, output
end
test "image" do
index = @wiki.repo.index
index.add("alpha.jpg", "hi")
index.commit("Add alpha.jpg")
@wiki.write_page("Bilbo Baggins", :markdown, "a [[/alpha.jpg]] b", @commit)
page = @wiki.page("Bilbo Baggins")
output = Gollum::Markup.new(page).render
assert_equal %{<p>a <img src="alpha.jpg" /> b</p>\n}, output
end
end
+6
View File
@@ -31,6 +31,12 @@ context "Wiki page writing" do
assert_equal "Gollum page", @wiki.repo.commits.first.message
assert_equal "Tom Preston-Werner", @wiki.repo.commits.first.author.name
assert_equal "tom@github.com", @wiki.repo.commits.first.author.email
assert @wiki.page("Gollum")
@wiki.write_page("Bilbo", :markdown, "# Bilbo", commit)
assert_equal 2, @wiki.repo.commits.size
assert @wiki.page("Bilbo")
assert @wiki.page("Gollum")
end
test "update_page" do