refactor common index-writing logic in wiki update methods

This commit is contained in:
rick
2010-12-08 13:24:15 -08:00
parent 5bb7417495
commit e7d37eaf38
+30 -34
View File
@@ -183,18 +183,11 @@ module Gollum
# Returns the String SHA1 of the newly written version.
def write_page(name, format, data, commit = {})
commit = normalize_commit(commit)
index = self.repo.index
if pcommit = @repo.commit('master')
index.read_tree(pcommit.tree.id)
index = nil
sha1 = commit_index(commit) do |index|
add_to_index(index, '', name, format, data)
end
add_to_index(index, '', name, format, data)
parents = pcommit ? [pcommit] : []
actor = Grit::Actor.new(commit[:name], commit[:email])
sha1 = index.commit(commit[:message], parents, actor)
@access.refresh
update_working_dir(index, '', name, format)
@@ -218,26 +211,20 @@ module Gollum
# Returns the String SHA1 of the newly written version.
def update_page(page, name, format, data, commit = {})
commit = normalize_commit(commit)
pcommit = @repo.commit('master')
name ||= page.name
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)
add_to_index(index, dir, name, format, data, :allow_same_ext)
dir = ::File.dirname(page.path)
dir = '' if dir == '.'
index = nil
sha1 = commit_index(commit) do |index|
if page.name == name && page.format == format
index.add(page.path, normalize(data))
else
index.delete(page.path)
add_to_index(index, dir, name, format, data, :allow_same_ext)
end
end
actor = Grit::Actor.new(commit[:name], commit[:email])
sha1 = index.commit(commit[:message], [pcommit], actor)
@access.refresh
update_working_dir(index, dir, page.name, page.format)
update_working_dir(index, dir, name, format)
@@ -255,18 +242,14 @@ module Gollum
#
# Returns the String SHA1 of the newly written version.
def delete_page(page, commit)
pcommit = @repo.commit('master')
index = self.repo.index
index.read_tree(pcommit.tree.id)
index.delete(page.path)
index = nil
sha1 = commit_index(commit) do |index|
index.delete(page.path)
end
dir = ::File.dirname(page.path)
dir = '' if dir == '.'
actor = Grit::Actor.new(commit[:name], commit[:email])
sha1 = index.commit(commit[:message], [pcommit], actor)
@access.refresh
update_working_dir(index, dir, page.name, page.format)
@@ -530,6 +513,19 @@ module Gollum
index.add(fullpath, normalize(data))
end
def commit_index(options = {})
options[:parent] ||= [@repo.commit('master')]
options[:parent].compact!
index = self.repo.index
if parent = options[:parent][0]
index.read_tree(parent.tree.id)
end
yield index if block_given?
actor = Grit::Actor.new(options[:name], options[:email])
index.commit(options[:message], options[:parent], actor)
end
# Ensures a commit hash has all the required fields for a commit.
#
# commit - The commit Hash details: