| 1 | # | ||||||||||
| 2 | # This file is part of Dist-Zilla-Plugin-Git | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is copyright (c) 2009 by Jerome Quelin. | ||||||||||
| 5 | # | ||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 8 | # | ||||||||||
| 9 | 11 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | package Dist::Zilla::Role::Git::DirtyFiles; | ||||||||||
| 14 | # ABSTRACT: Provide the allow_dirty & changelog attributes | ||||||||||
| 15 | |||||||||||
| 16 | 11 | our $VERSION = '2.052'; | |||||||||
| 17 | |||||||||||
| 18 | 11 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 11 | ▶ | use Types::Standard qw{ Any ArrayRef Str RegexpRef }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 11 | ▶ | use Type::Utils qw(coerce from as via subtype); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 11 | ▶ | use Types::Path::Tiny 'Path'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 11 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 11 | ▶ | use Path::Tiny 0.048 qw(); # subsumes | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 11 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 11 | requires qw(log_fatal repo_root zilla); | |||||||||
| 28 | |||||||||||
| 29 | # -- attributes | ||||||||||
| 30 | |||||||||||
| 31 | #pod =attr allow_dirty | ||||||||||
| 32 | #pod | ||||||||||
| 33 | #pod A list of paths that are allowed to be dirty in the git checkout. | ||||||||||
| 34 | #pod Defaults to C<dist.ini> and the changelog (as defined per the | ||||||||||
| 35 | #pod C<changelog> attribute. | ||||||||||
| 36 | #pod | ||||||||||
| 37 | #pod If your C<repo_root> is not the default (C<.>), then these pathnames | ||||||||||
| 38 | #pod are relative to Dist::Zilla's root directory, not the Git root directory. | ||||||||||
| 39 | #pod | ||||||||||
| 40 | #pod =attr allow_dirty_match | ||||||||||
| 41 | #pod | ||||||||||
| 42 | #pod A list of regular expressions that match paths allowed to be dirty in | ||||||||||
| 43 | #pod the git checkout. This is combined with C<allow_dirty>. Defaults to | ||||||||||
| 44 | #pod the empty list. | ||||||||||
| 45 | #pod | ||||||||||
| 46 | #pod The paths being matched are relative to the Git root directory, even | ||||||||||
| 47 | #pod if your C<repo_root> is not the default (C<.>). | ||||||||||
| 48 | #pod | ||||||||||
| 49 | #pod =attr changelog | ||||||||||
| 50 | #pod | ||||||||||
| 51 | #pod The name of the changelog. Defaults to C<Changes>. | ||||||||||
| 52 | #pod | ||||||||||
| 53 | #pod =cut | ||||||||||
| 54 | |||||||||||
| 55 | { | ||||||||||
| 56 | # We specifically allow the empty string to represent the empty list. | ||||||||||
| 57 | # Otherwise, there'd be no way to specify an empty list in an INI file. | ||||||||||
| 58 | 11 | my $type = subtype as ArrayRef[Path]; | |||||||||
| 59 | coerce $type, | ||||||||||
| 60 | 16 | ▶ | from ArrayRef, via { (ArrayRef[Path])->coerce( [ grep length, @$_ ] ) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 0 | ▶ | from Any, via { length($_) ? (ArrayRef[Path])->coerce($_) : [] }; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 11 | has allow_dirty => ( | |||||||||
| 64 | is => 'ro', lazy => 1, | ||||||||||
| 65 | isa => $type, | ||||||||||
| 66 | coerce => 1, | ||||||||||
| 67 | builder => '_build_allow_dirty', | ||||||||||
| 68 | ); | ||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 11 | has changelog => ( is => 'ro', isa=>Str, default => 'Changes' ); | |||||||||
| 72 | |||||||||||
| 73 | { | ||||||||||
| 74 | 11 | my $type = subtype as ArrayRef[RegexpRef]; | |||||||||
| 75 | 1 | ▶ | coerce $type, from ArrayRef[Str], via { [map qr/$_/, @$_] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 76 | has allow_dirty_match => ( | ||||||||||
| 77 | is => 'ro', | ||||||||||
| 78 | lazy => 1, | ||||||||||
| 79 | coerce => 1, | ||||||||||
| 80 | isa => $type, | ||||||||||
| 81 | 13 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 82 | 11 | ); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 19 | ▶ | around mvp_multivalue_args => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 86 | 19 | my ($orig, $self) = @_; | |||||||||
| 87 | |||||||||||
| 88 | 19 | my @start = $self->$orig; | |||||||||
| 89 | 19 | return (@start, 'allow_dirty', 'allow_dirty_match'); | |||||||||
| 90 | 11 | }; | |||||||||
| 91 | |||||||||||
| 92 | # -- builders & initializers | ||||||||||
| 93 | |||||||||||
| 94 | 12 | ▶ | sub _build_allow_dirty { [ 'dist.ini', shift->changelog ] } | ||||||||
|
Subroutine
_build_allow_dirty: called CC=1 SCAR=0.0
| |||||||||||
| 95 | |||||||||||
| 96 | around dump_config => sub | ||||||||||
| 97 | 16 | ▶ | { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 98 | 16 | my $orig = shift; | |||||||||
| 99 | 16 | my $self = shift; | |||||||||
| 100 | |||||||||||
| 101 | 16 | my $config = $self->$orig; | |||||||||
| 102 | |||||||||||
| 103 | $config->{+__PACKAGE__} = { | ||||||||||
| 104 | 16 | (map +($_ => [ sort @{ $self->$_ } ]), qw(allow_dirty allow_dirty_match)), | |||||||||
| 105 | changelog => $self->changelog, | ||||||||||
| 106 | }; | ||||||||||
| 107 | |||||||||||
| 108 | 16 | return $config; | |||||||||
| 109 | 11 | }; | |||||||||
| 110 | |||||||||||
| 111 | #pod =method list_dirty_files | ||||||||||
| 112 | #pod | ||||||||||
| 113 | #pod my @dirty = $plugin->list_dirty_files($git, $listAllowed); | ||||||||||
| 114 | #pod | ||||||||||
| 115 | #pod This returns a list of the modified or deleted files in C<$git>, | ||||||||||
| 116 | #pod filtered against the C<allow_dirty> attribute. If C<$listAllowed> is | ||||||||||
| 117 | #pod true, only allowed files are listed. If it's false, only files that | ||||||||||
| 118 | #pod are not allowed to be dirty are listed. | ||||||||||
| 119 | #pod | ||||||||||
| 120 | #pod In scalar context, returns the number of dirty files. | ||||||||||
| 121 | #pod | ||||||||||
| 122 | #pod =cut | ||||||||||
| 123 | |||||||||||
| 124 | sub list_dirty_files | ||||||||||
| 125 | 29 | ▶ | { | ||||||||
|
Subroutine
list_dirty_files: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 126 | 29 | my ($self, $git, $listAllowed) = @_; | |||||||||
| 127 | |||||||||||
| 128 | 29 | my $git_root = $self->repo_root; | |||||||||
| 129 | 29 | my @filenames = @{ $self->allow_dirty }; | |||||||||
| 130 | |||||||||||
| 131 | # Interpret allow_dirty relative to the dzil root, not git root nor cwd | ||||||||||
| 132 | 29 | my $dzil_root = Path::Tiny::path($self->zilla->root)->absolute->realpath; | |||||||||
| 133 | 29 | $git_root = Path::Tiny::path($git_root) | |||||||||
| 134 | ->absolute($dzil_root) | ||||||||||
| 135 | ->realpath; | ||||||||||
| 136 | |||||||||||
| 137 | 29 | ▶ | $self->log_fatal("Dzil root $dzil_root is not inside Git root $git_root") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | unless $git_root->subsumes($dzil_root); | ||||||||||
| 139 | |||||||||||
| 140 | 29 | for my $fn (@filenames) { | |||||||||
| 141 | 42 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 142 | 42 | $fn = Path::Tiny::path($fn) | |||||||||
| 143 | ->absolute($dzil_root) | ||||||||||
| 144 | ->realpath # process .. | ||||||||||
| 145 | ->relative($git_root) | ||||||||||
| 146 | ->stringify; | ||||||||||
| 147 | 42 | }; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 29 | my $allowed = join '|', @{ $self->allow_dirty_match }, map qr{^\Q$_\E$}, @filenames; | |||||||||
| 151 | |||||||||||
| 152 | 29 | ▶ | $allowed = qr/(?!X)X/ if $allowed eq ''; # this cannot match anything | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | |||||||||||
| 154 | 29 | ▶ | return grep /$allowed/ ? $listAllowed : !$listAllowed, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | $git->ls_files( { modified=>1, deleted=>1 } ); | ||||||||||
| 156 | } # end list_dirty_files | ||||||||||
| 157 | |||||||||||
| 158 | |||||||||||
| 159 | 11 | 1; | |||||||||
| 160 | |||||||||||
| 161 | __END__ | ||||||||||