Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error when attempting to make changes to a file modified in the worktree #453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/gollum-lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions lib/gollum-lib/committer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/gollum-lib/wiki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/test_committer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading