merge-recursive: provide pair of `unpack_trees_{start,finish}()`

Rename `git_merge_trees()` to `unpack_trees_start()` and extract the call to `discard_index()` into a new function `unpack_trees_finish()`. As a result, these are called early resp. late in `merge_trees()`, making the resource handling clearer. A later commit will expand on that, teaching `..._finish()` to free more memory. (So rather than moving the FIXME-comment, just drop it, since it will be addressed soon enough.) Also call `..._finish()` when `merge_trees()` returns early. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Martin Ågren <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed May 20, 2018 at 12:17 UTC 3f1c1c360080114fcc9492211601f41d56112e3c
1 file changed +15 -14
merge-recursive.c
+15 -14
@@ -337,10 +337,10 @@ static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
337 init_tree_desc(desc, tree->buffer, tree->size);
338 }
339
340 -static int git_merge_trees(struct merge_options *o,
341 - struct tree *common,
342 - struct tree *head,
343 - struct tree *merge)
340 +static int unpack_trees_start(struct merge_options *o,
341 + struct tree *common,
342 + struct tree *head,
343 + struct tree *merge)
344 {
345 int rc;
346 struct tree_desc t[3];
@@ -379,6 +379,11 @@ static int git_merge_trees(struct merge_options *o,
379 return rc;
380 }
381
382 +static void unpack_trees_finish(struct merge_options *o)
383 +{
384 + discard_index(&o->orig_index);
385 +}
386 +
387 struct tree *write_tree_from_memory(struct merge_options *o)
388 {
389 struct tree *result = NULL;
@@ -3088,13 +3093,14 @@ int merge_trees(struct merge_options *o,
3093 return 1;
3094 }
3095
3091 - code = git_merge_trees(o, common, head, merge);
3096 + code = unpack_trees_start(o, common, head, merge);
3097
3098 if (code != 0) {
3099 if (show(o, 4) || o->call_depth)
3100 err(o, _("merging of trees %s and %s failed"),
3101 oid_to_hex(&head->object.oid),
3102 oid_to_hex(&merge->object.oid));
3103 + unpack_trees_finish(o);
3104 return -1;
3105 }
3106
@@ -3147,20 +3153,15 @@ cleanup:
3153
3154 hashmap_free(&o->current_file_dir_set, 1);
3155
3150 - if (clean < 0)
3156 + if (clean < 0) {
3157 + unpack_trees_finish(o);
3158 return clean;
3159 + }
3160 }
3161 else
3162 clean = 1;
3163
3156 - /* Free the extra index left from git_merge_trees() */
3157 - /*
3158 - * FIXME: Need to also free data allocated by
3159 - * setup_unpack_trees_porcelain() tucked away in o->unpack_opts.msgs,
3160 - * but the problem is that only half of it refers to dynamically
3161 - * allocated data, while the other half points at static strings.
3162 - */
3163 - discard_index(&o->orig_index);
3164 + unpack_trees_finish(o);
3165
3166 if (o->call_depth && !(*result = write_tree_from_memory(o)))
3167 return -1;