X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=builtin-for-each-ref.c;h=29f70aabc3f1df027f59dfdbac82e025653a8d87;hb=6b6012e6ca628a00d2e0fa6f686017f9e2d66203;hp=2b218425aab2c37a826bfbe22535f2e5b7002a26;hpb=c63a3ad2c1d6f86cd7989055a133e06055239317;p=git.git diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c index 2b218425a..29f70aabc 100644 --- a/builtin-for-each-ref.c +++ b/builtin-for-each-ref.c @@ -1,3 +1,4 @@ +#include "builtin.h" #include "cache.h" #include "refs.h" #include "object.h" @@ -42,7 +43,7 @@ static struct { { "objectsize", FIELD_ULONG }, { "objectname" }, { "tree" }, - { "parent" }, /* NEEDSWORK: how to address 2nd and later parents? */ + { "parent" }, { "numparent", FIELD_ULONG }, { "object" }, { "type" }, @@ -261,24 +262,26 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object } if (!strcmp(name, "numparent")) { char *s = xmalloc(40); + v->ul = num_parents(commit); sprintf(s, "%lu", v->ul); v->s = s; - v->ul = num_parents(commit); } else if (!strcmp(name, "parent")) { int num = num_parents(commit); int i; struct commit_list *parents; - char *s = xmalloc(42 * num); + char *s = xmalloc(41 * num + 1); v->s = s; for (i = 0, parents = commit->parents; parents; - parents = parents->next, i = i + 42) { + parents = parents->next, i = i + 41) { struct commit *parent = parents->item; strcpy(s+i, sha1_to_hex(parent->object.sha1)); if (parents->next) s[i+40] = ' '; } + if (!i) + *s = '\0'; } } } @@ -796,7 +799,7 @@ static struct ref_sort *default_sort(void) return sort; } -int cmd_for_each_ref(int ac, const char **av, char *prefix) +int cmd_for_each_ref(int ac, const char **av, const char *prefix) { int i, num_refs; const char *format = NULL;