refs: allow "HEAD" as decoration filter

The normalize_glob_ref() method was introduced in 65516f586b693 (log: add option to choose which refs to decorate, 2017-11-21) to help with decoration filters such as --decorate-refs=<filter> and --decorate-refs-exclude=<filter>. The method has not been used anywhere else. At the moment, it is impossible to specify HEAD as a decoration filter since normalize_glob_ref() prepends "refs/" to the filter if it isn't already there. Allow adding HEAD as a decoration filter by allowing the exact string "HEAD" to not be prepended with "refs/". Add a test in t4202-log.sh that would previously fail since the HEAD decoration would exist in the output. It is sufficient to only cover "HEAD" here and not include other special refs like REBASE_HEAD. This is because HEAD is the only ref outside of refs/* that is added to the list of decorations. However, we may want to special-case these other refs in normalize_glob_ref() in the future. Leave a NEEDSWORK comment for now. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Aug 5, 2022 at 17:58 UTC b877e617e6e582553e21055deb272ce3a1354e1a
2 files changed +14 -3
refs.c
+8 -3
@@ -455,11 +455,16 @@ void normalize_glob_ref(struct string_list_item *item, const char *prefix,
455 if (*pattern == '/')
456 BUG("pattern must not start with '/'");
457
458 - if (prefix) {
458 + if (prefix)
459 strbuf_addstr(&normalized_pattern, prefix);
460 - }
461 - else if (!starts_with(pattern, "refs/"))
460 + else if (!starts_with(pattern, "refs/") &&
461 + strcmp(pattern, "HEAD"))
462 strbuf_addstr(&normalized_pattern, "refs/");
463 + /*
464 + * NEEDSWORK: Special case other symrefs such as REBASE_HEAD,
465 + * MERGE_HEAD, etc.
466 + */
467 +
468 strbuf_addstr(&normalized_pattern, pattern);
469 strbuf_strip_suffix(&normalized_pattern, "/");
470
t/t4202-log.sh
+6
@@ -1025,6 +1025,12 @@ test_expect_success 'decorate-refs and simplify-by-decoration without output' '
1025 test_cmp expect actual
1026 '
1027
1028 +test_expect_success 'decorate-refs-exclude HEAD' '
1029 + git log --decorate=full --oneline \
1030 + --decorate-refs-exclude="HEAD" >actual &&
1031 + ! grep HEAD actual
1032 +'
1033 +
1034 test_expect_success 'log.decorate config parsing' '
1035 git log --oneline --decorate=full >expect.full &&
1036 git log --oneline --decorate=short >expect.short &&