#!/usr/bin/env perl

use warnings;
use autodie;
use strict;

print "Running update hook...\n";

# Hook take three parameters: the name of ref to update, old object name, new object name
my ($refname, $old, $new) = @ARGV;

# Check that we are going to update ref/heads/master
if ($refname eq 'refs/heads/master') {
    print "Update for $refname\n";
    print "OLD is: $old\n";
    print "NEW is: $new\n";
    # Get a list of commit ids from $old to $new
    chomp(my @revisions = `git rev-list --reverse $old..$new`);
    # Iterate through a list of commit ids
    foreach (@revisions) {
        print("CURRENT: $_\n");
        # Get a parent commit id for the $_
        chomp(my $parent = (split ' ', `git rev-list --parents -n 1 $_`)[-1]);
        print "PARENT: $parent\n";
        # Get a list of files changed between $parent and $_
        chomp(my @changed_files = `git log --pretty=\"format:\" --name-only $parent..$_`);
        print "CHANGED FILES: @changed_files\n";
    }
} else {
    # Exit with non-zero value, so push is rejected
    exit 1;
}
