|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# Entrypoint for Overcommit hook integration. Installing Overcommit will result |
| 4 | +# in all of your git hooks being symlinked to this file, allowing the framework |
| 5 | +# to manage your hooks for you. |
| 6 | + |
| 7 | +# Prevent a Ruby stack trace from appearing when we interrupt the hook. |
| 8 | +# Note that this will be overridden when Overcommit is loaded, since the |
| 9 | +# InterruptHandler will redefine the trap at that time. |
| 10 | +Signal.trap('INT') do |
| 11 | + puts 'Hook run interrupted' |
| 12 | + exit 130 |
| 13 | +end |
| 14 | + |
| 15 | +# Allow hooks to be disabled via environment variable so git commands can be run |
| 16 | +# in scripts without Overcommit running hooks |
| 17 | +if ENV['OVERCOMMIT_DISABLE'].to_i != 0 || ENV['OVERCOMMIT_DISABLED'].to_i != 0 |
| 18 | + exit |
| 19 | +end |
| 20 | + |
| 21 | +hook_type = File.basename($0) |
| 22 | +if hook_type == 'overcommit-hook' |
| 23 | + puts "Don't run `overcommit-hook` directly; it is intended to be symlinked " \ |
| 24 | + "by each hook in a repository's .git/hooks directory." |
| 25 | + exit 64 # EX_USAGE |
| 26 | +end |
| 27 | + |
| 28 | +begin |
| 29 | + require 'overcommit' |
| 30 | +rescue LoadError |
| 31 | + puts 'This repository contains hooks installed by Overcommit, but the ' \ |
| 32 | + "overcommit gem is not installed.\n" \ |
| 33 | + 'Install it with `gem install overcommit`.' |
| 34 | + exit |
| 35 | +end |
| 36 | + |
| 37 | +begin |
| 38 | + logger = Overcommit::Logger.new(STDOUT) |
| 39 | + |
| 40 | + # Ensure master hook is up-to-date |
| 41 | + installer = Overcommit::Installer.new(logger) |
| 42 | + if installer.run(Overcommit::Utils.repo_root, action: :update) |
| 43 | + exec($0, *ARGV) # Execute the updated hook with all original arguments |
| 44 | + end |
| 45 | + |
| 46 | + config = Overcommit::ConfigurationLoader.load_repo_config |
| 47 | + |
| 48 | + context = Overcommit::HookContext.create(hook_type, config, ARGV) |
| 49 | + config.apply_environment!(context, ENV) |
| 50 | + |
| 51 | + printer = Overcommit::Printer.new(logger, context) |
| 52 | + runner = Overcommit::HookRunner.new(config, logger, context, printer) |
| 53 | + |
| 54 | + status = runner.run |
| 55 | + |
| 56 | + exit(status ? 0 : 65) # 65 = EX_DATAERR |
| 57 | +rescue Overcommit::Exceptions::ConfigurationError => error |
| 58 | + puts error |
| 59 | + exit 78 # EX_CONFIG |
| 60 | +rescue Overcommit::Exceptions::HookContextLoadError => error |
| 61 | + puts error |
| 62 | + puts 'Are you running an old version of Overcommit?' |
| 63 | + exit 69 # EX_UNAVAILABLE |
| 64 | +rescue Overcommit::Exceptions::HookSetupFailed, |
| 65 | + Overcommit::Exceptions::HookCleanupFailed => error |
| 66 | + puts error.message |
| 67 | + exit 74 # EX_IOERR |
| 68 | +rescue Overcommit::Exceptions::HookCancelled |
| 69 | + puts 'You cancelled the hook run' |
| 70 | + exit 130 # Ctrl-C cancel |
| 71 | +rescue Overcommit::Exceptions::InvalidGitRepo => error |
| 72 | + puts error |
| 73 | + exit 64 # EX_USAGE |
| 74 | +rescue Overcommit::Exceptions::InvalidHookSignature |
| 75 | + exit 1 |
| 76 | +rescue => error |
| 77 | + puts error.message |
| 78 | + puts error.backtrace |
| 79 | + puts "Report this bug at #{Overcommit::BUG_REPORT_URL}" |
| 80 | + exit 70 # EX_SOFTWARE |
| 81 | +end |
0 commit comments