unixorn/git-extra-commands

View on GitHub
bin/git-force-mtimes

Summary

Maintainability
Test Coverage
#!/usr/bin/env perl
#
# Source: https://github.com/jwiegley/git-scripts/blob/master/git-force-mtimes
#
# Sets mtimes of all files in the reprository their last change date
# based on git's log. Useful to avoid too new dates after a checkout.

my %seen;
my $date;
open(LOG, "git log --pretty=format:'date: %ct' --name-only |") || die "git log failed: $!";
while (<LOG>) {
    chomp;
    if (/^date: (\d+)$/) {
        $date=$1;
    }
    elsif (! /^$/ && ! $seen{$_}) {
        $seen{$_}=1;
        # git log can list deleted files, so error is ignored
        utime($date, $date, $_);
    }
}
close LOG || die "git log failed: $!";