Skip to content

Commit d32098f

Browse files
jawshooahsds
authored andcommitted
Use namespaced FileUtils module instead of patching File
1 parent 0ae1398 commit d32098f

File tree

9 files changed

+85
-66
lines changed

9 files changed

+85
-66
lines changed

lib/overcommit.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'overcommit/os'
2-
require 'overcommit/extend/file'
32
require 'overcommit/constants'
43
require 'overcommit/exceptions'
4+
require 'overcommit/utils/file_utils'
55
require 'overcommit/utils'
66
require 'overcommit/git_version'
77
require 'overcommit/configuration_validator'

lib/overcommit/extend/file.rb

-55
This file was deleted.

lib/overcommit/installer.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ def install_starter_config
177177
def overcommit_hook?(file)
178178
return true if File.read(file) =~ /OVERCOMMIT_DISABLE/
179179
# TODO: Remove these checks once we hit version 1.0
180-
File.symlink?(file) && File.readlink(file) == 'overcommit-hook'
180+
Overcommit::Utils::FileUtils.symlink?(file) &&
181+
Overcommit::Utils::FileUtils.readlink(file) == 'overcommit-hook'
181182
rescue Errno::ENOENT
182183
# Some Ruby implementations (e.g. JRuby) raise an error when the file
183184
# doesn't exist. Standardize the behavior to return false.

lib/overcommit/utils.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def with_environment(env)
230230
def broken_symlink?(file)
231231
# JRuby's implementation of File.exist? returns true for broken
232232
# symlinks, so we need use File.size?
233-
File.symlink?(file) && File.size?(file).nil?
233+
Overcommit::Utils::FileUtils.symlink?(file) && File.size?(file).nil?
234234
end
235235

236236
# Convert a glob pattern to an absolute path glob pattern rooted from the

