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

[PATCH v2] grep: add configuration variables for --heading

From
Simon Ser <contact@emersion.fr>
Date
May 27, 2020, 15:45 UTC
Message-ID
<C5PlGbi78_QpceNrgFm_-Wt6BooqbntaL8OdH7XjxjXCtyIfsisyeOQMslLOwNY68Dg7iUxGIvzLPAvUEWyMIC0GdpTgeWwgwD5sWlsyrSM=@emersion.fr>
In-Reply-To
<4WK2xPaKLXPbLE0jjY0rBR9M_BVt69rzJKBN17M0ily4VPYuMNC_a2kXsIKWR8y6lc7j58WpBmaIqdbJ1whnjonrfEWz7YE-WpdQqCu_lN0=@emersion.fr>

There are already configuration variables for -n and --column. Add one for --heading, allowing users to customize the default behaviour.

Signed-off-by: Simon Ser <contact@emersion.fr>
---
v2: add a test
 Documentation/git-grep.txt | 3 +++
 grep.c                     | 5 +++++
 t/t7810-grep.sh            | 5 +++++
 3 files changed, 13 insertions(+)
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index a7f9bc99eaf1..ed4f05d885a2 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -47,6 +47,9 @@ grep.lineNumber::
 grep.column::
 	If set to true, enable the `--column` option by default.
 
+grep.heading::
+	If set to true, enable the `--heading` option by default.
+
 grep.patternType::
 	Set the default matching behavior. Using a value of 'basic', 'extended',
 	'fixed', or 'perl' will enable the `--basic-regexp`, `--extended-regexp`,
diff --git a/grep.c b/grep.c
index 13232a904aca..4549bc722650 100644
--- a/grep.c
+++ b/grep.c
@@ -133,6 +133,10 @@ int grep_config(const char *var, const char *value, void *cb)
 		opt->columnnum = git_config_bool(var, value);
 		return 0;
 	}
+	if (!strcmp(var, "grep.heading")) {
+		opt->heading = git_config_bool(var, value);
+		return 0;
+	}
 
 	if (!strcmp(var, "grep.fullname")) {
 		opt->relative = !git_config_bool(var, value);
@@ -199,6 +203,7 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
 	opt->pattern_type_option = def->pattern_type_option;
 	opt->linenum = def->linenum;
 	opt->columnnum = def->columnnum;
+	opt->heading = def->heading;
 	opt->max_depth = def->max_depth;
 	opt->pathname = def->pathname;
 	opt->relative = def->relative;
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 991d5bd9c03f..1acc6fe89c51 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -1556,6 +1556,11 @@ test_expect_success 'grep --heading' '
 	test_cmp expected actual
 '
 
+test_expect_success 'grep with grep.heading=true' '
+	git -c grep.heading=true grep -e char -e lo_w hello.c hello_world >actual &&
+	test_cmp expected actual
+'
+
 cat >expected <<EOF
 <BOLD;GREEN>hello.c<RESET>
 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
-- 
2.26.2
Previous: Eric SunshineNext: Junio C Hamano
Message 3 of 6 in “grep: add configuration variables for --heading”
  1. grep: add configuration variables for --headingSimon Ser, May 26, 2020
  2. Eric SunshineMay 26, 2020
  3. grep: add configuration variables for --headingSimon Ser, May 27, 2020
  4. Junio C HamanoMay 27, 2020
  5. Simon SerJun 8, 2020
  6. Phillip WoodJun 8, 2020

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.