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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
fbdc05661d
commit
3d4ecc0e23
@ -569,7 +569,7 @@ static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
|
|||||||
/*
|
/*
|
||||||
* Shorten the refname to an non-ambiguous form
|
* Shorten the refname to an non-ambiguous form
|
||||||
*/
|
*/
|
||||||
static char *get_short_ref(struct refinfo *ref)
|
static char *get_short_ref(const char *ref)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
static char **scanf_fmts;
|
static char **scanf_fmts;
|
||||||
@ -598,17 +598,17 @@ static char *get_short_ref(struct refinfo *ref)
|
|||||||
|
|
||||||
/* bail out if there are no rules */
|
/* bail out if there are no rules */
|
||||||
if (!nr_rules)
|
if (!nr_rules)
|
||||||
return ref->refname;
|
return xstrdup(ref);
|
||||||
|
|
||||||
/* buffer for scanf result, at most ref->refname must fit */
|
/* buffer for scanf result, at most ref must fit */
|
||||||
short_name = xstrdup(ref->refname);
|
short_name = xstrdup(ref);
|
||||||
|
|
||||||
/* skip first rule, it will always match */
|
/* skip first rule, it will always match */
|
||||||
for (i = nr_rules - 1; i > 0 ; --i) {
|
for (i = nr_rules - 1; i > 0 ; --i) {
|
||||||
int j;
|
int j;
|
||||||
int short_name_len;
|
int short_name_len;
|
||||||
|
|
||||||
if (1 != sscanf(ref->refname, scanf_fmts[i], short_name))
|
if (1 != sscanf(ref, scanf_fmts[i], short_name))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
short_name_len = strlen(short_name);
|
short_name_len = strlen(short_name);
|
||||||
@ -642,7 +642,7 @@ static char *get_short_ref(struct refinfo *ref)
|
|||||||
}
|
}
|
||||||
|
|
||||||
free(short_name);
|
free(short_name);
|
||||||
return ref->refname;
|
return xstrdup(ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -684,7 +684,7 @@ static void populate_value(struct refinfo *ref)
|
|||||||
if (formatp) {
|
if (formatp) {
|
||||||
formatp++;
|
formatp++;
|
||||||
if (!strcmp(formatp, "short"))
|
if (!strcmp(formatp, "short"))
|
||||||
refname = get_short_ref(ref);
|
refname = get_short_ref(ref->refname);
|
||||||
else
|
else
|
||||||
die("unknown refname format %s",
|
die("unknown refname format %s",
|
||||||
formatp);
|
formatp);
|
||||||
|
Loading…
Reference in New Issue
Block a user