git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use
@ 2021-09-21 21:13 Hamza Mahfooz
  2021-09-21 21:13 ` [PATCH v7 2/2] pretty: colorize pattern matches in commit messages Hamza Mahfooz
  2021-09-23 17:25 ` [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use Junio C Hamano
  0 siblings, 2 replies; 4+ messages in thread
From: Hamza Mahfooz @ 2021-09-21 21:13 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Eric Sunshine, Hamza Mahfooz

These changes are made in preparation of, the colorization support for the
"git log" subcommands that, rely on regex functionality (i.e. "--author",
"--committer" and "--grep"). These changes are necessary primarily because
match_one_pattern() expects header lines to be prefixed, however, in
pretty, the prefixes are stripped from the lines because the name-email
pairs needs to go through additional parsing, before they can be printed
and because next_match() doesn't handle the case of
"ctx == GREP_CONTEXT_HEAD" at all. So, teach next_match() how to handle the
new case, move header_field[] so it can be used by pretty to reappend
relevant prefixes and teach match_one_pattern() how to handle subsequent
header line match attempts.

Signed-off-by: Hamza Mahfooz <someguy@effective-light.com>
---
v5: separate grep changes from pretty changes.

v6: rescope some variables.

v7: export header_field[] and allow for subsequent matches on header lines
    in match_one_pattern().
---
 grep.c | 53 ++++++++++++++++++++++++++++-------------------------
 grep.h | 13 +++++++++++++
 2 files changed, 41 insertions(+), 25 deletions(-)

diff --git a/grep.c b/grep.c
index 14fe8a0fd2..f4126011c5 100644
--- a/grep.c
+++ b/grep.c
@@ -935,15 +935,6 @@ static void strip_timestamp(const char *bol, const char **eol_p)
 	}
 }
 
