Skip to content

Commit 4b784a9

Browse files
committed
Fix renaming files during an amendment
Commit 7f23eb4 introduced support for including modifications from the last commit when amending. Unfortunately, this introduces a new case where renaming a file as part of an amendment would result in a crash as Overcommit would try to update the modification time of the non-existent old file. This also had the nasty effect of losing the user's changes, which is a regression that was introduced in c3d972d. I'll be addressing that in a separate commit. A simple workaround here is to simply not bother updating the modification time if the file does not exist. Note that `File.mtime` sometimes behaves strangely here--I've seen it return `nil` for files that don't exist, but raise errors at other times (and I have no idea under which conditions one outcome occurs versus the other). It's easiest to just check if the file exists before we call `mtime`.
1 parent 25f9e62 commit 4b784a9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/overcommit/hook_context/pre_commit.rb

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def store_modified_times
180180

181181
(staged_files + unstaged_files).each do |file|
182182
next if Overcommit::Utils.broken_symlink?(file)
183+
next unless File.exist?(file) # Ignore renamed files (old file no longer exists)
183184
@modified_times[file] = File.mtime(file)
184185
end
185186
end

spec/overcommit/hook_context/pre_commit_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,29 @@
150150
end
151151
end
152152

153+
context 'when renaming a file during an amendment' do
154+
around do |example|
155+
repo do
156+
`git commit --allow-empty -m "Initial commit"`
157+
`touch some-file`
158+
`git add some-file`
159+
`git commit -m "Add file"`
160+
`git mv some-file renamed-file`
161+
example.run
162+
end
163+
end
164+
165+
before do
166+
context.stub(:amendment?).and_return(true)
167+
end
168+
169+
it 'does not try to update modification time of the old non-existent file' do
170+
File.should_receive(:mtime).with(/renamed-file/)
171+
File.should_not_receive(:mtime).with(/some-file/)
172+
subject
173+
end
174+
end
175+
153176
context 'when only a submodule change is staged' do
154177
around do |example|
155178
submodule = repo do

0 commit comments

Comments
 (0)