A way to validate .gitignore file

情到浓时终转凉″ 提交于 2019-11-27 22:11:38

问题


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.


回答1:


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



回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/26908151/a-way-to-validate-gitignore-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!