From 046802d015b3be2e055ae68f29f76741023bc32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 2 Jul 2009 00:03:44 +0200 Subject: grep: print context hunk marks between files Print a hunk mark before matches from a new file are shown, in addition to the current behaviour of printing them if lines have been skipped. The result is easier to read, as (presumably unrelated) matches from different files are separated by a hunk mark. GNU grep does the same. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin-grep.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'builtin-grep.c') diff --git a/builtin-grep.c b/builtin-grep.c index 73fc922c49..48998af911 100644 --- a/builtin-grep.c +++ b/builtin-grep.c @@ -278,6 +278,17 @@ static int flush_grep(struct grep_opt *opt, argc -= 2; } + if (opt->pre_context || opt->post_context) { + /* + * grep handles hunk marks between files, but we need to + * do that ourselves between multiple calls. + */ + if (opt->show_hunk_mark) + write_or_die(1, "--\n", 3); + else + opt->show_hunk_mark = 1; + } + status = exec_grep(argc, argv); if (kept_0) { -- cgit v1.2.1 From 2944e4e6145bdfcb1a8730d7da671786d72c86ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 2 Jul 2009 00:06:34 +0200 Subject: grep: add option -p/--show-function The new option -p instructs git grep to print the previous function definition as a context line, similar to diff -p. Such context lines are marked with an equal sign instead of a dash. This option complements the existing context options -A, -B, -C. Function definitions are detected using the same heuristic that diff uses. User defined regular expressions are not supported, yet. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin-grep.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'builtin-grep.c') diff --git a/builtin-grep.c b/builtin-grep.c index 48998af911..037452ec79 100644 --- a/builtin-grep.c +++ b/builtin-grep.c @@ -278,13 +278,13 @@ static int flush_grep(struct grep_opt *opt, argc -= 2; } - if (opt->pre_context || opt->post_context) { + if (opt->pre_context || opt->post_context || opt->funcname) { /* * grep handles hunk marks between files, but we need to * do that ourselves between multiple calls. */ if (opt->show_hunk_mark) - write_or_die(1, "--\n", 3); + write_or_die(1, opt->funcname ? "==\n" : "--\n", 3); else opt->show_hunk_mark = 1; } @@ -721,6 +721,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix) "show context lines after matches"), OPT_NUMBER_CALLBACK(&opt, "shortcut for -C NUM", context_callback), + OPT_BOOLEAN('p', "show-function", &opt.funcname, + "show a line with the function name before matches"), OPT_GROUP(""), OPT_CALLBACK('f', NULL, &opt, "file", "read patterns from file", file_callback), @@ -789,7 +791,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix) argc--; } - if (opt.color && !opt.color_external) + if ((opt.color && !opt.color_external) || opt.funcname) external_grep_allowed = 0; if (!opt.pattern_list) die("no pattern given."); -- cgit v1.2.1 From 60ecac98ed6f420c6bf823491074219087c749d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 2 Jul 2009 00:07:24 +0200 Subject: grep -p: support user defined regular expressions Respect the userdiff attributes and config settings when looking for lines with function definitions in git grep -p. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin-grep.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'builtin-grep.c') diff --git a/builtin-grep.c b/builtin-grep.c index 037452ec79..9343cc5e5d 100644 --- a/builtin-grep.c +++ b/builtin-grep.c @@ -11,6 +11,7 @@ #include "tree-walk.h" #include "builtin.h" #include "parse-options.h" +#include "userdiff.h" #include "grep.h" #ifndef NO_EXTERNAL_GREP @@ -30,6 +31,12 @@ static int grep_config(const char *var, const char *value, void *cb) { struct grep_opt *opt = cb; + switch (userdiff_config(var, value)) { + case 0: break; + case -1: return -1; + default: return 0; + } + if (!strcmp(var, "color.grep")) { opt->color = git_config_colorbool(var, value, -1); return 0; -- cgit v1.2.1 From ed24e401e0e6ab860475b8575e28a2c6ea99cc69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 2 Jul 2009 00:06:34 +0200 Subject: grep: simplify -p output It was found a bit too loud to show == separators between the function headers. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- builtin-grep.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'builtin-grep.c') diff --git a/builtin-grep.c b/builtin-grep.c index 9343cc5e5d..ff8e51b43e 100644 --- a/builtin-grep.c +++ b/builtin-grep.c @@ -285,13 +285,13 @@ static int flush_grep(struct grep_opt *opt, argc -= 2; } - if (opt->pre_context || opt->post_context || opt->funcname) { + if (opt->pre_context || opt->post_context) { /* * grep handles hunk marks between files, but we need to * do that ourselves between multiple calls. */ if (opt->show_hunk_mark) - write_or_die(1, opt->funcname ? "==\n" : "--\n", 3); + write_or_die(1, "--\n", 3); else opt->show_hunk_mark = 1; } -- cgit v1.2.1