The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/env perl
# created on 2013-09-22

use warnings;
use strict;
use 5.010;

use Capture::Tiny qw/capture_stdout/;

my $ref;
my $err;
$ref = capture_stdout {
  $err = 1 if ( system("git rev-parse --short --verify HEAD 2>/dev/null || exit 1") );
};
chomp $ref;

my $fh = \*STDOUT;
if ($err) {
  $ref = "unversioned";
  say $fh "\\newcommand{\\gitinfo}{$ref}";
  say $fh "\\newcommand{\\gitinfofancy}{$ref}";
} else {
  system("git update-index -q --ignore-submodules --refresh 2>/dev/null");
  if ( system("git diff-files --quiet --ignore-submodules 2>/dev/null") ) {
    $ref .= ",dirty";
  }
  if ( system("git diff-index --cached --quiet --ignore-submodules HEAD -- 2>/dev/null") ) {
    $ref .= ",staged";
  }
  say $fh "\\newcommand{\\gitinfo}{$ref}";
  say $fh "\\newcommand{\\gitinfofancy}{v. $ref}";
}

#open my $fh, '>', "gitinfo.tex" or die "Can't open filehandle: $!";
close $fh;