Skip to contentSkip to conversation

pretty: colorize pattern matches in commit messages

PATCH Superseded Hamza Mahfooz 2 participants 2 messages Sep 5, 2021 Documentation

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pretty: colorize pattern matches in commit messages

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

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

---
 Documentation/git-log.txt |   8 +++
 grep.c                    |  41 +++++++++------
 grep.h                    |   3 ++
 pretty.c                  | 108 +++++++++++++++++++++++++++++++++-----
 t/t4202-log.sh            |  42 +++++++++++++++
 5 files changed, 175 insertions(+), 27 deletions(-)
Documentation/git-log.txt +8 -0
index 0498e7bacb..8d08785863 100644
@@ -232,6 +232,14 @@ notes.displayRef::
 	or `GIT_NOTES_REF`, to read notes from when showing commit
 	messages with the `log` family of commands.  See
 	linkgit:git-notes[1].
+
+color.grep.selected::
+	Determines the non matching text (background) color of selected lines,
+	when `--grep`, `--author` or `--committer` are used.
+
+color.grep.matchSelected::
+	Determines the matching text (foreground) color of selected lines, when
+	`--grep`, `--author` or `--committer` are used.
 +
 May be an unabbreviated ref name or a glob and may be specified
 multiple times.  A warning will be issued for refs that do not exist,
grep.c +26 -15

72 lines not on this page · raw message

grep.h +3 -0
index 72f82b1e30..d2943e29ea 100644
@@ -177,6 +177,9 @@ 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, char *buf, unsigned long size);
+int grep_next_match(struct grep_opt *opt, char *bol, char *eol,
+		    enum grep_context ctx, regmatch_t *pmatch,
+		    enum grep_header_field field, int eflags);
 
 struct grep_source {
 	char *name;
pretty.c +96 -12

180 lines not on this page · raw message

t/t4202-log.sh +42 -0

48 lines not on this page · raw message

Signature · 1 line
2.33.0
#1 lore raw
Quoted 13 lines · Hamza Mahfooz

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

---

Documentation/git-log.txt +1 -0
@@ -232,6 +232,14 @@ notes.displayRef::
        or `GIT_NOTES_REF`, to read notes from when showing commit
        messages with the `log` family of commands.  See
        linkgit:git-notes[1].
+

Documentation/git-log.txt notes.displayRef:: · line 245

Quoted 10 lines
Documentation/git-log.txt +7 -0
+color.grep.selected::
+       Determines the non matching text (background) color of selected lines,
+       when `--grep`, `--author` or `--committer` are used.
+
+color.grep.matchSelected::
+       Determines the matching text (foreground) color of selected lines, when
+       `--grep`, `--author` or `--committer` are used.
 +
 May be an unabbreviated ref name or a glob and may be specified
 multiple times.  A warning will be issued for refs that do not exist,

The paragraph which begins "May be an unabbreviated..." belongs to the notes.displayRef item (which is indicated by the line containing only a + just before the paragraph), so this newly-inserted text incorrectly divorces the item from the rest of its description. The two new color.grep.* items which this patch adds should be placed after _all_ the paragraphs connected by + lines.