-static struct {
-	const char *field;
-	size_t len;
-} header_field[] = {
-	{ "author ", 7 },
-	{ "committer ", 10 },
-	{ "reflog ", 7 },
-};
-
 static int match_one_pattern(struct grep_pat *p,
 			     const char *bol, const char *eol,
 			     enum grep_context ctx,
@@ -953,18 +944,23 @@ static int match_one_pattern(struct grep_pat *p,
 	const char *start = bol;
 
 	if ((p->token != GREP_PATTERN) &&
-	    ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
+	    ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)) &&
+	    ((p->token == GREP_PATTERN_BODY) != (ctx == GREP_CONTEXT_BODY)))
 		return 0;
 
 	if (p->token == GREP_PATTERN_HEAD) {
-		const char *field;
-		size_t len;
-		assert(p->field < ARRAY_SIZE(header_field));
-		field = header_field[p->field].field;
-		len = header_field[p->field].len;
-		if (strncmp(bol, field, len))
-			return 0;
-		bol += len;
+		if (!(eflags & REG_NOTBOL)) {
+			const char *field;
+			size_t len;
+
+			assert(p->field < ARRAY_SIZE(grep_header_fields));
+			field = grep_header_fields[p->field].field;
+			len = grep_header_fields[p->field].len;
+			if (strncmp(bol, field, len))
+				return 0;
+			bol += len;
+		}
+
 		switch (p->field) {
 		case GREP_HEADER_AUTHOR:
 		case GREP_HEADER_COMMITTER:
@@ -1158,22 +1154,28 @@ static int match_next_pattern(struct grep_pat *p,
 	return 1;
 }
 
-static int next_match(struct grep_opt *opt,
-		      const char *bol, const char *eol,
-		      enum grep_context ctx, regmatch_t *pmatch, int eflags)
+int grep_next_match(struct grep_opt *opt,
+		    const char *bol, const char *eol,
+		    enum grep_context ctx, regmatch_t *pmatch,
+		    enum grep_header_field field, int eflags)
 {
 	struct grep_pat *p;
 	int hit = 0;
 
 	pmatch->rm_so = pmatch->rm_eo = -1;
 	if (bol < eol) {
-		for (p = opt->pattern_list; p; p = p->next) {
+		for (p = ((ctx == GREP_CONTEXT_HEAD)
+			   ? opt->header_list : opt->pattern_list);
+			  p; p = p->next) {
 			switch (p->token) {
 			case GREP_PATTERN: /* atom */
 			case GREP_PATTERN_HEAD:
 			case GREP_PATTERN_BODY:
-				hit |= match_next_pattern(p, bol, eol, ctx,
-							  pmatch, eflags);
+				if ((field == GREP_HEADER_FIELD_MAX) ||
+				    (p->field == field))
+					hit |= match_next_pattern(p, bol, eol,
+								  ctx, pmatch,
+								  eflags);
 				break;
 			default:
 				break;
@@ -1261,7 +1263,8 @@ static void show_line(struct grep_opt *opt,
 			else if (sign == '=')
 				line_color = opt->colors[GREP_COLOR_FUNCTION];
 		}
-		while (next_match(opt, bol, eol, ctx, &match, eflags)) {
+		while (grep_next_match(opt, bol, eol, ctx, &match,
+				       GREP_HEADER_FIELD_MAX, eflags)) {
 			if (match.rm_so == match.rm_eo)
 				break;
 
diff --git a/grep.h b/grep.h
index 3cb8a83ae8..4847c37280 100644
--- a/grep.h
+++ b/grep.h
@@ -23,6 +23,15 @@ typedef int pcre2_general_context;
 #include "thread-utils.h"
 #include "userdiff.h"
 
+static const struct {
+	const char *field;
+	size_t len;
+} grep_header_fields[] = {
+	{ "author ", 7 },
+	{ "committer ", 10 },
+	{ "reflog ", 7 },
+};
+
 struct repository;
 
 enum grep_pat_token {
@@ -190,6 +199,10 @@ void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const
 void compile_grep_patterns(struct grep_opt *opt);
 void free_grep_patterns(struct grep_opt *opt);
 int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size);
+int grep_next_match(struct grep_opt *opt,
+		    const char *bol, const char *eol,
+		    enum grep_context ctx, regmatch_t *pmatch,
+		    enum grep_header_field field, int eflags);
 
 struct grep_source {
 	char *name;
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v7 2/2] pretty: colorize pattern matches in commit messages
  2021-09-21 21:13 [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use Hamza Mahfooz
@ 2021-09-21 21:13 ` Hamza Mahfooz
  2021-09-23 17:25 ` [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use Junio C Hamano
  1 sibling, 0 replies; 4+ messages in thread
From: Hamza Mahfooz @ 2021-09-21 21:13 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Eric Sunshine, Hamza Mahfooz

The "git log" command limits its output to the commits that contain strings
matched by a pattern when the "--grep=<pattern>" option is used, but unlike
output from "git grep -e <pattern>", the matches are not highlighted,
making them harder to spot.

Teach the pretty-printer code to highlight matches from the
"--grep=<pattern>", "--author=<pattern>" and "--committer=<pattern>"
options (to view the last one, you may have to ask for --pretty=fuller).

Also, it must be noted that we are effectively greping the content twice,
however it only slows down "git log --author=^H" on this repository by
around 1-2% (compared to v2.33.0), so it should be a small enough slow
down to justify the addition of the feature.

Signed-off-by: Hamza Mahfooz <someguy@effective-light.com>
---
v2: make the commit message whole (add the missing ingredients), rename
    append_matched_line() to append_line_with_color(), use
    colors[GREP_COLOR_MATCH_SELECTED] instead of
    colors[GREP_COLOR_MATCH_CONTEXT], allow the background color to be
    customized, don't copy strings to a buffer when not coloring in
    append_line_with_color(), rename next_match() to grep_next_match(),
    repurpose grep_next_match()/match_one_pattern() for use in
    append_line_with_color() (allowing us to remove duplicated matching
    code in append_line_with_color()), document how to customize the
    feature and modify some of the tests to fit the feature better.

v3: fix a formatting issue with the added documentation.

v4: add strbuf_add_with_color(), use the correct color code scheme in the
    unit tests and add more unit tests.

v5: separate grep changes from pretty changes and add some performance
    analysis in the commit message.

v6: put the documentation in the correct place, cleanup pretty.c and
    format the unit tests according to the current convention.

v7: get rid of all manual strbuf management, constify where now appropriate
    and fix the header line prefix issue properly.
---
 Documentation/config/color.txt |   7 +-
 pretty.c                       | 113 +++++++++++++++++++++++++++++----
 t/t4202-log.sh                 |  51 +++++++++++++++
 3 files changed, 157 insertions(+), 14 deletions(-)

diff --git a/Documentation/config/color.txt b/Documentation/config/color.txt
index e05d520a86..91d9a9da32 100644
--- a/Documentation/config/color.txt
+++ b/Documentation/config/color.txt
@@ -104,9 +104,12 @@ color.grep.<slot>::
 `matchContext`;;
 	matching text in context lines
 `matchSelected`;;
-	matching text in selected lines
+	matching text in selected lines. Also, used to customize the following
+	linkgit:git-log[1] subcommands: `--grep`, `--author` and `--committer`.
 `selected`;;
-	non-matching text in selected lines
+	non-matching text in selected lines. Also, used to customize the
+	following linkgit:git-log[1] subcommands: `--grep`, `--author` and
+	`--committer`.
 `separator`;;
 	separators between fields on a line (`:`, `-`, and `=`)
 	and between hunks (`--`)
diff --git a/pretty.c b/pretty.c
index 73b5ead509..2e5b02081e 100644
--- a/pretty.c
+++ b/pretty.c
@@ -431,6 +431,62 @@ const char *show_ident_date(const struct ident_split *ident,
 	return show_date(date, tz, mode);
 }
 
+static inline void strbuf_add_with_color(struct strbuf *sb, const char *color,
+					 const char *buf, size_t buflen)
+{
+	strbuf_addstr(sb, color);
+	strbuf_add(sb, buf, buflen);
+	if (*color)
+		strbuf_addstr(sb, GIT_COLOR_RESET);
+}
+
+static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
+				   const char *line, size_t linelen,
+				   int color, enum grep_context ctx,
+				   enum grep_header_field field)
+{
+	const char *buf, *eol, *line_color, *match_color;
+	regmatch_t match;
+	int eflags = 0;
+
+	buf = line;
+	eol = buf + linelen;
+
+	if (!opt || !want_color(color) || opt->invert)
+		goto end;
+
+	line_color = opt->colors[GREP_COLOR_SELECTED];
+	match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
+
+	while (grep_next_match(opt, buf, eol, ctx, &match, field, eflags)) {
+		if (match.rm_so == match.rm_eo)
+			break;
+
+		if (!eflags && (field < GREP_HEADER_FIELD_MAX)) {
+			int len = grep_header_fields[field].len;
+
+			buf += len;
+			match.rm_so -= len;
+			match.rm_eo -= len;
+		}
+
+		strbuf_add_with_color(sb, line_color, buf, match.rm_so);
+		strbuf_add_with_color(sb, match_color, buf + match.rm_so,
+				      match.rm_eo - match.rm_so);
+		buf += match.rm_eo;
+		eflags = REG_NOTBOL;
+	}
+
+	if (eflags)
+		strbuf_add_with_color(sb, line_color, buf, eol - buf);
+	else {
+end:
+		if (field < GREP_HEADER_FIELD_MAX)
+			buf += grep_header_fields[field].len;
+		strbuf_add(sb, buf, eol - buf);
+	}
+}
+
 void pp_user_info(struct pretty_print_context *pp,
 		  const char *what, struct strbuf *sb,
 		  const char *line, const char *encoding)
@@ -496,9 +552,28 @@ void pp_user_info(struct pretty_print_context *pp,
 			strbuf_addch(sb, '\n');
 		strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
 	} else {
-		strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
-			    (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, "    ",
-			    (int)namelen, namebuf, (int)maillen, mailbuf);
+		struct strbuf id = STRBUF_INIT;
+		enum grep_header_field field = GREP_HEADER_FIELD_MAX;
+		struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
+
+		if (!strcmp(what, "Author"))
+			field = GREP_HEADER_AUTHOR;
+		else if (!strcmp(what, "Commit"))
+			field = GREP_HEADER_COMMITTER;
+
+		strbuf_addf(sb, "%s: ", what);
+		if (pp->fmt == CMIT_FMT_FULLER)
+			strbuf_addchars(sb, ' ', 4);
+
+		if (field < GREP_HEADER_FIELD_MAX)
+			strbuf_addstr(&id, grep_header_fields[field].field);
+		strbuf_addf(&id, "%.*s <%.*s>", (int)namelen, namebuf,
+			    (int)maillen, mailbuf);
+
+		append_line_with_color(sb, opt, id.buf, id.len, pp->color,
+				       GREP_CONTEXT_HEAD, field);
+		strbuf_addch(sb, '\n');
+		strbuf_release(&id);
 	}
 
 	switch (pp->fmt) {
@@ -1939,8 +2014,9 @@ static int pp_utf8_width(const char *start, const char *end)
 	return width;
 }
 
-static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
-				 const char *line, int linelen)
+static void strbuf_add_tabexpand(struct strbuf *sb, struct grep_opt *opt,
+				 int color, int tabwidth, const char *line,
+				 int linelen)
 {
 	const char *tab;
 
@@ -1957,7 +2033,9 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
 			break;
 
 		/* Output the data .. */
-		strbuf_add(sb, line, tab - line);
+		append_line_with_color(sb, opt, line, tab - line, color,
+				       GREP_CONTEXT_BODY,
+				       GREP_HEADER_FIELD_MAX);
 
 		/* .. and the de-tabified tab */
 		strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
@@ -1972,7 +2050,8 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
 	 * worrying about width - there's nothing more to
 	 * align.
 	 */
-	strbuf_add(sb, line, linelen);
+	append_line_with_color(sb, opt, line, linelen, color, GREP_CONTEXT_BODY,
+			       GREP_HEADER_FIELD_MAX);
 }
 
 /*
@@ -1984,11 +2063,16 @@ static void pp_handle_indent(struct pretty_print_context *pp,
 			     struct strbuf *sb, int indent,
 			     const char *line, int linelen)
 {
+	struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
+
 	strbuf_addchars(sb, ' ', indent);
 	if (pp->expand_tabs_in_log)
-		strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen);
+		strbuf_add_tabexpand(sb, opt, pp->color, pp->expand_tabs_in_log,
+				     line, linelen);
 	else
-		strbuf_add(sb, line, linelen);
+		append_line_with_color(sb, opt, line, linelen, pp->color,
+				       GREP_CONTEXT_BODY,
+				       GREP_HEADER_FIELD_MAX);
 }
 
 static int is_mboxrd_from(const char *line, int len)
@@ -2006,7 +2090,9 @@ void pp_remainder(struct pretty_print_context *pp,
 		  struct strbuf *sb,
 		  int indent)
 {
+	struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
 	int first = 1;
+
 	for (;;) {
 		const char *line = *msg_p;
 		int linelen = get_one_line(line);
@@ -2027,14 +2113,17 @@ void pp_remainder(struct pretty_print_context *pp,
 		if (indent)
 			pp_handle_indent(pp, sb, indent, line, linelen);
 		else if (pp->expand_tabs_in_log)
-			strbuf_add_tabexpand(sb, pp->expand_tabs_in_log,
-					     line, linelen);
+			strbuf_add_tabexpand(sb, opt, pp->color,
+					     pp->expand_tabs_in_log, line,
+					     linelen);
 		else {
 			if (pp->fmt == CMIT_FMT_MBOXRD &&
 					is_mboxrd_from(line, linelen))
 				strbuf_addch(sb, '>');
 
-			strbuf_add(sb, line, linelen);
+			append_line_with_color(sb, opt, line, linelen,
+					       pp->color, GREP_CONTEXT_BODY,
+					       GREP_HEADER_FIELD_MAX);
 		}
 		strbuf_addch(sb, '\n');
 	}
diff --git a/t/t4202-log.sh b/t/t4202-log.sh
index 9dfead936b..3d240bba57 100755
--- a/t/t4202-log.sh
+++ b/t/t4202-log.sh
@@ -449,6 +449,57 @@ test_expect_success !FAIL_PREREQS 'log with various grep.patternType configurati
 	)
 '
 
+test_expect_success 'log --author' '
+	cat >expect <<-\EOF &&
+	Author: <BOLD;RED>A U<RESET> Thor <author@example.com>
+	EOF
+	git log -1 --color=always --author="A U" >log &&
+	grep Author log >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --committer' '
+	cat >expect <<-\EOF &&
+	Commit:     C O Mitter <committer@<BOLD;RED>example<RESET>.com>
+	EOF
+	git log -1 --color=always --pretty=fuller --committer="example" >log &&
+	grep "Commit:" log >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log -i --grep with color' '
+	cat >expect <<-\EOF &&
+	    <BOLD;RED>Sec<RESET>ond
+	    <BOLD;RED>sec<RESET>ond
+	EOF
+	git log --color=always -i --grep=^sec >log &&
+	grep -i sec log >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '-c color.grep.selected log --grep' '
+	cat >expect <<-\EOF &&
+	    <GREEN>th<RESET><BOLD;RED>ir<RESET><GREEN>d<RESET>
+	EOF
+	git -c color.grep.selected="green" log --color=always --grep=ir >log &&
+	grep ir log >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '-c color.grep.matchSelected log --grep' '
+	cat >expect <<-\EOF &&
+	    <BLUE>i<RESET>n<BLUE>i<RESET>t<BLUE>i<RESET>al
+	EOF
+	git -c color.grep.matchSelected="blue" log --color=always --grep=i >log &&
+	grep al log >actual.raw &&
+	test_decode_color <actual.raw >actual &&
+	test_cmp expect actual
+'
+
 cat > expect <<EOF
 * Second
 * sixth
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use
  2021-09-21 21:13 [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use Hamza Mahfooz
  2021-09-21 21:13 ` [PATCH v7 2/2] pretty: colorize pattern matches in commit messages Hamza Mahfooz
@ 2021-09-23 17:25 ` Junio C Hamano
  2021-09-24 12:04   ` Hamza Mahfooz
  1 sibling, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2021-09-23 17:25 UTC (permalink / raw)
  To: Hamza Mahfooz; +Cc: git, Jeff King, Eric Sunshine

Hamza Mahfooz <someguy@effective-light.com> writes:

> These changes are made in preparation of, the colorization support for the
> "git log" subcommands that, rely on regex functionality (i.e. "--author",
> "--committer" and "--grep"). These changes are necessary primarily because
> match_one_pattern() expects header lines to be prefixed, however, in
> pretty, the prefixes are stripped from the lines because the name-email
> pairs needs to go through additional parsing, before they can be printed
> and because next_match() doesn't handle the case of
> "ctx == GREP_CONTEXT_HEAD" at all. So, teach next_match() how to handle the
> new case, move header_field[] so it can be used by pretty to reappend
> relevant prefixes and teach match_one_pattern() how to handle subsequent
> header line match attempts.
>
> Signed-off-by: Hamza Mahfooz <someguy@effective-light.com>
> ---
> v5: separate grep changes from pretty changes.
>
> v6: rescope some variables.
>
> v7: export header_field[] and allow for subsequent matches on header lines
>     in match_one_pattern().
> ---
>  grep.c | 53 ++++++++++++++++++++++++++++-------------------------
>  grep.h | 13 +++++++++++++
>  2 files changed, 41 insertions(+), 25 deletions(-)
>
> diff --git a/grep.c b/grep.c
> index 14fe8a0fd2..f4126011c5 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -935,15 +935,6 @@ static void strip_timestamp(const char *bol, const char **eol_p)
>  	}
>  }
>  
> -static struct {
> -	const char *field;
> -	size_t len;
> -} header_field[] = {
> -	{ "author ", 7 },
> -	{ "committer ", 10 },
> -	{ "reflog ", 7 },
> -};
> -
>  static int match_one_pattern(struct grep_pat *p,
>  			     const char *bol, const char *eol,
>  			     enum grep_context ctx,
> @@ -953,18 +944,23 @@ static int match_one_pattern(struct grep_pat *p,
>  	const char *start = bol;
>  
>  	if ((p->token != GREP_PATTERN) &&
> -	    ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
> +	    ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)) &&
> +	    ((p->token == GREP_PATTERN_BODY) != (ctx == GREP_CONTEXT_BODY)))
>  		return 0;
>  
>  	if (p->token == GREP_PATTERN_HEAD) {
> -		const char *field;
> -		size_t len;
> -		assert(p->field < ARRAY_SIZE(header_field));
> -		field = header_field[p->field].field;
> -		len = header_field[p->field].len;
> -		if (strncmp(bol, field, len))
> -			return 0;
> -		bol += len;
> +		if (!(eflags & REG_NOTBOL)) {
> +			const char *field;
> +			size_t len;
> +
> +			assert(p->field < ARRAY_SIZE(grep_header_fields));
> +			field = grep_header_fields[p->field].field;
> +			len = grep_header_fields[p->field].len;
> +			if (strncmp(bol, field, len))
> +				return 0;
> +			bol += len;
> +		}
> +
>  		switch (p->field) {
>  		case GREP_HEADER_AUTHOR:
>  		case GREP_HEADER_COMMITTER:
>			saved_ch = strip_timestamp(bol, &eol);

Let's first see if we correctly understand how the original was
designed to be used.  It is called once per line, with "bol" set
to the true beginning of the line.

For a line in the header (e.g. "author A U Thor <au@th.or> 12345678
+0000"), therefore, we ensure we are looking at the right type of
the header (e.g. when looking for an "author" line, the line must
begin with "author "), or we return 0 (i.e. does not match).  And
then, for "author" and "committer" line, we adjust the eol to
exclude the timestamp part from pattern the matching by calling
strip_timetamp().

Both of these special case used to be unconditional because we _knew_
that the caller would call this function with "bol" pointing at the
true beginning of the line.

The patch adds a new !(eflags & REG_NOTBOL) guard, presumably
because new callers will start calling this function with "bol" set
to point in the middle of a line?  So we only do the "check the kind
of header and skip a line that records the commiter when we are
looking for an author" part when the guard passes, i.e. we know that
the caller called us with the true beginning of the line in bol.

But how would the new caller that points "bol" at middle of a line
make sure that we are looking at the right kind of header?  If the
pattern p is set to match only for an author line, the first call
with "bol" set to the true beginning of the line will correctly
reject a "committer" header, but because you lost the sanity check
above, the second and subsequent one will go ahead and scan for the
pattern p on the line, even if p->field asks for author line and the
line records the committer.  You'd end up finding a commit object
that is committed by (but not authored by) the person when you are
looking for a commit that was authored by somebody, no?  

If you ask for commits by somebody (e.g. "--author=Hazma") with an
output format that shows both the author and the committer
(e.g. "log --pretty=fuller"), wouldn't your "hit coloring" code
show Hazma on the committer name as a grep hit, too?

Also, because the function is designed to be called with bol and eol
set to true beginning and the end of the line, the strip_timestamp()
side of the logic should also be done only once, but that is not
what is happening in the patch.  Chomping the line immediately after
the last occurrence of '>' is idempotent, so it is OK to have the
logic outside the new !(eflags & REG_NOTBOL) guard, but discarding
the updated eol from the first call and not reusing the result of
the strip_timestamp() in the second and subsequent call smells like
a waste.

I am having a feeling that you'd need to make the caller of this
function (either the direct callers, or you may want to introduce
another layer between the current direct callers and this function)
perform these massaging for the prefix and trailing parts of the
line for GREP_PATTERN_HEAD patterns, so that this function, if you
were to change it to be callable many times starting at a middle of
the line, only needs to care about matching the pattern in the
portion between (bol, eol) and nothing else.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use
  2021-09-23 17:25 ` [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use Junio C Hamano
@ 2021-09-24 12:04   ` Hamza Mahfooz
  0 siblings, 0 replies; 4+ messages in thread
From: Hamza Mahfooz @ 2021-09-24 12:04 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Eric Sunshine


On Thu, Sep 23 2021 at 10:25:28 AM -0700, Junio C Hamano 
<gitster@pobox.com> wrote:
> But how would the new caller that points "bol" at middle of a line
> make sure that we are looking at the right kind of header?  If the
> pattern p is set to match only for an author line, the first call
> with "bol" set to the true beginning of the line will correctly
> reject a "committer" header, but because you lost the sanity check
> above, the second and subsequent one will go ahead and scan for the
> pattern p on the line, even if p->field asks for author line and the
> line records the committer.  You'd end up finding a commit object
> that is committed by (but not authored by) the person when you are
> looking for a commit that was authored by somebody, no?
> 
> If you ask for commits by somebody (e.g. "--author=Hazma") with an
> output format that shows both the author and the committer
> (e.g. "log --pretty=fuller"), wouldn't your "hit coloring" code
> show Hazma on the committer name as a grep hit, too?

Actually, this issue doesn't arise because I filter away the irrelevant
(header) patterns in grep_next_match(). However, maybe it's a better 
idea
to handle that in match_one_pattern().



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-09-24 12:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-21 21:13 [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use Hamza Mahfooz
2021-09-21 21:13 ` [PATCH v7 2/2] pretty: colorize pattern matches in commit messages Hamza Mahfooz
2021-09-23 17:25 ` [PATCH v7 1/2] grep: refactor next_match() and match_one_pattern() for external use Junio C Hamano
2021-09-24 12:04   ` Hamza Mahfooz

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).