summaryrefslogtreecommitdiff
path: root/bin/install-git-hooks
blob: ea070e7d5569483b03f6d8a07c053a89192bc38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 strict;
use warnings;

use Cwd qw(getcwd);
use List::Util qw(uniq);
use File::Spec::Functions qw(catfile);

die "usage: install-git-hooks NAME\n" unless @ARGV == 1;
my $target = $ENV{MR_REPO} || getcwd;

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 $target;
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;
}