From: David Greaves Date: Thu, 12 May 2005 21:30:32 GMT Subject: [PATCH] Ignore file filter Message-ID: <4283CAF8.3050304@dgreaves.com> Hi Petr This is an inline filter that introduces the concept of .git/ignore It is intended to be used within the cogito scripts like the other cg-X* files. Signed-off-by: David Greaves -- #!/usr/bin/env bash # # Takes a list of files on stdin and only passes valid ones agording to .git/ignore # Copyright (c) David Greaves, 2005 # # This filter implements cogito ignore rules and should typically be used in find pipelines # # Synopsis # cg-Xignore [-debug] [-f] [-h] [-d] < file-list >useful-file-list # # Options # -debug:: # produce helpful debug output # # -q:: # don't say what paths are ignored # # -f:: # passes files # # -d:: # passes directories # # -h:: # passes symbolic links # # The default is to pass all file types that are not ignored. # # Note that the .git/ignore file contains multiple expressions, 1 per line # Lines beginning with a '#' are ignored (allowing comments) # These are 'bash regular expressions' not glob patterns # This allows ignore rules to take the directory into account # Suggested contents: # # bash regexps (not globs) # ^\.[^/] # /\. # /$ # .*\.o$ # This doesn't allow the -h which is the [ arg for symlinks... #. ${COGITO_LIB}cg-Xlib _git=${GIT_DIR:-.git} IGNORE_FILE="$_git/ignore" if [ "$1" = "-0" ]; then # doesn't work :( zerosep=$'-d "\0"' shift fi # Defaults pass_files=0 pass_dirs=0 pass_links=0 pass_all=1 while [ $# -gt 0 ]; do case $1 in "-f") pass_all=0 pass_files=1 ;; "-d") pass_all=0 pass_dirs=1 ;; "-h") pass_all=0 pass_links=1 ;; "-q") quiet=1 ;; "-debug") debug=1 ;; esac shift done # save stderr exec 5>&2 if [ $quiet ]; then # turn off noise exec 2>&- fi if [ $debug ]; then exec 4>&5 else exec 4>/dev/null fi # Strip out the common leading ./ allowing "find ." sed 's:^./::' | \ while read $zerosep file; do echo "consider file: $file" >&4 ignore=0 if [ -f $IGNORE_FILE ]; then exec 3<$IGNORE_FILE while read -r -u3 patt ; do if [[ $patt =~ "^\w*#" ]]; then continue fi echo "consider pattern: $patt" >&4 if [[ $file =~ $patt ]]; then ignore=1 echo "Ignoring $file because of $patt" >&2 break fi done fi echo "passing file: $file" >&4 if [ $ignore != "1" \ -a \( $pass_all -eq 1 \ -o \( $pass_files -eq 1 -a -f $file \) \ -o \( $pass_dirs -eq 1 -a -d $file \) \ -o \( $pass_links -eq 1 -a -h $file \) \ \) \ ]; then echo $file fi done