git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] pretty: find pretty formats case-insensitively
@ 2024-03-24 21:43 Brian Lyles
  2024-03-25  6:14 ` Jeff King
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Brian Lyles @ 2024-03-24 21:43 UTC (permalink / raw)
  To: git; +Cc: Brian Lyles

User-defined pretty formats are stored in config, which is meant to use
case-insensitive matching for names as noted in config.txt's 'Syntax'
section:

    All the other lines [...] are recognized as setting variables, in
    the form 'name = value' [...]. The variable names are
    case-insensitive, [...].

When a user specifies one of their format aliases with an uppercase in
it, however, it is not found.

    $ git config pretty.testAlias %h
    $ git config --list | grep pretty
    pretty.testalias=%h
    $ git log --format=testAlias -1
    fatal: invalid --pretty format: testAlias
    $ git log --format=testalias -1
    3c2a3fdc38

This is true whether the name in the config file uses any uppercase
characters or not.

Normalize the format name specified via `--format` to lowercase so that
format aliases are found case-insensitively. The format aliases loaded
from config against which this name is compared are already normalized
to lowercase since they are loaded through `git_config()`.

`xstrdup_tolower` is used instead of modifying the string in-place to
ensure that the error shown to the user when the format is not found has
the same casing that the user entered. Otherwise, the mismatch may be
confusing to the user.

Signed-off-by: Brian Lyles <brianmlyles@gmail.com>
---
 pretty.c                      | 12 +++++++++++-
 t/t4205-log-pretty-formats.sh |  7 +++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/pretty.c b/pretty.c
index cf964b060c..78ec7a75ff 100644
--- a/pretty.c
+++ b/pretty.c
@@ -168,10 +168,20 @@ static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
 
 static struct cmt_fmt_map *find_commit_format(const char *sought)
 {
+	struct cmt_fmt_map *result;
+	char *sought_lower;
+
 	if (!commit_formats)
 		setup_commit_formats();
 
-	return find_commit_format_recursive(sought, sought, 0);
+	/*
+	 * The sought name will be compared to config names that have already
+	 * been normalized to lowercase.
+	 */
+	sought_lower = xstrdup_tolower(sought);
+	result = find_commit_format_recursive(sought_lower, sought_lower, 0);
+	free(sought_lower);
+	return result;
 }
 
 void get_commit_format(const char *arg, struct rev_info *rev)
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index e3d655e6b8..321e305979 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -59,6 +59,13 @@ test_expect_success 'alias user-defined format' '
 	test_cmp expected actual
 '
 
+test_expect_success 'alias user-defined format is matched case-insensitively' '
+	git log --pretty="format:%h" >expected &&
+	git config pretty.testalias "format:%h" &&
+	git log --pretty=testAlias >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'alias user-defined tformat with %s (ISO8859-1 encoding)' '
 	git config i18n.logOutputEncoding $test_encoding &&
 	git log --oneline >expected-s &&
-- 
2.43.2


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

end of thread, other threads:[~2024-03-25 18:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-24 21:43 [PATCH] pretty: find pretty formats case-insensitively Brian Lyles
2024-03-25  6:14 ` Jeff King
2024-03-25  7:08   ` Brian Lyles
2024-03-25 18:12   ` Junio C Hamano
2024-03-25  7:25 ` [PATCH v2 1/2] pretty: update tests to use `test_config` Brian Lyles
2024-03-25  9:44   ` Jeff King
2024-03-25  7:25 ` [PATCH v2 2/2] pretty: find pretty formats case-insensitively Brian Lyles
2024-03-25  9:46   ` Jeff King
2024-03-25 15:58     ` Brian Lyles

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 NNTP newsgroup(s).