diff --git a/lib/gollum.rb b/lib/gollum.rb index f4b82ca7..21a0c8ea 100644 --- a/lib/gollum.rb +++ b/lib/gollum.rb @@ -58,5 +58,19 @@ module Gollum 'img' => {'href' => ['http', 'https', :relative]} } } + + class Error < StandardError; end + class DuplicatePageError < Error + attr_accessor :dir + attr_accessor :existing_path + attr_accessor :attempted_path + + def initialize(dir, existing, attempted, message = nil) + @dir = dir + @existing_path = existing + @attempted_path = attempted + super(message || "Cannot write #{@dir}/#{@attempted_path}, found #{@dir}/#{@existing_path}.") + end + end end diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 49820a59..cd571314 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -315,13 +315,24 @@ module Gollum ext = @page_class.format_to_ext(format) path = @page_class.cname(name) + '.' + ext - parts = dir.split('/') + parts = dir.split('/') container = nil parts.each do |part| container = map[part] end - (container || map)[path] = normalize(data) + container ||= map + downpath = path.downcase + downpath.sub! /\.\w+$/, '' + container.keys.each do |existing| + file = existing.downcase + file.sub! /\.\w+$/, '' + if downpath == file + raise DuplicatePageError.new(dir, existing, path) + end + end + + container[path] = normalize(data) map end diff --git a/test/test_markup.rb b/test/test_markup.rb index e56d6851..184d270e 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -80,15 +80,16 @@ context "Markup" do end test "page link with custom base path" do - ["/wiki", "/wiki/"].each do |path| + ["/wiki", "/wiki/"].each_with_index do |path, i| + name = "Bilbo Baggins #{i}" @wiki = Gollum::Wiki.new(@path, :base_path => path) - @wiki.write_page("Bilbo Baggins", :markdown, "a [[Bilbo Baggins]] b", @commit) + @wiki.write_page(name, :markdown, "a [[#{name}]] b", @commit) - page = @wiki.page("Bilbo Baggins") + page = @wiki.page(name) output = page.formatted_data assert_match /class="internal present"/, output - assert_match /href="\/wiki\/Bilbo-Baggins"/, output - assert_match /\>Bilbo Baggins\Bilbo Baggins \d\a b

}, output end diff --git a/test/test_wiki.rb b/test/test_wiki.rb index 9ebb5113..f0edfd32 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -95,6 +95,16 @@ context "Wiki page writing" do assert @wiki.page("Gollum") end + test "is not allowed to overwrite file" do + commit = { :message => "Gollum page", + :name => "Tom Preston-Werner", + :email => "tom@github.com" } + @wiki.write_page("Abc-Def", :markdown, "# Gollum", commit) + assert_raises Gollum::DuplicatePageError do + @wiki.write_page("ABC DEF", :textile, "# Gollum", commit) + end + end + test "update_page" do commit = { :message => "Gollum page", :name => "Tom Preston-Werner",