@matheusbsbs / matheus-bagliano / commits / 9e1a2acfb2
Fork 0

for-each-ref: fix %(numparent) and %(parent)

The string value of %(numparent) was not returned correctly. Also %(parent) misbehaved for the root commits (returned garbage) and merge commits (returned first parent, followed by a space). Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 2, 2007 at 15:09 UTC 9e1a2acfb2198e29c90b59eef84eb0370ef9f03f
1 file changed +6 -4
builtin-for-each-ref.c
+6 -4
@@ -43,7 +43,7 @@ static struct {
43 { "objectsize", FIELD_ULONG },
44 { "objectname" },
45 { "tree" },
46 - { "parent" }, /* NEEDSWORK: how to address 2nd and later parents? */
46 + { "parent" },
47 { "numparent", FIELD_ULONG },
48 { "object" },
49 { "type" },
@@ -262,24 +262,26 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
262 }
263 if (!strcmp(name, "numparent")) {
264 char *s = xmalloc(40);
265 + v->ul = num_parents(commit);
266 sprintf(s, "%lu", v->ul);
267 v->s = s;
267 - v->ul = num_parents(commit);
268 }
269 else if (!strcmp(name, "parent")) {
270 int num = num_parents(commit);
271 int i;
272 struct commit_list *parents;
273 - char *s = xmalloc(42 * num);
273 + char *s = xmalloc(41 * num + 1);
274 v->s = s;
275 for (i = 0, parents = commit->parents;
276 parents;
277 - parents = parents->next, i = i + 42) {
277 + parents = parents->next, i = i + 41) {
278 struct commit *parent = parents->item;
279 strcpy(s+i, sha1_to_hex(parent->object.sha1));
280 if (parents->next)
281 s[i+40] = ' ';
282 }
283 + if (!i)
284 + *s = '\0';
285 }
286 }
287 }