Skip to content
Closed
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
7 changes: 3 additions & 4 deletions lib/overcommit/hook_context/pre_commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand Down
49 changes: 46 additions & 3 deletions spec/overcommit/hook_context/pre_commit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down