From edb3b4002bd7756a3774360f884ef001ce792e86 Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Tue, 29 Aug 2023 20:44:49 +0200 Subject: [PATCH] Check for modified worktree in Committer#add_to_index --- Gemfile | 1 + lib/gollum-lib.rb | 1 + lib/gollum-lib/committer.rb | 3 +++ lib/gollum-lib/wiki.rb | 2 +- test/test_committer.rb | 14 ++++++++++++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 8da7c0735..7dc099c5d 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 5a2c5858f..809f7fc18 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 103f1293a..ab9e46596 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 920399fb4..118fbf0b2 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 439818e0f..8a841c800 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