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 8eee9f9277b6e38ec46c84f4ca3be5d988ca0a33
1 file changed +6 -34
revision.c
+6 -34
@@ -45,46 +45,18 @@ char *path_name(const struct name_path *path, const char *name)
45 return n;
46 }
47
48 -static int show_path_component_truncated(FILE *out, const char *name, int len)
49 -{
50 - int cnt;
51 - for (cnt = 0; cnt < len; cnt++) {
52 - int ch = name[cnt];
53 - if (!ch || ch == '\n')
54 - return -1;
55 - fputc(ch, out);
56 - }
57 - return len;
58 -}
59 -
60 -static int show_path_truncated(FILE *out, const struct name_path *path)
61 -{
62 - int emitted, ours;
63 -
64 - if (!path)
65 - return 0;
66 - emitted = show_path_truncated(out, path->up);
67 - if (emitted < 0)
68 - return emitted;
69 - if (emitted)
70 - fputc('/', out);
71 - ours = show_path_component_truncated(out, path->elem, path->elem_len);
72 - if (ours < 0)
73 - return ours;
74 - return ours || emitted;
75 -}
76 -
48 void show_object_with_name(FILE *out, struct object *obj,
49 const struct name_path *path, const char *component)
50 {
80 - struct name_path leaf;
81 - leaf.up = (struct name_path *)path;
82 - leaf.elem = component;
83 - leaf.elem_len = strlen(component);
51 + char *name = path_name(path, component);
52 + char *p;
53
54 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
86 - show_path_truncated(out, &leaf);
55 + for (p = name; *p && *p != '\n'; p++)
56 + fputc(*p, out);
57 fputc('\n', out);
58 +
59 + free(name);
60 }
61
62 static void mark_blob_uninteresting(struct blob *blob)