From 63099ec5c088a0d3f801a264c63f49433d610782 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Tue, 20 Apr 2010 18:19:54 -0700 Subject: [PATCH] delete_page --- lib/gollum/wiki.rb | 27 +++++++++++++++++++++++++++ test/test_wiki.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 7ecbca77..a850f4d2 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -77,6 +77,33 @@ module Gollum index.commit(commit[:message], [pcommit], actor) end + # Public: Delete a page. + # + # page - The Gollum::Page to delete. + # commit - The commit Hash details: + # :message - The String commit message. + # :author - The String author full name. + # :email - The String email address. + # + # Returns the String SHA1 of the newly written version. + def delete_page(page, commit) + pcommit = @repo.commit('master') + map = tree_map(pcommit.tree) + + parts = page.path.split('/') + name = parts.pop + container = nil + parts.each do |part| + container = map[part] + end + (container || map).delete(name) + + index = tree_map_to_index(map) + + actor = Grit::Actor.new(commit[:name], commit[:email]) + index.commit(commit[:message], [pcommit], actor) + end + ######################################################################### # # Internal Methods diff --git a/test/test_wiki.rb b/test/test_wiki.rb index 8fbb2b70..c5e6982c 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -55,6 +55,39 @@ context "Wiki page writing" do assert_equal "tom@github.com", @wiki.repo.commits.first.author.email end + test "delete root page" do + commit = { :message => "Gollum page", + :name => "Tom Preston-Werner", + :email => "tom@github.com" } + @wiki.write_page("Gollum", :markdown, "# Gollum", commit) + + page = @wiki.page("Gollum") + @wiki.delete_page(page, commit) + + assert_equal 2, @wiki.repo.commits.size + assert_nil @wiki.page("Gollum") + end + + test "delete nested page" do + commit = { :message => "Gollum page", + :name => "Tom Preston-Werner", + :email => "tom@github.com" } + + index = @wiki.repo.index + index.add("greek/Bilbo-Baggins.md", "hi") + index.add("Gollum.md", "hi") + index.commit("Add alpha.jpg") + + page = @wiki.page("Bilbo-Baggins") + assert page + @wiki.delete_page(page, commit) + + assert_equal 2, @wiki.repo.commits.size + assert_nil @wiki.page("Bilbo-Baggins") + + assert @wiki.page("Gollum") + end + teardown do FileUtils.rm_r(File.join(File.dirname(__FILE__), *%w[examples test.git])) end