prevent Gollum::Wiki instances from creating new pages that overwrite pages with the same name.
This commit is contained in:
@@ -58,5 +58,19 @@ module Gollum
|
|||||||
'img' => {'href' => ['http', 'https', :relative]}
|
'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
|
end
|
||||||
|
|
||||||
|
|||||||
+13
-2
@@ -315,13 +315,24 @@ module Gollum
|
|||||||
ext = @page_class.format_to_ext(format)
|
ext = @page_class.format_to_ext(format)
|
||||||
path = @page_class.cname(name) + '.' + ext
|
path = @page_class.cname(name) + '.' + ext
|
||||||
|
|
||||||
parts = dir.split('/')
|
parts = dir.split('/')
|
||||||
container = nil
|
container = nil
|
||||||
parts.each do |part|
|
parts.each do |part|
|
||||||
container = map[part]
|
container = map[part]
|
||||||
end
|
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
|
map
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+9
-7
@@ -80,15 +80,16 @@ context "Markup" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "page link with custom base path" do
|
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 = 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
|
output = page.formatted_data
|
||||||
assert_match /class="internal present"/, output
|
assert_match /class="internal present"/, output
|
||||||
assert_match /href="\/wiki\/Bilbo-Baggins"/, output
|
assert_match /href="\/wiki\/Bilbo-Baggins-\d"/, output
|
||||||
assert_match /\>Bilbo Baggins\</, output
|
assert_match /\>Bilbo Baggins \d\</, output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -101,9 +102,10 @@ context "Markup" do
|
|||||||
|
|
||||||
test "image with http url" do
|
test "image with http url" do
|
||||||
['http', 'https'].each do |scheme|
|
['http', 'https'].each do |scheme|
|
||||||
@wiki.write_page("Bilbo Baggins", :markdown, "a [[#{scheme}://example.com/bilbo.jpg]] b", @commit)
|
name = "Bilbo Baggins #{scheme}"
|
||||||
|
@wiki.write_page(name, :markdown, "a [[#{scheme}://example.com/bilbo.jpg]] b", @commit)
|
||||||
|
|
||||||
page = @wiki.page("Bilbo Baggins")
|
page = @wiki.page(name)
|
||||||
output = page.formatted_data
|
output = page.formatted_data
|
||||||
assert_equal %{<p>a <img src="#{scheme}://example.com/bilbo.jpg" /> b</p>}, output
|
assert_equal %{<p>a <img src="#{scheme}://example.com/bilbo.jpg" /> b</p>}, output
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -95,6 +95,16 @@ context "Wiki page writing" do
|
|||||||
assert @wiki.page("Gollum")
|
assert @wiki.page("Gollum")
|
||||||
end
|
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
|
test "update_page" do
|
||||||
commit = { :message => "Gollum page",
|
commit = { :message => "Gollum page",
|
||||||
:name => "Tom Preston-Werner",
|
:name => "Tom Preston-Werner",
|
||||||
|
|||||||
Reference in New Issue
Block a user