Skip to content

Commit 3d4ecc0

Browse files
peffgitster
authored andcommitted
for-each-ref: refactor get_short_ref function
This function took a "refinfo" object which is unnecessarily restrictive; it only ever looked at the refname field. This patch refactors it to take just the ref name as a string. While we're touching the relevant lines, let's give it consistent memory semantics. Previously, some code paths would return an allocated string and some would return the original string; now it will always return a malloc'd string. This doesn't actually fix a bug or a leak, because for-each-ref doesn't clean up its memory, but it makes the function a lot less surprising for reuse (which will happen in a later patch). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fbdc056 commit 3d4ecc0

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

builtin-for-each-ref.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
569569
/*
570570
* Shorten the refname to an non-ambiguous form
571571
*/
572-
static char *get_short_ref(struct refinfo *ref)
572+
static char *get_short_ref(const char *ref)
573573
{
574574
int i;
575575
static char **scanf_fmts;
@@ -598,17 +598,17 @@ static char *get_short_ref(struct refinfo *ref)
598598

599599
/* bail out if there are no rules */
600600
if (!nr_rules)
601-
return ref->refname;
601+
return xstrdup(ref);
602602

603-
/* buffer for scanf result, at most ref->refname must fit */
604-
short_name = xstrdup(ref->refname);
603+
/* buffer for scanf result, at most ref must fit */
604+
short_name = xstrdup(ref);
605605

606606
/* skip first rule, it will always match */
607607
for (i = nr_rules - 1; i > 0 ; --i) {
608608
int j;
609609
int short_name_len;
610610

611-
if (1 != sscanf(ref->refname, scanf_fmts[i], short_name))
611+
if (1 != sscanf(ref, scanf_fmts[i], short_name))
612612
continue;
613613

614614
short_name_len = strlen(short_name);
@@ -642,7 +642,7 @@ static char *get_short_ref(struct refinfo *ref)
642642
}
643643

644644
free(short_name);
645-
return ref->refname;
645+
return xstrdup(ref);
646646
}
647647

648648

@@ -684,7 +684,7 @@ static void populate_value(struct refinfo *ref)
684684
if (formatp) {
685685
formatp++;
686686
if (!strcmp(formatp, "short"))
687-
refname = get_short_ref(ref);
687+
refname = get_short_ref(ref->refname);
688688
else
689689
die("unknown refname format %s",
690690
formatp);

0 commit comments

Comments
 (0)