for-each-ref: refactor refname handling

This code handles some special magic like *-deref and the
:short formatting specifier. The next patch will add another
field which outputs a ref and wants to use the same code.

This patch splits the "which ref are we outputting" from the
actual formatting. There should be no behavioral change.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King
2009-04-07 03:06:51 -04:00
committed by Junio C Hamano
parent 3d4ecc0e23
commit 8db9a4b85d

View File

@ -672,32 +672,37 @@ static void populate_value(struct refinfo *ref)
const char *name = used_atom[i]; const char *name = used_atom[i];
struct atom_value *v = &ref->value[i]; struct atom_value *v = &ref->value[i];
int deref = 0; int deref = 0;
const char *refname;
const char *formatp;
if (*name == '*') { if (*name == '*') {
deref = 1; deref = 1;
name++; name++;
} }
if (!prefixcmp(name, "refname")) {
const char *formatp = strchr(name, ':');
const char *refname = ref->refname;
/* look for "short" refname format */ if (!prefixcmp(name, "refname"))
if (formatp) { refname = ref->refname;
formatp++; else
if (!strcmp(formatp, "short")) continue;
refname = get_short_ref(ref->refname);
else
die("unknown refname format %s",
formatp);
}
if (!deref) formatp = strchr(name, ':');
v->s = refname; /* look for "short" refname format */
else { if (formatp) {
int len = strlen(refname); formatp++;
char *s = xmalloc(len + 4); if (!strcmp(formatp, "short"))
sprintf(s, "%s^{}", refname); refname = get_short_ref(refname);
v->s = s; else
} die("unknown %.*s format %s",
(int)(formatp - name), name, formatp);
}
if (!deref)
v->s = refname;
else {
int len = strlen(refname);
char *s = xmalloc(len + 4);
sprintf(s, "%s^{}", refname);
v->s = s;
} }
} }