git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/6] grep: move pattern-type bits support to top-level grep.[ch]

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 4, 2012, 01:33 UTC
Message-ID
<1349314419-8397-3-git-send-email-gitster@pobox.com>
In-Reply-To
<1349314419-8397-1-git-send-email-gitster@pobox.com>

Switching between -E/-G/-P/-F correctly needs a lot more than just flipping opt->regflags bit these days, and we have a nice helper function buried in builtin/grep.c for the sole use of "git grep".

Extract it so that "log --grep" family can also use it.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/grep.c | 38 +++-----------------------------------
 grep.c         | 32 ++++++++++++++++++++++++++++++++
 grep.h         |  1 +
 3 files changed, 36 insertions(+), 35 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index ce379d5..2b14fee 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -261,38 +261,6 @@ static int wait_all(void)
 }
 #endif
 
-static void grep_pattern_type_options(const int pattern_type, struct grep_opt *opt)
-{
-	switch (pattern_type) {
-	case GREP_PATTERN_TYPE_UNSPECIFIED:
-		/* fall through */
-
-	case GREP_PATTERN_TYPE_BRE:
-		opt->fixed = 0;
-		opt->pcre = 0;
-		opt->regflags &= ~REG_EXTENDED;
-		break;
-
-	case GREP_PATTERN_TYPE_ERE:
-		opt->fixed = 0;
-		opt->pcre = 0;
-		opt->regflags |= REG_EXTENDED;
-		break;
-
-	case GREP_PATTERN_TYPE_FIXED:
-		opt->fixed = 1;
-		opt->pcre = 0;
-		opt->regflags &= ~REG_EXTENDED;
-		break;
-
-	case GREP_PATTERN_TYPE_PCRE:
-		opt->fixed = 0;
-		opt->pcre = 1;
-		opt->regflags &= ~REG_EXTENDED;
-		break;
-	}
-}
-
 static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
 {
 	void *data;
@@ -810,11 +778,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			     PARSE_OPT_NO_INTERNAL_HELP);
 
 	if (pattern_type_arg != GREP_PATTERN_TYPE_UNSPECIFIED)
-		grep_pattern_type_options(pattern_type_arg, &opt);
+		grep_set_pattern_type_option(pattern_type_arg, &opt);
 	else if (opt.pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
-		grep_pattern_type_options(opt.pattern_type_option, &opt);
+		grep_set_pattern_type_option(opt.pattern_type_option, &opt);
 	else if (opt.extended_regexp_option)
-		grep_pattern_type_options(GREP_PATTERN_TYPE_ERE, &opt);
+		grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &opt);
 
 	if (use_index && !startup_info->have_repository)
 		/* die the same way as if we did it at the beginning */
diff --git a/grep.c b/grep.c
index 551a2ed..0d8df65 100644
--- a/grep.c
+++ b/grep.c
@@ -1585,3 +1585,35 @@ int grep_config(const char *var, const char *value, void *cb)
 	}
 	return 0;
 }
+
+void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
+{
+	switch (pattern_type) {
+	case GREP_PATTERN_TYPE_UNSPECIFIED:
+		/* fall through */
+
+	case GREP_PATTERN_TYPE_BRE:
+		opt->fixed = 0;
+		opt->pcre = 0;
+		opt->regflags &= ~REG_EXTENDED;
+		break;
+
+	case GREP_PATTERN_TYPE_ERE:
+		opt->fixed = 0;
+		opt->pcre = 0;
+		opt->regflags |= REG_EXTENDED;
+		break;
+
+	case GREP_PATTERN_TYPE_FIXED:
+		opt->fixed = 1;
+		opt->pcre = 0;
+		opt->regflags &= ~REG_EXTENDED;
+		break;
+
+	case GREP_PATTERN_TYPE_PCRE:
+		opt->fixed = 0;
+		opt->pcre = 1;
+		opt->regflags &= ~REG_EXTENDED;
+		break;
+	}
+}
diff --git a/grep.h b/grep.h
index 5381adc..2f6aaa5 100644
--- a/grep.h
+++ b/grep.h
@@ -145,6 +145,7 @@ extern void compile_grep_patterns(struct grep_opt *opt);
 extern void free_grep_patterns(struct grep_opt *opt);
 extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
 
+void grep_set_pattern_type_option(enum grep_pattern_type, struct grep_opt *opt);
 int grep_config(const char *var, const char *value, void *cb);
 
 struct grep_source {
-- 
1.8.0.rc0.57.g712528f
Previous: Junio C HamanoNext: Junio C Hamano
Message 8 of 23 in “[ANNOUNCE] Git v1.8.0-rc0”
  1. Junio C HamanoOct 1, 2012
  2. grep.patternType (was: Re: [ANNOUNCE] Git v1.8.0-rc0)Junio C Hamano, Oct 3, 2012
  3. Junio C HamanoOct 3, 2012
  4. Michal KiedrowiczOct 4, 2012
  5. J SmithOct 5, 2012
  6. 0/6 Tying loose ends of extended "grep"Junio C Hamano, Oct 4, 2012
  7. 1/6 grep: move configuration support to top-level grep.[ch]Junio C Hamano, Oct 4, 2012
  8. 2/6 grep: move pattern-type bits support to top-level grep.[ch]Junio C Hamano, Oct 4, 2012
  9. 3/6 log --grep: use the same helper to set -E/-F options as "git grep"Junio C Hamano, Oct 4, 2012
  10. Jeff KingOct 4, 2012
  11. 4/6 log --grep: accept --basic-regexp and --perl-regexpJunio C Hamano, Oct 4, 2012
  12. Jeff KingOct 4, 2012
  13. Junio C HamanoOct 4, 2012
  14. 5/6 log: pass rev_info to git_log_config()Junio C Hamano, Oct 4, 2012
  15. Junio C HamanoOct 4, 2012
  16. Junio C HamanoOct 5, 2012
  17. Jeff KingOct 5, 2012
  18. Junio C HamanoOct 5, 2012
  19. 6/6 log --grep: honor grep.patterntype etc. configuration variablesJunio C Hamano, Oct 4, 2012
  20. Jeff KingOct 4, 2012
  21. Junio C HamanoOct 4, 2012
  22. Jeff KingOct 4, 2012
  23. Junio C HamanoOct 4, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.