From 004eef6a7b2cc334e71cd2b7470ca798c240943e Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 22 May 2016 19:16:38 -0400 Subject: [PATCH] git-meta --- bin/git-meta | 48 +++++++++++++++++++++++++++ bin/git-show-meta | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100755 bin/git-meta create mode 100755 bin/git-show-meta diff --git a/bin/git-meta b/bin/git-meta new file mode 100755 index 0000000..39b6c06 --- /dev/null +++ b/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; diff --git a/bin/git-show-meta b/bin/git-show-meta new file mode 100755 index 0000000..3b8b496 --- /dev/null +++ b/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;