Skip to content

git commit hooks

Karl W. Schulz edited this page Jan 19, 2018 · 2 revisions

The following commit hook can be used to help prepare commit messages that highlight which component is being changed (in cases where only working on a single component). To use, download and place the contents in the local clone of your repository at the following location: .git/hooks/prepare-commit-msg

Note that this will only provide a starting comment if you are using an EDITOR to create your commit message (ie, no effect with git commit -m "your superfly message")

#!/usr/bin/env perl

my $filename = shift;
my $source   = shift;
my %changes  = ();

# NOOP if user supplied own message with "-m"
if ( $source eq "message" ) { exit(0); }

# Detect which component areas were updated
my $FILES=`git diff --cached --name-status -r`;
my @lines = split/\n/,$FILES;

foreach my $line (@lines) {
    if ($line =~ m|\S\s+components/([^/]+)/([^/]+)/\S+$| ) {
        $changes{"$1/$2"} = 1;
    } elsif ($line =~ m|\S\s+docs/recipes/\S+$| ) {
        $changes{"docs"} = 1;
    } elsif ($line =~ m|\S\s+tests/\S+$| ) {
        $changes{"test-suite"} = 1;
    }
}

my $numChanges = keys %changes;

open(FILE,"<",$filename);
@CONTENTS = <FILE>;
close(FILE);

open(FILE,">",$filename);

if ( $numChanges > 1 ) {
    print FILE "# [ohpc] Multiple component changes detected...please provide commit message highlighting all changes\n";
    foreach $key (keys %changes) {
        print FILE "#\n";
        print FILE "# $key\n";
        print FILE "#\n";
    }
} else {
    foreach $key (keys %changes) {
        print FILE "$key: \n";
        print FILE "#\n";
        print FILE "# [ohpc] please include (github issue #) at end of commit message if appropriate\n";
        print FILE "#\n";
    }
}

print FILE @CONTENTS;
close(FILE);
Clone this wiki locally