Skip to content

Commit 8a02e45

Browse files
committed
Gracefully handle pre-commit git stash failures
When attempting to `git stash` in the `setup_environment` method of the `PreCommit` hook context, it was possible for the underlying execution of `git stash` to fail. Handle this case by actually inspecting the status code of `git stash` before continuing the hook run. Change-Id: I05bcf22af55753bdda2e56f360bae78de4415910 Reviewed-on: http://gerrit.causes.com/40969 Tested-by: jenkins <jenkins@causes.com> Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
1 parent bfc0f47 commit 8a02e45

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Fix bug where incorrect "hook run interrupted" message displayed when
66
hook run failed
7+
* Gracefully handle `git stash` failures in pre-commit hook runs
78

89
## 0.14.1
910

lib/overcommit/exceptions.rb

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class ConfigurationError < StandardError; end
55
# Raised when trying to read/write to/from the local repo git config fails.
66
class GitConfigError < StandardError; end
77

8+
# Raised when a {HookContext} is unable to setup the environment before a run.
9+
class HookSetupFailed < StandardError; end
10+
811
# Raised when a hook run was cancelled by the user.
912
class HookCancelled < StandardError; end
1013

lib/overcommit/hook_context/pre_commit.rb

+16-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@ def setup_environment
1616
Overcommit::GitRepo.store_cherry_pick_state
1717

1818
if !initial_commit? && any_changes?
19+
@stash_attempted = true
20+
21+
result = Overcommit::Utils.execute(
22+
%w[git stash save --keep-index --quiet] +
23+
["Overcommit: Stash of repo state before hook run at #{Time.now}"]
24+
)
25+
26+
unless result.success?
27+
# Failure to stash in this case is likely due to a configuration
28+
# issue (e.g. author/email not set or GPG signing key incorrect)
29+
raise Overcommit::Exceptions::HookSetupFailed,
30+
"Unable to setup environment for #{hook_script_name} hook run:" \
31+
"\n#{result.stderr}"
32+
end
33+
1934
@changes_stashed = true
20-
`git stash save --keep-index --quiet #{<<-MSG}`
21-
"Overcommit: Stash of repo state before hook run at #{Time.now}"
22-
MSG
2335
end
2436

2537
# While running the hooks make it appear as if nothing changed
@@ -29,7 +41,7 @@ def setup_environment
2941
# Restore unstaged changes and reset file modification times so it appears
3042
# as if nothing ever changed.
3143
def cleanup_environment
32-
unless initial_commit?
44+
unless initial_commit? || (@stash_attempted && !@changes_stashed)
3345
`git reset --hard &> /dev/null` # Ensure working tree is clean before popping stash
3446
end
3547

template-dir/hooks/overcommit-hook

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ rescue Overcommit::Exceptions::HookContextLoadError => error
7878
puts error
7979
puts 'Are you running an old version of Overcommit?'
8080
exit 69 # EX_UNAVAILABLE
81+
rescue Overcommit::Exceptions::HookSetupFailed => error
82+
puts error
83+
exit 74 # EX_IOERR
8184
rescue Overcommit::Exceptions::HookCancelled
8285
puts 'You cancelled the hook run'
8386
exit 1

0 commit comments

Comments
 (0)