A way to validate .gitignore file
Asked Answered
M

3

21

Is there a way to validate a .gitignore file so you quickly find doubles paths or paths that don't exist anymore? Normally when a project is small this isn't really necessary but with lots of branches and mergings of .gitignore files this can come in hand.

Malfeasance answered 13/11, 2014 at 11:44 Comment(0)
G
28

Not exactly.
One command which could come close would be git check-ignore

Choose a file you know is supposed to be ignored, and check the output of:

git check-ignore -v -- /path/to/ignored/file

You will see the rules of your .gitignore which apply.


Update March 2016: Git 2.8 will add a new way to debug .gitignore files and their rules.

In the context of allowing a sub-folder to not be ignored (even if its parent folder is ignored: see example here), I have found this gem by Thái Ngọc Duy (pclouds):

dir.c: support tracing exclude

man git includes:

GIT_TRACE_EXCLUDE:

Enables trace messages that can help debugging .gitignore processing.
See 'GIT_TRACE' for available trace output options.

With GIT_TRACE_EXCLUDE set to 1, you will see (after a git status) stderr debug messages like:

exclude: from ...
exclude: xxx => n/a
exclude: xxx vs. yyy at line z: => www

But commit 5e57f9c (Merge branch 'nd/exclusion-regression-fix', 2016-02-24, Git v2.8.0-rc0 -- merge listed in batch #8) was reverted, reversing changes made to e79112d (Merge branch 'ce/https-public-key-pinning', 2016-02-24, Git v2.8.0-rc0 -- merge listed in batch #8).

We will be postponing nd/exclusion-regression-fix topic to a later cycle.

(Merged by Junio C Hamano -- gitster -- in commit 5cee349, 18 Mar 2016)

5cee349370:Revert "Merge branch 'nd/exclusion-regression-fix'"

Godship answered 13/11, 2014 at 12:20 Comment(2)
Sadly, it looks like GIT_TRACE_EXCLUDE was reverted shortly after that commit added it: github.com/git/git/commit/… . I didn't dig into why it was removed, just tracked this down while trying to understand why the flag wasn't doing anything in git 2.43Sue
@GrantG True, thank you for the feedback. I have edited the answer accordingly.Godship
I
2

You can do a script to check it. I have made one for you there:

#!/bin/bash
set -o noglob
for file in `cat .gitignore | grep -v \#`
do
    printf "$file"
    find . -name "$file" | wc -l
done

it will display the rules followed by the number of match in the current directory and recursively. Example:

*.log     31
*.gz      0
*~     42
*.swp      0
*.aux     33
*.pdf     51
*.out      7
*.toc      6
*.nav      1
*.snm      1
.DS_Store      0

You could restrict the output to the line containing 0 by piping into egrep "\b0\b" if you want.

Impetuous answered 13/11, 2014 at 12:43 Comment(2)
This assumes find's globbing matches git's .gitignore globbing. Is that a safe assumption?Screens
To be fair, I haven't check. I also rarely use the fancy things like negation or double *. It should work just fine for the standard * wildcard though.Henandchickens
V
1

In order to also validate git's ** pattern in paths I had to write a one-liner inspired by the script above:

find . -type f | git check-ignore -v --stdin | perl -pe 's,.*\.gitignore:,,; s,\t," "x100,e' | sort | uniq -w 100 -c | perl -pe 's, {100}.*,,'

Not exactly pretty but it works.

Valenevalenka answered 31/10, 2018 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.