diff --git a/Gemfile b/Gemfile index 8da7c073..7dc099c5 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,7 @@ source 'https://rubygems.org' gemspec :name => 'gollum-lib' gem 'irb' +gem 'gollum-rugged_adapter', git: 'https://github.com/dometto/rugged_adapter', branch: 'clean_workdir' if RUBY_PLATFORM == 'java' then group :development do diff --git a/lib/gollum-lib.rb b/lib/gollum-lib.rb index 5a2c5858..809f7fc1 100644 --- a/lib/gollum-lib.rb +++ b/lib/gollum-lib.rb @@ -54,5 +54,6 @@ def initialize(attempted, message = nil) class InvalidGitRepositoryError < StandardError; end class NoSuchPathError < StandardError; end class IllegalDirectoryPath < StandardError; end + class WorkdirModifiedError < StandardError; end end diff --git a/lib/gollum-lib/committer.rb b/lib/gollum-lib/committer.rb index 103f1293..ab9e4659 100644 --- a/lib/gollum-lib/committer.rb +++ b/lib/gollum-lib/committer.rb @@ -79,12 +79,15 @@ def parents # Raises Gollum::DuplicatePageError if a matching filename already exists, unless force_overwrite is explicitly enabled. # This way, pages are not inadvertently overwritten. # + # Even if force_overwrite is enabled, this throws a WorkdirModifiedError if the workdir path contains modifications that are not in the repository. + # # Returns nothing (modifies the Index in place). def add_to_index(path, data, options = {}, force_overwrite = false) if tree = index.current_tree unless page_path_scheduled_for_deletion?(index.tree, path) || force_overwrite raise DuplicatePageError.new(path) if tree / path end + raise WorkdirModifiedError.new(path) if index.workdir_path_modified?(path) end unless options[:normalize] == false diff --git a/lib/gollum-lib/wiki.rb b/lib/gollum-lib/wiki.rb index 920399fb..118fbf0b 100644 --- a/lib/gollum-lib/wiki.rb +++ b/lib/gollum-lib/wiki.rb @@ -355,7 +355,7 @@ def update_page(page, name, format, data, commit = {}) committer = multi_commit ? commit[:committer] : Committer.new(self, commit) if !rename - committer.add(page.path, normalize(data)) + committer.add_to_index(page.path, data, {normalize: true}, true) else committer.delete(page.path) committer.add_to_index(new_path, data) diff --git a/test/test_committer.rb b/test/test_committer.rb index 439818e0..8a841c80 100644 --- a/test/test_committer.rb +++ b/test/test_committer.rb @@ -148,6 +148,20 @@ assert_equal @wiki.repo.head.commit.note, 'My notes' end + test "raise WorkdirModifiedError when the workdir contains changes" do + committer = Gollum::Committer.new(@wiki) + path = 'Bilbo-Baggins.md' + filepath = File.join(@wiki.path, path) + + f = File.open(filepath, 'w') { |f| f.puts "Workdir no longer clean" } + assert_raises Gollum::WorkdirModifiedError do + committer.add_to_index(path, 'content', {}, true) + end + + File.delete(filepath) + assert_equal 'content', committer.add_to_index(path, 'content', {}, true) + end + teardown do FileUtils.rm_rf(@path) end