From 3a521503019de7ca0c550c3861619bb8881c388c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Tue, 12 Nov 2019 11:38:18 +0100 Subject: name-rev: restructure creating/updating 'struct rev_name' instances MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At the beginning of the recursive name_rev() function it creates a new 'struct rev_name' instance for each previously unvisited commit or, if this visit results in better name for an already visited commit, then updates the 'struct rev_name' instance attached to the commit, or returns early. Restructure this so it's caller creates or updates the 'struct rev_name' instance associated with the commit to be passed as parameter, i.e. both name_ref() before calling name_rev() and name_rev() itself as it iterates over the parent commits. This makes eliminating the recursion a bit easier to follow, and the condition moved to name_ref() will be moved back to name_rev() after the recursion is eliminated. This change also plugs the memory leak that was temporarily unplugged in the earlier "name-rev: pull out deref handling from the recursion" patch in this series. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- builtin/name-rev.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'builtin/name-rev.c') diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 50412277909..6416c49f67a 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -111,14 +111,12 @@ static void name_rev(struct commit *commit, struct commit_list *parents; int parent_number = 1; - if (!create_or_update_name(commit, tip_name, taggerdate, generation, - distance, from_tag)) - return; - for (parents = commit->parents; parents; parents = parents->next, parent_number++) { struct commit *parent = parents->item; + const char *new_name; + int new_generation, new_distance; parse_commit(parent); if (parent->date < cutoff) @@ -126,7 +124,6 @@ static void name_rev(struct commit *commit, if (parent_number > 1) { size_t len; - char *new_name; strip_suffix(tip_name, "^0", &len); if (generation > 0) @@ -135,15 +132,19 @@ static void name_rev(struct commit *commit, else new_name = xstrfmt("%.*s^%d", (int)len, tip_name, parent_number); - - name_rev(parent, new_name, taggerdate, 0, - distance + MERGE_TRAVERSAL_WEIGHT, - from_tag); + new_generation = 0; + new_distance = distance + MERGE_TRAVERSAL_WEIGHT; } else { - name_rev(parent, tip_name, taggerdate, - generation + 1, distance + 1, - from_tag); + new_name = tip_name; + new_generation = generation + 1; + new_distance = distance + 1; } + + if (create_or_update_name(parent, new_name, taggerdate, + new_generation, new_distance, + from_tag)) + name_rev(parent, new_name, taggerdate, + new_generation, new_distance, from_tag); } } @@ -280,11 +281,17 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo path = name_ref_abbrev(path, can_abbreviate_output); if (commit->date >= cutoff) { const char *tip_name; + char *to_free = NULL; if (deref) - tip_name = xstrfmt("%s^0", path); + tip_name = to_free = xstrfmt("%s^0", path); else tip_name = xstrdup(path); - name_rev(commit, tip_name, taggerdate, 0, 0, from_tag); + if (create_or_update_name(commit, tip_name, taggerdate, + 0, 0, from_tag)) + name_rev(commit, tip_name, taggerdate, 0, 0, + from_tag); + else + free(to_free); } } return 0; -- cgit v1.2.3