Hi,

the attached patch adds a new grep option: --include-exclude-dirs

Usually, when grepping recusively over a directory tree, the
include/exclude patterns are only applied to file but not directory names.

In some cases that is pretty anoying, e.g. when grepping in a subversion
working copy where one usually does not want to recurse into the .svn
directories. With this patch applied one can do that using:

        grep --exclude=.svn --include-exclude-dirs FOOBAR .

I'd really appreciate if you would apply this in CVS and so include it in
the next release.

The patch is against grep-2.5.1a and not CVS HEAD. I hope that does not
matter..

yours,
 - clifford

-- 
To understand recursion, you must first understand recursion.
Clifford Wolf <[EMAIL PROTECTED]>:
        Added --include-exclude-dirs option which causes include/exclude
        patterns to also be applied on directory names.

--- grep-2.5.1a/doc/grep.1.orig 2007-06-21 08:20:11.000000000 +0200
+++ grep-2.5.1a/doc/grep.1      2007-06-21 08:21:37.000000000 +0200
@@ -329,6 +329,10 @@
 Recurse in directories skip file matching
 .I PATTERN.
 .TP
+.BR "\fR \fP \-\^\-include-exclude-dirs"
+When processing directories recursively, apply the include and exclude patterns
+also to directory names.
+.TP
 .BR \-s ", " \-\^\-no-messages
 Suppress error messages about nonexistent or unreadable files.
 Portability note: unlike \s-1GNU\s0
--- grep-2.5.1a/doc/grep.texi.orig      2007-06-21 08:19:41.000000000 +0200
+++ grep-2.5.1a/doc/grep.texi   2007-06-21 08:14:34.000000000 +0200
@@ -425,6 +425,12 @@
 @cindex searching directory trees
 When processing directories recursively, skip files matching 
@var{file_pattern}.
 
[EMAIL PROTECTED] --include-exclude-dirs
[EMAIL PROTECTED] --include-exclude-dirs
[EMAIL PROTECTED] include/exclude directories
+When processing directories recursively, apply the include and exclude patterns
+also to directory names.
+
 @item -m @var{num}
 @itemx [EMAIL PROTECTED]
 @opindex -m
--- grep-2.5.1a/lib/savedir.c.orig      2007-06-21 08:06:24.000000000 +0200
+++ grep-2.5.1a/lib/savedir.c   2007-06-21 08:07:15.000000000 +0200
@@ -100,7 +100,7 @@
    Return NULL if DIR cannot be opened or if out of memory. */
 char *
 savedir (const char *dir, off_t name_size, struct exclude *included_patterns,
-        struct exclude *excluded_patterns)
+        struct exclude *excluded_patterns, int include_exclude_dirs)
 {
   DIR *dirp;
   struct dirent *dp;
@@ -134,7 +134,7 @@
          off_t size_needed = (namep - name_space) + NAMLEN (dp) + 2;
 
          if ((included_patterns || excluded_patterns)
-             && !isdir1 (dir, dp->d_name))
+             && (include_exclude_dirs || !isdir1 (dir, dp->d_name)))
            {
              if (included_patterns
                  && !excluded_filename (included_patterns, dp->d_name, 0))
--- grep-2.5.1a/lib/savedir.h.orig      2007-06-21 08:07:30.000000000 +0200
+++ grep-2.5.1a/lib/savedir.h   2007-06-21 08:07:38.000000000 +0200
@@ -13,6 +13,6 @@
 
 extern char *
 savedir PARAMS ((const char *dir, off_t name_size,
-                struct exclude *, struct exclude *));
+                struct exclude *, struct exclude *, int));
 
 #endif
--- grep-2.5.1a/src/grep.c.orig 2007-06-21 08:10:08.000000000 +0200
+++ grep-2.5.1a/src/grep.c      2007-06-21 08:12:11.000000000 +0200
@@ -78,6 +78,8 @@
 
 static struct exclude *excluded_patterns;
 static struct exclude *included_patterns;
+static int include_exclude_dirs;
+
 /* Short options.  */
 static char const short_options[] =
 "0123456789A:B:C:D:EFGHIPUVX:abcd:e:f:hiKLlm:noqRrsuvwxyZz";
@@ -88,6 +90,7 @@
   BINARY_FILES_OPTION = CHAR_MAX + 1,
   COLOR_OPTION,
   INCLUDE_OPTION,
+  INCLUDE_EXCLUDE_DIRS_OPTION,
   EXCLUDE_OPTION,
   EXCLUDE_FROM_OPTION,
   LINE_BUFFERED_OPTION,
@@ -118,6 +121,7 @@
   {"fixed-strings", no_argument, NULL, 'F'},
   {"help", no_argument, &show_help, 1},
   {"include", required_argument, NULL, INCLUDE_OPTION},
+  {"include-exclude-dirs", no_argument, NULL, INCLUDE_EXCLUDE_DIRS_OPTION},
   {"ignore-case", no_argument, NULL, 'i'},
   {"label", required_argument, NULL, LABEL_OPTION},
   {"line-buffered", no_argument, NULL, LINE_BUFFERED_OPTION},
@@ -1002,7 +1006,7 @@
        }
 
   name_space = savedir (dir, stats->stat.st_size, included_patterns,
-                       excluded_patterns);
+                       excluded_patterns, include_exclude_dirs);
 
   if (! name_space)
     {
@@ -1102,6 +1106,7 @@
       --include=PATTERN     files that match PATTERN will be examined\n\
       --exclude=PATTERN     files that match PATTERN will be skipped.\n\
       --exclude-from=FILE   files that match PATTERN in FILE will be 
skipped.\n\
+      --include-exclude-dirs also apply include/exclude patterns on 
directories\n\
   -L, --files-without-match only print FILE names containing no match\n\
   -l, --files-with-matches  only print FILE names containing matches\n\
   -c, --count               only print a count of matching lines per FILE\n\
@@ -1613,6 +1618,10 @@
        add_exclude (included_patterns, optarg);
        break;
 
+      case INCLUDE_EXCLUDE_DIRS_OPTION:
+       include_exclude_dirs = 1;
+       break;
+
       case LINE_BUFFERED_OPTION:
        line_buffered = 1;
        break;

Reply via email to