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. # Returns the String SHA1 of the newly written version.
def write_page(name, format, data, commit = {}) def write_page(name, format, data, commit = {})
commit = normalize_commit(commit) commit = normalize_commit(commit)
index = self.repo.index index = nil
sha1 = commit_index(commit) do |index|
if pcommit = @repo.commit('master') add_to_index(index, '', name, format, data)
index.read_tree(pcommit.tree.id)
end 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 @access.refresh
update_working_dir(index, '', name, format) update_working_dir(index, '', name, format)
@@ -218,26 +211,20 @@ module Gollum
# Returns the String SHA1 of the newly written version. # Returns the String SHA1 of the newly written version.
def update_page(page, name, format, data, commit = {}) def update_page(page, name, format, data, commit = {})
commit = normalize_commit(commit) commit = normalize_commit(commit)
pcommit = @repo.commit('master')
name ||= page.name name ||= page.name
format ||= page.format format ||= page.format
index = self.repo.index dir = ::File.dirname(page.path)
dir = '' if dir == '.'
dir = ::File.dirname(page.path) index = nil
dir = '' if dir == '.' sha1 = commit_index(commit) do |index|
if page.name == name && page.format == format
index.read_tree(pcommit.tree.id) index.add(page.path, normalize(data))
else
if page.name == name && page.format == format index.delete(page.path)
index.add(page.path, normalize(data)) add_to_index(index, dir, name, format, data, :allow_same_ext)
else end
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 @access.refresh
update_working_dir(index, dir, page.name, page.format) update_working_dir(index, dir, page.name, page.format)
update_working_dir(index, dir, name, format) update_working_dir(index, dir, name, format)
@@ -255,18 +242,14 @@ module Gollum
# #
# Returns the String SHA1 of the newly written version. # Returns the String SHA1 of the newly written version.
def delete_page(page, commit) def delete_page(page, commit)
pcommit = @repo.commit('master') index = nil
sha1 = commit_index(commit) do |index|
index = self.repo.index index.delete(page.path)
index.read_tree(pcommit.tree.id) end
index.delete(page.path)
dir = ::File.dirname(page.path) dir = ::File.dirname(page.path)
dir = '' if dir == '.' dir = '' if dir == '.'
actor = Grit::Actor.new(commit[:name], commit[:email])
sha1 = index.commit(commit[:message], [pcommit], actor)
@access.refresh @access.refresh
update_working_dir(index, dir, page.name, page.format) update_working_dir(index, dir, page.name, page.format)
@@ -530,6 +513,19 @@ module Gollum
index.add(fullpath, normalize(data)) index.add(fullpath, normalize(data))
end 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. # Ensures a commit hash has all the required fields for a commit.
# #
# commit - The commit Hash details: # commit - The commit Hash details: