Skip to content

Commit cdb6d2e

Browse files
committedJul 19, 2016
Move hook files instead of linking them
1 parent d79942c commit cdb6d2e

12 files changed

+40
-57
lines changed
 

‎lib/overcommit/installer.rb

+8-13
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def install
3333
ensure_directory(hooks_path)
3434
preserve_old_hooks
3535
install_master_hook
36-
install_hook_symlinks
36+
install_hook_files
3737
install_starter_config
3838

3939
log.success "Successfully installed hooks into #{@target}"
@@ -42,7 +42,7 @@ def install
4242
def uninstall
4343
log.log "Removing hooks from #{@target}"
4444

45-
uninstall_hook_symlinks
45+
uninstall_hook_files
4646
uninstall_master_hook
4747
restore_old_hooks
4848

@@ -54,7 +54,7 @@ def update
5454
unless FileUtils.compare_file(MASTER_HOOK, master_hook_install_path)
5555
preserve_old_hooks
5656
install_master_hook
57-
install_hook_symlinks
57+
install_hook_files
5858

5959
log.success "Hooks updated to Overcommit version #{Overcommit::VERSION}"
6060
true
@@ -103,10 +103,8 @@ def uninstall_master_hook
103103
FileUtils.rm_rf(master_hook_install_path, secure: true)
104104
end
105105

106-
def install_hook_symlinks
107-
# Link each hook type (pre-commit, commit-msg, etc.) to the master hook.
108-
# We change directories so that the relative symlink paths work regardless
109-
# of where the repository is located.
106+
def install_hook_files
107+
# Copy each hook type (pre-commit, commit-msg, etc.) from the master hook.
110108
Dir.chdir(hooks_path) do
111109
Overcommit::Utils.supported_hook_types.each do |hook_type|
112110
unless can_replace_file?(hook_type)
@@ -115,7 +113,7 @@ def install_hook_symlinks
115113
'was not installed by Overcommit'
116114
end
117115
FileUtils.rm_f(hook_type)
118-
Overcommit::Utils::FileUtils.symlink('overcommit-hook', hook_type)
116+
FileUtils.cp('overcommit-hook', hook_type)
119117
end
120118
end
121119
end
@@ -158,7 +156,7 @@ def restore_old_hooks
158156
log.success "Successfully restored old hooks from #{old_hooks_path}"
159157
end
160158

161-
def uninstall_hook_symlinks
159+
def uninstall_hook_files
162160
return unless File.directory?(hooks_path)
163161

164162
Dir.chdir(hooks_path) do
@@ -176,10 +174,7 @@ def install_starter_config
176174
end
177175

178176
def overcommit_hook?(file)
179-
return true if File.read(file) =~ /OVERCOMMIT_DISABLE/
180-
# TODO: Remove these checks once we hit version 1.0
181-
Overcommit::Utils::FileUtils.symlink?(file) &&
182-
Overcommit::Utils::FileUtils.readlink(file) == 'overcommit-hook'
177+
File.read(file) =~ /OVERCOMMIT_DISABLE/
183178
rescue Errno::ENOENT
184179
# Some Ruby implementations (e.g. JRuby) raise an error when the file
185180
# doesn't exist. Standardize the behavior to return false.

‎spec/integration/installing_overcommit_spec.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
end
1010

1111
it 'automatically installs Overcommit hooks for new repositories' do
12-
if Overcommit::OS.windows?
13-
# Symlinks in template-dir are not compatible with Windows.
14-
# Windows users will need to manually install Overcommit for now.
15-
skip 'Unix symlinks not compatible with Windows'
16-
end
17-
1812
Overcommit::Utils.supported_hook_types.each do |hook_type|
1913
hook_file = File.join('.git', 'hooks', hook_type)
2014
File.read(hook_file).should include 'OVERCOMMIT'
@@ -26,10 +20,10 @@
2620
`overcommit --install`
2721
end
2822

