File tree 2 files changed +50
-1
lines changed
lib/overcommit/hook_context
spec/overcommit/hook_context
2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,21 @@ module Overcommit::HookContext
7
7
# This includes staged files, which lines of those files have been modified,
8
8
# etc. It is also responsible for saving/restoring the state of the repo so
9
9
# hooks only inspect staged changes.
10
- class PreCommit < Base
10
+ class PreCommit < Base # rubocop:disable ClassLength
11
+ # Returns whether this hook run was triggered by `git commit --amend`
12
+ def amend?
13
+ if @amended . nil?
14
+ cmd = Overcommit ::Utils . parent_command
15
+ @amended = !( /--amend/ =~ cmd ) . nil?
16
+
17
+ amend_alias = `git config --get-regexp '^alias\\ .' '--amend'` .
18
+ slice ( /(?<=alias\. )\w +/ )
19
+
20
+ @amended ||= !( /git #{ amend_alias } / =~ cmd ) . nil? unless amend_alias . nil?
21
+ end
22
+ @amended
23
+ end
24
+
11
25
# Stash unstaged contents of files so hooks don't see changes that aren't
12
26
# about to be committed.
13
27
def setup_environment
Original file line number Diff line number Diff line change 7
7
let ( :input ) { double ( 'input' ) }
8
8
let ( :context ) { described_class . new ( config , args , input ) }
9
9
10
+ describe '#amend?' do
11
+ subject { context . amend? }
12
+
13
+ context 'when amending a commit using `git commit --amend`' do
14
+ before do
15
+ Overcommit ::Utils . stub ( :parent_command ) . and_return ( 'git commit --amend' )
16
+ end
17
+
18
+ it { should == true }
19
+ end
20
+
21
+ context 'when amending a commit using a git alias' do
22
+ around do |example |
23
+ repo do
24
+ `git config alias.amend 'commit --amend'`
25
+ example . run
26
+ end
27
+ end
28
+
29
+ before do
30
+ Overcommit ::Utils . stub ( :parent_command ) . and_return ( 'git amend' )
31
+ end
32
+
33
+ it { should == true }
34
+ end
35
+
36
+ context 'when not amending a commit' do
37
+ before do
38
+ Overcommit ::Utils . stub ( :parent_command ) . and_return ( 'git commit' )
39
+ end
40
+
41
+ it { should == false }
42
+ end
43
+ end
44
+
10
45
describe '#setup_environment' do
11
46
subject { context . setup_environment }
12
47
You can’t perform that action at this time.
0 commit comments