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>
Jeff King committed
Apr 7, 2009 at 03:06 UTC
8db9a4b85d6b0d7424c8a19b77a5baa8529ab64c
1 file changed
+26
-21
builtin-for-each-ref.c
+26
-21
@@ -672,32 +672,37 @@ static void populate_value(struct refinfo *ref)
672
const char *name = used_atom[i];
673
struct atom_value *v = &ref->value[i];
674
int deref = 0;
675
+ const char *refname;
676
+ const char *formatp;
677
+
678
if (*name == '*') {
679
deref = 1;
680
name++;
681
}
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
- }
682
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;
706
}
707
}
708