diff --git a/lib/gollum/blob_entry.rb b/lib/gollum/blob_entry.rb index 535f16c7..102a095e 100644 --- a/lib/gollum/blob_entry.rb +++ b/lib/gollum/blob_entry.rb @@ -48,6 +48,18 @@ module Gollum page end + # Gets a File instance for this blob. + # + # wiki - Gollum::Wiki instance for the Gollum::File + # + # Returns a Gollum::File instance. + def file(wiki, commit) + blob = self.blob(wiki.repo) + file = wiki.file_class.new(wiki).populate(blob, self.dir) + file.version = commit + file + end + def inspect %(#) end @@ -75,4 +87,4 @@ module Gollum dir end end -end \ No newline at end of file +end diff --git a/lib/gollum/file.rb b/lib/gollum/file.rb index acea37a6..48a470db 100644 --- a/lib/gollum/file.rb +++ b/lib/gollum/file.rb @@ -19,6 +19,7 @@ module Gollum def name @blob && @blob.name end + alias filename name # Public: The raw contents of the page. # @@ -28,7 +29,7 @@ module Gollum end # Public: The Grit::Commit version of the file. - attr_reader :version + attr_accessor :version # Public: The String path of the file. attr_reader :path @@ -38,6 +39,18 @@ module Gollum @blob.mime_type end + # Populate the Page with information from the Blob. + # + # blob - The Grit::Blob that contains the info. + # path - The String directory path of the page file. + # + # Returns the populated Gollum::Page. + def populate(blob, path=nil) + @blob = blob + @path = "#{path}/#{blob.name}"[1..-1] + self + end + ######################################################################### # # Internal Methods @@ -61,4 +74,4 @@ module Gollum end end end -end \ No newline at end of file +end diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index f39b541b..8cf6f426 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -446,6 +446,15 @@ module Gollum tree_list(treeish || @ref) end + # Public: Lists all non-page files for this wiki. + # + # treeish - The String commit ID or ref to find (default: @ref) + # + # Returns an Array of Gollum::File instances. + def files(treeish = nil) + file_list(treeish || @ref) + end + # Public: Returns the number of pages accessible from a commit # # ref - A String ref that is either a commit SHA or references one. @@ -595,6 +604,24 @@ module Gollum end end + # Fill an array with a list of files. + # + # ref - A String ref that is either a commit SHA or references one. + # + # Returns a flat Array of Gollum::File instances. + def file_list(ref) + if sha = @access.ref_to_sha(ref) + commit = @access.commit(sha) + tree_map_for(sha).inject([]) do |list, entry| + next list if entry.name.start_with?('_') + next list if @page_class.valid_page_name?(entry.name) + list << entry.file(self, commit) + end + else + [] + end + end + # Creates a reverse diff for the given SHAs on the given Gollum::Page. # # page - The Gollum::Page to scope the patch to, or a String Path. diff --git a/test/test_wiki.rb b/test/test_wiki.rb index 1086ad57..ef8942a7 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -58,6 +58,13 @@ context "Wiki" do pages.map { |p| p.filename }.sort end + test "list files" do + files = @wiki.files + assert_equal \ + ['Data.csv', 'eye.jpg', 'todo.txt'], + files.map { |p| p.filename }.sort + end + test "counts pages" do assert_equal 6, @wiki.size end