diff options
author | Francisco Javier López <fjlopez@gitlab.com> | 2018-02-05 17:17:21 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2018-02-05 17:17:21 +0000 |
commit | 27c08a16891193227fc83bf4692b4d31a3b2539f (patch) | |
tree | ecf87006574bee3e9ede3cd3ac9b3ac4bda6f427 /config | |
parent | 0a30a9ea595530499e2d3ae8062506b0a94eebad (diff) | |
download | gitlab-ce-27c08a16891193227fc83bf4692b4d31a3b2539f.tar.gz |
Allow moving wiki pages from the UI
Diffstat (limited to 'config')
-rw-r--r-- | config/initializers/gollum.rb | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/config/initializers/gollum.rb b/config/initializers/gollum.rb index 0b86cac51a7..6dfaceb8427 100644 --- a/config/initializers/gollum.rb +++ b/config/initializers/gollum.rb @@ -35,6 +35,88 @@ module Gollum [] end end + + # Remove if https://github.com/gollum/gollum-lib/pull/292 has been merged + def update_page(page, name, format, data, commit = {}) + name = name ? ::File.basename(name) : page.name + format ||= page.format + dir = ::File.dirname(page.path) + dir = '' if dir == '.' + filename = (rename = page.name != name) ? Gollum::Page.cname(name) : page.filename_stripped + + multi_commit = !!commit[:committer] + committer = multi_commit ? commit[:committer] : Committer.new(self, commit) + + if !rename && page.format == format + committer.add(page.path, normalize(data)) + else + committer.delete(page.path) + committer.add_to_index(dir, filename, format, data) + end + + committer.after_commit do |index, _sha| + @access.refresh + index.update_working_dir(dir, page.filename_stripped, page.format) + index.update_working_dir(dir, filename, format) + end + + multi_commit ? committer : committer.commit + end + + # Remove if https://github.com/gollum/gollum-lib/pull/292 has been merged + def rename_page(page, rename, commit = {}) + return false if page.nil? + return false if rename.nil? || rename.empty? + + (target_dir, target_name) = ::File.split(rename) + (source_dir, source_name) = ::File.split(page.path) + source_name = page.filename_stripped + + # File.split gives us relative paths with ".", commiter.add_to_index doesn't like that. + target_dir = '' if target_dir == '.' + source_dir = '' if source_dir == '.' + target_dir = target_dir.gsub(/^\//, '') # rubocop:disable Style/RegexpLiteral + + # if the rename is a NOOP, abort + if source_dir == target_dir && source_name == target_name + return false + end + + multi_commit = !!commit[:committer] + committer = multi_commit ? commit[:committer] : Committer.new(self, commit) + + # This piece only works for multi_commit + # If we are in a commit batch and one of the previous operations + # has updated the page, any information we ask to the page can be outdated. + # Therefore, we should ask first to the current committer tree to see if + # there is any updated change. + raw_data = raw_data_in_committer(committer, source_dir, page.filename) || + raw_data_in_committer(committer, source_dir, "#{target_name}.#{Page.format_to_ext(page.format)}") || + page.raw_data + + committer.delete(page.path) + committer.add_to_index(target_dir, target_name, page.format, raw_data) + + committer.after_commit do |index, _sha| + @access.refresh + index.update_working_dir(source_dir, source_name, page.format) + index.update_working_dir(target_dir, target_name, page.format) + end + + multi_commit ? committer : committer.commit + end + + # Remove if https://github.com/gollum/gollum-lib/pull/292 has been merged + def raw_data_in_committer(committer, dir, filename) + data = nil + + [*dir.split(::File::SEPARATOR), filename].each do |key| + data = data ? data[key] : committer.tree[key] + break unless data + end + + data + end end module Git |