#!/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 . 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; }