29-
it 'replaces the hooks with symlinks' do
23+
it 'leaves the hooks intact' do
3024
Overcommit::Utils.supported_hook_types.each do |hook_type|
3125
hook_file = File.join('.git', 'hooks', hook_type)
32-
Overcommit::Utils::FileUtils.symlink?(hook_file).should == true
26+
File.read(hook_file).should include 'OVERCOMMIT'
3327
end
3428
end
3529
end

‎spec/overcommit/installer_spec.rb

+21-27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
let(:logger) { Overcommit::Logger.silent }
55
let(:installer) { described_class.new(logger) }
66

7+
def hook_files_installed?(hooks_dir)
8+
Overcommit::Utils.supported_hook_types.all? do |hook_type|
9+
hook_file = File.join(hooks_dir, hook_type)
10+
master_hook = File.join(hooks_dir, 'overcommit-hook')
11+
File.exist?(hook_file) && File.exist?(master_hook) &&
12+
File.read(hook_file) == File.read(master_hook)
13+
end
14+
end
15+
716
describe '#run' do
817
let(:options) { { action: :install } }
918
subject { installer.run(target.to_s, options) }
@@ -28,22 +37,19 @@
2837
let(:target) { repo }
2938
let(:hooks_dir) { File.join(target, '.git', 'hooks') }
3039
let(:old_hooks_dir) { File.join(hooks_dir, 'old-hooks') }
40+
let(:master_hook) { File.join(hooks_dir, 'overcommit-hook') }
3141

3242
context 'and an install is requested' do
3343
context 'and Overcommit hooks were not previously installed' do
3444
it 'installs the master hook into the hooks directory' do
3545
expect { subject }.to change {
36-
File.file?(File.join(hooks_dir, 'overcommit-hook'))
46+
File.file?(master_hook)
3747
}.from(false).to(true)
3848
end
3949

40-
it 'symlinks all supported hooks to the master hook' do
50+
it 'copies all supported hooks from the master hook' do
4151
expect { subject }.to change {
42-
Overcommit::Utils.supported_hook_types.all? do |hook_type|
43-
hook_file = File.join(hooks_dir, hook_type)
44-
Overcommit::Utils::FileUtils.symlink?(hook_file) &&
45-
Overcommit::Utils::FileUtils.readlink(hook_file) == 'overcommit-hook'
46-
end
52+
hook_files_installed?(hooks_dir)
4753
}.from(false).to(true)
4854
end
4955
end
@@ -55,17 +61,13 @@
5561

5662
it 'keeps the master hook' do
5763
expect { subject }.to_not change {
58-
File.file?(File.join(hooks_dir, 'overcommit-hook'))
64+
File.file?(master_hook)
5965
}.from(true)
6066
end
6167

62-
it 'maintains all symlinks to the master hook' do
68+
it 'maintains all copies of the master hook' do
6369
expect { subject }.to_not change {
64-
Overcommit::Utils.supported_hook_types.all? do |hook_type|
65-
hook_file = File.join(hooks_dir, hook_type)
66-
Overcommit::Utils::FileUtils.symlink?(hook_file) &&
67-
Overcommit::Utils::FileUtils.readlink(hook_file) == 'overcommit-hook'
68-
end
70+
hook_files_installed?(hooks_dir)
6971
}.from(true)
7072
end
7173
end
@@ -99,13 +101,9 @@
99101
expect { subject }.to_not raise_error
100102
end
101103

102-
it 'symlinks all supported hooks to the master hook' do
104+
it 'copies all supported hooks from the master hook' do
103105
expect { subject }.to change {
104-
Overcommit::Utils.supported_hook_types.all? do |hook_type|
105-
hook_file = File.join(hooks_dir, hook_type)
106-
Overcommit::Utils::FileUtils.symlink?(hook_file) &&
107-
Overcommit::Utils::FileUtils.readlink(hook_file) == 'overcommit-hook'
108-
end
106+
hook_files_installed?(hooks_dir)
109107
}.from(false).to(true)
110108
end
111109
end
@@ -156,17 +154,13 @@
156154

157155
it 'removes the master hook from the hooks directory' do
158156
expect { subject }.to change {
159-
File.exist?(File.join(hooks_dir, 'overcommit-hook'))
157+
File.exist?(master_hook)
160158
}.from(true).to(false)
161159
end
162160

