merge
This commit is contained in:
+214
-38
@@ -63,6 +63,7 @@ module Gollum
|
||||
@base_path = options[:base_path] || "/"
|
||||
@page_class = options[:page_class] || self.class.page_class
|
||||
@file_class = options[:file_class] || self.class.file_class
|
||||
clear_cache
|
||||
end
|
||||
|
||||
# Public: check whether the wiki's git repo exists on the filesystem.
|
||||
@@ -134,7 +135,12 @@ module Gollum
|
||||
|
||||
parents = pcommit ? [pcommit] : []
|
||||
actor = Grit::Actor.new(commit[:name], commit[:email])
|
||||
index.commit(commit[:message], parents, actor)
|
||||
sha1 = index.commit(commit[:message], parents, actor)
|
||||
|
||||
@ref_map.clear
|
||||
update_working_dir(index, '', name, format)
|
||||
|
||||
sha1
|
||||
end
|
||||
|
||||
# Public: Update an existing page with new content. The location of the
|
||||
@@ -159,19 +165,26 @@ module Gollum
|
||||
format ||= page.format
|
||||
index = self.repo.index
|
||||
|
||||
dir = ::File.dirname(page.path)
|
||||
dir = '' if dir == '.'
|
||||
|
||||
index.read_tree(pcommit.tree.id)
|
||||
|
||||
if page.name == name && page.format == format
|
||||
index.add(page.path, normalize(data))
|
||||
else
|
||||
index.delete(page.path)
|
||||
dir = ::File.dirname(page.path)
|
||||
dir = '' if dir == '.'
|
||||
add_to_index(index, dir, name, format, data, :allow_same_ext)
|
||||
end
|
||||
|
||||
actor = Grit::Actor.new(commit[:name], commit[:email])
|
||||
index.commit(commit[:message], [pcommit], actor)
|
||||
sha1 = index.commit(commit[:message], [pcommit], actor)
|
||||
|
||||
@ref_map.clear
|
||||
update_working_dir(index, dir, page.name, page.format)
|
||||
update_working_dir(index, dir, name, format)
|
||||
|
||||
sha1
|
||||
end
|
||||
|
||||
# Public: Delete a page.
|
||||
@@ -190,8 +203,16 @@ module Gollum
|
||||
index.read_tree(pcommit.tree.id)
|
||||
index.delete(page.path)
|
||||
|
||||
dir = ::File.dirname(page.path)
|
||||
dir = '' if dir == '.'
|
||||
|
||||
actor = Grit::Actor.new(commit[:name], commit[:email])
|
||||
index.commit(commit[:message], [pcommit], actor)
|
||||
sha1 = index.commit(commit[:message], [pcommit], actor)
|
||||
|
||||
@ref_map.clear
|
||||
update_working_dir(index, dir, page.name, page.format)
|
||||
|
||||
sha1
|
||||
end
|
||||
|
||||
# Public: Lists all pages for this wiki.
|
||||
@@ -200,11 +221,19 @@ module Gollum
|
||||
#
|
||||
# Returns an Array of Gollum::Page instances.
|
||||
def pages(treeish = nil)
|
||||
treeish ||= 'master'
|
||||
if commit = @repo.commit(treeish)
|
||||
tree_list(commit)
|
||||
else
|
||||
[]
|
||||
tree_list(treeish || 'master').sort! do |x, y|
|
||||
x.title.downcase <=> y.title.downcase
|
||||
end
|
||||
end
|
||||
|
||||
# Public: Returns the number of pages accessible from a commit
|
||||
#
|
||||
# ref - A String ref that is either a commit SHA or references one.
|
||||
#
|
||||
# Returns a Fixnum
|
||||
def size(ref = nil)
|
||||
tree_map_for(ref || 'master').inject(0) do |num, entry|
|
||||
num + (@page_class.valid_page_name?(entry.name) ? 1 : 0)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -255,6 +284,26 @@ module Gollum
|
||||
# Returns the String path.
|
||||
attr_reader :path
|
||||
|
||||
# Gets a Hash cache of refs to commit SHAs.
|
||||
#
|
||||
# {"master" => "abc123", ...}
|
||||
#
|
||||
# Returns the Hash cache.
|
||||
attr_reader :ref_map
|
||||
|
||||
# Gets a Hash cache of commit SHAs to a recursive tree of blobs.
|
||||
#
|
||||
# {"abc123" => [["lib/foo.rb", "blob-sha"], [file, sha], ...], ...}
|
||||
#
|
||||
# Returns the Hash cache.
|
||||
attr_reader :tree_map
|
||||
|
||||
# Gets the page class used by all instances of this Wiki.
|
||||
attr_reader :page_class
|
||||
|
||||
# Gets the file class used by all instances of this Wiki.
|
||||
attr_reader :file_class
|
||||
|
||||
# Normalize the data.
|
||||
#
|
||||
# data - The String data to be normalized.
|
||||
@@ -264,33 +313,84 @@ module Gollum
|
||||
data.gsub(/\r/, '')
|
||||
end
|
||||
|
||||
# Fill an array with a list of pages.
|
||||
# Assemble a Page's filename from its name and format.
|
||||
#
|
||||
# commit - The Grit::Commit
|
||||
# tree - The Grit::Tree to start with.
|
||||
# sub_tree - Optional String specifying the parent path of the Page.
|
||||
# name - The String name of the page (may be in human format).
|
||||
# format - The Symbol format of the page.
|
||||
#
|
||||
# Returns a flat Array of Gollum::Page instances.
|
||||
def tree_list(commit, tree = commit.tree, sub_tree = nil)
|
||||
list = []
|
||||
path = tree.name ? "#{sub_tree}/#{tree.name}" : ''
|
||||
tree.contents.each do |item|
|
||||
case item
|
||||
when Grit::Blob
|
||||
if @page_class.valid_page_name?(item.name)
|
||||
page = @page_class.new(self).populate(item, path)
|
||||
page.version = commit
|
||||
list << page
|
||||
end
|
||||
when Grit::Tree
|
||||
list.push *tree_list(commit, item, path)
|
||||
end
|
||||
end
|
||||
list
|
||||
# Returns the String filename.
|
||||
def page_file_name(name, format)
|
||||
ext = @page_class.format_to_ext(format)
|
||||
@page_class.cname(name) + '.' + ext
|
||||
end
|
||||
|
||||
# Determine if a given page path is scheduled to be deleted in the next
|
||||
# commit for the given Index.
|
||||
# Update the given file in the repository's working directory if there
|
||||
# is a working directory present.
|
||||
#
|
||||
# index - The Grit::Index with which to sync.
|
||||
# dir - The String directory in which the file lives.
|
||||
# name - The String name of the page (may be in human format).
|
||||
# format - The Symbol format of the page.
|
||||
#
|
||||
# Returns nothing.
|
||||
def update_working_dir(index, dir, name, format)
|
||||
unless @repo.bare
|
||||
path =
|
||||
if dir == ''
|
||||
page_file_name(name, format)
|
||||
else
|
||||
::File.join(dir, page_file_name(name, format))
|
||||
end
|
||||
|
||||
Dir.chdir(::File.join(@repo.path, '..')) do
|
||||
if file_path_scheduled_for_deletion?(index.tree, path)
|
||||
@repo.git.rm({'f' => true}, '--', path)
|
||||
else
|
||||
@repo.git.checkout({}, 'HEAD', '--', path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Fill an array with a list of pages.
|
||||
#
|
||||
# ref - A String ref that is either a commit SHA or references one.
|
||||
#
|
||||
# Returns a flat Array of Gollum::Page instances.
|
||||
def tree_list(ref)
|
||||
tree_map_for(ref).inject([]) do |list, entry|
|
||||
next list unless @page_class.valid_page_name?(entry.name)
|
||||
sha = ref_map[ref]
|
||||
list << entry.page(self, @repo.commit(sha))
|
||||
end
|
||||
end
|
||||
|
||||
# Determine if a given file is scheduled to be deleted in the next commit
|
||||
# for the given Index.
|
||||
#
|
||||
# map - The Hash map:
|
||||
# key - The String directory or filename.
|
||||
# val - The Hash submap or the String contents of the file.
|
||||
# path - The String path of the file including extension.
|
||||
#
|
||||
# Returns the Boolean response.
|
||||
def file_path_scheduled_for_deletion?(map, path)
|
||||
parts = path.split('/')
|
||||
if parts.size == 1
|
||||
deletions = map.keys.select { |k| !map[k] }
|
||||
deletions.any? { |d| d == parts.first }
|
||||
else
|
||||
part = parts.shift
|
||||
if rest = map[part]
|
||||
file_path_scheduled_for_deletion?(rest, parts.join('/'))
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Determine if a given page (regardless of format) is scheduled to be
|
||||
# deleted in the next commit for the given Index.
|
||||
#
|
||||
# map - The Hash map:
|
||||
# key - The String directory or filename.
|
||||
@@ -310,7 +410,7 @@ module Gollum
|
||||
if rest = map[part]
|
||||
page_path_scheduled_for_deletion?(rest, parts.join('/'))
|
||||
else
|
||||
nil
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -331,8 +431,7 @@ module Gollum
|
||||
#
|
||||
# Returns nothing (modifies the Index in place).
|
||||
def add_to_index(index, dir, name, format, data, allow_same_ext = false)
|
||||
ext = @page_class.format_to_ext(format)
|
||||
path = @page_class.cname(name) + '.' + ext
|
||||
path = page_file_name(name, format)
|
||||
|
||||
dir = '/' if dir.strip.empty?
|
||||
|
||||
@@ -364,9 +463,86 @@ module Gollum
|
||||
#
|
||||
# Returns the commit Hash
|
||||
def normalize_commit(commit = {})
|
||||
commit[:name] = self.class.default_committer_name if commit[:name].to_s.empty?
|
||||
commit[:email] = self.class.default_committer_email if commit[:email].to_s.empty?
|
||||
commit[:name] = default_committer_name if commit[:name].to_s.empty?
|
||||
commit[:email] = default_committer_email if commit[:email].to_s.empty?
|
||||
commit
|
||||
end
|
||||
|
||||
# Gets the default name for commits.
|
||||
def default_committer_name
|
||||
@default_committer_name ||= \
|
||||
@repo.config['user.name'] || self.class.default_committer_name
|
||||
end
|
||||
|
||||
# Gets the default email for commits.
|
||||
def default_committer_email
|
||||
@default_committer_email ||= \
|
||||
@repo.config['user.email'] || self.class.default_committer_email
|
||||
end
|
||||
|
||||
# Finds a full listing of files and their blob SHA for a given ref. Each
|
||||
# listing is cached based on its actual commit SHA.
|
||||
#
|
||||
# ref - A String ref that is either a commit SHA or references one.
|
||||
#
|
||||
# Returns an Array of BlobEntry instances.
|
||||
def tree_map_for(ref)
|
||||
sha = @ref_map[ref] || ref
|
||||
@tree_map[sha] || begin
|
||||
real_sha = @repo.git.rev_list({:max_count=>1}, ref)
|
||||
@ref_map[ref] = real_sha if real_sha != ref
|
||||
@tree_map[real_sha] ||= parse_tree_for(real_sha)
|
||||
end
|
||||
rescue Grit::GitRuby::Repository::NoSuchShaFound
|
||||
[]
|
||||
end
|
||||
|
||||
# Finds the full listing of files and their blob SHA for a given commit
|
||||
# SHA. No caching or ref lookups are performed.
|
||||
#
|
||||
# sha - String commit SHA.
|
||||
#
|
||||
# Returns an Array of BlobEntry instances.
|
||||
def parse_tree_for(sha)
|
||||
tree = @repo.git.native(:ls_tree, {:r => true, :z => true}, sha)
|
||||
items = []
|
||||
tree.split("\0").each do |line|
|
||||
items << parse_tree_line(line)
|
||||
end
|
||||
items
|
||||
end
|
||||
|
||||
# Parses a line of output from the `ls-tree` command.
|
||||
#
|
||||
# line - A String line of output:
|
||||
# "100644 blob 839c2291b30495b9a882c17d08254d3c90d8fb53 Home.md"
|
||||
#
|
||||
# Returns an Array of BlobEntry instances.
|
||||
def parse_tree_line(line)
|
||||
data, name = line.split("\t")
|
||||
mode, type, sha = data.split(' ')
|
||||
name = decode_git_path(name)
|
||||
BlobEntry.new sha, name
|
||||
end
|
||||
|
||||
# Decode octal sequences (\NNN) in tree path names.
|
||||
#
|
||||
# path - String path name.
|
||||
#
|
||||
# Returns a decoded String.
|
||||
def decode_git_path(path)
|
||||
if path[0] == ?" && path[-1] == ?"
|
||||
path = path[1...-1]
|
||||
path.gsub!(/\\\d{3}/) { |m| m[1..-1].to_i(8).chr }
|
||||
end
|
||||
path.gsub!(/\\[rn"\\]/) { |m| eval(%("#{m.to_s}")) }
|
||||
path
|
||||
end
|
||||
|
||||
# Resets the ref and tree caches for this wiki.
|
||||
def clear_cache
|
||||
@ref_map = {}
|
||||
@tree_map = {}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user