lib/overcommit/utils/file_utils.rb

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require 'overcommit/os'
2+
require 'overcommit/subprocess'
3+
4+
module Overcommit::Utils
5+
# Utility functions for file IO.
6+
module FileUtils
7+
class << self
8+
# When the host OS is Windows, uses the `mklink` command to create an
9+
# NTFS symbolic link from `new_name` to `old_name`. Otherwise delegates
10+
# to `File.symlink`
11+
def symlink(old_name, new_name)
12+
return File.symlink(old_name, new_name) unless Overcommit::OS.windows?
13+
14+
result = win32_mklink_cmd(old_name, new_name)
15+
result.status
16+
end
17+
18+
# When the host OS is Windows, uses the `dir` command to check whether
19+
# `file_name` is an NTFS symbolic link. Otherwise delegates to
20+
# `File.symlink`.
21+
def symlink?(file_name)
22+
return File.symlink?(file_name) unless Overcommit::OS.windows?
23+
24+
result = win32_dir_cmd(file_name)
25+
win32_symlink?(result.stdout)
26+
end
27+
28+
# When the host OS is Windows, uses the `dir` command to check whether
29+
# `link_name` is an NTFS symbolic link. If so, it parses the target from
30+
# the command output. Otherwise raises an `ArgumentError`. Delegates to
31+
# `File.readlink` if the host OS is not Windows.
32+
def readlink(link_name)
33+
return File.readlink(link_name) unless Overcommit::OS.windows?
34+
35+
result = win32_dir_cmd(link_name)
36+
37+
unless win32_symlink?(result.stdout)
38+
raise ArgumentError, "#{link_name} is not a symlink"
39+
end
40+
41+
# Extract symlink target from output, which looks like:
42+
# 11/13/2012 12:53 AM <SYMLINK> mysymlink [C:\Windows\Temp\somefile.txt]
43+
result.stdout[/\[(.+)\]/, 1]
44+
end
45+
46+
private
47+
48+
def win32_dir_cmd(file_name)
49+
Overcommit::Subprocess.spawn(
50+
%W[dir #{win32_fix_pathsep(file_name)}]
51+
)
52+
end
53+
54+
def win32_mklink_cmd(old_name, new_name)
55+
Overcommit::Subprocess.spawn(
56+
%W[mklink #{win32_fix_pathsep(new_name)} #{win32_fix_pathsep(old_name)}]
57+
)
58+
end
59+
60+
def win32_fix_pathsep(path)
61+
path.gsub('/', '\\')
62+
end
63+
64+
def win32_symlink?(dir_output)
65+
!(dir_output =~ /<SYMLINK>/).nil?
66+
end
67+
end
68+
end
69+
end

spec/integration/installing_overcommit_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
it 'replaces the hooks with symlinks' do
3030
Overcommit::Utils.supported_hook_types.each do |hook_type|
3131
hook_file = File.join('.git', 'hooks', hook_type)
32-
File.symlink?(hook_file).should == true
32+
Overcommit::Utils::FileUtils.symlink?(hook_file).should == true
3333
end
3434
end
3535
end

spec/integration/template_dir_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
master_hook = File.join(hooks_dir, 'overcommit-hook')
1414
File.exist?(master_hook).should == true
1515
File.size?(master_hook).should > 0
16-
File.symlink?(master_hook).should == false
16+
Overcommit::Utils::FileUtils.symlink?(master_hook).should == false
1717
end
1818

1919
it 'contains all other hooks as symlinks to the master hook' do
@@ -24,7 +24,7 @@
2424
end
2525

2626
Overcommit::Utils.supported_hook_types.each do |hook_type|
27-
File.symlink?(File.join(hooks_dir, hook_type)).should == true
27+
Overcommit::Utils::FileUtils.symlink?(File.join(hooks_dir, hook_type)).should == true
2828
end
2929
end
3030
end

spec/overcommit/hook_context/pre_commit_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
context 'when a broken symlink is staged' do
202202
around do |example|
203203
repo do
204-
File.symlink('non-existent-file', 'symlink')
204+
Overcommit::Utils::FileUtils.symlink('non-existent-file', 'symlink')
205205
`git add symlink`
206206
example.run
207207
end

spec/overcommit/installer_spec.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
expect { subject }.to change {
4242
Overcommit::Utils.supported_hook_types.all? do |hook_type|
4343
hook_file = File.join(hooks_dir, hook_type)
44-
File.symlink?(hook_file) && File.readlink(hook_file) == 'overcommit-hook'
44+
Overcommit::Utils::FileUtils.symlink?(hook_file) &&
45+
Overcommit::Utils::FileUtils.readlink(hook_file) == 'overcommit-hook'
4546
end
4647
}.from(false).to(true)
4748
end
@@ -62,7 +63,8 @@
6263
expect { subject }.to_not change {
6364
Overcommit::Utils.supported_hook_types.all? do |hook_type|
6465
hook_file = File.join(hooks_dir, hook_type)
65-
File.symlink?(hook_file) && File.readlink(hook_file) == 'overcommit-hook'
66+
Overcommit::Utils::FileUtils.symlink?(hook_file) &&
67+
Overcommit::Utils::FileUtils.readlink(hook_file) == 'overcommit-hook'
6668
end
6769
}.from(true)
6870
end
@@ -101,7 +103,8 @@
101103
expect { subject }.to change {
102104
Overcommit::Utils.supported_hook_types.all? do |hook_type|
103105
hook_file = File.join(hooks_dir, hook_type)
104-
File.symlink?(hook_file) && File.readlink(hook_file) == 'overcommit-hook'
106+
Overcommit::Utils::FileUtils.symlink?(hook_file) &&
107+
Overcommit::Utils::FileUtils.readlink(hook_file) == 'overcommit-hook'
105108
end
106109
}.from(false).to(true)
107110
end
@@ -161,7 +164,8 @@
161164
expect { subject }.to change {
162165
Overcommit::Utils.supported_hook_types.all? do |hook_type|
163166
hook_file = File.join(hooks_dir, hook_type)
164-
File.symlink?(hook_file) && File.readlink(hook_file) == 'overcommit-hook'
167+
Overcommit::Utils::FileUtils.symlink?(hook_file) &&
168+
Overcommit::Utils::FileUtils.readlink(hook_file) == 'overcommit-hook'
165169
end
166170
}.from(true).to(false)
167171
end

0 commit comments

Comments
 (0)