Git tips: Getting the first commit date of a file

Up a Level

USAGE: git-first-commit-date [–bare|-b] file…

##

Setup

##

Directives

use strict; use warnings;

Modules

use Getopt::Long;

##

Options

##

–bare means don't put the filename in the line. Otherwise it will

–put the filename, followed by a colon and a space.

my $bare = 0;

&GetOptions( “b|bare!” => $bare, );

##

Go through the input files.

##

while (@ARGV) { # Pull out the filename. my $filename = shift @ARGV; my $reason = "<missing>"; my $valid = 0;

if (-f $filename)
{
    # Get the date for the file. We tell Git to only give us the
    # ISO date (https://xkcd.com/1179/) for the files using
    # --pretty=format:%ad --date=short. We use --follow to handle
    # renames. Finally, we get the last one (the earliest
    # date). --reverse didn't seem to work, so we skip that.
    $reason = `git log --follow --pretty=format:%ad --date=short "$filename" | tail -n 1`;
    chomp $reason;

    # If we have a date, use it. Otherwise say it is untracked.
    $reason = "" if $reason =~ /^\s*$/s;
}

# Write out the results.
if (!$bare)
{
    print "$filename: ";
}

# Print out the reason which will be , , or a
# date.
print $reason, "\n";

}

Metadata

Categories:

Programming

Tags:

Perl

Footer

Below are various useful links within this site and to related sites (not all have been converted over to Gemini).

Now
Contact
Biography
Bibliography
Support
Fiction
Fedran
Coding
The Moonfires
Categories
Tags
Privacy
Colophon
License
Mailing List