Old-Man-Programmer / tree

Tree for Unix/LInux
GNU General Public License v2.0
140 stars 31 forks source link

Fix for --gitignore option #3

Closed codeandfire closed 8 months ago

codeandfire commented 2 years ago

Hi,

This is a small fix for the --gitignore option.

According to the gitignore manual,

If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.

They also give two examples:

For example, a pattern doc/frotz/ matches doc/frotz directory, but not a/doc/frotz directory; however frotz/ matches frotz and a/frotz that is a directory (all paths are relative from the .gitignore file).

A leading ** followed by a slash means match in all directories. For example, **/foo matches file or directory "foo" anywhere, the same as pattern "foo".

Currently I believe tree --gitignore doesn't follow this correctly. #1 is a similar issue. If we consider the following directory structure:

.
├── dir_a
│   └── a.txt
└── dir_b
    ├── b.txt
    └── dir_a
        └── ab.txt

If .gitignore contains

dir_a/
.gitignore

then git add . followed by git status yields dir_b/b.txt as the only added file, i.e. directories dir_a and dir_b/dir_a are both ignored. But

$ tree --gitignore
.
└── dir_b
    ├── b.txt
    └── dir_a
        └── ab.txt

i.e. dir_a is ignored but not dir_b/dir_a.

On the other hand if .gitignore contains

/dir_a/
.gitignore

then dir_b/b.txt and dir_b/dir_a/ab.txt both files are added, i.e. only the outer directory dir_a is ignored. But

$ tree --gitignore
.
├── dir_a
│   └── a.txt
└── dir_b
    ├── b.txt
    └── dir_a
        └── ab.txt

i.e. the outer directory dir_a is not ignored.

With this fix we get the expected result in both cases: in the first case

.
└── dir_b
    └── b.txt

and in the second

└── dir_b
    ├── b.txt
    └── dir_a
        └── ab.txt

Please let me know your comments! Thanks!