@matheusbsbs / matheus-bagliano / commits / dd090a8a37
Fork 0

name-rev: pull out deref handling from the recursion

The 'if (deref) { ... }' condition near the beginning of the recursive name_rev() function can only ever be true in the first invocation, because the 'deref' parameter is always 0 in the subsequent recursive invocations. Extract this condition from the recursion into name_rev()'s caller and drop the function's 'deref' parameter. This makes eliminating the recursion a bit easier to follow, and it will be moved back into name_rev() after the recursion is eliminated. Furthermore, drop the condition that die()s when both 'deref' and 'generation' are non-null (which should have been a BUG() to begin with). Note that this change reintroduces the memory leak that was plugged in in commit 5308224633 (name-rev: avoid leaking memory in the `deref` case, 2017-05-04), but a later patch (name-rev: restructure creating/updating 'struct rev_name' instances) in this series will plug it in again. 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 dd090a8a37b4507bf6c79ad93ec076673fa6313c
1 file changed +10 -17
builtin/name-rev.c
+10 -17
@@ -106,30 +106,19 @@ copy_data:
106
107 static void name_rev(struct commit *commit,
108 const char *tip_name, timestamp_t taggerdate,
109 - int generation, int distance, int from_tag,
110 - int deref)
109 + int generation, int distance, int from_tag)
110 {
111 struct commit_list *parents;
112 int parent_number = 1;
114 - char *to_free = NULL;
113
114 parse_commit(commit);
115
116 if (commit->date < cutoff)
117 return;
118
121 - if (deref) {
122 - tip_name = to_free = xstrfmt("%s^0", tip_name);
123 -
124 - if (generation)
125 - die("generation: %d, but deref?", generation);
126 - }
127 -
119 if (!create_or_update_name(commit, tip_name, taggerdate, generation,
129 - distance, from_tag)) {
130 - free(to_free);
120 + distance, from_tag))
121 return;
132 - }
122
123 for (parents = commit->parents;
124 parents;
@@ -148,11 +137,11 @@ static void name_rev(struct commit *commit,
137
138 name_rev(parents->item, new_name, taggerdate, 0,
139 distance + MERGE_TRAVERSAL_WEIGHT,
151 - from_tag, 0);
140 + from_tag);
141 } else {
142 name_rev(parents->item, tip_name, taggerdate,
143 generation + 1, distance + 1,
155 - from_tag, 0);
144 + from_tag);
145 }
146 }
147 }
@@ -284,12 +273,16 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
273 if (o && o->type == OBJ_COMMIT) {
274 struct commit *commit = (struct commit *)o;
275 int from_tag = starts_with(path, "refs/tags/");
276 + const char *tip_name;
277
278 if (taggerdate == TIME_MAX)
279 taggerdate = commit->date;
280 path = name_ref_abbrev(path, can_abbreviate_output);
291 - name_rev(commit, xstrdup(path), taggerdate, 0, 0,
292 - from_tag, deref);
281 + if (deref)
282 + tip_name = xstrfmt("%s^0", path);
283 + else
284 + tip_name = xstrdup(path);
285 + name_rev(commit, tip_name, taggerdate, 0, 0, from_tag);
286 }
287 return 0;
288 }