Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prints file's history ready for inclusion in LaTeX documents
#!/usr/bin/env perl
use warnings;
use strict;
use Git;
use File::Basename;
use Date::Parse;
use Date::Format;
# States
use constant {
LOOKING_FOR_COMMIT => 0,
LOOKING_FOR_DATE => 1,
LOOKING_FOR_COMMENT => 2,
LOOKING_FOR_AUTHOR => 3
};
sub print_version {
my ($number, $commit, $comment, $date, $author) = @_;
print "$number & " . time2str("%d.%m.%Y", $date) .
" & $author & $comment \\\\\n";
}
my $filename = shift @ARGV or die 'Missing filename';
my $repo = Git->repository(dirname $filename) or die $!;
my $state = LOOKING_FOR_COMMIT;
my ($commit, $date, $author, @versions);
for ($repo->command('log', '--no-merges', $filename)) {
if ($state == LOOKING_FOR_COMMIT) {
next unless /^commit ([a-f0-9])+$/;
$commit = $1;
$state = LOOKING_FOR_AUTHOR;
} elsif ($state == LOOKING_FOR_AUTHOR) {
next unless /^Author:\s+(.+?)</;
$author = $1;
$state = LOOKING_FOR_DATE;
} elsif ($state == LOOKING_FOR_DATE) {
next unless /^Date:\s+(.+)$/;
$date = str2time($1);
$state = LOOKING_FOR_COMMENT;
} elsif ($state == LOOKING_FOR_COMMENT) {
next unless /^\s+(\S.*)$/;
my @list = ($commit, $1, $date, $author);
push @versions, \@list;
$state = LOOKING_FOR_COMMIT;
} else {
die "Unexpected state: $state"
}
}
my $commit_number = 0;
for (reverse @versions) {
unshift @$_, $commit_number += 1;
print_version @$_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment