diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index 094bba2a..289eaffe 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -18,9 +18,9 @@ def setup_environment if !initial_commit? && any_changes? @stash_attempted = true + stash_message = "Overcommit: Stash of repo state before hook run at #{Time.now}" result = Overcommit::Utils.execute( - %w[git stash save --keep-index --quiet] + - ["Overcommit: Stash of repo state before hook run at #{Time.now}"] + %w[git stash save --keep-index --quiet] + [stash_message] ) unless result.success? @@ -31,8 +31,7 @@ def setup_environment "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end - # False if only submodule references were changed - @changes_stashed = modified_files.any? + @changes_stashed = `git stash list -1`.include?(stash_message) end # While running the hooks make it appear as if nothing changed diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index cdf3f8ba..b1af2d0e 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -53,7 +53,7 @@ it 'keeps staged changes' do subject - File.open('tracked-file', 'r').read.should == "Hello World\nSome more text\n" + `git show :tracked-file`.should == "Hello World\nSome more text\n" end it 'keeps untracked files' do @@ -119,13 +119,21 @@ around do |example| repo do `echo "Hello World" > tracked-file` - `git add tracked-file` - `git commit -m "Add tracked-file"` + `echo "Hello Other World" > other-tracked-file` + `git add tracked-file other-tracked-file` + `git commit -m "Add tracked-file and other-tracked-file"` `echo "Hello Again" > untracked-file` + `echo "Some more text" >> other-tracked-file` example.run end end + it 'restores the unstaged changes' do + subject + File.open('other-tracked-file', 'r').read. + should == "Hello Other World\nSome more text\n" + end + it 'keeps already-committed files' do subject File.open('tracked-file', 'r').read.should == "Hello World\n" @@ -162,6 +170,11 @@ should == "Hello World\nSome more text\nYet some more text\n" end + it 'keeps staged changes' do + subject + `git show :tracked-file`.should == "Hello World\nSome more text\n" + end + it 'keeps untracked files' do subject File.open('untracked-file', 'r').read.should == "Hello Again\n" @@ -213,6 +226,36 @@ }.from(false) end end + + context 'when submodule changes were staged along with other changes' do + around do |example| + submodule = repo do + `git commit --allow-empty -m "Initial commit"` + end + + repo do + `git submodule add #{submodule} sub &>/dev/null` + `git commit -m "Add submodule"` + `echo "Hello World" > sub/submodule-file` + `git submodule foreach 'git add submodule-file'` + `git submodule foreach 'git commit -m "Another commit"'` + `echo "Hello Again" > tracked-file` + `git add sub tracked-file` + example.run + end + end + + it 'keeps staged submodule change' do + expect { subject }.to_not change { + (`git diff --cached` =~ /-Subproject commit[\s\S]*\+Subproject commit/).nil? + }.from(false) + end + + it 'keeps staged file change' do + subject + `git show :tracked-file`.should == "Hello Again\n" + end + end end describe '#modified_files' do