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>
This commit is contained in:
SZEDER Gábor 2019-11-12 11:38:16 +01:00 committed by Junio C Hamano
parent 766f9e39c0
commit dd090a8a37

View file

@ -106,30 +106,19 @@ static struct rev_name *create_or_update_name(struct commit *commit,
static void name_rev(struct commit *commit, static void name_rev(struct commit *commit,
const char *tip_name, timestamp_t taggerdate, const char *tip_name, timestamp_t taggerdate,
int generation, int distance, int from_tag, int generation, int distance, int from_tag)
int deref)
{ {
struct commit_list *parents; struct commit_list *parents;
int parent_number = 1; int parent_number = 1;
char *to_free = NULL;
parse_commit(commit); parse_commit(commit);
if (commit->date < cutoff) if (commit->date < cutoff)
return; return;
if (deref) {
tip_name = to_free = xstrfmt("%s^0", tip_name);
if (generation)
die("generation: %d, but deref?", generation);
}
if (!create_or_update_name(commit, tip_name, taggerdate, generation, if (!create_or_update_name(commit, tip_name, taggerdate, generation,
distance, from_tag)) { distance, from_tag))
free(to_free);
return; return;
}
for (parents = commit->parents; for (parents = commit->parents;
parents; parents;
@ -148,11 +137,11 @@ static void name_rev(struct commit *commit,
name_rev(parents->item, new_name, taggerdate, 0, name_rev(parents->item, new_name, taggerdate, 0,
distance + MERGE_TRAVERSAL_WEIGHT, distance + MERGE_TRAVERSAL_WEIGHT,
from_tag, 0); from_tag);
} else { } else {
name_rev(parents->item, tip_name, taggerdate, name_rev(parents->item, tip_name, taggerdate,
generation + 1, distance + 1, generation + 1, distance + 1,
from_tag, 0); from_tag);
} }
} }
} }
@ -284,12 +273,16 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
if (o && o->type == OBJ_COMMIT) { if (o && o->type == OBJ_COMMIT) {
struct commit *commit = (struct commit *)o; struct commit *commit = (struct commit *)o;
int from_tag = starts_with(path, "refs/tags/"); int from_tag = starts_with(path, "refs/tags/");
const char *tip_name;
if (taggerdate == TIME_MAX) if (taggerdate == TIME_MAX)
taggerdate = commit->date; taggerdate = commit->date;
path = name_ref_abbrev(path, can_abbreviate_output); path = name_ref_abbrev(path, can_abbreviate_output);
name_rev(commit, xstrdup(path), taggerdate, 0, 0, if (deref)
from_tag, deref); tip_name = xstrfmt("%s^0", path);
else
tip_name = xstrdup(path);
name_rev(commit, tip_name, taggerdate, 0, 0, from_tag);
} }
return 0; return 0;
} }