From 2e328e6a83a80b7c1e2626557cfd85816375cd3e Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Sun, 11 Apr 2010 13:08:16 -0600 Subject: [PATCH] implement File finding --- lib/gollum/file.rb | 25 +++++++++++++++++- lib/gollum/wiki.rb | 7 ++--- .../0a/de1e2916346d4c1f2fb63b863fd3c16808fe44 | Bin 0 -> 40 bytes .../0e/ea197b933bd98373114d59c7e49728741af3f9 | Bin 0 -> 93 bytes .../bf/b7c7a5cde53272a1d202e08bdef4058de85133 | Bin 0 -> 158 bytes .../f0/1428b3138994aab19d5f880b6f37336ddf1f24 | Bin 0 -> 166 bytes test/examples/lotr.git/refs/heads/master | 2 +- test/test_file.rb | 6 +++++ test/test_page.rb | 2 +- 9 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 test/examples/lotr.git/objects/0a/de1e2916346d4c1f2fb63b863fd3c16808fe44 create mode 100644 test/examples/lotr.git/objects/0e/ea197b933bd98373114d59c7e49728741af3f9 create mode 100644 test/examples/lotr.git/objects/bf/b7c7a5cde53272a1d202e08bdef4058de85133 create mode 100644 test/examples/lotr.git/objects/f0/1428b3138994aab19d5f880b6f37336ddf1f24 diff --git a/lib/gollum/file.rb b/lib/gollum/file.rb index da244d63..d641d720 100644 --- a/lib/gollum/file.rb +++ b/lib/gollum/file.rb @@ -1,6 +1,6 @@ module Gollum class File - attr_accessor :wiki + attr_accessor :wiki, :blob # Initialize a file. # @@ -10,5 +10,28 @@ module Gollum def initialize(wiki) self.wiki = wiki end + + # The raw contents of the page. + # + # Returns the String data. + def raw_data + self.blob.data rescue nil + end + + # Find a file in the given Gollum repo. + # + # name - The full String path. + # version - The String version ID to find. + # + # Returns a Gollum::File or nil if the file could not be found. + def find(name, version) + commit = self.wiki.repo.commit(version) + if blob = commit.tree / name + self.blob = blob + self + else + nil + end + end end end \ No newline at end of file diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index b13e6cb2..2e8f8583 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -24,11 +24,12 @@ module Gollum # Get the static file for a given name. # - # name - The full String pathname to the file. + # name - The full String pathname to the file. + # version - The String version ID to find (default: "master"). # # Returns a Gollum::File or nil if no matching file was found. - def file(name) - File.new(self).find(name) + def file(name, version = 'master') + File.new(self).find(name, version) end end end \ No newline at end of file diff --git a/test/examples/lotr.git/objects/0a/de1e2916346d4c1f2fb63b863fd3c16808fe44 b/test/examples/lotr.git/objects/0a/de1e2916346d4c1f2fb63b863fd3c16808fe44 new file mode 100644 index 0000000000000000000000000000000000000000..a2627090a8477d6a83137a8c0fab862ecfa0486c GIT binary patch literal 40 wcmb>05(_-od5s; literal 0 HcmV?d00001 diff --git a/test/examples/lotr.git/objects/0e/ea197b933bd98373114d59c7e49728741af3f9 b/test/examples/lotr.git/objects/0e/ea197b933bd98373114d59c7e49728741af3f9 new file mode 100644 index 0000000000000000000000000000000000000000..0ee8820b2b0cb6516570f12c0f82ea33123da083 GIT binary patch literal 93 zcmV-j0HXhR0V^p=O;xZkU@$Z=Ff%bxaIH+$^-t3cPAo0T&(q6IVc>hUO)JuXEo}>@ z&8PDVrycxvm#+}2wj@6#U$3O1gn{dxoTivbu8+L_HtRO~%Lg+!{<#1E7F-`0Y@I4v literal 0 HcmV?d00001 diff --git a/test/examples/lotr.git/objects/bf/b7c7a5cde53272a1d202e08bdef4058de85133 b/test/examples/lotr.git/objects/bf/b7c7a5cde53272a1d202e08bdef4058de85133 new file mode 100644 index 0000000000000000000000000000000000000000..e66b57688cc0dafa202d9859162a7c2d8057c714 GIT binary patch literal 158 zcmV;P0Ac@l0V^p=O;s>7HDoX}FfcPQQEe>(?DaW5-R-VmJwJwPziF(P!WekV6*v;pdmd(*u#W?AS+4dDJA$qJP20)NPYeZH6-3GKfffk2xQfpTSlz=9=a;5+PZwVLGdIu M@5z1503_==wn;HhIRF3v literal 0 HcmV?d00001 diff --git a/test/examples/lotr.git/objects/f0/1428b3138994aab19d5f880b6f37336ddf1f24 b/test/examples/lotr.git/objects/f0/1428b3138994aab19d5f880b6f37336ddf1f24 new file mode 100644 index 0000000000000000000000000000000000000000..86aa8a93198551ff831cf45ac6770a9ecd1a1075 GIT binary patch literal 166 zcmV;X09pTd0iBLPN(3S#!i=~OSl5!5;wDfQoOw81FltGnxE}U<1Ly?W7 zw%BiDuiQEZdiaMec(VzwOMl#s%e$^a7d*So`?N2cY}an{1rpX26P7)}CGIhXlb)A; U%Kz&FiGZ8f)qg$w0Xc?Cq$2`S`v3p{ literal 0 HcmV?d00001 diff --git a/test/examples/lotr.git/refs/heads/master b/test/examples/lotr.git/refs/heads/master index d3d48f48..64d557b8 100644 --- a/test/examples/lotr.git/refs/heads/master +++ b/test/examples/lotr.git/refs/heads/master @@ -1 +1 @@ -fbabba862dfa7ac35b39042dd4ad780c9f67b8cb +f01428b3138994aab19d5f880b6f37336ddf1f24 diff --git a/test/test_file.rb b/test/test_file.rb index 47b9c97a..8b96e5e7 100644 --- a/test/test_file.rb +++ b/test/test_file.rb @@ -7,5 +7,11 @@ context "File" do test "new file" do file = Gollum::File.new(@wiki) + assert_nil file.raw_data + end + + test "existing file" do + file = @wiki.file("Mordor/todo.txt") + assert_equal "[ ] Write section on Ents\n", file.raw_data end end \ No newline at end of file diff --git a/test/test_page.rb b/test/test_page.rb index da398ef6..9b2693d6 100644 --- a/test/test_page.rb +++ b/test/test_page.rb @@ -18,7 +18,7 @@ context "Page" do assert page.formatted_data =~ /

Bilbo Baggins<\/h1>\n\n

Bilbo Baggins/ assert_equal 'Bilbo-Baggins.md', page.path assert_equal :markdown, page.format - assert_equal 'fbabba862dfa7ac35b39042dd4ad780c9f67b8cb', page.version.id + assert_equal @wiki.repo.commits.first.id, page.version.id end test "get nested page" do