diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2020-02-02 12:16:53 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2020-02-02 12:16:53 -0700 |
commit | 0506efebfd091c8f8e30be323d75f456c03e005f (patch) | |
tree | 97345053c438053f65ba9dfba4b58a472a6d288e /bin/install-git-hooks | |
parent | 3a7ad6cb7da8ef30bd12ce3ecdfd0c03a02cde44 (diff) | |
download | dotfiles-0506efebfd091c8f8e30be323d75f456c03e005f.tar.gz |
all git hooks use chained_hook script
Diffstat (limited to 'bin/install-git-hooks')
-rwxr-xr-x | bin/install-git-hooks | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/bin/install-git-hooks b/bin/install-git-hooks new file mode 100755 index 00000000..1d461041 --- /dev/null +++ b/bin/install-git-hooks @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +# Copyright (C) 2020 Sean Whitton +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +use 5.028; +use strict; +use warnings; + +use File::Spec::Functions qw(catfile); +use List::Util qw(uniq); + +die "usage: install-git-hooks NAME\n" unless @ARGV == 1; +die "must be run by mr(1)\n" unless $ENV{MR_REPO}; + +my $source = catfile $ENV{HOME}, qw(src dotfiles hooks git), $ARGV[0]; +my $chained_hook = catfile $ENV{HOME}, qw(src dotfiles hooks git chained_hook); + +opendir(my $dirh, $source) or die "could not find $source dir\n"; +my @hooks = grep { $_ ne '.' and $_ ne '..' } readdir $dirh; +my @hook_types = uniq map { /^([^_]+)_/; $1 // () } @hooks; + +chdir $ENV{MR_REPO}; +my $config_hooks_path = `git config core.hooksPath`; +chomp(my $hook_dir = $config_hooks_path || `git rev-parse --git-path hooks`); + +foreach my $hook (@hooks) { + unlink catfile $hook_dir, $hook; + symlink catfile($source, $hook), catfile($hook_dir, $hook); +} + +foreach my $hook_type (@hook_types) { + unlink catfile $hook_dir, $hook_type; + symlink $chained_hook, catfile $hook_dir, $hook_type; +} |