Skip to content

Commit 8db9a4b

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3d4ecc0 commit 8db9a4b

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

builtin-for-each-ref.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -672,32 +672,37 @@ static void populate_value(struct refinfo *ref)
672672
const char *name = used_atom[i];
673673
struct atom_value *v = &ref->value[i];
674674
int deref = 0;
675+
const char *refname;
676+
const char *formatp;
677+
675678
if (*name == '*') {
676679
deref = 1;
677680
name++;
678681
}
679-
if (!prefixcmp(name, "refname")) {
680-
const char *formatp = strchr(name, ':');
681-
const char *refname = ref->refname;
682-
683-
/* look for "short" refname format */
684-
if (formatp) {
685-
formatp++;
686-
if (!strcmp(formatp, "short"))
687-
refname = get_short_ref(ref->refname);
688-
else
689-
die("unknown refname format %s",
690-
formatp);
691-
}
692682

693-
if (!deref)
694-
v->s = refname;
695-
else {
696-
int len = strlen(refname);
697-
char *s = xmalloc(len + 4);
698-
sprintf(s, "%s^{}", refname);
699-
v->s = s;
700-
}
683+
if (!prefixcmp(name, "refname"))
684+
refname = ref->refname;
685+
else
686+
continue;
687+
688+
formatp = strchr(name, ':');
689+
/* look for "short" refname format */
690+
if (formatp) {
691+
formatp++;
692+
if (!strcmp(formatp, "short"))
693+
refname = get_short_ref(refname);
694+
else
695+
die("unknown %.*s format %s",
696+
(int)(formatp - name), name, formatp);
697+
}
698+
699+
if (!deref)
700+
v->s = refname;
701+
else {
702+
int len = strlen(refname);
703+
char *s = xmalloc(len + 4);
704+
sprintf(s, "%s^{}", refname);
705+
v->s = s;
701706
}
702707
}
703708

0 commit comments

Comments
 (0)