@matheusbsbs / matheus-bagliano / commits / 3a52150301
Fork 0

name-rev: restructure creating/updating 'struct rev_name' instances

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 <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Nov 12, 2019 at 11:38 UTC 3a521503019de7ca0c550c3861619bb8881c388c
1 file changed +21 -14
builtin/name-rev.c
+21 -14
@@ -111,14 +111,12 @@ static void name_rev(struct commit *commit,
111 struct commit_list *parents;
112 int parent_number = 1;
113
114 - if (!create_or_update_name(commit, tip_name, taggerdate, generation,
115 - distance, from_tag))
116 - return;
117 -
114 for (parents = commit->parents;
115 parents;
116 parents = parents->next, parent_number++) {
117 struct commit *parent = parents->item;
118 + const char *new_name;
119 + int new_generation, new_distance;
120
121 parse_commit(parent);
122 if (parent->date < cutoff)
@@ -126,7 +124,6 @@ static void name_rev(struct commit *commit,
124
125 if (parent_number > 1) {
126 size_t len;
129 - char *new_name;
127
128 strip_suffix(tip_name, "^0", &len);
129 if (generation > 0)
@@ -135,15 +132,19 @@ static void name_rev(struct commit *commit,
132 else
133 new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
134 parent_number);
138 -
139 - name_rev(parent, new_name, taggerdate, 0,
140 - distance + MERGE_TRAVERSAL_WEIGHT,
141 - from_tag);
135 + new_generation = 0;
136 + new_distance = distance + MERGE_TRAVERSAL_WEIGHT;
137 } else {
143 - name_rev(parent, tip_name, taggerdate,
144 - generation + 1, distance + 1,
145 - from_tag);
138 + new_name = tip_name;
139 + new_generation = generation + 1;
140 + new_distance = distance + 1;
141 }
142 +
143 + if (create_or_update_name(parent, new_name, taggerdate,
144 + new_generation, new_distance,
145 + from_tag))
146 + name_rev(parent, new_name, taggerdate,
147 + new_generation, new_distance, from_tag);
148 }
149 }
150
@@ -280,11 +281,17 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
281 path = name_ref_abbrev(path, can_abbreviate_output);
282 if (commit->date >= cutoff) {
283 const char *tip_name;
284 + char *to_free = NULL;
285 if (deref)
284 - tip_name = xstrfmt("%s^0", path);
286 + tip_name = to_free = xstrfmt("%s^0", path);
287 else
288 tip_name = xstrdup(path);
287 - name_rev(commit, tip_name, taggerdate, 0, 0, from_tag);
289 + if (create_or_update_name(commit, tip_name, taggerdate,
290 + 0, 0, from_tag))
291 + name_rev(commit, tip_name, taggerdate, 0, 0,
292 + from_tag);
293 + else
294 + free(to_free);
295 }
296 }
297 return 0;