Skip to content

Commit

Permalink
git-meta
Browse files Browse the repository at this point in the history
  • Loading branch information
yanick committed May 22, 2016
1 parent b0d76ca commit 004eef6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
48 changes: 48 additions & 0 deletions bin/git-meta
@@ -0,0 +1,48 @@
#!/usr/bin/env perl

package App::Git::Meta;

use 5.10.0;

use strict;
use warnings;

use Git::Wrapper;

use Moose;
use MooseX::App::Simple;
use MooseX::MungeHas 'is_ro';

use experimental 'signatures';

has git => sub { Git::Wrapper->new('.') };

option branch => (
is => 'ro',
isa => 'Str',
lazy => 1,
default => 'HEAD',
documentation => 'target branch',
);

parameter key => (
is => 'ro',
required => 1,
);

parameter value => (
is => 'ro',
required => 1,
);

sub run($self) {
my( $branch ) = $self->git->rev_parse( qw/ --abbrev-ref /, $self->branch );

$self->git->config( "branch.$branch." . $self->key => $self->value );

say join ' ', $branch, $self->key, $self->value;
}

__PACKAGE__->meta->make_immutable;

__PACKAGE__->new_with_options->run unless caller;
82 changes: 82 additions & 0 deletions bin/git-show-meta
@@ -0,0 +1,82 @@
#!/usr/bin/env perl

package App::Git::ShowMeta;

use 5.10.0;

use Git::Wrapper;

use Moose;

use MooseX::App::Simple;

use MooseX::MungeHas 'is_ro';

use Config::GitLike::Git;
use List::AllUtils qw/ pairgrep pairmap /;
use JSON;
use Data::Printer;

use experimental 'signatures', 'postderef';

has git => sub { Git::Wrapper->new('.') };

option branch => (
is => 'ro',
isa => 'ArrayRef',
predicate => 'has_branch',
documentation => 'target branches',
);

parameter key_filter => ( is => 'ro' );
parameter value_filter => ( is => 'ro' );

option format => (
is => 'ro',
isa => 'Str',
default => '',
);

has git_config => sub {
Config::GitLike::Git->new->load('.');
};

sub run($self) {

my %branches;
$branches{$_->[0]}{$_->[1]} = $_->[2] for pairmap {
[ ( split /\./, $a, 2 ), $b ]
} pairgrep { $a =~ s/^branch\.// } $self->git_config->%*;

if ( $self->has_branch ) {
my %keepers = map { $self->git->rev_parse( qw/ --abbrev-ref /, $_ ) => 1 } $self->branch->@*;
%branches = pairgrep { $keepers{$a} } %branches;
}

if( my $k = $self->key_filter ) {
%branches = pairmap { $a => { $k => $b } } pairgrep { $b } pairmap { $a => $b->{$k} } %branches;

if( my $v = $self->value_filter ) {
%branches = pairgrep { $b->{$k} eq $v } %branches;
}
}


if ( $self->format eq 'json' ) {
say to_json( \%branches, { canonical => 1, pretty => 1 } );
}
elsif ( $self->format eq 'column' ) {
for my $branch ( sort keys %branches ) {
for my $key ( sort keys $branches{$branch}->%* ) {
say join ' ', $branch, $key, $branches{$branch}{$key};
}
}
}
else {
p %branches, output => 'stdout';
}
}

__PACKAGE__->meta->make_immutable;

__PACKAGE__->new_with_options->run unless caller;

0 comments on commit 004eef6

Please sign in to comment.