163-
it 'removes all symlinks from the hooks directory' do
161+
it 'removes all hook files from the hooks directory' do
164162
expect { subject }.to change {
165-
Overcommit::Utils.supported_hook_types.all? do |hook_type|
166-
hook_file = File.join(hooks_dir, hook_type)
167-
Overcommit::Utils::FileUtils.symlink?(hook_file) &&
168-
Overcommit::Utils::FileUtils.readlink(hook_file) == 'overcommit-hook'
169-
end
163+
hook_files_installed?(hooks_dir)
170164
}.from(true).to(false)
171165
end
172166
end

‎template-dir/hooks/commit-msg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
# Entrypoint for Overcommit hook integration. Installing Overcommit will result
4-
# in all of your git hooks being symlinked to this file, allowing the framework
4+
# in all of your git hooks being copied from this file, allowing the framework
55
# to manage your hooks for you.
66

77
# Prevent a Ruby stack trace from appearing when we interrupt the hook.

‎template-dir/hooks/overcommit-hook

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
# Entrypoint for Overcommit hook integration. Installing Overcommit will result
4-
# in all of your git hooks being symlinked to this file, allowing the framework
4+
# in all of your git hooks being copied from this file, allowing the framework
55
# to manage your hooks for you.
66

77
# Prevent a Ruby stack trace from appearing when we interrupt the hook.

‎template-dir/hooks/post-checkout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
# Entrypoint for Overcommit hook integration. Installing Overcommit will result
4-
# in all of your git hooks being symlinked to this file, allowing the framework
4+
# in all of your git hooks being copied from this file, allowing the framework
55
# to manage your hooks for you.
66

77
# Prevent a Ruby stack trace from appearing when we interrupt the hook.

‎template-dir/hooks/post-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
# Entrypoint for Overcommit hook integration. Installing Overcommit will result
4-
# in all of your git hooks being symlinked to this file, allowing the framework
4+
# in all of your git hooks being copied from this file, allowing the framework
55
# to manage your hooks for you.
66

77
# Prevent a Ruby stack trace from appearing when we interrupt the hook.

‎template-dir/hooks/post-merge

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
# Entrypoint for Overcommit hook integration. Installing Overcommit will result
4-
# in all of your git hooks being symlinked to this file, allowing the framework
4+
# in all of your git hooks being copied from this file, allowing the framework
55
# to manage your hooks for you.
66

77
# Prevent a Ruby stack trace from appearing when we interrupt the hook.

‎template-dir/hooks/post-rewrite

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
# Entrypoint for Overcommit hook integration. Installing Overcommit will result
4-
# in all of your git hooks being symlinked to this file, allowing the framework
4+
# in all of your git hooks being copied from this file, allowing the framework
55
# to manage your hooks for you.
66

77
# Prevent a Ruby stack trace from appearing when we interrupt the hook.

‎template-dir/hooks/pre-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
# Entrypoint for Overcommit hook integration. Installing Overcommit will result
4-
# in all of your git hooks being symlinked to this file, allowing the framework
4+
# in all of your git hooks being copied from this file, allowing the framework
55
# to manage your hooks for you.
66

77
# Prevent a Ruby stack trace from appearing when we interrupt the hook.

‎template-dir/hooks/pre-push

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
# Entrypoint for Overcommit hook integration. Installing Overcommit will result
4-
# in all of your git hooks being symlinked to this file, allowing the framework
4+
# in all of your git hooks being copied from this file, allowing the framework
55
# to manage your hooks for you.
66

77
# Prevent a Ruby stack trace from appearing when we interrupt the hook.

‎template-dir/hooks/pre-rebase

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
# Entrypoint for Overcommit hook integration. Installing Overcommit will result
4-
# in all of your git hooks being symlinked to this file, allowing the framework
4+
# in all of your git hooks being copied from this file, allowing the framework
55
# to manage your hooks for you.
66

77
# Prevent a Ruby stack trace from appearing when we interrupt the hook.

0 commit comments

Comments
 (0)
Please sign in to comment.