Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ The `overcommit` executable supports the following command-line flags:
Command Line Flag | Description
--------------------------|----------------------------------------------------
`-i`/`--install` | Install Overcommit hooks in a repository
`-d`/`--directory` | Install Overcommit hooks in a repository to <hook-type>.d/overcommit. Eg. (.git/hooks/pre-commit.d/overcommit)
`-u`/`--uninstall` | Remove Overcommit hooks from a repository
`-f`/`--force` | Don't bail on install if other hooks already exist--overwrite them
`-l`/`--list-hooks` | Display all available hooks in the current repository
Expand Down
4 changes: 4 additions & 0 deletions lib/overcommit/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def add_installation_options(opts)
@options[:action] = :install
end

opts.on('-d', '--directory', 'Use directory format for hooks: .git/hooks/<type>.d/overcommit') do
@options[:directory] = true
end

opts.on('-f', '--force', 'Overwrite any previously installed hooks') do
@options[:force] = true
end
Expand Down
18 changes: 15 additions & 3 deletions lib/overcommit/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def master_hook_install_path

def ensure_directory(path)
FileUtils.mkdir_p(path)

dirs = Overcommit::Utils.supported_hook_types(@options[:directory]).
map{|hook_path| path + '/' + File.dirname(hook_path) }.uniq.compact
dirs.each do |hook_dir|
FileUtils.mkdir_p(hook_dir)
end
end

def validate_target
Expand Down Expand Up @@ -110,7 +116,7 @@ def uninstall_master_hook
def install_hook_files
# Copy each hook type (pre-commit, commit-msg, etc.) from the master hook.
Dir.chdir(hooks_path) do
Overcommit::Utils.supported_hook_types.each do |hook_type|
Overcommit::Utils.supported_hook_types(@options[:directory]).each do |hook_type|
unless can_replace_file?(hook_type)
raise Overcommit::Exceptions::PreExistingHooks,
"Hook '#{File.expand_path(hook_type)}' already exists and " \
Expand All @@ -132,12 +138,18 @@ def preserve_old_hooks
return unless File.directory?(hooks_path)

ensure_directory(old_hooks_path)
Overcommit::Utils.supported_hook_types.each do |hook_type|
Overcommit::Utils.supported_hook_types(@options[:directory]).each do |hook_type|
hook_file = File.join(hooks_path, hook_type)
unless can_replace_file?(hook_file)
log.warning "Hook '#{File.expand_path(hook_type)}' already exists and " \
"was not installed by Overcommit. Moving to '#{old_hooks_path}'"
FileUtils.mv(hook_file, old_hooks_path)
if @options[:directory]
hook_dir = File.basename(File.dirname(hook_file))
dest_dir = old_hooks_path + "/" + hook_dir + "/"
FileUtils.mv(hook_file, dest_dir, force: true)
else
FileUtils.mv(hook_file, old_hooks_path)
end
end
end
# Remove old-hooks directory if empty (i.e. no old hooks were preserved)
Expand Down
6 changes: 4 additions & 2 deletions lib/overcommit/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ def camel_case(str)
end

# Returns a list of supported hook types (pre-commit, commit-msg, etc.)
def supported_hook_types
Dir[File.join(HOOK_DIRECTORY, '*')].
def supported_hook_types(directory=nil)
result = Dir[File.join(HOOK_DIRECTORY, '*')].
select { |file| File.directory?(file) }.
reject { |file| File.basename(file) == 'shared' }.
map { |file| File.basename(file).tr('_', '-') }
result = result.map { |file| file + ".d/overcommit" } if directory
result
end

# Returns a list of supported hook classes (PreCommit, CommitMsg, etc.)
Expand Down
2 changes: 1 addition & 1 deletion lib/overcommit/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

# Defines the gem version.
module Overcommit
VERSION = '0.58.0'
VERSION = '0.58.0-miry'
end
16 changes: 16 additions & 0 deletions spec/integration/installing_overcommit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
end
end

context 'when to folders' do
around do |example|
repo do
puts `overcommit --install --directory`
example.run
end
end

it 'leaves the hooks intact' do
Overcommit::Utils.supported_hook_types.each do |hook_type|
hook_file = File.join('.git', 'hooks', hook_type + '.d', 'overcommit')
File.read(hook_file).should include 'OVERCOMMIT'
end
end
end

context 'when template directory points to the Overcommit template directory' do
around do |example|
repo(template_dir: Overcommit::Installer::TEMPLATE_DIRECTORY) do
Expand Down
5 changes: 3 additions & 2 deletions spec/integration/template_dir_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@

it 'contains all other hooks as copies of the master hook' do
Overcommit::Utils.supported_hook_types.each do |hook_type|
FileUtils.compare_file(File.join(hooks_dir, hook_type),
File.join(hooks_dir, 'overcommit-hook')).should == true
hook_file = File.join(hooks_dir, hook_type)
master_hook_file = File.join(hooks_dir, 'overcommit-hook')
expect(FileUtils.compare_file(hook_file, master_hook_file)).to be_truthy, "#{hook_file} is not equal #{master_hook_file}"
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/overcommit/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@
# rubocop:disable Metrics/LineLength
it { should =~ %w[commit-msg pre-commit post-checkout post-commit post-merge post-rewrite pre-push pre-rebase prepare-commit-msg] }
# rubocop:enable Metrics/LineLength

context 'with directory option' do
subject { described_class.supported_hook_types(true) }
# rubocop:disable Metrics/LineLength
it { should =~ %w[commit-msg.d/overcommit pre-commit.d/overcommit post-checkout.d/overcommit post-commit.d/overcommit post-merge.d/overcommit post-rewrite.d/overcommit pre-push.d/overcommit pre-rebase.d/overcommit prepare-commit-msg.d/overcommit] }
# rubocop:enable Metrics/LineLength
end
end

describe '.supported_hook_type_classes' do
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/overcommit-hook
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/post-checkout
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/post-commit
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/post-merge
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/post-rewrite
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/pre-rebase
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down
8 changes: 8 additions & 0 deletions template-dir/hooks/prepare-commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ if hook_type == 'overcommit-hook'
exit 64 # EX_USAGE
end

# Check if the hook located in .git/hooks/<hook_type>.d/
parent_dir = File.basename(File.dirname($0))
if parent_dir != "hooks"
if parent_dir.match?(/.d$/)
hook_type = parent_dir[..-3]
end
end

# Check if Overcommit should invoke a Bundler context for loading gems
require 'yaml'
# rubocop:disable Style/RescueModifier
Expand Down