A way to validate .gitignore file

前端 未结 3 909
夕颜
夕颜 2020-12-16 15:56

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 necess

相关标签:
3条回答
  • 2020-12-16 16:06

    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.

    0 讨论(0)
  • 2020-12-16 16:15

    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
    
    0 讨论(0)
  • 2020-12-16 16:25

    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.

    0 讨论(0)
提交回复
热议问题