File tree 5 files changed +81
-2
lines changed
5 files changed +81
-2
lines changed Original file line number Diff line number Diff line change 3
3
## master
4
4
5
5
* Add [ ` hadolint ` ] ( https://github.com/lukasmartinelli/hadolint ) pre-commit hook
6
+ * Use the ` core.hooksPath ` Git configuration option when installing hooks
6
7
7
8
## 0.39.1
8
9
Original file line number Diff line number Diff line change
1
+ require 'overcommit/utils'
2
+
1
3
module Overcommit
2
4
# Get configuration options from git
3
5
module GitConfig
@@ -8,5 +10,11 @@ def comment_character
8
10
char = '#' if char == ''
9
11
char
10
12
end
13
+
14
+ def hooks_path
15
+ path = `git config --get core.hooksPath` . chomp
16
+ return File . join ( Overcommit ::Utils . repo_root , '.git' , 'hooks' ) if path . empty?
17
+ File . absolute_path ( path )
18
+ end
11
19
end
12
20
end
Original file line number Diff line number Diff line change @@ -65,8 +65,7 @@ def update
65
65
end
66
66
67
67
def hooks_path
68
- absolute_target = File . expand_path ( @target )
69
- File . join ( Overcommit ::Utils . git_dir ( absolute_target ) , 'hooks' )
68
+ @hooks_path ||= Dir . chdir ( @target ) { GitConfig . hooks_path }
70
69
end
71
70
72
71
def old_hooks_path
Original file line number Diff line number Diff line change 22
22
end
23
23
end
24
24
end
25
+
26
+ describe '.hooks_path' do
27
+ subject { described_class . hooks_path }
28
+
29
+ context 'when not explicitly set' do
30
+ around do |example |
31
+ repo do
32
+ example . run
33
+ end
34
+ end
35
+
36
+ it 'returns the default hook path' do
37
+ expect ( subject ) . to eq File . expand_path ( File . join ( '.git' , 'hooks' ) )
38
+ end
39
+ end
40
+
41
+ context 'when explicitly set to an empty string' do
42
+ around do |example |
43
+ repo do
44
+ `git config --local core.hooksPath ""`
45
+ example . run
46
+ end
47
+ end
48
+
49
+ it 'returns the default hook path' do
50
+ expect ( subject ) . to eq File . expand_path ( File . join ( '.git' , 'hooks' ) )
51
+ end
52
+ end
53
+
54
+ context 'when explicitly set to an absolute path' do
55
+ around do |example |
56
+ repo do
57
+ `git config --local core.hooksPath /etc/hooks`
58
+ example . run
59
+ end
60
+ end
61
+
62
+ it 'returns the absolute path' do
63
+ expect ( subject ) . to eq '/etc/hooks'
64
+ end
65
+ end
66
+
67
+ context 'when explicitly set to a relative path' do
68
+ around do |example |
69
+ repo do
70
+ `git config --local core.hooksPath my-hooks`
71
+ example . run
72
+ end
73
+ end
74
+
75
+ it 'returns the absolute path to the directory relative to the repo root' do
76
+ expect ( subject ) . to eq File . expand_path ( 'my-hooks' )
77
+ end
78
+ end
79
+ end
25
80
end
Original file line number Diff line number Diff line change @@ -142,6 +142,22 @@ def hook_files_installed?(hooks_dir)
142
142
} . from ( false ) . to ( true )
143
143
end
144
144
end
145
+
146
+ context 'and a custom core.hooksPath directory is set' do
147
+ around do |example |
148
+ Dir . chdir ( target ) do
149
+ FileUtils . mkdir 'my-hooks'
150
+ `git config core.hooksPath my-hooks`
151
+ example . run
152
+ end
153
+ end
154
+
155
+ it 'installs the hooks in the custom directory' do
156
+ expect { subject } . to change { hook_files_installed? ( File . join ( target , 'my-hooks' ) ) } .
157
+ from ( false ) .
158
+ to ( true )
159
+ end
160
+ end
145
161
end
146
162
147
163
context 'and an uninstall is requested' do
You can’t perform that action at this time.
0 commit comments