Skip to content

Commit

Permalink
for-each-ref: refactor refname handling
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Apr 8, 2009
1 parent 3d4ecc0 commit 8db9a4b
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions builtin-for-each-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,32 +672,37 @@ static void populate_value(struct refinfo *ref)
const char *name = used_atom[i];
struct atom_value *v = &ref->value[i];
int deref = 0;
const char *refname;
const char *formatp;

if (*name == '*') {
deref = 1;
name++;
}
if (!prefixcmp(name, "refname")) {
const char *formatp = strchr(name, ':');
const char *refname = ref->refname;

/* look for "short" refname format */
if (formatp) {
formatp++;
if (!strcmp(formatp, "short"))
refname = get_short_ref(ref->refname);
else
die("unknown refname format %s",
formatp);
}

if (!deref)
v->s = refname;
else {
int len = strlen(refname);
char *s = xmalloc(len + 4);
sprintf(s, "%s^{}", refname);
v->s = s;
}
if (!prefixcmp(name, "refname"))
refname = ref->refname;
else
continue;

formatp = strchr(name, ':');
/* look for "short" refname format */
if (formatp) {
formatp++;
if (!strcmp(formatp, "short"))
refname = get_short_ref(refname);
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;
}
}

Expand Down

0 comments on commit 8db9a4b

Please sign in to comment.