make add_object_array_with_context interface more sane

When you resolve a sha1, you can optionally keep any context found during the resolution, including the path and mode of a tree entry (e.g., when looking up "HEAD:subdir/file.c"). The add_object_array_with_context function lets you then attach that context to an entry in a list. Unfortunately, the interface for doing so is horrible. The object_context structure is large and most object_array users do not use it. Therefore we keep a pointer to the structure to avoid burdening other users too much. But that means when we do use it that we must allocate the struct ourselves. And the struct contains a fixed PATH_MAX-sized buffer, which makes this wholly unsuitable for any large arrays. We can observe that there is only a single user of the "with_context" variant: builtin/grep.c. And in that use case, the only element we care about is the path. We can therefore store only the path as a pointer (the context's mode field was redundant with the object_array_entry itself, and nobody actually cared about the surrounding tree). This still requires a strdup of the pathname, but at least we are only consuming the minimum amount of memory for each string. We can also handle the copying ourselves in add_object_array_*, and free it as appropriate in object_array_release_entry. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 15, 2014 at 18:42 UTC 9e0c3c4fcdf3775a9e0256ee231efa4698297a0e
3 files changed +15 -20
builtin/grep.c
+4 -4
@@ -456,10 +456,10 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
456 }
457
458 static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
459 - struct object *obj, const char *name, struct object_context *oc)
459 + struct object *obj, const char *name, const char *path)
460 {
461 if (obj->type == OBJ_BLOB)
462 - return grep_sha1(opt, obj->sha1, name, 0, oc ? oc->path : NULL);
462 + return grep_sha1(opt, obj->sha1, name, 0, path);
463 if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
464 struct tree_desc tree;
465 void *data;
@@ -501,7 +501,7 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
501 for (i = 0; i < nr; i++) {
502 struct object *real_obj;
503 real_obj = deref_tag(list->objects[i].item, NULL, 0);
504 - if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].context)) {
504 + if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].path)) {
505 hit = 1;
506 if (opt->status_only)
507 break;
@@ -821,7 +821,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
821 struct object *object = parse_object_or_die(sha1, arg);
822 if (!seen_dashdash)
823 verify_non_filename(prefix, arg);
824 - add_object_array_with_context(object, arg, &list, xmemdupz(&oc, sizeof(struct object_context)));
824 + add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
825 continue;
826 }
827 if (!strcmp(arg, "--")) {
object.c
+9 -14
@@ -307,10 +307,9 @@ int object_list_contains(struct object_list *list, struct object *obj)
307 */
308 static char object_array_slopbuf[1];
309
310 -static void add_object_array_with_mode_context(struct object *obj, const char *name,
311 - struct object_array *array,
312 - unsigned mode,
313 - struct object_context *context)
310 +void add_object_array_with_path(struct object *obj, const char *name,
311 + struct object_array *array,
312 + unsigned mode, const char *path)
313 {
314 unsigned nr = array->nr;
315 unsigned alloc = array->alloc;
@@ -333,7 +332,10 @@ static void add_object_array_with_mode_context(struct object *obj, const char *n
332 else
333 entry->name = xstrdup(name);
334 entry->mode = mode;
336 - entry->context = context;
335 + if (path)
336 + entry->path = xstrdup(path);
337 + else
338 + entry->path = NULL;
339 array->nr = ++nr;
340 }
341
@@ -344,15 +346,7 @@ void add_object_array(struct object *obj, const char *name, struct object_array
346
347 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
348 {
347 - add_object_array_with_mode_context(obj, name, array, mode, NULL);
348 -}
349 -
350 -void add_object_array_with_context(struct object *obj, const char *name, struct object_array *array, struct object_context *context)
351 -{
352 - if (context)
353 - add_object_array_with_mode_context(obj, name, array, context->mode, context);
354 - else
355 - add_object_array_with_mode_context(obj, name, array, S_IFINVALID, context);
349 + add_object_array_with_path(obj, name, array, mode, NULL);
350 }
351
352 /*
@@ -363,6 +357,7 @@ static void object_array_release_entry(struct object_array_entry *ent)
357 {
358 if (ent->name != object_array_slopbuf)
359 free(ent->name);
360 + free(ent->path);
361 }
362
363 void object_array_filter(struct object_array *array,
object.h
+2 -2
@@ -18,8 +18,8 @@ struct object_array {
18 * empty string.
19 */
20 char *name;
21 + char *path;
22 unsigned mode;
22 - struct object_context *context;
23 } *objects;
24 };
25
@@ -115,7 +115,7 @@ int object_list_contains(struct object_list *list, struct object *obj);
115 /* Object array handling .. */
116 void add_object_array(struct object *obj, const char *name, struct object_array *array);
117 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode);
118 -void add_object_array_with_context(struct object *obj, const char *name, struct object_array *array, struct object_context *context);
118 +void add_object_array_with_path(struct object *obj, const char *name, struct object_array *array, unsigned mode, const char *path);
119
120 typedef int (*object_array_each_func_t)(struct object_array_entry *, void *);
121