-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-show-meta
executable file
·82 lines (60 loc) · 1.87 KB
/
git-show-meta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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;