Skip to content

Commit dd090a8

Browse files
szedergitster
authored andcommitted
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 5308224 (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>
1 parent 766f9e3 commit dd090a8

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

builtin/name-rev.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,19 @@ static struct rev_name *create_or_update_name(struct commit *commit,
106106

107107
static void name_rev(struct commit *commit,
108108
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)
111110
{
112111
struct commit_list *parents;
113112
int parent_number = 1;
114-
char *to_free = NULL;
115113

116114
parse_commit(commit);
117115

118116
if (commit->date < cutoff)
119117
return;
120118

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-
128119
if (!create_or_update_name(commit, tip_name, taggerdate, generation,
129-
distance, from_tag)) {
130-
free(to_free);
120+
distance, from_tag))
131121
return;
132-
}
133122

134123
for (parents = commit->parents;
135124
parents;
@@ -148,11 +137,11 @@ static void name_rev(struct commit *commit,
148137

149138
name_rev(parents->item, new_name, taggerdate, 0,
150139
distance + MERGE_TRAVERSAL_WEIGHT,
151-
from_tag, 0);
140+
from_tag);
152141
} else {
153142
name_rev(parents->item, tip_name, taggerdate,
154143
generation + 1, distance + 1,
155-
from_tag, 0);
144+
from_tag);
156145
}
157146
}
158147
}
@@ -284,12 +273,16 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
284273
if (o && o->type == OBJ_COMMIT) {
285274
struct commit *commit = (struct commit *)o;
286275
int from_tag = starts_with(path, "refs/tags/");
276+
const char *tip_name;
287277

288278
if (taggerdate == TIME_MAX)
289279
taggerdate = commit->date;
290280
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);
293286
}
294287
return 0;
295288
}

0 commit comments

Comments
 (0)