From 5797f6b1f37022f1e5d60178967b23b1002bf93d Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 25 Mar 2015 23:43:48 -0400 Subject: [PATCH 1/3] Test pre-commit environment submodule change Ensure that when only a submodule change is staged, the change is not affected by #setup_environment and #cleanup_environment. Change-Id: Idbfd394c156369d9f86355e808097f2548f4e8a8 --- .../hook_context/pre_commit_spec.rb | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index e498290e..376c295f 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -66,6 +66,30 @@ end end + context 'when only a submodule change is staged' 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"'` + `git add sub` + example.run + end + end + + it 'keeps staged submodule change' do + subject + `git diff`.should be_empty + File.open('sub/submodule-file', 'r').read.should == "Hello World\n" + end + end + context 'when a broken symlink is staged' do around do |example| repo do @@ -164,6 +188,30 @@ File.exist?('tracked-file').should == false end end + + context 'when only a submodule change was staged' 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"'` + `git add sub` + example.run + end + end + + it 'keeps staged submodule change' do + subject + `git diff`.should be_empty + File.open('sub/submodule-file', 'r').read.should == "Hello World\n" + end + end end describe '#modified_files' do From 26cba7645ca231a230caa258f63f446c3f1da515 Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Wed, 25 Mar 2015 19:31:24 -0400 Subject: [PATCH 2/3] Don't clear working tree for only submodule changes Change-Id: I5fd7db9a291aa87fcdf3fed860b18e7a9ac6a450 --- lib/overcommit/hook_context/pre_commit.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/overcommit/hook_context/pre_commit.rb b/lib/overcommit/hook_context/pre_commit.rb index ef0733c1..094bba2a 100644 --- a/lib/overcommit/hook_context/pre_commit.rb +++ b/lib/overcommit/hook_context/pre_commit.rb @@ -31,7 +31,8 @@ def setup_environment "\nSTDOUT:#{result.stdout}\nSTDERR:#{result.stderr}" end - @changes_stashed = true + # False if only submodule references were changed + @changes_stashed = modified_files.any? end # While running the hooks make it appear as if nothing changed From 9dbd6558df08d7c5ee8e4d4ba9da8116380523cf Mon Sep 17 00:00:00 2001 From: Josh Hagins Date: Thu, 26 Mar 2015 13:23:34 -0400 Subject: [PATCH 3/3] Check that submodule change is still staged Change-Id: Ibe489e9d606761f4001c6749a199522a6a591803 --- spec/overcommit/hook_context/pre_commit_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/overcommit/hook_context/pre_commit_spec.rb b/spec/overcommit/hook_context/pre_commit_spec.rb index 376c295f..4a640a93 100644 --- a/spec/overcommit/hook_context/pre_commit_spec.rb +++ b/spec/overcommit/hook_context/pre_commit_spec.rb @@ -84,9 +84,9 @@ end it 'keeps staged submodule change' do - subject - `git diff`.should be_empty - File.open('sub/submodule-file', 'r').read.should == "Hello World\n" + expect { subject }.to_not change { + (`git diff --cached` =~ /-Subproject commit[\s\S]*\+Subproject commit/).nil? + }.from(false) end end @@ -207,9 +207,9 @@ end it 'keeps staged submodule change' do - subject - `git diff`.should be_empty - File.open('sub/submodule-file', 'r').read.should == "Hello World\n" + expect { subject }.to_not change { + (`git diff --cached` =~ /-Subproject commit[\s\S]*\+Subproject commit/).nil? + }.from(false) end end end