From 67e86f990f2556f12465052911eb7b96a03b8dcc Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Thu, 7 Nov 2019 13:49:10 -0700 Subject: [PATCH] initial commit --- .editorconfig | 20 ++ .gitignore | 7 + Changes | 4 + LICENSE | 379 +++++++++++++++++++++++ Makefile | 43 +++ bin/git-codeowners | 114 +++++++ dist.ini | 30 ++ eg/test.t | 14 + lib/App/Codeowners.pm | 320 ++++++++++++++++++++ lib/App/Codeowners/Options.pm | 301 ++++++++++++++++++ lib/App/Codeowners/Util.pm | 229 ++++++++++++++ lib/File/Codeowners.pm | 502 +++++++++++++++++++++++++++++++ lib/Test/File/Codeowners.pm | 106 +++++++ t/app-codeowners-util.t | 93 ++++++ t/app-codeowners.t | 100 ++++++ t/file-codeowners.t | 143 +++++++++ t/samples/basic.CODEOWNERS | 2 + t/samples/kitchensink.CODEOWNERS | 18 ++ 18 files changed, 2425 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 Changes create mode 100644 LICENSE create mode 100644 Makefile create mode 100755 bin/git-codeowners create mode 100644 dist.ini create mode 100755 eg/test.t create mode 100644 lib/App/Codeowners.pm create mode 100644 lib/App/Codeowners/Options.pm create mode 100644 lib/App/Codeowners/Util.pm create mode 100644 lib/File/Codeowners.pm create mode 100644 lib/Test/File/Codeowners.pm create mode 100644 t/app-codeowners-util.t create mode 100644 t/app-codeowners.t create mode 100644 t/file-codeowners.t create mode 100644 t/samples/basic.CODEOWNERS create mode 100644 t/samples/kitchensink.CODEOWNERS diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5c166f9 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,20 @@ + +# Please follow these code style guidelines. You can use this file to +# automatically configure your editor. +# For instructions, see: http://editorconfig.org/ + +[*] +charset = utf8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[{**.md,**.pl,**.pm,**.pod,**.t,bin/**}] +indent_style = space +indent_size = 4 +max_line_length = 100 + +[{.editorconfig,**.ini}] +indent_style = space +indent_size = 4 + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2df827a --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.tar* +*~ +/.build +/.perl-version +/App-Codeowners-* +/cover_db +/local diff --git a/Changes b/Changes new file mode 100644 index 0000000..b4ef68e --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for App-Codeowners. + +{{$NEXT}} + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2eb2acb --- /dev/null +++ b/LICENSE @@ -0,0 +1,379 @@ +This software is copyright (c) 2019 by Charles McGarvey. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +Terms of the Perl programming language system itself + +a) the GNU General Public License as published by the Free + Software Foundation; either version 1, or (at your option) any + later version, or +b) the "Artistic License" + +--- The GNU General Public License, Version 1, February 1989 --- + +This software is Copyright (c) 2019 by Charles McGarvey. + +This is free software, licensed under: + + The GNU General Public License, Version 1, February 1989 + + GNU GENERAL PUBLIC LICENSE + Version 1, February 1989 + + Copyright (C) 1989 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The license agreements of most software companies try to keep users +at the mercy of those companies. By contrast, our General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. The +General Public License applies to the Free Software Foundation's +software and to any other program whose authors commit to using it. +You can use it for your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Specifically, the General Public License is designed to make +sure that you have the freedom to give away or sell copies of free +software, that you receive source code or can get it if you want it, +that you can change the software or use pieces of it in new free +programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of a such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must tell them their rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any program or other work which +contains a notice placed by the copyright holder saying it may be +distributed under the terms of this General Public License. The +"Program", below, refers to any such program or work, and a "work based +on the Program" means either the Program or any work containing the +Program or a portion of it, either verbatim or with modifications. Each +licensee is addressed as "you". + + 1. You may copy and distribute verbatim copies of the Program's source +code as you receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice and +disclaimer of warranty; keep intact all the notices that refer to this +General Public License and to the absence of any warranty; and give any +other recipients of the Program a copy of this General Public License +along with the Program. You may charge a fee for the physical act of +transferring a copy. + + 2. You may modify your copy or copies of the Program or any portion of +it, and copy and distribute such modifications under the terms of Paragraph +1 above, provided that you also do the following: + + a) cause the modified files to carry prominent notices stating that + you changed the files and the date of any change; and + + b) cause the whole of any work that you distribute or publish, that + in whole or in part contains the Program or any part thereof, either + with or without modifications, to be licensed at no charge to all + third parties under the terms of this General Public License (except + that you may choose to grant warranty protection to some or all + third parties, at your option). + + c) If the modified program normally reads commands interactively when + run, you must cause it, when started running for such interactive use + in the simplest and most usual way, to print or display an + announcement including an appropriate copyright notice and a notice + that there is no warranty (or else, saying that you provide a + warranty) and that users may redistribute the program under these + conditions, and telling the user how to view a copy of this General + Public License. + + d) You may charge a fee for the physical act of transferring a + copy, and you may at your option offer warranty protection in + exchange for a fee. + +Mere aggregation of another independent work with the Program (or its +derivative) on a volume of a storage or distribution medium does not bring +the other work under the scope of these terms. + + 3. You may copy and distribute the Program (or a portion or derivative of +it, under Paragraph 2) in object code or executable form under the terms of +Paragraphs 1 and 2 above provided that you also do one of the following: + + a) accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of + Paragraphs 1 and 2 above; or, + + b) accompany it with a written offer, valid for at least three + years, to give any third party free (except for a nominal charge + for the cost of distribution) a complete machine-readable copy of the + corresponding source code, to be distributed under the terms of + Paragraphs 1 and 2 above; or, + + c) accompany it with the information you received as to where the + corresponding source code may be obtained. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form alone.) + +Source code for a work means the preferred form of the work for making +modifications to it. For an executable file, complete source code means +all the source code for all modules it contains; but, as a special +exception, it need not include source code for modules which are standard +libraries that accompany the operating system on which the executable +file runs, or for standard header files or definitions files that +accompany that operating system. + + 4. You may not copy, modify, sublicense, distribute or transfer the +Program except as expressly provided under this General Public License. +Any attempt otherwise to copy, modify, sublicense, distribute or transfer +the Program is void, and will automatically terminate your rights to use +the Program under this License. However, parties who have received +copies, or rights to use copies, from you under this General Public +License will not have their licenses terminated so long as such parties +remain in full compliance. + + 5. By copying, distributing or modifying the Program (or any work based +on the Program) you indicate your acceptance of this license to do so, +and all its terms and conditions. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the original +licensor to copy, distribute or modify the Program subject to these +terms and conditions. You may not impose any further restrictions on the +recipients' exercise of the rights granted herein. + + 7. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of the license which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +the license, you may choose any version ever published by the Free Software +Foundation. + + 8. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to humanity, the best way to achieve this is to make it +free software which everyone can redistribute and change under these +terms. + + To do so, attach the following notices to the program. It is safest to +attach them to the start of each source file to most effectively convey +the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 1, or (at your option) + any later version. + + 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. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19xx name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the +appropriate parts of the General Public License. Of course, the +commands you use may be called something other than `show w' and `show +c'; they could even be mouse-clicks or menu items--whatever suits your +program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + program `Gnomovision' (a program to direct compilers to make passes + at assemblers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +That's all there is to it! + + +--- The Artistic License 1.0 --- + +This software is Copyright (c) 2019 by Charles McGarvey. + +This is free software, licensed under: + + The Artistic License 1.0 + +The Artistic License + +Preamble + +The intent of this document is to state the conditions under which a Package +may be copied, such that the Copyright Holder maintains some semblance of +artistic control over the development of the package, while giving the users of +the package the right to use and distribute the Package in a more-or-less +customary fashion, plus the right to make reasonable modifications. + +Definitions: + + - "Package" refers to the collection of files distributed by the Copyright + Holder, and derivatives of that collection of files created through + textual modification. + - "Standard Version" refers to such a Package if it has not been modified, + or has been modified in accordance with the wishes of the Copyright + Holder. + - "Copyright Holder" is whoever is named in the copyright or copyrights for + the package. + - "You" is you, if you're thinking about copying or distributing this Package. + - "Reasonable copying fee" is whatever you can justify on the basis of media + cost, duplication charges, time of people involved, and so on. (You will + not be required to justify it to the Copyright Holder, but only to the + computing community at large as a market that must bear the fee.) + - "Freely Available" means that no fee is charged for the item itself, though + there may be fees involved in handling the item. It also means that + recipients of the item may redistribute it under the same conditions they + received it. + +1. You may make and give away verbatim copies of the source form of the +Standard Version of this Package without restriction, provided that you +duplicate all of the original copyright notices and associated disclaimers. + +2. You may apply bug fixes, portability fixes and other modifications derived +from the Public Domain or from the Copyright Holder. A Package modified in such +a way shall still be considered the Standard Version. + +3. You may otherwise modify your copy of this Package in any way, provided that +you insert a prominent notice in each changed file stating how and when you +changed that file, and provided that you do at least ONE of the following: + + a) place your modifications in the Public Domain or otherwise make them + Freely Available, such as by posting said modifications to Usenet or an + equivalent medium, or placing the modifications on a major archive site + such as ftp.uu.net, or by allowing the Copyright Holder to include your + modifications in the Standard Version of the Package. + + b) use the modified Package only within your corporation or organization. + + c) rename any non-standard executables so the names do not conflict with + standard executables, which must also be provided, and provide a separate + manual page for each non-standard executable that clearly documents how it + differs from the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +4. You may distribute the programs of this Package in object code or executable +form, provided that you do at least ONE of the following: + + a) distribute a Standard Version of the executables and library files, + together with instructions (in the manual page or equivalent) on where to + get the Standard Version. + + b) accompany the distribution with the machine-readable source of the Package + with your modifications. + + c) accompany any non-standard executables with their corresponding Standard + Version executables, giving the non-standard executables non-standard + names, and clearly documenting the differences in manual pages (or + equivalent), together with instructions on where to get the Standard + Version. + + d) make other distribution arrangements with the Copyright Holder. + +5. You may charge a reasonable copying fee for any distribution of this +Package. You may charge any fee you choose for support of this Package. You +may not charge a fee for this Package itself. However, you may distribute this +Package in aggregate with other (possibly commercial) programs as part of a +larger (possibly commercial) software distribution provided that you do not +advertise this Package as a product of your own. + +6. The scripts and library files supplied as input to or produced as output +from the programs of this Package do not automatically fall under the copyright +of this Package, but belong to whomever generated them, and may be sold +commercially, and may be aggregated with this Package. + +7. C or perl subroutines supplied by you and linked into this Package shall not +be considered part of this Package. + +8. The name of the Copyright Holder may not be used to endorse or promote +products derived from this software without specific prior written permission. + +9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + +The End + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1df821f --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ + +# This is not a Perl distribution, but it can build one using Dist::Zilla. + +COVER = cover +CPANM = cpanm +DZIL = dzil +PERL = perl +PERLCRITIC = perlcritic +PROVE = prove + +all: dist + +bootstrap: + $(CPANM) $(CPANM_FLAGS) -n Dist::Zilla + $(DZIL) authordeps --missing |$(CPANM) $(CPANM_FLAGS) -n + $(DZIL) listdeps --develop --missing |$(CPANM) $(CPANM_FLAGS) -n + +check: + $(PERLCRITIC) bin lib t + +clean: + $(DZIL) $@ + +cover: + $(COVER) -test + +debug: + $(PERL) -Ilib -d bin/git-codeowners $(GIT_CODEOWNERS_FLAGS) + +dist: + $(DZIL) build + +distclean: clean + rm -rf cover_db + +run: + $(PERL) -Ilib bin/git-codeowners $(GIT_CODEOWNERS_FLAGS) + +test: + $(PROVE) -l$(if $(findstring 1,$(V)),v) t + +.PHONY: all bootstrap clean cover debug dist distclean run test + diff --git a/bin/git-codeowners b/bin/git-codeowners new file mode 100755 index 0000000..e620ea8 --- /dev/null +++ b/bin/git-codeowners @@ -0,0 +1,114 @@ +#! perl +# ABSTRACT: A tool for managing CODEOWNERS files +# PODNAME: git-codeowners + +=head1 SYNOPSIS + + git-codeowners [--version|--help|--manual] + + git-codeowners [show] [--format FORMAT] [--[no-]project] [PATH...] + + git-codeowners owners [--format FORMAT] [--pattern PATTERN] + + git-codeowners patterns [--format FORMAT] [--owner OWNER] + + git-codeowners create|update [REPO_DIRPATH|CODEOWNERS_FILEPATH] + + # enable bash shell completion + eval "$(git-codeowners --shell-completion)" + +=head1 DESCRIPTION + +F is yet another CLI tool for managing F files in +git repos. In particular, it can be used to quickly find out who owns +a particular file in a monorepo (or monolith). + +B The interface of this tool and its modules will +probably change as I field test some things. Feedback welcome. + +=head1 OPTIONS + +=head2 --version + +Print the program name and version to C, and exit. + +Alias: C<-v> + +=head2 --help + +Print the synopsis to C, and exit. + +Alias: C<-h> + +You can also use C<--manual> to print the full documentation. + +=head2 --format + +Specify the output format to use. See L. + +Alias: C<-f> + +=head2 --shell-completion + + eval "$(lintany --shell-completion)" + +Print shell code to enable completion to C, and exit. + +Does not yet support Zsh... + +=head1 COMMANDS + +=head2 show + + git-codeowners [show] [--format FORMAT] [--[no-]project] [PATH...] + +Show owners of one or more files in a repo. + +=head2 owners + + git-codeowners owners [--format FORMAT] [--pattern PATTERN] + +=head2 patterns + + git-codeowners patterns [--format FORMAT] [--owner OWNER] + +=head2 create + + git-codeowners create [REPO_DIRPATH|CODEOWNERS_FILEPATH] + +Create a new F file for a specified repo (or current directory). + +=head2 update + + git-codeowners update [REPO_DIRPATH|CODEOWNERS_FILEPATH] + +Update the "unowned" list of an existing F file for a specified +repo (or current directory). + +=head1 FORMAT + +The C<--format> argument can be one of: + +=for :list +* C - Comma-separated values (requires L) +* C - Pretty JSON (requires L) +* C - JSON (requires L) +* C - Table (requires L) +* C - Tab-separated values (requires L) +* C - YAML (requires L) +* C - Custom format (see below) + +You can specify a custom format using printf-like format sequences. + +=cut + +# FATPACK - Do not remove this line. + +use warnings; +use strict; + +use App::Codeowners; + +our $VERSION = '9999.999'; # VERSION + +App::Codeowners->main(@ARGV); diff --git a/dist.ini b/dist.ini new file mode 100644 index 0000000..c1f4b41 --- /dev/null +++ b/dist.ini @@ -0,0 +1,30 @@ + +name = App-Codeowners +main_module = bin/git-codeowners +author = Charles McGarvey +copyright_holder = Charles McGarvey +copyright_year = 2019 +license = Perl_5 + +[@Filter] +-bundle = @Author::CCM +-remove = PodCoverageTests +-remove = Test::CleanNamespaces +max_target_perl = 5.10.1 + +[ConsistentVersionTest] + +[RemovePhasedPrereqs] +remove_runtime = JSON::MaybeXS +remove_runtime = Text::CSV +remove_runtime = Text::Table +remove_runtime = Unicode::GCString +remove_runtime = YAML +[Prereqs / RuntimeRecommends] +Unicode::GCString = 0 +[Prereqs / RuntimeSuggests] +JSON::MaybeXS = 0 +Text::CSV = 0 +Text::Table = 0 +YAML = 0 + diff --git a/eg/test.t b/eg/test.t new file mode 100755 index 0000000..8c06662 --- /dev/null +++ b/eg/test.t @@ -0,0 +1,14 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +use Test::More; + +eval 'use Test::File::Codeowners'; +warn $@ if $@; +plan skip_all => 'Test::File::Codeowners required for testing CODEOWNERS' if $@; + +codeowners_syntax_ok(); +codeowners_git_files_ok(); +done_testing; diff --git a/lib/App/Codeowners.pm b/lib/App/Codeowners.pm new file mode 100644 index 0000000..eebe058 --- /dev/null +++ b/lib/App/Codeowners.pm @@ -0,0 +1,320 @@ +package App::Codeowners; +# ABSTRACT: A tool for managing CODEOWNERS files + +use v5.10.1; # defined-or +use utf8; +use warnings; +use strict; + +use App::Codeowners::Options; +use App::Codeowners::Util qw(find_codeowners_in_directory run_git git_ls_files git_toplevel stringf); +use Color::ANSI::Util qw(ansifg ansi_reset); +use Encode qw(encode); +use File::Codeowners; +use Path::Tiny; + +our $VERSION = '9999.999'; # VERSION + +=method main + + App::Codeowners->main(@ARGV); + +Run the script and exit; does not return. + +=cut + +sub main { + my $class = shift; + my $self = bless {}, $class; + + my $opts = App::Codeowners::Options->new(@_); + + my $color = $opts->{color}; + local $ENV{NO_COLOR} = 1 if defined $color && !$color; + + my $command = $opts->command; + my $handler = $self->can("_command_$command") + or die "Unknown command: $command\n"; + $self->$handler($opts); + + exit 0; +} + +sub _command_show { + my $self = shift; + my $opts = shift; + + my $toplevel = git_toplevel('.') or die "Not a git repo\n"; + + my $codeowners_path = find_codeowners_in_directory($toplevel) + or die "No CODEOWNERS file in $toplevel\n"; + my $codeowners = File::Codeowners->parse_from_filepath($codeowners_path); + + my ($cdup) = run_git(qw{rev-parse --show-cdup}); + + my @results; + + my $filepaths = git_ls_files('.', $opts->args) or die "Cannot list files\n"; + for my $filepath (@$filepaths) { + my $match = $codeowners->match(path($filepath)->relative($cdup)); + push @results, [ + $filepath, + $match->{owners}, + $opts->{project} ? $match->{project} : (), + ]; + } + + _format( + format => $opts->{format} || ' * %-50F %O', + out => *STDOUT, + headers => [qw(File Owner), $opts->{project} ? 'Project' : ()], + rows => \@results, + ); +} + +sub _command_owners { + my $self = shift; + my $opts = shift; + + my $toplevel = git_toplevel('.') or die "Not a git repo\n"; + + my $codeowners_path = find_codeowners_in_directory($toplevel) + or die "No CODEOWNERS file in $toplevel\n"; + my $codeowners = File::Codeowners->parse_from_filepath($codeowners_path); + + my $results = $codeowners->owners($opts->{pattern}); + + _format( + format => $opts->{format} || '%O', + out => *STDOUT, + headers => [qw(Owner)], + rows => [map { [$_] } @$results], + ); +} + +sub _command_patterns { + my $self = shift; + my $opts = shift; + + my $toplevel = git_toplevel('.') or die "Not a git repo\n"; + + my $codeowners_path = find_codeowners_in_directory($toplevel) + or die "No CODEOWNERS file in $toplevel\n"; + my $codeowners = File::Codeowners->parse_from_filepath($codeowners_path); + + my $results = $codeowners->patterns($opts->{owner}); + + _format( + format => $opts->{format} || '%T', + out => *STDOUT, + headers => [qw(Pattern)], + rows => [map { [$_] } @$results], + ); +} + +sub _command_create { goto &_command_update } +sub _command_update { + my $self = shift; + my $opts = shift; + + my ($filepath) = $opts->args; + + my $path = path($filepath || '.'); + my $repopath; + + die "Does not exist: $path\n" if !$path->parent->exists; + + if ($path->is_dir) { + $repopath = $path; + $path = find_codeowners_in_directory($path) || $repopath->child('CODEOWNERS'); + } + + my $is_new = !$path->is_file; + + my $codeowners; + if ($is_new) { + $codeowners = File::Codeowners->new; + my $template = <<'END'; + This file shows mappings between subdirs/files and the individuals and + teams who own them. You can read this file yourself or use tools to query it, + so you can quickly determine who to speak with or send pull requests to. ❤️ + + Simply write a gitignore pattern followed by one or more names/emails/groups. + Examples: + /project_a/** @team1 + *.js @harry @javascript-cabal +END + for my $line (split(/\n/, $template)) { + $codeowners->append(comment => $line); + } + } + else { + $codeowners = File::Codeowners->parse_from_filepath($path); + } + + if ($repopath) { + # if there is a repo we can try to update the list of unowned files + my $git_files = git_ls_files($repopath); + if (@$git_files) { + $codeowners->clear_unowned; + $codeowners->add_unowned(grep { !$codeowners->match($_) } @$git_files); + } + } + + $codeowners->write_to_filepath($path); + print STDERR "Wrote $path\n"; +} + +sub _format { + my %args = @_; + + my $format = $args{format} || 'table'; + my $fh = $args{out} || *STDOUT; + my $headers = $args{headers} || []; + my $rows = $args{rows} || []; + + if ($format eq 'table') { + eval { require Text::Table } or die "Missing dependency: Text::Table\n"; + + my $table = Text::Table->new(@$headers); + $table->load(map { [map { _stringify($_) } @$_] } @$rows); + print { $fh } encode('UTF-8', "$table"); + } + elsif ($format =~ /^json(:pretty)?$/) { + my $pretty = !!$1; + eval { require JSON::MaybeXS } or die "Missing dependency: JSON::MaybeXS\n"; + + my $json = JSON::MaybeXS->new(canonical => 1, utf8 => 1, pretty => $pretty); + my $data = _combine_headers_rows($headers, $rows); + print { $fh } $json->encode($data); + } + elsif ($format =~ /^([ct])sv$/) { + my $sep = $1 eq 'c' ? ',' : "\t"; + eval { require Text::CSV } or die "Missing dependency: Text::CSV\n"; + + my $csv = Text::CSV->new({binary => 1, eol => $/, sep => $sep}); + $csv->print($fh, $headers); + $csv->print($fh, [map { encode('UTF-8', _stringify($_)) } @$_]) for @$rows; + } + elsif ($format =~ /^ya?ml$/) { + eval { require YAML } or die "Missing dependency: YAML\n"; + + my $data = _combine_headers_rows($headers, $rows); + print { $fh } encode('UTF-8', YAML::Dump($data)); + } + else { + my $data = _combine_headers_rows($headers, $rows); + + # https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/ + my @contrasting_colors = qw( + e6194b 3cb44b ffe119 4363d8 f58231 + 911eb4 42d4f4 f032e6 bfef45 fabebe + 469990 e6beff 9a6324 fffac8 800000 + aaffc3 808000 ffd8b1 000075 a9a9a9 + ); + + # assign a color to each owner, on demand + my %owner_colors; + my $num = -1; + my $owner_color = sub { + my $owner = shift or return; + $owner_colors{$owner} ||= do { + $num = ($num + 1) % scalar @contrasting_colors; + $contrasting_colors[$num]; + }; + }; + + my %filter = ( + quote => sub { local $_ = $_[0]; s/"/\"/s; "\"$_\"" }, + ); + + my $create_filterer = sub { + my $value = shift || ''; + my $color = shift || ''; + my $gencolor = ref($color) eq 'CODE' ? $color : sub { $color }; + return sub { + my $arg = shift; + my ($filters, $color) = _expand_filter_args($arg); + if (ref($value) eq 'ARRAY') { + $value = join(',', map { _colored($_, $color // $gencolor->($_)) } @$value); + } + else { + $value = _colored($value, $color // $gencolor->($value)); + } + for my $key (@$filters) { + if (my $filter = $filter{$key}) { + $value = $filter->($value); + } + else { + warn "Unknown filter: $key\n" + } + } + $value || ''; + }; + }; + + for my $row (@$data) { + my %info = ( + F => $create_filterer->($row->{File}, undef), + O => $create_filterer->($row->{Owner}, $owner_color), + P => $create_filterer->($row->{Project}, undef), + T => $create_filterer->($row->{Pattern}, undef), + ); + + my $text = stringf($format, %info); + print { $fh } encode('UTF-8', $text), "\n"; + } + } +} + +sub _expand_filter_args { + my $arg = shift || ''; + + my @filters = split(/,/, $arg); + my $color_override; + + for (my $i = 0; $i < @filters; ++$i) { + my $filter = $filters[$i] or next; + if ($filter =~ /^(?:nocolor|color:([0-9a-fA-F]{6}))$/) { + $color_override = $1 || ''; + splice(@filters, $i, 1); + redo; + } + } + + return (\@filters, $color_override); +} + +sub _colored { + my $text = shift; + my $rgb = shift or return $text; + + # ansifg honors NO_COLOR already, but ansi_reset does not. + return $text if $ENV{NO_COLOR}; + + my ($begin, $end) = (ansifg($rgb), ansi_reset); + return "${begin}${text}${end}"; +} + +sub _combine_headers_rows { + my $headers = shift; + my $rows = shift; + + my @new_rows; + + for my $row (@$rows) { + push @new_rows, (my $new_row = {}); + for (my $i = 0; $i < @$headers; ++$i) { + $new_row->{$headers->[$i]} = $row->[$i]; + } + } + + return \@new_rows; +} + +sub _stringify { + my $item = shift; + return ref($item) eq 'ARRAY' ? join(',', @$item) : $item; +} + +1; diff --git a/lib/App/Codeowners/Options.pm b/lib/App/Codeowners/Options.pm new file mode 100644 index 0000000..064aff9 --- /dev/null +++ b/lib/App/Codeowners/Options.pm @@ -0,0 +1,301 @@ +package App::Codeowners::Options; +# ABSTRACT: Getopt and shell completion for App::Codeowners + +use warnings; +use strict; + +use Getopt::Long 2.39 (); +use Path::Tiny; +use Pod::Usage; + +our $VERSION = '9999.999'; # VERSION + +sub early_options { + return { + 'color|colour!' => (-t STDOUT ? 1 : 0), ## no critic (InputOutput::ProhibitInteractiveTest) + 'format|f=s' => undef, + 'help|h|?' => 0, + 'manual|man' => 0, + 'shell-completion:s' => undef, + 'version|v' => 0, + }; +} + +sub command_options { + return { + 'create' => {}, + 'owners' => { + 'pattern=s' => '', + }, + 'patterns' => { + 'owner=s' => '', + }, + 'show' => { + 'project!' => 1, + }, + 'update' => {}, + }; +} + +sub commands { + my $self = shift; + my @commands = sort keys %{$self->command_options}; + return @commands; +} + +sub options { + my $self = shift; + my @command_options; + if (my $command = $self->{command}) { + @command_options = keys %{$self->command_options->{$command} || {}}; + } + return (keys %{$self->early_options}, @command_options); +} + +sub new { + my $class = shift; + my @args = @_; + + my $self = bless {}, $class; + + my @args_copy = @args; + + my $opts = $self->get_options( + args => \@args, + spec => $self->early_options, + config => 'pass_through', + ) or pod2usage(2); + + if ($ENV{CODEOWNERS_COMPLETIONS}) { + $self->{command} = $args[0] || ''; + my $cword = $ENV{CWORD}; + my $cur = $ENV{CUR} || ''; + # Adjust cword to remove progname + while (0 < --$cword) { + last if $cur eq ($args_copy[$cword] || ''); + } + $self->completions($cword, @args_copy); + exit 0; + } + + if ($opts->{version}) { + my $progname = path($0)->basename; + print "${progname} ${VERSION}\n"; + exit 0; + } + if ($opts->{help}) { + pod2usage(-exitval => 0, -verbose => 99, -sections => [qw(NAME SYNOPSIS OPTIONS)]); + } + if ($opts->{manual}) { + pod2usage(-exitval => 0, -verbose => 2); + } + if (defined $opts->{shell_completion}) { + $self->shell_completion($opts->{shell_completion}); + exit 0; + } + + # figure out the command (or default to "show") + my $command = shift @args; + my $command_options = $self->command_options->{$command || ''}; + if (!$command_options) { + unshift @args, $command if defined $command; + $command = 'show'; + $command_options = $self->command_options->{$command}; + } + + my $more_opts = $self->get_options( + args => \@args, + spec => $command_options, + ) or pod2usage(2); + + %$self = (%$opts, %$more_opts, command => $command, args => \@args); + return $self; +} + +sub command { + my $self = shift; + my $command = $self->{command}; + my @commands = sort keys %{$self->command_options}; + return if not grep { $_ eq $command } @commands; + $command =~ s/[^a-z]/_/g; + return $command; +} + +sub args { + my $self = shift; + return @{$self->{args} || []}; +} + +=method get_options + + $options = $options->get_options( + args => \@ARGV, + spec => \@expected_options, + callback => sub { my ($arg, $results) = @_; ... }, + ); + +Convert command-line arguments to options, based on specified rules. + +Returns a hashref of options or C if an error occurred. + +=for :list +* C - Arguments from the caller (e.g. C<@ARGV>). +* C - List of L compatible option strings. +* C - Optional coderef to call for non-option arguments. +* C - Optional L configuration string. + +=cut + +sub get_options { + my $self = shift; + my $args = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_}; + + my %options; + my %results; + while (my ($opt, $default_value) = each %{$args->{spec}}) { + my ($name) = $opt =~ /^([^=:!|]+)/; + $name =~ s/-/_/g; + $results{$name} = $default_value; + $options{$opt} = \$results{$name}; + } + + if (my $fn = $args->{callback}) { + $options{'<>'} = sub { + my $arg = shift; + $fn->($arg, \%results); + }; + } + + my $p = Getopt::Long::Parser->new; + $p->configure($args->{config} || 'default'); + return if !$p->getoptionsfromarray($args->{args}, %options); + + return \%results; +} + +=method shell_completion + + $options->shell_completion($shell_type); + +Print shell code to C for the given type of shell. When eval'd, the shell code enables +completion for the F command. + +=cut + +sub shell_completion { + my $self = shift; + my $type = lc(shift || 'bash'); + + if ($type eq 'bash') { + print <<'END'; +# git-codeowners - Bash completion +# To use, eval this code: +# eval "$(git-codeowners --shell-completion)" +# This will work without the bash-completion package, but handling of colons +# in the completion word will work better with bash-completion installed and +# enabled. +_git_codeowners() { + local cur words cword + if declare -f _get_comp_words_by_ref >/dev/null + then + _get_comp_words_by_ref -n : cur cword words + else + words=("${COMP_WORDS[@]}") + cword=${COMP_CWORD} + cur=${words[cword]} + fi + local IFS=$'\n' + COMPREPLY=($(CODEOWNERS_COMPLETIONS=1 CWORD="$cword" CUR="$cur" ${words[@]})) + # COMPREPLY=($(${words[0]} --completions "$cword" "${words[@]}")) + if [[ "$?" -eq 9 ]] + then + COMPREPLY=($(compgen -A "${COMPREPLY[0]}" -- "$cur")) + fi + declare -f __ltrim_colon_completions >/dev/null && \ + __ltrim_colon_completions "$cur" + return 0 +} +complete -F _git_codeowners git-codeowners +END + } + else { + # TODO - Would be nice to support Zsh + warn "No such shell completion: $type\n"; + } +} + +=method completions + + $options->completions($current_arg_index, @args); + +Print completions to C for the given argument list and cursor position, and exit. + +May also exit with status 9 and a compgen action printed to C to indicate that the shell +should generate its own completions. + +Doesn't return. + +=cut + +sub completions { + my $self = shift; + my $cword = shift; + my @words = @_; + + my $current = $words[$cword] || ''; + my $prev = $words[$cword - 1] || ''; + + my $reply; + + if ($prev eq '--format' || $prev eq '-f') { + $reply = $self->_completion_formats; + } + elsif ($current =~ /^-/) { + $reply = $self->_completion_options; + } + else { + if (!$self->command) { + $reply = [$self->commands, @{$self->_completion_options([keys %{$self->early_options}])}]; + } + else { + print 'file'; + exit 9; + } + } + + local $, = "\n"; + print grep { /^\Q$current\E/ } @$reply; + exit 0; +} + +sub _completion_options { + my $self = shift; + my $opts = shift || [$self->options]; + + my @options; + + for my $option (@$opts) { + my ($names, $op, $vtype) = $option =~ /^([^=:!]+)([=:!]?)(.*)$/; + my @names = split(/\|/, $names); + + for my $name (@names) { + if ($op eq '!') { + push @options, "--$name", "--no-$name"; + } + else { + if (length($name) > 1) { + push @options, "--$name"; + } + else { + push @options, "-$name"; + } + } + } + } + + return [sort @options]; +} + +sub _completion_formats { [qw(csv json json:pretty tsv yaml)] } + +1; diff --git a/lib/App/Codeowners/Util.pm b/lib/App/Codeowners/Util.pm new file mode 100644 index 0000000..afad95d --- /dev/null +++ b/lib/App/Codeowners/Util.pm @@ -0,0 +1,229 @@ +package App::Codeowners::Util; +# ABSTRACT: Grab bag of utility subs for Codeowners modules + +=head1 DESCRIPTION + +B except in L and related modules. + +=cut + +use warnings; +use strict; + +use Encode qw(decode); +use Exporter qw(import); +use Path::Tiny; + +our @EXPORT_OK = qw( + colorstrip + find_codeowners_in_directory + find_nearest_codeowners + git_ls_files + git_toplevel + run_git + stringf + unbackslash +); + +our $VERSION = '9999.999'; # VERSION + +=func find_nearest_codeowners + + $filepath = find_nearest_codeowners($dirpath); + +Find the F file in the current working directory, or search in the +parent directory recursively until a F file is found. + +Returns C if no F is found. + +=cut + +sub find_nearest_codeowners { + my $path = path(shift || '.')->absolute; + + while (!$path->is_rootdir) { + my $filepath = find_codeowners_in_directory($path); + return $filepath if $filepath; + $path = $path->parent; + } +} + +=func find_codeowners_in_directory + + $filepath = find_codeowners_in_directory($dirpath); + +Find the F file in a given directory. No recursive searching is done. + +Returns the first of (or undef if none found): + +=for :list +* F +* F +* F<.bitbucket/CODEOWNERS> +* F<.github/CODEOWNERS> +* F<.gitlab/CODEOWNERS> + +=cut + +sub find_codeowners_in_directory { + my $path = path(shift) or die; + + my @tries = ( + [qw(CODEOWNERS)], + [qw(docs CODEOWNERS)], + [qw(.bitbucket CODEOWNERS)], + [qw(.github CODEOWNERS)], + [qw(.gitlab CODEOWNERS)], + ); + + for my $parts (@tries) { + my $try = $path->child(@$parts); + return $try if $try->is_file; + } +} + +sub run_git { + my @cmd = ('git', @_); + + require IPC::Open2; + + my ($child_in, $child_out); + my $pid = IPC::Open2::open2($child_out, $child_in, @cmd); + close($child_in); + + binmode($child_out, ':encoding(UTF-8)'); + chomp(my @lines = <$child_out>); + + waitpid($pid, 0); + return if $? != 0; + + return @lines; +} + +sub git_ls_files { + my $dir = shift || '.'; + + my @files = run_git('-C', $dir, qw{ls-files}, @_); + + return undef if !@files; ## no critic (Subroutines::ProhibitExplicitReturn) + + # Depending on git's "core.quotepath" config, non-ASCII chars may be + # escaped (identified by surrounding dquotes), so try to unescape. + for my $file (@files) { + next if $file !~ /^"(.+)"$/; + $file = $1; + $file = unbackslash($file); + $file = decode('UTF-8', $file); + } + + return \@files; +} + +sub git_toplevel { + my $dir = shift || '.'; + + my ($path) = run_git('-C', $dir, qw{rev-parse --show-toplevel}); + + return if !$path; + return path($path); +} + +sub colorstrip { + my $str = shift || ''; + $str =~ s/\e\[[\d;]*m//g; + return $str; +} + +# The stringf code is from String::Format (thanks SREZIC), with changes: +# - Use Unicode::GCString for better Unicode character padding, +# - Strip ANSI color sequences, +# - Prevent 'Negative repeat count does nothing' warnings +sub _replace { + my ($args, $orig, $alignment, $min_width, + $max_width, $passme, $formchar) = @_; + + # For unknown escapes, return the orignial + return $orig unless defined $args->{$formchar}; + + $alignment = '+' unless defined $alignment; + + my $replacement = $args->{$formchar}; + if (ref $replacement eq 'CODE') { + # $passme gets passed to subrefs. + $passme ||= ""; + $passme =~ tr/{}//d; + $replacement = $replacement->($passme); + } + + my $replength; + if (eval { require Unicode::GCString }) { + my $gcstring = Unicode::GCString->new(colorstrip($replacement)); + $replength = $gcstring->columns; + } + else { + $replength = length colorstrip($replacement); + } + + $min_width ||= $replength; + $max_width ||= $replength; + + # length of replacement is between min and max + if (($replength > $min_width) && ($replength < $max_width)) { + return $replacement; + } + + # length of replacement is longer than max; truncate + if ($replength > $max_width) { + return substr($replacement, 0, $max_width); + } + + my $padding = $min_width - $replength; + $padding = 0 if $padding < 0; + + # length of replacement is less than min: pad + if ($alignment eq '-') { + # left align; pad in front + return $replacement . ' ' x $padding; + } + + # right align, pad at end + return ' ' x $padding . $replacement; +} +my $regex = qr/ + (% # leading '%' + (-)? # left-align, rather than right + (\d*)? # (optional) minimum field width + (?:\.(\d*))? # (optional) maximum field width + (\{.*?\})? # (optional) stuff inside + (\S) # actual format character + )/x; +sub stringf { + my $format = shift || return; + my $args = UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ }; + $args->{'n'} = "\n" unless exists $args->{'n'}; + $args->{'t'} = "\t" unless exists $args->{'t'}; + $args->{'%'} = "%" unless exists $args->{'%'}; + + $format =~ s/$regex/_replace($args, $1, $2, $3, $4, $5, $6)/ge; + + return $format; +} + +# The unbacklash code is from String::Escape (thanks EVO), with changes: +# - Handle \a, \b, \f and \v (thanks Berk Akinci) +my %unbackslash; +sub unbackslash { + my $str = shift; + # Earlier definitions are preferred to later ones, thus we output \n not \x0d + %unbackslash = ( + ( map { $_ => $_ } ( '\\', '"', '$', '@' ) ), + ( 'r' => "\r", 'n' => "\n", 't' => "\t" ), + ( map { 'x' . unpack('H2', chr($_)) => chr($_) } (0..255) ), + ( map { sprintf('%03o', $_) => chr($_) } (0..255) ), + ( 'a' => "\x07", 'b' => "\x08", 'f' => "\x0c", 'v' => "\x0b" ), + ) if !%unbackslash; + $str =~ s/ (\A|\G|[^\\]) \\ ( [0-7]{3} | x[\da-fA-F]{2} | . ) / $1 . $unbackslash{lc($2)} /gsxe; + return $str; +} + +1; diff --git a/lib/File/Codeowners.pm b/lib/File/Codeowners.pm new file mode 100644 index 0000000..fb3ec55 --- /dev/null +++ b/lib/File/Codeowners.pm @@ -0,0 +1,502 @@ +package File::Codeowners; +# ABSTRACT: Read and write CODEOWNERS files + +use v5.10.1; # defined-or +use warnings; +use strict; + +use Encode qw(encode); +use Path::Tiny; +use Scalar::Util qw(openhandle); +use Text::Gitignore qw(build_gitignore_matcher); + +our $VERSION = '9999.999'; # VERSION + +sub _croak { require Carp; Carp::croak(@_); } +sub _usage { _croak("Usage: @_\n") } + +=method new + + $codeowners = File::Codeowners->new; + +Construct a new L. + +=cut + +sub new { + my $class = shift; + my $self = bless {}, $class; +} + +=method parse + + $codeowners = File::Codeowners->parse('path/to/CODEOWNERS'); + $codeowners = File::Codeowners->parse($filehandle); + $codeowners = File::Codeowners->parse(\@lines); + $codeowners = File::Codeowners->parse(\$string); + +Parse a F file. + +This is a shortcut for the C methods. + +=cut + +sub parse { + my $self = shift; + my $input = shift or _usage(q{$codeowners->parse($input)}); + + return $self->parse_from_array($input, @_) if @_; + return $self->parse_from_array($input) if ref($input) eq 'ARRAY'; + return $self->parse_from_string($input) if ref($input) eq 'SCALAR'; + return $self->parse_from_fh($input) if openhandle($input); + return $self->parse_from_filepath($input); +} + +=method parse_from_filepath + + $codeowners = File::Codeowners->parse_from_filepath('path/to/CODEOWNERS'); + +Parse a F file from the filesystem. + +=cut + +sub parse_from_filepath { + my $self = shift; + my $path = shift or _usage(q{$codeowners->parse_from_filepath($filepath)}); + + $self = bless({}, $self) if !ref($self); + + return $self->parse_from_fh(path($path)->openr_utf8); +} + +=method parse_from_fh + + $codeowners = File::Codeowners->parse_from_fh($filehandle); + +Parse a F file from an open filehandle. + +=cut + +sub parse_from_fh { + my $self = shift; + my $fh = shift or _usage(q{$codeowners->parse_from_fh($fh)}); + + $self = bless({}, $self) if !ref($self); + + my @lines; + + my $parse_unowned; + my %unowned; + my $current_project; + + while (my $line = <$fh>) { + my $lineno = $. - 1; + chomp $line; + if ($line eq '### UNOWNED (File::Codeowners)') { + $parse_unowned++; + last; + } + elsif ($line =~ /^\h*#(.*)/) { + my $comment = $1; + if ($comment =~ /^\h*Project:\h*(.+?)\h*$/i) { + $current_project = $1 || undef; + } + $lines[$lineno] = { + comment => $comment, + }; + } + elsif ($line =~ /^\h*$/) { + # blank line + } + elsif ($line =~ /^\h*(.+?)(? $pattern, + owners => \@owners, + $current_project ? (project => $current_project) : (), + }; + } + else { + die "Parse error on line $.: $line\n"; + } + } + + if ($parse_unowned) { + while (my $line = <$fh>) { + chomp $line; + if ($line =~ /# (.+)/) { + my $filepath = $1; + $unowned{$filepath}++; + } + } + } + + $self->{lines} = \@lines; + $self->{unowned} = \%unowned; + + return $self; +} + +=method parse_from_array + + $codeowners = File::Codeowners->parse_from_array(\@lines); + +Parse a F file stored as lines in an array. + +=cut + +sub parse_from_array { + my $self = shift; + my $arr = shift or _usage(q{$codeowners->parse_from_array(\@lines)}); + + $self = bless({}, $self) if !ref($self); + + $arr = [$arr, @_] if @_; + my $str = join("\n", @$arr); + return $self->parse_from_string(\$str); +} + +=method parse_from_string + + $codeowners = File::Codeowners->parse_from_string(\$string); + $codeowners = File::Codeowners->parse_from_string($string); + +Parse a F file stored as a string. String should be UTF-8 encoded. + +=cut + +sub parse_from_string { + my $self = shift; + my $str = shift or _usage(q{$codeowners->parse_from_string(\$string)}); + + $self = bless({}, $self) if !ref($self); + + my $ref = ref($str) eq 'SCALAR' ? $str : \$str; + open(my $fh, '<:encoding(UTF-8)', $ref) or die "open failed: $!"; + + return $self->parse_from_fh($fh); +} + +=method write_to_filepath + + $codeowners->write_to_filepath($filepath); + +Write the contents of the file to the filesystem atomically. + +=cut + +sub write_to_filepath { + my $self = shift; + my $path = shift or _usage(q{$codeowners->write_to_filepath($filepath)}); + + path($path)->spew_utf8([map { "$_\n" } @{$self->write_to_array('')}]); +} + +=method write_to_fh + + $codeowners->write_to_fh($fh); + +Format the file contents and write to a filehandle. + +=cut + +sub write_to_fh { + my $self = shift; + my $fh = shift or _usage(q{$codeowners->write_to_fh($fh)}); + + for my $line (@{$self->write_to_array}) { + print $fh "$line\n"; + } +} + +=method write_to_string + + $scalarref = $codeowners->write_to_string; + +Format the file contents and return a reference to a formatted string. + +=cut + +sub write_to_string { + my $self = shift; + + my $str = join("\n", @{$self->write_to_array}) . "\n"; + return \$str; +} + +=method write_to_array + + $lines = $codeowners->write_to_array; + +Format the file contents as an arrayref of lines. + +=cut + +sub write_to_array { + my $self = shift; + my $charset = shift // 'UTF-8'; + + my @format; + + for my $line (@{$self->_lines}) { + if (my $comment = $line->{comment}) { + push @format, "#$comment"; + } + elsif (my $pattern = $line->{pattern}) { + my $owners = join(' ', @{$line->{owners}}); + push @format, "$pattern $owners"; + } + else { + push @format, ''; + } + } + + my @unowned = sort keys %{$self->_unowned}; + if (@unowned) { + push @format, '' if $format[-1]; + push @format, '### UNOWNED (File::Codeowners)'; + for my $unowned (@unowned) { + push @format, "# $unowned"; + } + } + + if ($charset) { + $_ = encode($charset, $_) for @format; + } + return \@format; +} + +=method match + + $owners = $codeowners->match($filepath); + +Match the given filepath against the available patterns and return just the +owners for the matching pattern. Patterns are checked in the reverse order +they were defined in the file. + +Returns C if no patterns match. + +=cut + +sub match { + my $self = shift; + my $filepath = shift or _usage(q{$codeowners->match($filepath)}); + + my $lines = $self->{match_lines} ||= [reverse grep { ($_ || {})->{pattern} } @{$self->_lines}]; + + for my $line (@$lines) { + my $matcher = $line->{matcher} ||= build_gitignore_matcher([$line->{pattern}]); + return { # deep copy + pattern => $line->{pattern}, + owners => [@{$line->{owners} || []}], + $line->{project} ? (project => $line->{project}) : (), + } if $matcher->($filepath); + } + + return undef; ## no critic (Subroutines::ProhibitExplicitReturn) +} + +=method owners + + $owners = $codeowners->owners; # get all defined owners + $owners = $codeowners->owners($pattern); + +Get an arrayref of owners defined in the file. If a pattern argument is given, +only owners for the given pattern are returned (or empty arrayref if the +pattern does not exist). If no argument is given, simply returns all owners +defined in the file. + +=cut + +sub owners { + my $self = shift; + my $pattern = shift; + + return $self->{owners} if !$pattern && $self->{owners}; + + my %owners; + for my $line (@{$self->_lines}) { + next if $pattern && $line->{pattern} && $pattern ne $line->{pattern}; + $owners{$_}++ for (@{$line->{owners} || []}); + } + + my $owners = [sort keys %owners]; + $self->{owners} = $owners if !$pattern; + + return $owners; +} + +=method patterns + + $patterns = $codeowners->patterns; + $patterns = $codeowners->patterns($owner); + +Get an arrayref of all patterns defined. + +=cut + +sub patterns { + my $self = shift; + my $owner = shift; + + return $self->{patterns} if !$owner && $self->{patterns}; + + my %patterns; + for my $line (@{$self->_lines}) { + next if $owner && !grep { $_ eq $owner } @{$line->{owners} || []}; + my $pattern = $line->{pattern}; + $patterns{$pattern}++ if $pattern; + } + + my $patterns = [sort keys %patterns]; + $self->{patterns} = $patterns if !$owner; + + return $patterns; +} + +=method update_owners + + $codeowners->update_owners($pattern => \@new_owners); + +Set a new set of owners for a given pattern. If for some reason the file has +multiple such patterns, they will all be updated. + +Nothing happens if the file does not already have at least one such pattern. + +=cut + +sub update_owners { + my $self = shift; + my $pattern = shift; + my $owners = shift; + $pattern && $owners or _usage(q{$codeowners->update_owners($pattern => \@owners)}); + + $owners = [$owners] if ref($owners) ne 'ARRAY'; + + $self->_clear; + + for my $line (@{$self->_lines}) { + next if !$line->{pattern}; + next if $pattern ne $line->{pattern}; + $line->{owners} = [@$owners]; + } +} + +=method append + + $codeowners->append(comment => $str); + $codeowners->append(pattern => $pattern, owners => \@owners); + $codeowners->append(); # blank line + +Append a new line. + +=cut + +sub append { + my $self = shift; + $self->_clear; + push @{$self->_lines}, (@_ ? {@_} : undef); +} + +=method prepend + + $codeowners->prepend(comment => $str); + $codeowners->prepend(pattern => $pattern, owners => \@owners); + $codeowners->prepend(); # blank line + +Prepend a new line. + +=cut + +sub prepend { + my $self = shift; + $self->_clear; + unshift @{$self->_lines}, (@_ ? {@_} : undef); +} + +=method unowned + + $filepaths = $codeowners->unowned; + +Get the list of filepaths in the "unowned" section. + +This parser supports an "extension" to the F file format which +lists unowned files at the end of the file. This list can be useful to have in +order to figure out what files we know are unowned versus what files we don't +know are unowned. + +=cut + +sub unowned { + my $self = shift; + [sort keys %{$self->{unowned} || {}}]; +} + +=method add_unowned + + $codeowners->add_unowned($filepath, ...); + +Add one or more filepaths to the "unowned" list. + +This method does not check to make sure the filepath(s) actually do not match +any patterns in the file, so you might want to call L first. + +See L for an explanation. + +=cut + +sub add_unowned { + my $self = shift; + $self->_unowned->{$_}++ for @_; +} + +=method remove_unowned + + $codeowners->remove_unowned($filepath, ...); + +Remove one or more filepaths from the "unowned" list. + +Silently ignores filepaths that are already not listed. + +See L for an explanation. + +=cut + +sub remove_unowned { + my $self = shift; + delete $self->_unowned->{$_} for @_; +} + +sub is_unowned { + my $self = shift; + my $filepath = shift; + $self->_unowned->{$filepath}; +} + +=method clear_unowned + + $codeowners->clear_unowned; + +Remove all filepaths from the "unowned" list. + +See L for an explanation. + +=cut + +sub clear_unowned { + my $self = shift; + $self->{unowned} = {}; +} + +sub _lines { shift->{lines} ||= [] } +sub _unowned { shift->{unowned} ||= {} } + +sub _clear { + my $self = shift; + delete $self->{match_lines}; + delete $self->{owners}; + delete $self->{patterns}; +} + +1; diff --git a/lib/Test/File/Codeowners.pm b/lib/Test/File/Codeowners.pm new file mode 100644 index 0000000..8bdf14b --- /dev/null +++ b/lib/Test/File/Codeowners.pm @@ -0,0 +1,106 @@ +package Test::File::Codeowners; +# ABSTRACT: Write tests for CODEOWNERS files + +=head1 SYNOPSIS + + use Test::More; + + eval 'use Test::File::Codeowners'; + plan skip_all => 'Test::File::Codeowners required for testing CODEOWNERS' if $@; + + codeowners_syntax_ok(); + done_testing; + +=head1 DESCRIPTION + +This package has assertion subroutines for testing F files. + +=cut + +use warnings; +use strict; + +use App::Codeowners::Util qw(find_nearest_codeowners git_ls_files git_toplevel); +use Encode qw(encode); +use File::Codeowners; +use Test::Builder; + +our $VERSION = '9999.999'; # VERSION + +my $Test = Test::Builder->new; + +sub import { + my $self = shift; + my $caller = caller; + no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict) + *{$caller.'::codeowners_syntax_ok'} = \&codeowners_syntax_ok; + *{$caller.'::codeowners_git_files_ok'} = \&codeowners_git_files_ok; + + $Test->exported_to($caller); + $Test->plan(@_); +} + +=func codeowners_syntax_ok + + codeowners_syntax_ok(); # search up the tree for a CODEOWNERS file + codeowners_syntax_ok($filepath); + +Check the syntax of a F file. + +=cut + +sub codeowners_syntax_ok { + my $filepath = shift || find_nearest_codeowners(); + + eval { File::Codeowners->parse($filepath) }; + my $err = $@; + + $Test->ok(!$err, "Check syntax: $filepath"); + $Test->diag($err) if $err; +} + +=func codeowners_git_files_ok + + codeowners_git_files_ok(); # search up the tree for a CODEOWNERS file + codeowners_git_files_ok($filepath); + +=cut + +sub codeowners_git_files_ok { + my $filepath = shift || find_nearest_codeowners(); + + $Test->subtest('codeowners_git_files_ok' => sub { + my $codeowners = eval { File::Codeowners->parse($filepath) }; + if (my $err = $@) { + $Test->plan(tests => 1); + $Test->ok(0, "Parse $filepath"); + $Test->diag($err); + return; + } + + my $files = git_ls_files(git_toplevel()); + + $Test->plan(@$files ? (tests => scalar @$files) : (skip_all => 'git ls-files failed')); + + for my $filepath (@$files) { + my $msg = encode('UTF-8', "Check file: $filepath"); + + my $match = $codeowners->match($filepath); + my $is_unowned = $codeowners->is_unowned($filepath); + + if (!$match && !$is_unowned) { + $Test->ok(0, $msg); + $Test->diag("File is unowned\n"); + } + elsif ($match && $is_unowned) { + $Test->ok(0, $msg); + $Test->diag("File is owned but listed as unowned\n"); + } + else { + $Test->ok(1, $msg); + } + } + }); +} + +1; diff --git a/t/app-codeowners-util.t b/t/app-codeowners-util.t new file mode 100644 index 0000000..93fdce4 --- /dev/null +++ b/t/app-codeowners-util.t @@ -0,0 +1,93 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +use App::Codeowners::Util qw(run_git); +use Path::Tiny qw(path tempdir); +use Test::More; + +can_ok('App::Codeowners::Util', qw{ + find_nearest_codeowners + find_codeowners_in_directory + run_git + git_ls_files + git_toplevel +}); + +my $can_git = _can_git(); + +subtest 'git_ls_files' => sub { + plan skip_all => 'Cannot run git' if !$can_git; + my $repodir =_setup_git_repo(); + + my $r = App::Codeowners::Util::git_ls_files($repodir); + is($r, undef, 'git ls-files returns undef when no repo files') or diag explain $r; + + run_git('-C', $repodir, qw{add .}); + run_git('-C', $repodir, qw{commit -m}, 'initial commit'); + + $r = App::Codeowners::Util::git_ls_files($repodir); + is_deeply($r, [ + qw(a/b/c/bar.txt foo.txt) + ], 'git ls-files returns correct repo files') or diag explain $r; +}; + +subtest 'git_toplevel' => sub { + plan skip_all => 'Cannot run git' if !$can_git; + my $repodir =_setup_git_repo(); + + my $r = App::Codeowners::Util::git_toplevel($repodir); + is($r, $repodir, 'found toplevel directory from toplevel'); + + $r = App::Codeowners::Util::git_toplevel($repodir->child('a/b')); + is($r, $repodir, 'found toplevel directory'); +}; + +subtest 'find_nearest_codeowners' => sub { + my $repodir =_setup_git_repo(); + $repodir->child('docs')->mkpath; + my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS')); + + my $r = App::Codeowners::Util::find_nearest_codeowners($repodir->child('a/b/c')); + is($r, $filepath, 'found CODEOWNERS file'); +}; + +subtest 'find_codeowners_in_directory' => sub { + my $repodir =_setup_git_repo(); + $repodir->child('docs')->mkpath; + + my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS')); + + my $r = App::Codeowners::Util::find_codeowners_in_directory($repodir); + is($r, $filepath, 'found CODEOWNERS file in docs'); + + $filepath = _spew_codeowners($repodir->child('CODEOWNERS')); + $r = App::Codeowners::Util::find_codeowners_in_directory($repodir); + is($r, $filepath, 'found CODEOWNERS file in toplevel'); +}; + +done_testing; +exit; + +sub _can_git { + my ($version) = run_git('--version'); + return $version; +} + +sub _setup_git_repo { + my $repodir = tempdir; + + run_git('-C', $repodir, 'init'); + + $repodir->child('foo.txt')->touchpath; + $repodir->child('a/b/c/bar.txt')->touchpath; + + return $repodir; +} + +sub _spew_codeowners { + my $path = path(shift); + $path->spew_utf8(\"foo.txt \@twix\n"); + return $path; +} diff --git a/t/app-codeowners.t b/t/app-codeowners.t new file mode 100644 index 0000000..5d37841 --- /dev/null +++ b/t/app-codeowners.t @@ -0,0 +1,100 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +use FindBin '$Bin'; +use Test::Exit; # must be first +use App::Codeowners::Util qw(run_git); +use App::Codeowners; +use Capture::Tiny qw(capture); +use File::pushd; +use Path::Tiny qw(path tempdir); +use Test::More; + +my $can_git = _can_git(); +plan skip_all => 'Cannot run git' if !$can_git; + +# Set progname so that pod2usage knows how to find the script after we chdir +$0 = path($Bin)->parent->child('bin/git-codeowners')->absolute; + +$ENV{NO_COLOR} = 1; + +subtest 'basic options' => sub { + my $repodir = _setup_git_repo(); + my $chdir = pushd($repodir); + + my ($stdout, $stderr, $exit) = capture { exit_code { App::Codeowners->main('--help') } }; + is($exit, 0, 'exited 0 when --help'); + like($stdout, qr/Usage:/, 'correct --help output') or diag $stdout; + + ($stdout, $stderr, $exit) = capture { exit_code { App::Codeowners->main('--version') } }; + is($exit, 0, 'exited 0 when --version'); + like($stdout, qr/git-codeowners [\d.]+\n/, 'correct --version output') or diag $stdout; +}; + +subtest 'bad options' => sub { + my $repodir = _setup_git_repo(); + my $chdir = pushd($repodir); + + my ($stdout, $stderr, $exit) = capture { exit_code { App::Codeowners->main(qw{show --not-an-option}) } }; + is($exit, 2, 'exited with error on bad option'); + like($stderr, qr/Unknown option: not-an-option/, 'correct error message') or diag $stderr; +}; + +subtest 'show' => sub { + my $repodir = _setup_git_repo(); + my $chdir = pushd($repodir); + + my ($stdout, $stderr, $exit) = capture { exit_code { App::Codeowners->main(qw{-f %F;%O show}) } }; + is($exit, 0, 'exited without error'); + is($stdout, <<'END', 'correct output'); +CODEOWNERS; +a/b/c/bar.txt;@snickers +foo.txt;@twix +END + + ($stdout, $stderr, $exit) = capture { exit_code { App::Codeowners->main(qw{-f %F;%O;%P show}) } }; + is($exit, 0, 'exited without error'); + is($stdout, <<'END', 'correct output'); +CODEOWNERS;; +a/b/c/bar.txt;@snickers;peanuts +foo.txt;@twix; +END + + subtest 'format json' => sub { + plan skip_all => 'No JSON::MaybeXS' if !eval { require JSON::MaybeXS }; + + ($stdout, $stderr, $exit) = capture { exit_code { App::Codeowners->main(qw{-f json show --no-project}) } }; + is($exit, 0, 'exited without error'); + my $expect = '[{"File":"CODEOWNERS","Owner":null},{"File":"a/b/c/bar.txt","Owner":["@snickers"]},{"File":"foo.txt","Owner":["@twix"]}]'; + is($stdout, $expect, 'correct output with json format'); + }; +}; + +done_testing; +exit; + +sub _can_git { + my ($version) = run_git('--version'); + return $version; +} + +sub _setup_git_repo { + my $repodir = tempdir; + + $repodir->child('foo.txt')->touchpath; + $repodir->child('a/b/c/bar.txt')->touchpath; + $repodir->child('CODEOWNERS')->spew_utf8([<<'END']); +# whatever +/foo.txt @twix +# Project: peanuts +a/ @snickers +END + + run_git('-C', $repodir, qw{init}); + run_git('-C', $repodir, qw{add .}); + run_git('-C', $repodir, qw{commit -m}, 'initial commit'); + + return $repodir; +} diff --git a/t/file-codeowners.t b/t/file-codeowners.t new file mode 100644 index 0000000..a50a050 --- /dev/null +++ b/t/file-codeowners.t @@ -0,0 +1,143 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +use FindBin '$Bin'; + +use File::Codeowners; +use Test::More; + +subtest 'parse CODEOWNERS files', sub { + my @basic_arr = ('#wat', '* @whatever'); + my $basic_str = "#wat\n* \@whatever\n"; + my $expected = [ + {comment => 'wat'}, + {pattern => '*', owners => ['@whatever']}, + ]; + my $r; + + my $file = File::Codeowners->parse_from_filepath("$Bin/samples/basic.CODEOWNERS"); + is_deeply($r = $file->_lines, $expected, 'parse from filepath') or diag explain $r; + + $file = File::Codeowners->parse_from_array(\@basic_arr); + is_deeply($r = $file->_lines, $expected, 'parse from array') or diag explain $r; + + $file = File::Codeowners->parse_from_string(\$basic_str); + is_deeply($r = $file->_lines, $expected, 'parse from string') or diag explain $r; + + open(my $fh, '<', \$basic_str) or die "open failed: $!"; + $file = File::Codeowners->parse_from_fh($fh); + is_deeply($r = $file->_lines, $expected, 'parse from filehandle') or diag explain $r; + close($fh); +}; + +subtest 'query information from CODEOWNERS', sub { + my $file = File::Codeowners->parse("$Bin/samples/kitchensink.CODEOWNERS"); + my $r; + + is_deeply($r = $file->owners, [ + '@"Lucius Fox"', + '@bane', + '@batman', + '@joker', + '@robin', + '@the-penguin', + 'alfred@waynecorp.example.com', + ], 'list all owners') or diag explain $r; + + is_deeply($r = $file->owners('tricks/Grinning/'), [qw( + @joker + @the-penguin + )], 'list owners matching pattern') or diag explain $r; + + is_deeply($r = $file->patterns, [qw( + * + /a/b/c/deep + /vehicles/**/batmobile.cad + mansion.txt + tricks/Explosions.doc + tricks/Grinning/ + )], 'list all patterns') or diag explain $r; + + is_deeply($r = $file->patterns('@joker'), [qw( + tricks/Explosions.doc + tricks/Grinning/ + )], 'list patterns matching owner') or diag explain $r; + + is_deeply($r = $file->unowned, [qw( + lightcycle.cad + )], 'list unowned') or diag explain $r; + + is_deeply($r = $file->match('whatever'), { + owners => [qw(@batman @robin)], + pattern => '*', + }, 'match solitary wildcard') or diag explain $r; + is_deeply($r = $file->match('subdir/mansion.txt'), { + owners => ['alfred@waynecorp.example.com'], + pattern => 'mansion.txt', + }, 'match filename') or diag explain $r; + is_deeply($r = $file->match('vehicles/batmobile.cad'), { + owners => ['@"Lucius Fox"'], + pattern => '/vehicles/**/batmobile.cad', + project => 'Transportation', + }, 'match double asterisk') or diag explain $r; + is_deeply($r = $file->match('vehicles/extra/batmobile.cad'), { + owners => ['@"Lucius Fox"'], + pattern => '/vehicles/**/batmobile.cad', + project => 'Transportation', + }, 'match double asterisk again') or diag explain $r; +}; + +subtest 'parse errors', sub { + eval { File::Codeowners->parse(\q{meh}) }; + like($@, qr/^Parse error on line 1/, 'parse error'); +}; + +subtest 'editing and writing files', sub { + my $file = File::Codeowners->parse("$Bin/samples/basic.CODEOWNERS"); + my $r; + + $file->update_owners('*' => [qw(@foo @bar @baz)]); + is_deeply($r = $file->_lines, [ + {comment => 'wat'}, + {pattern => '*', owners => [qw(@foo @bar @baz)]}, + ], 'update owners for a pattern') or diag explain $r; + is_deeply($r = $file->owners, [qw(@bar @baz @foo)], 'got updated owners') or diag explain $r; + + $file->update_owners('no/such/pattern' => [qw(@wuf)]); + is_deeply($r = $file->_lines, [ + {comment => 'wat'}, + {pattern => '*', owners => [qw(@foo @bar @baz)]}, + ], 'no change when updating nonexistent pattern') or diag explain $r; + + $file->prepend(comment => 'start'); + $file->append(pattern => 'end', owners => ['@qux']); + is_deeply($r = $file->_lines, [ + {comment => 'start'}, + {comment => 'wat'}, + {pattern => '*', owners => [qw(@foo @bar @baz)]}, + {pattern => 'end', owners => [qw(@qux)]}, + ], 'prepand and append') or diag explain $r; + + $file->add_unowned('lonely', 'afraid'); + is_deeply($r = $file->unowned, [qw(afraid lonely)], 'set unowned files') or diag explain $r; + + $file->remove_unowned('afraid'); + is_deeply($r = $file->unowned, [qw(lonely)], 'remove unowned files') or diag explain $r; + + is_deeply($r = $file->write_to_array, [ + '#start', + '#wat', + '* @foo @bar @baz', + 'end @qux', + '', + '### UNOWNED (File::Codeowners)', + '# lonely', + ], 'format file') or diag explain $r; + + $file->clear_unowned; + is_deeply($r = $file->unowned, [], 'clear unowned files') or diag explain $r; +}; + +done_testing; diff --git a/t/samples/basic.CODEOWNERS b/t/samples/basic.CODEOWNERS new file mode 100644 index 0000000..cbbe999 --- /dev/null +++ b/t/samples/basic.CODEOWNERS @@ -0,0 +1,2 @@ +#wat +* @whatever diff --git a/t/samples/kitchensink.CODEOWNERS b/t/samples/kitchensink.CODEOWNERS new file mode 100644 index 0000000..06c1688 --- /dev/null +++ b/t/samples/kitchensink.CODEOWNERS @@ -0,0 +1,18 @@ +# This is a comment. +* @batman @robin + +mansion.txt alfred@waynecorp.example.com + +tricks/Explosions.doc @joker +tricks/Grinning/ @joker @the-penguin + + # not the hero gotham deserves! +/a/b/c/deep @bane @the-penguin + +# project: Transportation + +/vehicles/**/batmobile.cad @"Lucius Fox" + + +### UNOWNED (File::Codeowners) +# lightcycle.cad -- 2.43.0