Used quite a lot, especially with `--exclude-dir=.git`.
---
 tests/grep.test   |  8 ++++++++
 toys/posix/grep.c | 16 ++++++++++------
 2 files changed, 18 insertions(+), 6 deletions(-)
From 67ac8d4e8cf7a83981796af6243abb5cd731bd28 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Fri, 24 May 2019 13:30:51 -0700
Subject: [PATCH] grep: add --exclude-dir.

Used quite a lot, especially with `--exclude-dir=.git`.
---
 tests/grep.test   |  8 ++++++++
 toys/posix/grep.c | 16 ++++++++++------
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/tests/grep.test b/tests/grep.test
index 0e0f3c47..dee59924 100755
--- a/tests/grep.test
+++ b/tests/grep.test
@@ -167,3 +167,11 @@ ln -s nope sub/link
 testing "" 'grep -r walrus sub 2>/dev/null; echo $?' "1\n" "" ""
 rm -rf sub
 
+# --exclude-dir
+mkdir sub
+mkdir sub/yes
+echo "hello world" > sub/yes/test
+mkdir sub/no
+echo "hello world" > sub/no/test
+testing "--exclude-dir" 'grep --exclude-dir=no -r world sub' "sub/yes/test:hello world\n" "" ""
+rm -rf sub
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index 5afe38ca..30a98190 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -1,4 +1,4 @@
-/* grep.c - print lines what match given regular expression
+/* grep.c - show lines matching regular expressions
  *
  * Copyright 2013 CE Strake <strake888 at gmail.com>
  *
@@ -10,7 +10,7 @@
 * echo hello | grep -f </dev/null
 *
 
-USE_GREP(NEWTOY(grep, "(line-buffered)(color):;S(exclude)*M(include)*ZzEFHIab(byte-offset)h(no-filename)ino(only-matching)rsvwcl(files-with-matches)q(quiet)(silent)e*f*C#B#A#m#x[!wx][!EFw]", TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
+USE_GREP(NEWTOY(grep, "(line-buffered)(color):;(exclude-dir)*S(exclude)*M(include)*ZzEFHIab(byte-offset)h(no-filename)ino(only-matching)rsvwcl(files-with-matches)q(quiet)(silent)e*f*C#B#A#m#x[!wx][!EFw]", TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
 USE_EGREP(OLDTOY(egrep, grep, TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
 USE_FGREP(OLDTOY(fgrep, grep, TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
 
@@ -31,6 +31,7 @@ config GREP
     -r  Recurse into subdirectories (defaults FILE to ".")
     -M  Match filename pattern (--include)
     -S  Skip filename pattern (--exclude)
+    --exclude-dir=PATTERN  Skip directory pattern
     -I  Ignore binary files
 
     match type:
@@ -67,7 +68,7 @@ config FGREP
 
 GLOBALS(
   long m, A, B, C;
-  struct arg_list *f, *e, *M, *S;
+  struct arg_list *f, *e, *M, *S, *exclude_dir;
   char *color;
 
   char *purple, *cyan, *red, *green, *grey;
@@ -416,14 +417,17 @@ static void parse_regex(void)
 
 static int do_grep_r(struct dirtree *new)
 {
+  struct arg_list *al;
   char *name;
 
   if (!new->parent) TT.tried++;
   if (!dirtree_notdotdot(new)) return 0;
-  if (S_ISDIR(new->st.st_mode)) return DIRTREE_RECURSE;
+  if (S_ISDIR(new->st.st_mode)) {
+    for (al = TT.exclude_dir; al; al = al->next)
+      if (!fnmatch(al->arg, new->name, 0)) return 0;
+    return DIRTREE_RECURSE;
+  }
   if (TT.S || TT.M) {
-    struct arg_list *al;
-
     for (al = TT.S; al; al = al->next)
       if (!fnmatch(al->arg, new->name, 0)) return 0;
 
-- 
2.22.0.rc1.257.g3120a18244-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to