Skip to content

Instantly share code, notes, and snippets.

@dancingfrog
Last active January 4, 2016 17:24
Show Gist options
  • Save dancingfrog/869f965f8c3eb45049cc to your computer and use it in GitHub Desktop.
Save dancingfrog/869f965f8c3eb45049cc to your computer and use it in GitHub Desktop.
git-check.pl
#!/usr/bin/perl
use strict;
use warnings;
use Git;
use Fcntl qw(:flock SEEK_END);
my $git_dir = $ARGV[0];
die "Must provide path to directory containing git projects:\n".
"Ex.\n".
"./git-check.pl /home/john/projects\n\n" if(! $git_dir );
print "Using Git.pm: ". Git::command_oneline('version');
print "\n";
# Subroutines for using flock on data files
sub lock {
#no strict 'subs';
my ($fh) = @_;
flock($fh, LOCK_EX) or die "Cannot lock data file - $!\n";
}
sub unlock {
#no strict 'subs';
my ($fh) = @_;
flock($fh, LOCK_UN) or die "Cannot unlock data file - $!\n";
}
my @gits = $ARGV[1];
@gits = ( # List of installed sites to be checked by default
'poly-game',
'uni-sol',
'www'
) if(! $gits[0] );
#while( sleep 333 ) {
for( @gits ) {
# Create file handle and open/lock the input file
my $inputf;
my $file = "/home/git/$_.git/git-check.txt";
#$file = $ARGV[0] or die "Invalid output filename";
#my( $safe_file_name ) = $file =~ /^([\/|\w]+.*)$/;
#if ( !$safe_file_name ) {
# die "Invalid output filename";
#}
#open $inputf, '<', $safe_file_name or die "Can't open $safe_file_name: $!\n";
my $saved_git_rev;
if( open $inputf, '<', $file ) { #die "Can't open $file: $!\n";
lock($inputf);
while (my $line = <$inputf>) {
chop $line;
#print $line;
if( $line =~ /$_\.git\:\s(\w+)/ ) {
$saved_git_rev = $1;
}
}
#print "$saved_git_rev \n";
unlock($inputf);
close $inputf;
} else {
$saved_git_rev = undef;
}
my $outputf;
#open $outputf, '>', $safe_file_name or die "Can't open $safe_file_name: $!\n";
open $outputf, '>', $file or die "Can't open $file: $!\n";
lock($outputf);
print "\n";
my $repo = Git->repository (Directory => "/home/git/$_.git");
my @revs = $repo->command('rev-list', '--all');
my $current_git_rev = $repo->command_oneline( ['rev-list', '--all'], STDERR => 0 );
unless( ($saved_git_rev) && ($saved_git_rev ne $current_git_rev) ) {
print "/home/git/$_.git: $current_git_rev \n";
print $outputf "/home/git/$_.git: $current_git_rev \n";
#for my $rev( @revs ){ print "$rev \n"; }
} else {
print "$_.git has been updated!\n";
print "$_.git: $current_git_rev \n";
print $outputf "$_.git: $current_git_rev \n";
}
$repo = Git->repository (Directory => "$git_dir/$_");
my $release_rev = $repo->command_oneline( ['rev-list', '--all'], STDERR => 0 );
unless( $current_git_rev ne $release_rev ) {
print "revlin/$_: $release_rev \n";
print $outputf "revlin/$_: $release_rev \n";
} else {
print "revlin/$_ is out-of-date!\n";
print "revlin/$_: $release_rev \n";
print $outputf "revlin/$_: $release_rev \n";
my @pulls = $repo->command('pull');
for my $pull( @pulls ){
print "$pull \n";
my $subs = $repo->command('submodule','update');
print "$subs\n" if( $subs );
}
}
unlock($outputf);
close $outputf;
}
print "\n";
#}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment