From 578b23cd4f43f23d40867ba56151adf86320c159 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sun, 5 Apr 2015 20:40:20 -0400 Subject: [PATCH 1/4] Add failing test case for restoring unstaged changes Add test case for #cleanup_environment to check that unstaged changes are restored when no changes were staged. This test is currently failing. --- spec/overcommit/hook_context/pre_commit_spec.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index cdf3f8ba..aff89714 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -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" From 87ee242852e99d6d3b080fea3fbfd9f30826f0ee Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sun, 5 Apr 2015 22:19:07 -0400 Subject: [PATCH 2/4] Check last stash message to confirm changes were stashed Fixes #158 --- lib/overcommit/hook_context/pre_commit.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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 From 6a828143bb82ee015b972a0705a818e9b90870a7 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sun, 5 Apr 2015 22:53:52 -0400 Subject: [PATCH 3/4] Check that staged changes remain in the index --- spec/overcommit/hook_context/pre_commit_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index aff89714..f2a29d08 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 @@ -170,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" From 7812e726209fe463f2bb67ff11cdfe3ae686f1bc Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Sun, 5 Apr 2015 22:54:54 -0400 Subject: [PATCH 4/4] Check staged submodule changes with file changes --- .../hook_context/pre_commit_spec.rb | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index f2a29d08..b1af2d0e 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -226,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