From: Taylor Blau <me@ttaylorr.com>
To: git@vger.kernel.org
Cc: avarab@gmail.com, peff@peff.net, gitster@pobox.com
Subject: [PATCH v3 3/7] grep.[ch]: extend grep_opt to allow showing matched column
Date: Fri, 22 Jun 2018 10:49:39 -0500 [thread overview]
Message-ID: <a3971a1a44bfbac599e74a9b177b21ccd2bb9a95.1529682173.git.me@ttaylorr.com> (raw)
In-Reply-To: <cover.1529682173.git.me@ttaylorr.com>
To support showing the matched column when calling 'git-grep(1)', teach
'grep_opt' the normal set of options to configure the default behavior
and colorization of this feature.
Now that we have opt->columnnum, use it to disable short-circuiting over
ORs and ANDs so that col and icol are always filled with the earliest
matches on each line. In addition, don't return the first match from
match_line(), for the same reason.
Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
grep.c | 47 +++++++++++++++++++++++++++++++++++++----------
grep.h | 2 ++
2 files changed, 39 insertions(+), 10 deletions(-)
diff --git a/grep.c b/grep.c
index dedfe17f93..c885101017 100644
--- a/grep.c
+++ b/grep.c
@@ -46,6 +46,7 @@ void init_grep_defaults(void)
color_set(opt->color_filename, "");
color_set(opt->color_function, "");
color_set(opt->color_lineno, "");
+ color_set(opt->color_columnno, "");
color_set(opt->color_match_context, GIT_COLOR_BOLD_RED);
color_set(opt->color_match_selected, GIT_COLOR_BOLD_RED);
color_set(opt->color_selected, "");
@@ -155,6 +156,7 @@ void grep_init(struct grep_opt *opt, const char *prefix)
opt->extended_regexp_option = def->extended_regexp_option;
opt->pattern_type_option = def->pattern_type_option;
opt->linenum = def->linenum;
+ opt->columnnum = def->columnnum;
opt->max_depth = def->max_depth;
opt->pathname = def->pathname;
opt->relative = def->relative;
@@ -164,6 +166,7 @@ void grep_init(struct grep_opt *opt, const char *prefix)
color_set(opt->color_filename, def->color_filename);
color_set(opt->color_function, def->color_function);
color_set(opt->color_lineno, def->color_lineno);
+ color_set(opt->color_columnno, def->color_columnno);
color_set(opt->color_match_context, def->color_match_context);
color_set(opt->color_match_selected, def->color_match_selected);
color_set(opt->color_selected, def->color_selected);
@@ -1277,23 +1280,36 @@ static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
0);
break;
case GREP_NODE_AND:
- if (!match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
- icol, 0))
- return 0;
- h = match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col,
+ h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
icol, 0);
+ if (h || opt->columnnum) {
+ /*
+ * Don't short-circuit AND when given --column, since a
+ * NOT earlier in the tree may turn this into an OR. In
+ * this case, see the below comment.
+ */
+ h &= match_expr_eval(opt, x->u.binary.right, bol, eol,
+ ctx, col, icol, 0);
+ }
break;
case GREP_NODE_OR:
- if (!collect_hits)
+ if (!(collect_hits || opt->columnnum)) {
+ /*
+ * Don't short-circuit OR when given --column (or
+ * collecting hits) to ensure we don't skip a later
+ * child that would produce an earlier match.
+ */
return (match_expr_eval(opt, x->u.binary.left, bol, eol,
ctx, col, icol, 0) ||
match_expr_eval(opt, x->u.binary.right, bol,
eol, ctx, col, icol, 0));
+ }
h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
icol, 0);
- x->u.binary.left->hit |= h;
+ if (collect_hits)
+ x->u.binary.left->hit |= h;
h |= match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col,
- icol, 1);
+ icol, collect_hits);
break;
default:
die("Unexpected node type (internal error) %d", x->node);
@@ -1316,6 +1332,7 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
enum grep_context ctx, int collect_hits)
{
struct grep_pat *p;
+ int hit = 0;
if (opt->extended)
return match_expr(opt, bol, eol, ctx, col, icol,
@@ -1325,11 +1342,21 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
for (p = opt->pattern_list; p; p = p->next) {
regmatch_t tmp;
if (match_one_pattern(p, bol, eol, ctx, &tmp, 0)) {
- *col = tmp.rm_so;
- return 1;
+ hit |= 1;
+ if (!opt->columnnum) {
+ /*
+ * Without --column, any single match on a line
+ * is enough to know that it needs to be
+ * printed. With --column, scan _all_ patterns
+ * to find the earliest.
+ */
+ break;
+ }
+ if (*col < 0 || tmp.rm_so < *col)
+ *col = tmp.rm_so;
}
}
- return 0;
+ return hit;
}
static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
diff --git a/grep.h b/grep.h
index 399381c908..08a0b391c5 100644
--- a/grep.h
+++ b/grep.h
@@ -127,6 +127,7 @@ struct grep_opt {
int prefix_length;
regex_t regexp;
int linenum;
+ int columnnum;
int invert;
int ignore_case;
int status_only;
@@ -159,6 +160,7 @@ struct grep_opt {
char color_filename[COLOR_MAXLEN];
char color_function[COLOR_MAXLEN];
char color_lineno[COLOR_MAXLEN];
+ char color_columnno[COLOR_MAXLEN];
char color_match_context[COLOR_MAXLEN];
char color_match_selected[COLOR_MAXLEN];
char color_selected[COLOR_MAXLEN];
--
2.18.0
next prev parent reply other threads:[~2018-06-22 15:49 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-18 23:43 [PATCH 0/7] grep.c: teach --column to 'git-grep(1)' Taylor Blau
2018-06-18 23:43 ` [PATCH 1/7] Documentation/config.txt: camel-case lineNumber for consistency Taylor Blau
2018-06-18 23:43 ` [PATCH 2/7] grep.c: expose {,inverted} match column in match_line() Taylor Blau
2018-06-19 16:49 ` Junio C Hamano
2018-06-19 17:02 ` Taylor Blau
2018-06-18 23:43 ` [PATCH 3/7] grep.[ch]: extend grep_opt to allow showing matched column Taylor Blau
2018-06-18 23:43 ` [PATCH 4/7] grep.c: display column number of first match Taylor Blau
2018-06-19 16:28 ` Jeff King
2018-06-19 16:34 ` Taylor Blau
2018-06-18 23:43 ` [PATCH 5/7] builtin/grep.c: add '--column' option to 'git-grep(1)' Taylor Blau
2018-06-18 23:43 ` [PATCH 6/7] grep.c: add configuration variables to show matched option Taylor Blau
2018-06-18 23:43 ` [PATCH 7/7] contrib/git-jump/git-jump: jump to exact location Taylor Blau
2018-06-19 16:35 ` [PATCH 0/7] grep.c: teach --column to 'git-grep(1)' Jeff King
2018-06-19 17:33 ` René Scharfe
2018-06-19 17:44 ` Taylor Blau
2018-06-19 17:50 ` René Scharfe
2018-06-19 20:26 ` René Scharfe
2018-06-19 17:48 ` Jeff King
2018-06-19 17:54 ` Taylor Blau
2018-06-19 17:58 ` Junio C Hamano
2018-06-19 18:02 ` Taylor Blau
2018-06-19 18:05 ` Jeff King
2018-06-19 18:09 ` Junio C Hamano
2018-06-19 18:50 ` René Scharfe
2018-06-19 19:11 ` Jeff King
2018-06-19 20:34 ` René Scharfe
2018-06-19 20:51 ` Junio C Hamano
2018-06-19 16:46 ` Junio C Hamano
2018-06-19 17:02 ` Taylor Blau
2018-06-19 22:51 ` Taylor Blau
2018-06-20 20:05 ` [PATCH v2 " Taylor Blau
2018-06-20 20:05 ` [PATCH v2 1/7] Documentation/config.txt: camel-case lineNumber for consistency Taylor Blau
2018-06-20 20:05 ` [PATCH v2 2/7] grep.c: expose {,inverted} match column in match_line() Taylor Blau
2018-06-20 20:05 ` [PATCH v2 3/7] grep.[ch]: extend grep_opt to allow showing matched column Taylor Blau
2018-06-20 20:05 ` [PATCH v2 4/7] grep.c: display column number of first match Taylor Blau
2018-06-20 20:05 ` [PATCH v2 5/7] builtin/grep.c: add '--column' option to 'git-grep(1)' Taylor Blau
2018-06-20 20:05 ` [PATCH v2 6/7] grep.c: add configuration variables to show matched option Taylor Blau
2018-06-20 20:05 ` [PATCH v2 7/7] contrib/git-jump/git-jump: jump to exact location Taylor Blau
2018-06-21 11:53 ` [PATCH v2 0/7] grep.c: teach --column to 'git-grep(1)' Jeff King
2018-06-21 12:01 ` Jeff King
2018-06-22 21:45 ` Johannes Schindelin
2018-06-22 22:26 ` Jeff King
2018-06-21 20:52 ` Junio C Hamano
2018-06-21 21:45 ` Taylor Blau
2018-06-22 7:22 ` Jeff King
2018-06-22 15:49 ` [PATCH v3 " Taylor Blau
2018-06-22 15:49 ` [PATCH v3 1/7] Documentation/config.txt: camel-case lineNumber for consistency Taylor Blau
2018-06-22 15:49 ` [PATCH v3 2/7] grep.c: expose {,inverted} match column in match_line() Taylor Blau
2018-06-22 15:49 ` Taylor Blau [this message]
2018-06-22 15:49 ` [PATCH v3 4/7] grep.c: display column number of first match Taylor Blau
2018-06-22 15:49 ` [PATCH v3 5/7] builtin/grep.c: add '--column' option to 'git-grep(1)' Taylor Blau
2018-06-22 15:49 ` [PATCH v3 6/7] grep.c: add configuration variables to show matched option Taylor Blau
2018-06-22 15:49 ` [PATCH v3 7/7] contrib/git-jump/git-jump: jump to exact location Taylor Blau
2018-06-25 18:43 ` [PATCH v3 0/7] grep.c: teach --column to 'git-grep(1)' Jeff King
2018-06-25 18:47 ` Taylor Blau
2018-06-26 16:45 ` Junio C Hamano
-- strict thread matches above, loose matches on Subject: below --
2018-04-21 3:45 [PATCH 1/6] grep.c: take regmatch_t as argument in match_line() Taylor Blau
2018-04-24 5:07 ` [PATCH v3 0/7] Teach '--column-number' to 'git-grep(1)' Taylor Blau
2018-04-24 5:07 ` [PATCH v3 3/7] grep.[ch]: extend grep_opt to allow showing matched column Taylor Blau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a3971a1a44bfbac599e74a9b177b21ccd2bb9a95.1529682173.git.me@ttaylorr.com \
--to=me@ttaylorr.com \
--cc=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).