From 0047d0a57e8ffe0d7cfde415fa6f50dae30201d1 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Sat, 9 Nov 2019 17:46:16 -0700 Subject: [PATCH 1/1] Version 0.41 --- Changes | 4 + LICENSE | 379 ++++++++++++++++++++++ MANIFEST | 33 ++ META.json | 152 +++++++++ META.yml | 74 +++++ Makefile.PL | 94 ++++++ README | 209 +++++++++++++ bin/git-codeowners | 283 +++++++++++++++++ eg/test.t | 14 + lib/App/Codeowners.pm | 366 ++++++++++++++++++++++ lib/App/Codeowners/Options.pm | 350 +++++++++++++++++++++ lib/App/Codeowners/Util.pm | 281 +++++++++++++++++ lib/File/Codeowners.pm | 520 +++++++++++++++++++++++++++++++ lib/Test/File/Codeowners.pm | 141 +++++++++ t/00-compile.t | 101 ++++++ t/00-report-prereqs.dd | 90 ++++++ t/00-report-prereqs.t | 193 ++++++++++++ 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 ++ xt/author/critic.t | 7 + xt/author/eol.t | 37 +++ xt/author/minimum-version.t | 6 + xt/author/no-tabs.t | 37 +++ xt/author/pod-no404s.t | 21 ++ xt/author/pod-syntax.t | 7 + xt/author/portability.t | 10 + xt/release/consistent-version.t | 10 + xt/release/cpan-changes.t | 10 + xt/release/distmeta.t | 6 + 32 files changed, 3791 insertions(+) create mode 100644 Changes create mode 100644 LICENSE create mode 100644 MANIFEST create mode 100644 META.json create mode 100644 META.yml create mode 100644 Makefile.PL create mode 100644 README create mode 100755 bin/git-codeowners 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/00-compile.t create mode 100644 t/00-report-prereqs.dd create mode 100644 t/00-report-prereqs.t 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 create mode 100644 xt/author/critic.t create mode 100644 xt/author/eol.t create mode 100644 xt/author/minimum-version.t create mode 100644 xt/author/no-tabs.t create mode 100644 xt/author/pod-no404s.t create mode 100644 xt/author/pod-syntax.t create mode 100644 xt/author/portability.t create mode 100644 xt/release/consistent-version.t create mode 100644 xt/release/cpan-changes.t create mode 100644 xt/release/distmeta.t diff --git a/Changes b/Changes new file mode 100644 index 0000000..91ff0ab --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for App-Codeowners. + +0.41 2019-11-09 17:45:16-07:00 MST7MDT + * First public release 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/MANIFEST b/MANIFEST new file mode 100644 index 0000000..f072e92 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,33 @@ +# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.012. +Changes +LICENSE +MANIFEST +META.json +META.yml +Makefile.PL +README +bin/git-codeowners +eg/test.t +lib/App/Codeowners.pm +lib/App/Codeowners/Options.pm +lib/App/Codeowners/Util.pm +lib/File/Codeowners.pm +lib/Test/File/Codeowners.pm +t/00-compile.t +t/00-report-prereqs.dd +t/00-report-prereqs.t +t/app-codeowners-util.t +t/app-codeowners.t +t/file-codeowners.t +t/samples/basic.CODEOWNERS +t/samples/kitchensink.CODEOWNERS +xt/author/critic.t +xt/author/eol.t +xt/author/minimum-version.t +xt/author/no-tabs.t +xt/author/pod-no404s.t +xt/author/pod-syntax.t +xt/author/portability.t +xt/release/consistent-version.t +xt/release/cpan-changes.t +xt/release/distmeta.t diff --git a/META.json b/META.json new file mode 100644 index 0000000..773b23b --- /dev/null +++ b/META.json @@ -0,0 +1,152 @@ +{ + "abstract" : "A tool for managing CODEOWNERS files", + "author" : [ + "Charles McGarvey " + ], + "dynamic_config" : 0, + "generated_by" : "Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010", + "license" : [ + "perl_5" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : 2 + }, + "name" : "App-Codeowners", + "no_index" : { + "directory" : [ + "eg", + "share", + "shares", + "t", + "xt" + ] + }, + "prereqs" : { + "configure" : { + "requires" : { + "ExtUtils::MakeMaker" : "0" + } + }, + "develop" : { + "recommends" : { + "App::FatPacker" : "0", + "CPAN::Meta" : "0", + "Capture::Tiny" : "0", + "Config" : "0", + "File::pushd" : "0", + "Getopt::Long" : "0", + "MetaCPAN::API" : "0", + "Module::CoreList" : "0", + "Path::Tiny" : "0", + "Perl::Strip" : "0" + }, + "requires" : { + "Dist::Zilla" : "5", + "Dist::Zilla::Plugin::ConsistentVersionTest" : "0", + "Dist::Zilla::Plugin::Prereqs" : "0", + "Dist::Zilla::Plugin::RemovePhasedPrereqs" : "0", + "Dist::Zilla::Plugin::Run::Release" : "0", + "Dist::Zilla::PluginBundle::Author::CCM" : "0", + "Dist::Zilla::PluginBundle::Filter" : "0", + "Software::License::Perl_5" : "0", + "Test::CPAN::Changes" : "0.19", + "Test::CPAN::Meta" : "0", + "Test::ConsistentVersion" : "0", + "Test::EOL" : "0", + "Test::MinimumVersion" : "0", + "Test::More" : "0.96", + "Test::NoTabs" : "0", + "Test::Perl::Critic" : "0", + "Test::Pod" : "1.41", + "Test::Pod::No404s" : "0", + "Test::Portability::Files" : "0" + } + }, + "runtime" : { + "recommends" : { + "Term::Detect::Software" : "0", + "Unicode::GCString" : "0" + }, + "requires" : { + "Carp" : "0", + "Color::ANSI::Util" : "0", + "Encode" : "0", + "Exporter" : "0", + "Getopt::Long" : "2.39", + "IPC::Open2" : "0", + "Path::Tiny" : "0", + "Pod::Usage" : "0", + "Scalar::Util" : "0", + "Test::Builder" : "0", + "Text::Gitignore" : "0", + "Text::Table::Any" : "0", + "perl" : "v5.10.1", + "strict" : "0", + "utf8" : "0", + "warnings" : "0" + }, + "suggests" : { + "JSON::MaybeXS" : "0", + "Text::CSV" : "0", + "Text::Table" : "0", + "YAML" : "0" + } + }, + "test" : { + "recommends" : { + "CPAN::Meta" : "2.120900" + }, + "requires" : { + "Capture::Tiny" : "0", + "ExtUtils::MakeMaker" : "0", + "File::Spec" : "0", + "File::pushd" : "0", + "FindBin" : "0", + "IO::Handle" : "0", + "IPC::Open3" : "0", + "Test::Exit" : "0", + "Test::More" : "0" + } + } + }, + "provides" : { + "App::Codeowners" : { + "file" : "lib/App/Codeowners.pm", + "version" : "0.41" + }, + "App::Codeowners::Options" : { + "file" : "lib/App/Codeowners/Options.pm", + "version" : "0.41" + }, + "App::Codeowners::Util" : { + "file" : "lib/App/Codeowners/Util.pm", + "version" : "0.41" + }, + "File::Codeowners" : { + "file" : "lib/File/Codeowners.pm", + "version" : "0.41" + }, + "Test::File::Codeowners" : { + "file" : "lib/Test/File/Codeowners.pm", + "version" : "0.41" + } + }, + "release_status" : "stable", + "resources" : { + "bugtracker" : { + "web" : "https://github.com/chazmcgarvey/git-codeowners/issues" + }, + "homepage" : "https://github.com/chazmcgarvey/git-codeowners", + "repository" : { + "type" : "git", + "url" : "https://github.com/chazmcgarvey/git-codeowners.git", + "web" : "https://github.com/chazmcgarvey/git-codeowners" + } + }, + "version" : "0.41", + "x_authority" : "cpan:CCM", + "x_generated_by_perl" : "v5.28.0", + "x_serialization_backend" : "Cpanel::JSON::XS version 4.15" +} + diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..78c7ee4 --- /dev/null +++ b/META.yml @@ -0,0 +1,74 @@ +--- +abstract: 'A tool for managing CODEOWNERS files' +author: + - 'Charles McGarvey ' +build_requires: + Capture::Tiny: '0' + ExtUtils::MakeMaker: '0' + File::Spec: '0' + File::pushd: '0' + FindBin: '0' + IO::Handle: '0' + IPC::Open3: '0' + Test::Exit: '0' + Test::More: '0' +configure_requires: + ExtUtils::MakeMaker: '0' +dynamic_config: 0 +generated_by: 'Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010' +license: perl +meta-spec: + url: http://module-build.sourceforge.net/META-spec-v1.4.html + version: '1.4' +name: App-Codeowners +no_index: + directory: + - eg + - share + - shares + - t + - xt +provides: + App::Codeowners: + file: lib/App/Codeowners.pm + version: '0.41' + App::Codeowners::Options: + file: lib/App/Codeowners/Options.pm + version: '0.41' + App::Codeowners::Util: + file: lib/App/Codeowners/Util.pm + version: '0.41' + File::Codeowners: + file: lib/File/Codeowners.pm + version: '0.41' + Test::File::Codeowners: + file: lib/Test/File/Codeowners.pm + version: '0.41' +recommends: + Term::Detect::Software: '0' + Unicode::GCString: '0' +requires: + Carp: '0' + Color::ANSI::Util: '0' + Encode: '0' + Exporter: '0' + Getopt::Long: '2.39' + IPC::Open2: '0' + Path::Tiny: '0' + Pod::Usage: '0' + Scalar::Util: '0' + Test::Builder: '0' + Text::Gitignore: '0' + Text::Table::Any: '0' + perl: v5.10.1 + strict: '0' + utf8: '0' + warnings: '0' +resources: + bugtracker: https://github.com/chazmcgarvey/git-codeowners/issues + homepage: https://github.com/chazmcgarvey/git-codeowners + repository: https://github.com/chazmcgarvey/git-codeowners.git +version: '0.41' +x_authority: cpan:CCM +x_generated_by_perl: v5.28.0 +x_serialization_backend: 'YAML::Tiny version 1.73' diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..c4ebbbe --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,94 @@ +# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.012. +use strict; +use warnings; + +use 5.010001; + +use ExtUtils::MakeMaker; + +my %WriteMakefileArgs = ( + "ABSTRACT" => "A tool for managing CODEOWNERS files", + "AUTHOR" => "Charles McGarvey ", + "CONFIGURE_REQUIRES" => { + "ExtUtils::MakeMaker" => 0 + }, + "DISTNAME" => "App-Codeowners", + "EXE_FILES" => [ + "bin/git-codeowners" + ], + "LICENSE" => "perl", + "MIN_PERL_VERSION" => "5.010001", + "NAME" => "App::Codeowners", + "PREREQ_PM" => { + "Carp" => 0, + "Color::ANSI::Util" => 0, + "Encode" => 0, + "Exporter" => 0, + "Getopt::Long" => "2.39", + "IPC::Open2" => 0, + "Path::Tiny" => 0, + "Pod::Usage" => 0, + "Scalar::Util" => 0, + "Test::Builder" => 0, + "Text::Gitignore" => 0, + "Text::Table::Any" => 0, + "strict" => 0, + "utf8" => 0, + "warnings" => 0 + }, + "TEST_REQUIRES" => { + "Capture::Tiny" => 0, + "ExtUtils::MakeMaker" => 0, + "File::Spec" => 0, + "File::pushd" => 0, + "FindBin" => 0, + "IO::Handle" => 0, + "IPC::Open3" => 0, + "Test::Exit" => 0, + "Test::More" => 0 + }, + "VERSION" => "0.41", + "test" => { + "TESTS" => "t/*.t" + } +); + + +my %FallbackPrereqs = ( + "Capture::Tiny" => 0, + "Carp" => 0, + "Color::ANSI::Util" => 0, + "Encode" => 0, + "Exporter" => 0, + "ExtUtils::MakeMaker" => 0, + "File::Spec" => 0, + "File::pushd" => 0, + "FindBin" => 0, + "Getopt::Long" => "2.39", + "IO::Handle" => 0, + "IPC::Open2" => 0, + "IPC::Open3" => 0, + "Path::Tiny" => 0, + "Pod::Usage" => 0, + "Scalar::Util" => 0, + "Test::Builder" => 0, + "Test::Exit" => 0, + "Test::More" => 0, + "Text::Gitignore" => 0, + "Text::Table::Any" => 0, + "strict" => 0, + "utf8" => 0, + "warnings" => 0 +); + + +unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { + delete $WriteMakefileArgs{TEST_REQUIRES}; + delete $WriteMakefileArgs{BUILD_REQUIRES}; + $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; +} + +delete $WriteMakefileArgs{CONFIGURE_REQUIRES} + unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; + +WriteMakefile(%WriteMakefileArgs); diff --git a/README b/README new file mode 100644 index 0000000..a5e5712 --- /dev/null +++ b/README @@ -0,0 +1,209 @@ +NAME + + git-codeowners - A tool for managing CODEOWNERS files + +VERSION + + version 0.41 + +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)" + +DESCRIPTION + + git-codeowners is yet another CLI tool for managing CODEOWNERS files in + git repos. In particular, it can be used to quickly find out who owns a + particular file in a monorepo (or monolith). + + THIS IS EXPERIMENTAL! The interface of this tool and its modules will + probably change as I field test some things. Feedback welcome. + +INSTALL + + There are several ways to install git-codeowners to your system. + + from CPAN + + You can install git-codeowners using cpanm: + + cpanm App::Codeowners + + from GitHub + + You can also choose to download git-codeowners as a self-contained + executable: + + curl -OL https://raw.githubusercontent.com/chazmcgarvey/git-codeowners/solo/git-codeowners + chmod +x git-codeowners + + To hack on the code, clone the repo instead: + + git clone https://github.com/chazmcgarvey/git-codeowners.git + cd git-codeowners + make bootstrap # installs dependencies; requires cpanm + +OPTIONS + + --version + + Print the program name and version to STDOUT, and exit. + + Alias: -v + + --help + + Print the synopsis to STDOUT, and exit. + + Alias: -h + + You can also use --manual to print the full documentation. + + --color + + Enable colorized output. + + Color is ON by default on terminals; use --no-color to disable. Some + environment variables may also alter the behavior of colorizing output: + + * NO_COLOR - Set to disable color (same as --no-color). + + * COLOR_DEPTH - Set the number of supportable colors (e.g. 0, 16, + 256, 16777216). + + --format + + Specify the output format to use. See "FORMAT". + + Alias: -f + + --shell-completion + + eval "$(lintany --shell-completion)" + + Print shell code to enable completion to STDOUT, and exit. + + Does not yet support Zsh... + +COMMANDS + + show + + git-codeowners [show] [--format FORMAT] [--[no-]project] [PATH...] + + Show owners of one or more files in a repo. + + owners + + git-codeowners owners [--format FORMAT] [--pattern PATTERN] + + patterns + + git-codeowners patterns [--format FORMAT] [--owner OWNER] + + create + + git-codeowners create [REPO_DIRPATH|CODEOWNERS_FILEPATH] + + Create a new CODEOWNERS file for a specified repo (or current + directory). + + update + + git-codeowners update [REPO_DIRPATH|CODEOWNERS_FILEPATH] + + Update the "unowned" list of an existing CODEOWNERS file for a + specified repo (or current directory). + +FORMAT + + The --format argument can be one of: + + * csv - Comma-separated values (requires Text::CSV) + + * json:pretty - Pretty JSON (requires JSON::MaybeXS) + + * json - JSON (requires JSON::MaybeXS) + + * table - Table (requires Text::Table::Any) + + * tsv - Tab-separated values (requires Text::CSV) + + * yaml - YAML (requires YAML) + + * FORMAT - Custom format (see below) + + Custom + + You can specify a custom format using printf-like format sequences. + These are the items that can be substituted: + + * %F - Filename + + * %O - Owner or owners + + * %P - Project + + * %T - Pattern + + * %n - newline + + * %t - tab + + * %% - percent sign + + The syntax also allows padding and some filters. Examples: + + git-codeowners show -f ' * %-50F %O' # default for "show" + git-codeowners show -f '%{quote}F,%{quote}O' # ad hoc CSV + git-codeowners patterns -f '--> %{color:0c0}T' # whatever... + + Available filters: + + * quote - Quote the replacement string. + + * color:FFFFFF - Colorize the replacement string (if color is ON). + + * nocolor - Do not colorize replacement string. + + Table + + Table formatting can be done by one of several different modules, each + with its own features and bugs. The default module is + Text::Table::Tiny, but this can be overridden using the PERL_TEXT_TABLE + environment variable if desired, like this: + + PERL_TEXT_TABLE=Text::Table::HTML git-codeowners -f table + + The list of available modules is at "@BACKENDS" in Text::Table::Any. + +BUGS + + Please report any bugs or feature requests on the bugtracker website + https://github.com/chazmcgarvey/git-codeowners/issues + + When submitting a bug or request, please include a test-file or a patch + to an existing test-file that illustrates the bug or desired feature. + +AUTHOR + + Charles McGarvey + +COPYRIGHT AND LICENSE + + 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. + diff --git a/bin/git-codeowners b/bin/git-codeowners new file mode 100755 index 0000000..a0bc3fe --- /dev/null +++ b/bin/git-codeowners @@ -0,0 +1,283 @@ +#! perl +# ABSTRACT: A tool for managing CODEOWNERS files +# PODNAME: git-codeowners + + +# FATPACK - Do not remove this line. + +use warnings; +use strict; + +use App::Codeowners; + +our $VERSION = '0.41'; # VERSION + +App::Codeowners->main(@ARGV); + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +git-codeowners - A tool for managing CODEOWNERS files + +=head1 VERSION + +version 0.41 + +=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 INSTALL + +There are several ways to install F to your system. + +=head2 from CPAN + +You can install F using L: + + cpanm App::Codeowners + +=head2 from GitHub + +You can also choose to download F as a self-contained executable: + + curl -OL https://raw.githubusercontent.com/chazmcgarvey/git-codeowners/solo/git-codeowners + chmod +x git-codeowners + +To hack on the code, clone the repo instead: + + git clone https://github.com/chazmcgarvey/git-codeowners.git + cd git-codeowners + make bootstrap # installs dependencies; requires cpanm + +=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 --color + +Enable colorized output. + +Color is ON by default on terminals; use C<--no-color> to disable. Some environment variables may +also alter the behavior of colorizing output: + +=over 4 + +=item * + +C - Set to disable color (same as C<--no-color>). + +=item * + +C - Set the number of supportable colors (e.g. 0, 16, 256, 16777216). + +=back + +=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: + +=over 4 + +=item * + +C - Comma-separated values (requires L) + +=item * + +C - Pretty JSON (requires L) + +=item * + +C - JSON (requires L) + +=item * + +C - Table (requires L) + +=item * + +C - Tab-separated values (requires L) + +=item * + +C - YAML (requires L) + +=item * + +C - Custom format (see below) + +=back + +=head2 Custom + +You can specify a custom format using printf-like format sequences. These are the items that can be +substituted: + +=over 4 + +=item * + +C<%F> - Filename + +=item * + +C<%O> - Owner or owners + +=item * + +C<%P> - Project + +=item * + +C<%T> - Pattern + +=item * + +C<%n> - newline + +=item * + +C<%t> - tab + +=item * + +C<%%> - percent sign + +=back + +The syntax also allows padding and some filters. Examples: + + git-codeowners show -f ' * %-50F %O' # default for "show" + git-codeowners show -f '%{quote}F,%{quote}O' # ad hoc CSV + git-codeowners patterns -f '--> %{color:0c0}T' # whatever... + +Available filters: + +=over 4 + +=item * + +C - Quote the replacement string. + +=item * + +C - Colorize the replacement string (if color is ON). + +=item * + +C - Do not colorize replacement string. + +=back + +=head2 Table + +Table formatting can be done by one of several different modules, each with its own features and +bugs. The default module is L, but this can be overridden using the +C environment variable if desired, like this: + + PERL_TEXT_TABLE=Text::Table::HTML git-codeowners -f table + +The list of available modules is at L. + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +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. + +=cut 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..626fc82 --- /dev/null +++ b/lib/App/Codeowners.pm @@ -0,0 +1,366 @@ +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 = '0.41'; # VERSION + + +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::Any } or die "Missing dependency: Text::Table::Any\n"; + + my $table = Text::Table::Any::table( + header_row => 1, + rows => [$headers, map { [map { _stringify($_) } @$_] } @$rows], + backend => $ENV{PERL_TEXT_TABLE}, + ); + 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]{3,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}; + + $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/; + if ($rgb !~ m/^[0-9a-fA-F]{6}$/) { + warn "Color value must be in 'ffffff' or 'fff' form.\n"; + return $text; + } + + 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; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +App::Codeowners - A tool for managing CODEOWNERS files + +=head1 VERSION + +version 0.41 + +=head1 METHODS + +=head2 main + + App::Codeowners->main(@ARGV); + +Run the script and exit; does not return. + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +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. + +=cut diff --git a/lib/App/Codeowners/Options.pm b/lib/App/Codeowners/Options.pm new file mode 100644 index 0000000..950f0b8 --- /dev/null +++ b/lib/App/Codeowners/Options.pm @@ -0,0 +1,350 @@ +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 = '0.41'; # 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} || []}; +} + + +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; +} + + +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"; + } +} + + +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; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +App::Codeowners::Options - Getopt and shell completion for App::Codeowners + +=head1 VERSION + +version 0.41 + +=head1 METHODS + +=head2 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. + +=over 4 + +=item * + +C - Arguments from the caller (e.g. C<@ARGV>). + +=item * + +C - List of L compatible option strings. + +=item * + +C - Optional coderef to call for non-option arguments. + +=item * + +C - Optional L configuration string. + +=back + +=head2 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. + +=head2 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. + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +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. + +=cut diff --git a/lib/App/Codeowners/Util.pm b/lib/App/Codeowners/Util.pm new file mode 100644 index 0000000..cb0b795 --- /dev/null +++ b/lib/App/Codeowners/Util.pm @@ -0,0 +1,281 @@ +package App::Codeowners::Util; +# ABSTRACT: Grab bag of utility subs for Codeowners modules + + +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 = '0.41'; # VERSION + + +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; + } +} + + +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; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +App::Codeowners::Util - Grab bag of utility subs for Codeowners modules + +=head1 VERSION + +version 0.41 + +=head1 DESCRIPTION + +B except in L and related modules. + +=head1 FUNCTIONS + +=head2 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. + +=head2 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): + +=over 4 + +=item * + +F + +=item * + +F + +=item * + +F<.bitbucket/CODEOWNERS> + +=item * + +F<.github/CODEOWNERS> + +=item * + +F<.gitlab/CODEOWNERS> + +=back + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +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. + +=cut diff --git a/lib/File/Codeowners.pm b/lib/File/Codeowners.pm new file mode 100644 index 0000000..f987561 --- /dev/null +++ b/lib/File/Codeowners.pm @@ -0,0 +1,520 @@ +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 = '0.41'; # VERSION + +sub _croak { require Carp; Carp::croak(@_); } +sub _usage { _croak("Usage: @_\n") } + + +sub new { + my $class = shift; + my $self = bless {}, $class; +} + + +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); +} + + +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); +} + + +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; +} + + +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); +} + + +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); +} + + +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('')}]); +} + + +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"; + } +} + + +sub write_to_string { + my $self = shift; + + my $str = join("\n", @{$self->write_to_array}) . "\n"; + return \$str; +} + + +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; +} + + +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) +} + + +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; +} + + +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; +} + + +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]; + } +} + + +sub append { + my $self = shift; + $self->_clear; + push @{$self->_lines}, (@_ ? {@_} : undef); +} + + +sub prepend { + my $self = shift; + $self->_clear; + unshift @{$self->_lines}, (@_ ? {@_} : undef); +} + + +sub unowned { + my $self = shift; + [sort keys %{$self->{unowned} || {}}]; +} + + +sub add_unowned { + my $self = shift; + $self->_unowned->{$_}++ for @_; +} + + +sub remove_unowned { + my $self = shift; + delete $self->_unowned->{$_} for @_; +} + +sub is_unowned { + my $self = shift; + my $filepath = shift; + $self->_unowned->{$filepath}; +} + + +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; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +File::Codeowners - Read and write CODEOWNERS files + +=head1 VERSION + +version 0.41 + +=head1 METHODS + +=head2 new + + $codeowners = File::Codeowners->new; + +Construct a new L. + +=head2 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. + +=head2 parse_from_filepath + + $codeowners = File::Codeowners->parse_from_filepath('path/to/CODEOWNERS'); + +Parse a F file from the filesystem. + +=head2 parse_from_fh + + $codeowners = File::Codeowners->parse_from_fh($filehandle); + +Parse a F file from an open filehandle. + +=head2 parse_from_array + + $codeowners = File::Codeowners->parse_from_array(\@lines); + +Parse a F file stored as lines in an array. + +=head2 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. + +=head2 write_to_filepath + + $codeowners->write_to_filepath($filepath); + +Write the contents of the file to the filesystem atomically. + +=head2 write_to_fh + + $codeowners->write_to_fh($fh); + +Format the file contents and write to a filehandle. + +=head2 write_to_string + + $scalarref = $codeowners->write_to_string; + +Format the file contents and return a reference to a formatted string. + +=head2 write_to_array + + $lines = $codeowners->write_to_array; + +Format the file contents as an arrayref of lines. + +=head2 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. + +=head2 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. + +=head2 patterns + + $patterns = $codeowners->patterns; + $patterns = $codeowners->patterns($owner); + +Get an arrayref of all patterns defined. + +=head2 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. + +=head2 append + + $codeowners->append(comment => $str); + $codeowners->append(pattern => $pattern, owners => \@owners); + $codeowners->append(); # blank line + +Append a new line. + +=head2 prepend + + $codeowners->prepend(comment => $str); + $codeowners->prepend(pattern => $pattern, owners => \@owners); + $codeowners->prepend(); # blank line + +Prepend a new line. + +=head2 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. + +=head2 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. + +=head2 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. + +=head2 clear_unowned + + $codeowners->clear_unowned; + +Remove all filepaths from the "unowned" list. + +See L for an explanation. + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +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. + +=cut diff --git a/lib/Test/File/Codeowners.pm b/lib/Test/File/Codeowners.pm new file mode 100644 index 0000000..44a384f --- /dev/null +++ b/lib/Test/File/Codeowners.pm @@ -0,0 +1,141 @@ +package Test::File::Codeowners; +# ABSTRACT: Write tests for CODEOWNERS files + + +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 = '0.41'; # 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(@_); +} + + +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; +} + + +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; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +Test::File::Codeowners - Write tests for CODEOWNERS files + +=head1 VERSION + +version 0.41 + +=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. + +=head1 FUNCTIONS + +=head2 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. + +=head2 codeowners_git_files_ok + + codeowners_git_files_ok(); # search up the tree for a CODEOWNERS file + codeowners_git_files_ok($filepath); + +=head1 BUGS + +Please report any bugs or feature requests on the bugtracker website +L + +When submitting a bug or request, please include a test-file or a +patch to an existing test-file that illustrates the bug or desired +feature. + +=head1 AUTHOR + +Charles McGarvey + +=head1 COPYRIGHT AND LICENSE + +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. + +=cut diff --git a/t/00-compile.t b/t/00-compile.t new file mode 100644 index 0000000..379fc9b --- /dev/null +++ b/t/00-compile.t @@ -0,0 +1,101 @@ +use 5.006; +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::Compile 2.058 + +use Test::More; + +plan tests => 6 + ($ENV{AUTHOR_TESTING} ? 1 : 0); + +my @module_files = ( + 'App/Codeowners.pm', + 'App/Codeowners/Options.pm', + 'App/Codeowners/Util.pm', + 'File/Codeowners.pm', + 'Test/File/Codeowners.pm' +); + +my @scripts = ( + 'bin/git-codeowners' +); + +# no fake home requested + +my @switches = ( + -d 'blib' ? '-Mblib' : '-Ilib', +); + +use File::Spec; +use IPC::Open3; +use IO::Handle; + +open my $stdin, '<', File::Spec->devnull or die "can't open devnull: $!"; + +my @warnings; +for my $lib (@module_files) +{ + # see L + my $stderr = IO::Handle->new; + + diag('Running: ', join(', ', map { my $str = $_; $str =~ s/'/\\'/g; q{'} . $str . q{'} } + $^X, @switches, '-e', "require q[$lib]")) + if $ENV{PERL_COMPILE_TEST_DEBUG}; + + my $pid = open3($stdin, '>&STDERR', $stderr, $^X, @switches, '-e', "require q[$lib]"); + binmode $stderr, ':crlf' if $^O eq 'MSWin32'; + my @_warnings = <$stderr>; + waitpid($pid, 0); + is($?, 0, "$lib loaded ok"); + + shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/ + and not eval { +require blib; blib->VERSION('1.01') }; + + if (@_warnings) + { + warn @_warnings; + push @warnings, @_warnings; + } +} + +foreach my $file (@scripts) +{ SKIP: { + open my $fh, '<', $file or warn("Unable to open $file: $!"), next; + my $line = <$fh>; + + close $fh and skip("$file isn't perl", 1) unless $line =~ /^#!\s*(?:\S*perl\S*)((?:\s+-\w*)*)(?:\s*#.*)?$/; + @switches = (@switches, split(' ', $1)) if $1; + + close $fh and skip("$file uses -T; not testable with PERL5LIB", 1) + if grep { $_ eq '-T' } @switches and $ENV{PERL5LIB}; + + my $stderr = IO::Handle->new; + + diag('Running: ', join(', ', map { my $str = $_; $str =~ s/'/\\'/g; q{'} . $str . q{'} } + $^X, @switches, '-c', $file)) + if $ENV{PERL_COMPILE_TEST_DEBUG}; + + my $pid = open3($stdin, '>&STDERR', $stderr, $^X, @switches, '-c', $file); + binmode $stderr, ':crlf' if $^O eq 'MSWin32'; + my @_warnings = <$stderr>; + waitpid($pid, 0); + is($?, 0, "$file compiled ok"); + + shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/ + and not eval { +require blib; blib->VERSION('1.01') }; + + # in older perls, -c output is simply the file portion of the path being tested + if (@_warnings = grep { !/\bsyntax OK$/ } + grep { chomp; $_ ne (File::Spec->splitpath($file))[2] } @_warnings) + { + warn @_warnings; + push @warnings, @_warnings; + } +} } + + + +is(scalar(@warnings), 0, 'no warnings found') + or diag 'got warnings: ', ( Test::More->can('explain') ? Test::More::explain(\@warnings) : join("\n", '', @warnings) ) if $ENV{AUTHOR_TESTING}; + + diff --git a/t/00-report-prereqs.dd b/t/00-report-prereqs.dd new file mode 100644 index 0000000..2f32b18 --- /dev/null +++ b/t/00-report-prereqs.dd @@ -0,0 +1,90 @@ +do { my $x = { + 'configure' => { + 'requires' => { + 'ExtUtils::MakeMaker' => '0' + } + }, + 'develop' => { + 'recommends' => { + 'App::FatPacker' => '0', + 'CPAN::Meta' => '0', + 'Capture::Tiny' => '0', + 'Config' => '0', + 'File::pushd' => '0', + 'Getopt::Long' => '0', + 'MetaCPAN::API' => '0', + 'Module::CoreList' => '0', + 'Path::Tiny' => '0', + 'Perl::Strip' => '0' + }, + 'requires' => { + 'Dist::Zilla' => '5', + 'Dist::Zilla::Plugin::ConsistentVersionTest' => '0', + 'Dist::Zilla::Plugin::Prereqs' => '0', + 'Dist::Zilla::Plugin::RemovePhasedPrereqs' => '0', + 'Dist::Zilla::Plugin::Run::Release' => '0', + 'Dist::Zilla::PluginBundle::Author::CCM' => '0', + 'Dist::Zilla::PluginBundle::Filter' => '0', + 'Software::License::Perl_5' => '0', + 'Test::CPAN::Changes' => '0.19', + 'Test::CPAN::Meta' => '0', + 'Test::ConsistentVersion' => '0', + 'Test::EOL' => '0', + 'Test::MinimumVersion' => '0', + 'Test::More' => '0.96', + 'Test::NoTabs' => '0', + 'Test::Perl::Critic' => '0', + 'Test::Pod' => '1.41', + 'Test::Pod::No404s' => '0', + 'Test::Portability::Files' => '0' + } + }, + 'runtime' => { + 'recommends' => { + 'Term::Detect::Software' => '0', + 'Unicode::GCString' => '0' + }, + 'requires' => { + 'Carp' => '0', + 'Color::ANSI::Util' => '0', + 'Encode' => '0', + 'Exporter' => '0', + 'Getopt::Long' => '2.39', + 'IPC::Open2' => '0', + 'Path::Tiny' => '0', + 'Pod::Usage' => '0', + 'Scalar::Util' => '0', + 'Test::Builder' => '0', + 'Text::Gitignore' => '0', + 'Text::Table::Any' => '0', + 'perl' => 'v5.10.1', + 'strict' => '0', + 'utf8' => '0', + 'warnings' => '0' + }, + 'suggests' => { + 'JSON::MaybeXS' => '0', + 'Text::CSV' => '0', + 'Text::Table' => '0', + 'YAML' => '0' + } + }, + 'test' => { + 'recommends' => { + 'CPAN::Meta' => '2.120900' + }, + 'requires' => { + 'Capture::Tiny' => '0', + 'ExtUtils::MakeMaker' => '0', + 'File::Spec' => '0', + 'File::pushd' => '0', + 'FindBin' => '0', + 'IO::Handle' => '0', + 'IPC::Open3' => '0', + 'Test::Exit' => '0', + 'Test::More' => '0' + } + } + }; + $x; + } \ No newline at end of file diff --git a/t/00-report-prereqs.t b/t/00-report-prereqs.t new file mode 100644 index 0000000..c72183a --- /dev/null +++ b/t/00-report-prereqs.t @@ -0,0 +1,193 @@ +#!perl + +use strict; +use warnings; + +# This test was generated by Dist::Zilla::Plugin::Test::ReportPrereqs 0.027 + +use Test::More tests => 1; + +use ExtUtils::MakeMaker; +use File::Spec; + +# from $version::LAX +my $lax_version_re = + qr/(?: undef | (?: (?:[0-9]+) (?: \. | (?:\.[0-9]+) (?:_[0-9]+)? )? + | + (?:\.[0-9]+) (?:_[0-9]+)? + ) | (?: + v (?:[0-9]+) (?: (?:\.[0-9]+)+ (?:_[0-9]+)? )? + | + (?:[0-9]+)? (?:\.[0-9]+){2,} (?:_[0-9]+)? + ) + )/x; + +# hide optional CPAN::Meta modules from prereq scanner +# and check if they are available +my $cpan_meta = "CPAN::Meta"; +my $cpan_meta_pre = "CPAN::Meta::Prereqs"; +my $HAS_CPAN_META = eval "require $cpan_meta; $cpan_meta->VERSION('2.120900')" && eval "require $cpan_meta_pre"; ## no critic + +# Verify requirements? +my $DO_VERIFY_PREREQS = 1; + +sub _max { + my $max = shift; + $max = ( $_ > $max ) ? $_ : $max for @_; + return $max; +} + +sub _merge_prereqs { + my ($collector, $prereqs) = @_; + + # CPAN::Meta::Prereqs object + if (ref $collector eq $cpan_meta_pre) { + return $collector->with_merged_prereqs( + CPAN::Meta::Prereqs->new( $prereqs ) + ); + } + + # Raw hashrefs + for my $phase ( keys %$prereqs ) { + for my $type ( keys %{ $prereqs->{$phase} } ) { + for my $module ( keys %{ $prereqs->{$phase}{$type} } ) { + $collector->{$phase}{$type}{$module} = $prereqs->{$phase}{$type}{$module}; + } + } + } + + return $collector; +} + +my @include = qw( + +); + +my @exclude = qw( + +); + +# Add static prereqs to the included modules list +my $static_prereqs = do './t/00-report-prereqs.dd'; + +# Merge all prereqs (either with ::Prereqs or a hashref) +my $full_prereqs = _merge_prereqs( + ( $HAS_CPAN_META ? $cpan_meta_pre->new : {} ), + $static_prereqs +); + +# Add dynamic prereqs to the included modules list (if we can) +my ($source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; +my $cpan_meta_error; +if ( $source && $HAS_CPAN_META + && (my $meta = eval { CPAN::Meta->load_file($source) } ) +) { + $full_prereqs = _merge_prereqs($full_prereqs, $meta->prereqs); +} +else { + $cpan_meta_error = $@; # capture error from CPAN::Meta->load_file($source) + $source = 'static metadata'; +} + +my @full_reports; +my @dep_errors; +my $req_hash = $HAS_CPAN_META ? $full_prereqs->as_string_hash : $full_prereqs; + +# Add static includes into a fake section +for my $mod (@include) { + $req_hash->{other}{modules}{$mod} = 0; +} + +for my $phase ( qw(configure build test runtime develop other) ) { + next unless $req_hash->{$phase}; + next if ($phase eq 'develop' and not $ENV{AUTHOR_TESTING}); + + for my $type ( qw(requires recommends suggests conflicts modules) ) { + next unless $req_hash->{$phase}{$type}; + + my $title = ucfirst($phase).' '.ucfirst($type); + my @reports = [qw/Module Want Have/]; + + for my $mod ( sort keys %{ $req_hash->{$phase}{$type} } ) { + next if $mod eq 'perl'; + next if grep { $_ eq $mod } @exclude; + + my $file = $mod; + $file =~ s{::}{/}g; + $file .= ".pm"; + my ($prefix) = grep { -e File::Spec->catfile($_, $file) } @INC; + + my $want = $req_hash->{$phase}{$type}{$mod}; + $want = "undef" unless defined $want; + $want = "any" if !$want && $want == 0; + + my $req_string = $want eq 'any' ? 'any version required' : "version '$want' required"; + + if ($prefix) { + my $have = MM->parse_version( File::Spec->catfile($prefix, $file) ); + $have = "undef" unless defined $have; + push @reports, [$mod, $want, $have]; + + if ( $DO_VERIFY_PREREQS && $HAS_CPAN_META && $type eq 'requires' ) { + if ( $have !~ /\A$lax_version_re\z/ ) { + push @dep_errors, "$mod version '$have' cannot be parsed ($req_string)"; + } + elsif ( ! $full_prereqs->requirements_for( $phase, $type )->accepts_module( $mod => $have ) ) { + push @dep_errors, "$mod version '$have' is not in required range '$want'"; + } + } + } + else { + push @reports, [$mod, $want, "missing"]; + + if ( $DO_VERIFY_PREREQS && $type eq 'requires' ) { + push @dep_errors, "$mod is not installed ($req_string)"; + } + } + } + + if ( @reports ) { + push @full_reports, "=== $title ===\n\n"; + + my $ml = _max( map { length $_->[0] } @reports ); + my $wl = _max( map { length $_->[1] } @reports ); + my $hl = _max( map { length $_->[2] } @reports ); + + if ($type eq 'modules') { + splice @reports, 1, 0, ["-" x $ml, "", "-" x $hl]; + push @full_reports, map { sprintf(" %*s %*s\n", -$ml, $_->[0], $hl, $_->[2]) } @reports; + } + else { + splice @reports, 1, 0, ["-" x $ml, "-" x $wl, "-" x $hl]; + push @full_reports, map { sprintf(" %*s %*s %*s\n", -$ml, $_->[0], $wl, $_->[1], $hl, $_->[2]) } @reports; + } + + push @full_reports, "\n"; + } + } +} + +if ( @full_reports ) { + diag "\nVersions for all modules listed in $source (including optional ones):\n\n", @full_reports; +} + +if ( $cpan_meta_error || @dep_errors ) { + diag "\n*** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING ***\n"; +} + +if ( $cpan_meta_error ) { + my ($orig_source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; + diag "\nCPAN::Meta->load_file('$orig_source') failed with: $cpan_meta_error\n"; +} + +if ( @dep_errors ) { + diag join("\n", + "\nThe following REQUIRED prerequisites were not satisfied:\n", + @dep_errors, + "\n" + ); +} + +pass; + +# vim: ts=4 sts=4 sw=4 et: 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 diff --git a/xt/author/critic.t b/xt/author/critic.t new file mode 100644 index 0000000..80ccdad --- /dev/null +++ b/xt/author/critic.t @@ -0,0 +1,7 @@ +#!perl + +use strict; +use warnings; + +use Test::Perl::Critic (-profile => "perlcritic.rc") x!! -e "perlcritic.rc"; +all_critic_ok(); diff --git a/xt/author/eol.t b/xt/author/eol.t new file mode 100644 index 0000000..37c6b05 --- /dev/null +++ b/xt/author/eol.t @@ -0,0 +1,37 @@ +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::EOL 0.19 + +use Test::More 0.88; +use Test::EOL; + +my @files = ( + 'bin/git-codeowners', + 'lib/App/Codeowners.pm', + 'lib/App/Codeowners/Options.pm', + 'lib/App/Codeowners/Util.pm', + 'lib/File/Codeowners.pm', + 'lib/Test/File/Codeowners.pm', + 't/00-compile.t', + 't/00-report-prereqs.dd', + 't/00-report-prereqs.t', + 't/app-codeowners-util.t', + 't/app-codeowners.t', + 't/file-codeowners.t', + 't/samples/basic.CODEOWNERS', + 't/samples/kitchensink.CODEOWNERS', + 'xt/author/critic.t', + 'xt/author/eol.t', + 'xt/author/minimum-version.t', + 'xt/author/no-tabs.t', + 'xt/author/pod-no404s.t', + 'xt/author/pod-syntax.t', + 'xt/author/portability.t', + 'xt/release/consistent-version.t', + 'xt/release/cpan-changes.t', + 'xt/release/distmeta.t' +); + +eol_unix_ok($_, { trailing_whitespace => 1 }) foreach @files; +done_testing; diff --git a/xt/author/minimum-version.t b/xt/author/minimum-version.t new file mode 100644 index 0000000..277e084 --- /dev/null +++ b/xt/author/minimum-version.t @@ -0,0 +1,6 @@ +use strict; +use warnings; + +use Test::More; +use Test::MinimumVersion; +all_minimum_version_ok( qq{5.10.1} ); diff --git a/xt/author/no-tabs.t b/xt/author/no-tabs.t new file mode 100644 index 0000000..1d28f3b --- /dev/null +++ b/xt/author/no-tabs.t @@ -0,0 +1,37 @@ +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::NoTabs 0.15 + +use Test::More 0.88; +use Test::NoTabs; + +my @files = ( + 'bin/git-codeowners', + 'lib/App/Codeowners.pm', + 'lib/App/Codeowners/Options.pm', + 'lib/App/Codeowners/Util.pm', + 'lib/File/Codeowners.pm', + 'lib/Test/File/Codeowners.pm', + 't/00-compile.t', + 't/00-report-prereqs.dd', + 't/00-report-prereqs.t', + 't/app-codeowners-util.t', + 't/app-codeowners.t', + 't/file-codeowners.t', + 't/samples/basic.CODEOWNERS', + 't/samples/kitchensink.CODEOWNERS', + 'xt/author/critic.t', + 'xt/author/eol.t', + 'xt/author/minimum-version.t', + 'xt/author/no-tabs.t', + 'xt/author/pod-no404s.t', + 'xt/author/pod-syntax.t', + 'xt/author/portability.t', + 'xt/release/consistent-version.t', + 'xt/release/cpan-changes.t', + 'xt/release/distmeta.t' +); + +notabs_ok($_) foreach @files; +done_testing; diff --git a/xt/author/pod-no404s.t b/xt/author/pod-no404s.t new file mode 100644 index 0000000..eb9760c --- /dev/null +++ b/xt/author/pod-no404s.t @@ -0,0 +1,21 @@ +#!perl + +use strict; +use warnings; +use Test::More; + +foreach my $env_skip ( qw( + SKIP_POD_NO404S + AUTOMATED_TESTING +) ){ + plan skip_all => "\$ENV{$env_skip} is set, skipping" + if $ENV{$env_skip}; +} + +eval "use Test::Pod::No404s"; +if ( $@ ) { + plan skip_all => 'Test::Pod::No404s required for testing POD'; +} +else { + all_pod_files_ok(); +} diff --git a/xt/author/pod-syntax.t b/xt/author/pod-syntax.t new file mode 100644 index 0000000..e563e5d --- /dev/null +++ b/xt/author/pod-syntax.t @@ -0,0 +1,7 @@ +#!perl +# This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. +use strict; use warnings; +use Test::More; +use Test::Pod 1.41; + +all_pod_files_ok(); diff --git a/xt/author/portability.t b/xt/author/portability.t new file mode 100644 index 0000000..c531252 --- /dev/null +++ b/xt/author/portability.t @@ -0,0 +1,10 @@ +use strict; +use warnings; + +use Test::More; + +eval 'use Test::Portability::Files'; +plan skip_all => 'Test::Portability::Files required for testing portability' + if $@; + +run_tests(); diff --git a/xt/release/consistent-version.t b/xt/release/consistent-version.t new file mode 100644 index 0000000..7f200c5 --- /dev/null +++ b/xt/release/consistent-version.t @@ -0,0 +1,10 @@ +use strict; +use warnings; + +use Test::More; + +eval "use Test::ConsistentVersion"; +plan skip_all => "Test::ConsistentVersion required for this test" + if $@; + +Test::ConsistentVersion::check_consistent_versions(); diff --git a/xt/release/cpan-changes.t b/xt/release/cpan-changes.t new file mode 100644 index 0000000..286005a --- /dev/null +++ b/xt/release/cpan-changes.t @@ -0,0 +1,10 @@ +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::CPAN::Changes 0.012 + +use Test::More 0.96 tests => 1; +use Test::CPAN::Changes; +subtest 'changes_ok' => sub { + changes_file_ok('Changes'); +}; diff --git a/xt/release/distmeta.t b/xt/release/distmeta.t new file mode 100644 index 0000000..c2280dc --- /dev/null +++ b/xt/release/distmeta.t @@ -0,0 +1,6 @@ +#!perl +# This file was automatically generated by Dist::Zilla::Plugin::MetaTests. + +use Test::CPAN::Meta; + +meta_yaml_ok(); -- 2.45.2