NAME

Parse::Gitignore - parse .gitignore files

SYNOPSIS

use FindBin '$Bin';
use Parse::Gitignore;
chdir "$Bin/../" or die $!;
my $gitignore = Parse::Gitignore->new (".gitignore");
for my $file ('examples/synopsis.pl', 'MANIFEST') {
    if ($gitignore->ignored ($file)) {
        print "$file is ignored.\n";
    }
    else {
        print "$file is not ignored.\n";
    }
}

produces output

examples/synopsis.pl is not ignored.
MANIFEST is ignored.

(This example is included as synopsis.pl in the distribution.)

VERSION

This documents Parse::Gitignore version 1.0 corresponding to git commit 66d95b057677120fce6309993e9f9324d2d665ab released on Tue Nov 12 06:23:43 2024 +0900.

DESCRIPTION

Parse a .gitignore file and check whether a file matches it.

METHODS

new

my $pg = Parse::Gitignore->new ('./.gitignore');

To get messages on what it is doing, set verbose to a true value:

my $pg = Parse::Gitignore->new ('./.gitignore', verbose => 1);

ignored

    if ($pg->ignored ('./Makefile')) {
	print "Makefile is ignored.\n";
    }

Check whether a file matches a .gitignore.

read_gitignore

$pg->read_gitignore ('.gitignore');

Read a .gitignore file. Because different directories in the same git repository may feature their own .gitignore files, the information is accumulated in $pg along with the specific directory of the gitignore file relative to the top directory of the repository, for example

$pg->read_gitignore ("$top/.gitignore");
$pg->read_gitignore ("$top/src/.gitignore");

If the suggested .gitignore does not actually exist, a warning is printed and processing continues. If the argument is a directory, the module assumes that you intended to read a file called .gitignore in that directory.

excludesfile

$pg->excludesfile ('/home/user/.gitignore');

Read an excludesfile which is applied to all directories where .git is found.

DEPENDENCIES

Carp

Used to report errors.

"read_lines" in File::Slurper

Used to read the gitignore file.

File::Spec

Used to disambiguate file path names.

SEE ALSO

man gitignore
man git-check-ignore

HISTORY

I wrote this because I wanted to have a way to ignore generated files for a desktop file search application.

Version 1.0 changes the way that subdirectories are dealt with. The older versions turned out to have a problem whereby files in subdirectories were not picked up correctly.

AUTHOR

Ben Bullock, <benkasminbullock@gmail.com>

COPYRIGHT & LICENCE

This package and associated files are copyright (C) 2016-2024 Ben Bullock.

You can use, copy, modify and redistribute this package and associated files under the Perl Artistic Licence or the GNU General Public Licence.