#!/usr/bin/perl
# Created on: 2013-05-04 16:16:56
# Create by: Ivan Wills
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$
use strict;
use warnings;
use version;
use Getopt::Alt qw/get_options/;
use Pod::Usage;
use Data::Dumper qw/Dumper/;
use English qw/ -no_match_vars /;
use FindBin qw/$Bin/;
use Path::Class;
use Group::Git;
use Config::Any;
use Term::ANSIColor qw/colored/;
our $VERSION = version->new('0.1.2');
my ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
main();
exit 0;
sub main {
my ($opt, $cmd) = get_options(
{
sub_command => 1,
default => {
page => 1,
}
},
[
'skip|s=s',
'match|m=s',
'local|l!',
'recurse|r!',
'page|p!',
'tag|t=s',
'test|T!',
'verbose|v+',
'quiet|q',
]
);
# do stuff here
my $conf = Config::Any->load_stems({
stems => [ qw/group group-git/ ],
use_ext => 1,
});
$conf = {
map { %$_ }
map { values %$_ }
@{$conf}
};
my $module = $conf->{type} && !$opt->local ? 'Group::Git::' . ucfirst $conf->{type} : 'Group::Git';
my $file = "$module.pm";
$file =~ s{::}{/}g;
require $file;
my $group = $module->new(
%{ $opt },
conf => $conf,
);
my $action = $cmd || ( warn "No command specified!\n" && pod2usage( -verbose => 1 ) );
my $skip = $opt->skip;
my $match = $opt->match;
my %exclude = map { $_ => 1 } @{ $conf->{exclude} || [] };
my %include = map { $_ => 1 } @{ $conf->{include} || [] };
my $outfh = \*STDOUT;
if ($opt->page) {
my $tmp;
$outfh = $tmp if open $tmp, '|-', $ENV{PAGER} || 'less -Rx4SFX';
}
my $cmd_start = do_cmd( $group, $action . '_start' );
my $cmd_end = do_cmd( $group, $action . '_end' );
my $cmd_body = do_cmd( $group, $action, 1 );
die "Can't find group-git sub-command '$action'!\n" if !$cmd_body;
print {$outfh} $cmd_start->() if $cmd_start;
for my $project ( sort sorter keys %{ $group->repos } ) {
next if $skip && $project =~ /$skip/;
next if $match && $project !~ /$match/;
next if $exclude{$project};
next if %include && !$include{$project};
next if !tagged($conf, $opt->tag, $project);
my $out = $cmd_body->($project);
if ($out) {
$project = colored($project, 'blue');
print {$outfh} "\n$project\n" unless $opt->quiet;
print {$outfh} $out;
print {$outfh} "-- $project --\n" if $opt->verbose;
}
}
print {$outfh} $cmd_end->() if $cmd_end;
return;
}
sub do_cmd {
my ($group, $cmd, $fallback) = @_;
if ( $group->can($cmd) ) {
return sub { $group->$cmd( @_ ) };
}
else {
for my $path ( split /[:;]/, $ENV{PATH} ) {
next if !-d $path;
my $script = "$path/group-git-$cmd";
next if !-x $script;
return sub { system $script, @_; }
}
}
return if !$fallback;
return sub { $group->cmd( $cmd, @_ ) };
}
sub sorter {
no warnings qw/once/;
my $A = $a;
$A =~ s/(\d+)/sprintf "%06d", $1/egxms;
my $B = $b;
$B =~ s/(\d+)/sprintf "%06d", $1/egxms;
return lc $A cmp lc $B;
}
my $tagged;
sub tagged {
my ($conf, $tag, $project) = @_;
# everything is "tagged" if none is specified
return 1 if !$tag;
if ( !defined $tagged ) {
$tagged = { map { $_ => 1 } @{ $conf->{tags}{$tag} || [] } };
}
# return true if the project is tagged in the config file or has a tag file
return $tagged->{$project} || -e "$project/.$tag.tag";
}
__DATA__
=head1 NAME
group-git - Perform operations over many git repositories at once
=head1 VERSION
This documentation refers to group-git version 0.1.2.
=head1 SYNOPSIS
group-git [option] (git-command) -- [args]
OPTIONS:
Filtering:
-s --skip[=]regexp
Skip any project matching this regexp
-m --match[=]regexp
Only process any project matching this regexp
-l --local Ignore any management type and just look at what is checked out
Only useful if you have a group-git.yml file defining Github,
Bitbucked or Gitosis types.
-t --tag[=]str
Only opperate on repositories tagged with str (tags can be
specified in the config file see --man or with a file .[tag].tag
in the project directory)
Finding projects:
-r --recurse Recurses into subdirectories to check if they contain git
repositories rather than only stopping at the current level.
Output:
-p --page Turns on paginating out put uses $PAGER environment variable if
set or 'less -Rx4SFX' if not.
--no-page Turn off pagination.
-q --quiet Don't show project names before running git on the directory
-v --verbose Show more details
Other:
--VERSION Prints the version information
--help Prints this help information
--man Prints the full documentation for group-git
eg
group-git log -- -n 1
group-git --local status
group-git --verbose grep 'where is it'
group-git --tag perl pull
=head1 DESCRIPTION
Run any git command over all repositories in the current directory.
=head2 Configuration
If you have a config file set up in the current directory (group-git.yml or some
other suffix group-git file supported by L<Config::Any>) extra functionality
becomes available if you use Github, Bitbucket or Gitosis as your repository
remote store. The config file will be read to supply the appropriate user
credentials or in the case of gitosis the admin project location.
With this extra information both the pull and the update commands will try to
clone any repository that are not currently checked out in the current directory.
eg group-git.yml
---
# Type may be any of Github, Bitbucket or Gitosis
type: Github
#type: Bitbucket
#type: Gitosis
# if username or password are missing they will be prompted for
username: joeblogs@example.com
password: securepass
# for github you can specify an access token if using 2 factor auth
#access_token: ....
# Gitosis uses it's git url which needs to be defined or will be prompted for
#gitosis: gituser@gitosis.server.com:gitosis-admin.git
# Global Exclude a git project
#exclude:
# - project_not_to_be_managed
# - ...
# Global Include only these projects (if mentioned only projects listed will be
# processed any others found will be ignored)
#include:
# - project_of_interest_1
# - project_of_interest_2
# - ...
# Set up tags for groups of projects so you can opperate only on that tag
# Tags can also be defined with the projects directory creating a file with
# a name of the format ".[tag].tag"
# eg to tag a project as mytag create a file .mytag.tag
#tags:
# mytag:
# - project1
# - project2
# myothertag:
# - project2
# - project3
For Github and Bitbucket if username or password are missing you will be
prompted to enter the details.
=head1 SUBROUTINES/METHODS
=head1 DIAGNOSTICS
=head1 CONFIGURATION AND ENVIRONMENT
If paginating the C<$PAGER> environment variable is used if it is not set the
command C<'less -Rx4SFX'> is used for paginating, if C<$PAGER> not set and
less not installed no pagination will be done.
=head1 DEPENDENCIES
=head1 INCOMPATIBILITIES
=head1 BUGS AND LIMITATIONS
There are no known bugs in this module.
Please report problems to Ivan Wills (ivan.wills@gmail.com).
Patches are welcome.
=head1 AUTHOR
Ivan Wills - (ivan.wills@gmail.com)
=head1 LICENSE AND COPYRIGHT
Copyright (c) 2013 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
All rights reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. See L<perlartistic>. This program is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
=cut