]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: simplify lstrip_ref_components() memory handling
authorJeff King <peff@peff.net>
Sun, 15 Feb 2026 09:02:23 +0000 (04:02 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Feb 2026 17:45:29 +0000 (09:45 -0800)
We're walking forward in the string, skipping path components from
left-to-right. So when we've stripped as much as we want, the pointer we
have is a complete NUL-terminated string and we can just return it
(after duplicating it, of course). So there is no need for a temporary
allocated string.

But we do make an extra temporary copy due to f0062d3b74 (ref-filter:
free item->value and item->value->s, 2018-10-18). This is probably from
cargo-culting the technique used in rstrip_ref_components(), which
_does_ need a separate string (since it is stripping from the end and
ties off the temporary string with a NUL).

Let's drop the extra allocation. This is slightly more efficient, but
more importantly makes the code much simpler.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ref-filter.c

index ff14ac53de2ed2e2d126f5644db42e510bef213f..f5f0cb4ad6963274bf714d482029c78250c525af 100644 (file)
@@ -2196,13 +2196,10 @@ static int normalize_component_count(const char *refname, int len)
 static const char *lstrip_ref_components(const char *refname, int len)
 {
        int remaining = normalize_component_count(refname, len);
-       const char *start = xstrdup(refname);
-       const char *to_free = start;
 
        while (remaining > 0) {
-               switch (*start++) {
+               switch (*refname++) {
                case '\0':
-                       free((char *)to_free);
                        return xstrdup("");
                case '/':
                        remaining--;
@@ -2210,9 +2207,7 @@ static const char *lstrip_ref_components(const char *refname, int len)
                }
        }
 
-       start = xstrdup(start);
-       free((char *)to_free);
-       return start;
+       return xstrdup(refname);
 }
 
 static const char *rstrip_ref_components(const char *refname, int len)