From 5f037e5e475680b617865317e195804a606c8924 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Mon, 19 Apr 2010 10:50:40 -0700 Subject: [PATCH] fix index clobbering and tighten some stuff up --- lib/gollum/file.rb | 9 ++++++- lib/gollum/markup.rb | 12 ++++++++- lib/gollum/page.rb | 11 +++++--- lib/gollum/wiki.rb | 64 +++++++++++++++++++++++++++++++++++++++----- test/test_markup.rb | 21 ++++++++++++--- test/test_wiki.rb | 6 +++++ 6 files changed, 107 insertions(+), 16 deletions(-) diff --git a/lib/gollum/file.rb b/lib/gollum/file.rb index 30e1acda..03c1700d 100644 --- a/lib/gollum/file.rb +++ b/lib/gollum/file.rb @@ -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. diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index d9c62b67..4ada4b5e 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -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) + %{} + else + nil + end + else + nil + end end # Attempt to process the tag as a file link tag. diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 96717f22..943618f7 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -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. diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 822a18d1..6c7cca90 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -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 \ No newline at end of file diff --git a/test/test_markup.rb b/test/test_markup.rb index c208d841..08134359 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -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 %{

a Bilbo Baggins b

\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 %{

a b

\n}, output + end end diff --git a/test/test_wiki.rb b/test/test_wiki.rb index 892d0038..8fbb2b70 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -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