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

Re: [PATCH v8 2/2] pretty: colorize pattern matches in commit messages

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 28, 2021, 00:03 UTC
Message-ID
<xmqqilylmuqx.fsf@gitster.g>
In-Reply-To
<20210924155000.1259649-2-someguy@effective-light.com>
Hamza Mahfooz <someguy@effective-light.com> writes:
Show 19 quoted lines
> +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)) {

So, this expects <buf, eol> to be only payload without "author " or timestamp, ...

Show 22 quoted lines
>  	} 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);
> +
> +		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);
... which is exactly what this caller is doing.
Very nice.
Previous: Hamza MahfoozNext: Junio C Hamano
Message 3 of 4 in “grep: refactor next_match() and match_one_pattern() for external use”
  1. 1/2 grep: refactor next_match() and match_one_pattern() for external useHamza Mahfooz, Sep 24, 2021
  2. 2/2 pretty: colorize pattern matches in commit messagesHamza Mahfooz, Sep 24, 2021
  3. Junio C HamanoSep 28, 2021
  4. Junio C HamanoSep 24, 2021

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.