Skip to content

Commit 87cb6dc

Browse files
peffgitster
authored andcommitted
ref-filter: simplify lstrip_ref_components() memory handling
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 f0062d3 (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>
1 parent 5ec4c22 commit 87cb6dc

1 file changed

Lines changed: 2 additions & 7 deletions

File tree

ref-filter.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,23 +2196,18 @@ static int normalize_component_count(const char *refname, int len)
21962196
static const char *lstrip_ref_components(const char *refname, int len)
21972197
{
21982198
int remaining = normalize_component_count(refname, len);
2199-
const char *start = xstrdup(refname);
2200-
const char *to_free = start;
22012199

22022200
while (remaining > 0) {
2203-
switch (*start++) {
2201+
switch (*refname++) {
22042202
case '\0':
2205-
free((char *)to_free);
22062203
return xstrdup("");
22072204
case '/':
22082205
remaining--;
22092206
break;
22102207
}
22112208
}
22122209

2213-
start = xstrdup(start);
2214-
free((char *)to_free);
2215-
return start;
2210+
return xstrdup(refname);
22162211
}
22172212

22182213
static const char *rstrip_ref_components(const char *refname, int len)

0 commit comments

Comments
 (0)