Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 149 lines (113 sloc) 3.33 KB
#!/usr/bin/perl
use strict;
my $APP = 'git-rec';
our $VERSION = '0.200';
use Getopt::Long;
use Pod::Usage;
usage() if !@ARGV;
our($cmd, $dir, $branch) = (undef) x 3;
GetOptions(
'c|md:s' => \$cmd,
'd|ir:s' => \$dir,
'b|ranch:s' => \$branch,
'h|help' => sub { pod2usage(verbose => 1) and exit; },
'm|man' => sub { pod2usage(verbose => 3) and exit; },
'v|version' => sub { print "$APP v$VERSION\n" and exit; },
);
($cmd) = $cmd ? $cmd : 'pull';
($dir) = $dir ? $dir : "$ENV{HOME}/devel";
($branch) = $branch ? $branch : 'origin master';
cmd($dir, $cmd);
sub cmd {
my($start, $operation) = @_;
$start =~ s;/$;;;
my @content = grep{! /(?:\.\.?)$/ }<$start/.* $start/*>;
for(@content) {
if(-d $_) {
if($_ =~ /\.git$/) {
my ($basename) = $_ =~ m;(.+)/\.git;;
my ($project) = $_ =~ m;.+/(.+)/\.git;;
my $status = undef;
if($operation eq 'pull') {
chomp($status = `cd $basename && git $cmd $branch 2> /dev/null`);
if($status =~ /Already up-to-date\./) {
printf("\e[1m::\e[38;5;178m %s\e[0m\n\e[38;5;34m Up to date\e[0m\n", $project);
next;
}
else {
printf("\e[1m::\e[38;5;178m %s\e[0m - \e[38;5;162mPulling\e[0m\n", $project);
print "$status\n";
next;
}
}
elsif($operation eq 'status') {
my @records;
chomp(@records = `cd $basename && git status -s`);
@records = git_status(@records);
printf("\e[1m::\e[38;5;178m %s\e[0m\n", $project);
printf(" %s\n", $_) for @records;
}
elsif($operation eq 'log') {
my @records;
chomp(@records = `cd $basename && git --no-pager log`);
@records = git_log(@records);
print "$_\n" for @records;
}
else {
print $operation;
}
}
else {
cmd($_, $operation);
}
}
}
return 0;
}
sub git_status {
for(@_) {
s/^\?\? (.+)$/\e[38;5;137m\e[1mUntracked: \e[0m$1/gms;
s/^ M (.+)$/\e[38;5;34m\e[1m Modified: \e[0m$1\e[0m/gms;
s/^ D (.+)$/\e[38;5;244m\e[1m Deleted: \e[1m$1\e[0m/gms;
}
return(@_);
}
sub git_log {
for(@_) {
s/^(commit) (.+)$/\e[1m$1 \e[0m\e[3m\e[38;5;197m$2\e[0m/;
s/^(Author:) (.+) <(.+)>$/$1 \e[1m\e[38;5;65m$2\e[0m < \e[38;5;208m$3\e[0m >\e[0m/;
s/^(Date) (.+)$/\e[1m$1 \e[0m\e[3m$2\e[0m/;
s/^\s+ (.+)/\e[38;5;249m\t$1\e[0m/;
}
return(@_);
}
sub usage {
print "$APP $VERSION\n";
pod2usage(verbose => 1);
exit(0);
}
=pod
=head1 NAME
git-rec - perform git stuff recursively
=head1 SYNOPSIS
git-rec -d [DIRECTORY] -c [COMMAND] -b [BRANCH]
=head1 OPTIONS
-d, --dir root directory; default: $HOME/devel
-c, --cmd git command; default: pull
-b, --branch branch; default: master
-h, --help show the help and exit
-v, --version show version info and exit
-m, --man show the manpage and exit
=head1 AUTHOR
Magnus Woldrich
CPAN ID: WOLDRICH
magnus@trapd00r.se
http://japh.se
=head1 CONTRIBUTORS
None required yet.
=head1 COPYRIGHT
Copyright 2011 B<THIS PROGRAM>s L</AUTHOR> and L</CONTRIBUTORS> as listed above.
=head1 LICENSE
This program is free software; you may redistribute it and/or modify it under
the same terms as Perl itself.
=cut