show_object_with_name: simplify by using path_name()

When "git rev-list" shows an object with its associated path name, it does so by walking the name_path linked list and printing each component (stopping at any embedded NULs or newlines). We'd like to eventually get rid of name_path entirely in favor of a single buffer, and dropping this custom printing code is part of that. As a first step, let's use path_name() to format the list into a single buffer, and print that. This is strictly less efficient than the original, but it's a temporary step in the refactoring; our end game will be to get the fully formatted name in the first place. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 11, 2016 at 17:24 UTC f9fb9d0e3caebe97c54c101d8235e6529d5a1273
1 file changed +6 -34
revision.c
+6 -34
@@ -49,46 +49,18 @@ char *path_name(const struct name_path *path, const char *name)
49 return n;
50 }
51
52 -static int show_path_component_truncated(FILE *out, const char *name, int len)
53 -{
54 - int cnt;
55 - for (cnt = 0; cnt < len; cnt++) {
56 - int ch = name[cnt];
57 - if (!ch || ch == '\n')
58 - return -1;
59 - fputc(ch, out);
60 - }
61 - return len;
62 -}
63 -
64 -static int show_path_truncated(FILE *out, const struct name_path *path)
65 -{
66 - int emitted, ours;
67 -
68 - if (!path)
69 - return 0;
70 - emitted = show_path_truncated(out, path->up);
71 - if (emitted < 0)
72 - return emitted;
73 - if (emitted)
74 - fputc('/', out);
75 - ours = show_path_component_truncated(out, path->elem, path->elem_len);
76 - if (ours < 0)
77 - return ours;
78 - return ours || emitted;
79 -}
80 -
52 void show_object_with_name(FILE *out, struct object *obj,
53 const struct name_path *path, const char *component)
54 {
84 - struct name_path leaf;
85 - leaf.up = (struct name_path *)path;
86 - leaf.elem = component;
87 - leaf.elem_len = strlen(component);
55 + char *name = path_name(path, component);
56 + char *p;
57
58 fprintf(out, "%s ", oid_to_hex(&obj->oid));
90 - show_path_truncated(out, &leaf);
59 + for (p = name; *p && *p != '\n'; p++)
60 + fputc(*p, out);
61 fputc('\n', out);
62 +
63 + free(name);
64 }
65
66 static void mark_blob_uninteresting(struct blob *blob)