Skip to content

Commit

Permalink
Expand tildes(~) for hooksPath (#853)
Browse files Browse the repository at this point in the history
While installing overcommit git hooks i noticed that tildes are not
expanded to home folders for the `hooksPath` config, the result was a
tilde folder in the current directory.

This PR addresses that by switching `File.absolute_path` to
`File.expand_path`. The [underlaying
implementation](https://github.com/ruby/ruby/blob/v3_3_4/file.c#L3753)
in Ruby is exactly the same just with this difference in how `~` is
handled.

---------

Co-authored-by: Shane da Silva <[email protected]>
  • Loading branch information
anakinj and sds authored Aug 11, 2024
1 parent 5a3d68e commit 31c83ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/overcommit/git_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def hooks_path
path = `git config --get core.hooksPath`.chomp
return File.join(Overcommit::Utils.git_dir, 'hooks') if path.empty?

File.absolute_path(path, Dir.pwd)
File.expand_path(path, Dir.pwd)
end
end
end
14 changes: 14 additions & 0 deletions spec/overcommit/git_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,19 @@
expect(subject).to eq File.expand_path('my-hooks')
end
end

context 'when explicitly set to a path starting with a tilde' do
around do |example|
repo do
`git config --local core.hooksPath ~/my-hooks`
example.run
end
end

it 'returns the absolute path to the folder in the users home path' do
expect(subject).to eq File.expand_path('~/my-hooks')
expect(subject).not_to include('~')
end
end
end
end

0 comments on commit 31c83ce

Please sign in to comment.