Skip to content

Commit d08565b

Browse files
peffgitster
authored andcommitted
reflog-walk: stop using fake parents
The reflog-walk system works by putting a ref's tip into the pending queue, and then "traversing" the reflog by pretending that the parent of each commit is the previous reflog entry. This causes a number of user-visible oddities, as documented in t1414 (and the commit message which introduced it). We can fix all of them in one go by replacing the fake-reflog system with a much simpler one: just keeping a list of reflogs to show, and walking through them entry by entry. The implementation is fairly straight-forward, but there are a few items to note: 1. We obviously must skip calling add_parents_to_list() when we are traversing reflogs, since we do not want to walk the original parents at all. As a result, we must call try_to_simplify_commit() ourselves. There are other parts of add_parents_to_list() we skip, as well, but none of them should matter for a reflog traversal: - We do not allow UNINTERESTING commits, nor symmetric ranges (and we bail when these are used with "-g"). - Using --source makes no sense, since we aren't traversing. The reflog selector shows the same information with more detail. - Using --first-parent is still sensible, since you may want to see the first-parent diff for each entry. But since we're not traversing, we don't need to cull the parent list here. 2. Since we now just walk the reflog entries themselves, rather than starting with the ref tip, we now look at the "new" field of each entry rather than the "old" (i.e., we are showing entries, not faking parents). This removes all of the tricky logic around skipping past root commits. But note that we have no way to show an entry with the null sha1 in its "new" field (because such a commit obviously does not exist). Normally this would not happen, since we delete reflogs along with refs, but there is one special case. When we rename the currently checked out branch, we write two reflog entries into the HEAD log: one where the commit goes away, and another where it comes back. Prior to this commit, we show both entries with identical reflog messages. After this commit, we show only the "comes back" entry. See the update in t3200 which demonstrates this. Arguably either is fine, as the whole double-entry thing is a bit hacky in the first place. And until a recent fix, we truncated the traversal in such a case anyway, which was _definitely_ wrong. 3. We show individual reflogs in order, but choose which reflog to show at each stage based on which has the most recent timestamp. This interleaves the output from multiple reflogs based on date order, which is probably what you'd want with limiting like "-n 30". Note that the implementation aims for simplicity. It does a linear walk over the reflog queue for each commit it pulls, which may perform badly if you interleave an enormous number of reflogs. That seems like an unlikely use case; if we did want to handle it, we could probably keep a priority queue of reflogs, ordered by the timestamp of their current tip entry. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7f97de5 commit d08565b

File tree

5 files changed

+75
-108
lines changed

5 files changed

+75
-108
lines changed

reflog-walk.c

Lines changed: 51 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -94,45 +94,6 @@ static int get_reflog_recno_by_time(struct complete_reflogs *array,
9494
return -1;
9595
}
9696

97-
struct commit_info_lifo {
98-
struct commit_info {
99-
struct commit *commit;
100-
void *util;
101-
} *items;
102-
int nr, alloc;
103-
};
104-
105-
static struct commit_info *get_commit_info(struct commit *commit,
106-
struct commit_info_lifo *lifo, int pop)
107-
{
108-
int i;
109-
for (i = 0; i < lifo->nr; i++)
110-
if (lifo->items[i].commit == commit) {
111-
struct commit_info *result = &lifo->items[i];
112-
if (pop) {
113-
if (i + 1 < lifo->nr)
114-
memmove(lifo->items + i,
115-
lifo->items + i + 1,
116-
(lifo->nr - i) *
117-
sizeof(struct commit_info));
118-
lifo->nr--;
119-
}
120-
return result;
121-
}
122-
return NULL;
123-
}
124-
125-
static void add_commit_info(struct commit *commit, void *util,
126-
struct commit_info_lifo *lifo)
127-
{
128-
struct commit_info *info;
129-
ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
130-
info = lifo->items + lifo->nr;
131-
info->commit = commit;
132-
info->util = util;
133-
lifo->nr++;
134-
}
135-
13697
struct commit_reflog {
13798
int recno;
13899
enum selector_type {
@@ -144,7 +105,8 @@ struct commit_reflog {
144105
};
145106

146107
struct reflog_walk_info {
147-
struct commit_info_lifo reflogs;
108+
struct commit_reflog **logs;
109+
size_t nr, alloc;
148110
struct string_list complete_reflogs;
149111
struct commit_reflog *last_commit_reflog;
150112
};
@@ -233,52 +195,10 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
233195
commit_reflog->selector = selector;
234196
commit_reflog->reflogs = reflogs;
235197

236-
add_commit_info(commit, commit_reflog, &info->reflogs);
237-
return 0;
238-
}
239-
240-
void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
241-
{
242-
struct commit_info *commit_info =
243-
get_commit_info(commit, &info->reflogs, 0);
244-
struct commit_reflog *commit_reflog;
245-
struct object *logobj;
246-
struct reflog_info *reflog;
247-
248-
info->last_commit_reflog = NULL;
249-
if (!commit_info)
250-
return;
251-
252-
commit_reflog = commit_info->util;
253-
if (commit_reflog->recno < 0) {
254-
commit->parents = NULL;
255-
return;
256-
}
257-
info->last_commit_reflog = commit_reflog;
258-
259-
do {
260-
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
261-
commit_reflog->recno--;
262-
logobj = parse_object(&reflog->ooid);
263-
} while (commit_reflog->recno && (logobj && logobj->type != OBJ_COMMIT));
264-
265-
if (!logobj && commit_reflog->recno >= 0 && is_null_oid(&reflog->ooid)) {
266-
/* a root commit, but there are still more entries to show */
267-
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
268-
logobj = parse_object(&reflog->noid);
269-
if (!logobj)
270-
logobj = parse_object(&reflog->ooid);
271-
}
272-
273-
if (!logobj || logobj->type != OBJ_COMMIT) {
274-
commit_info->commit = NULL;
275-
commit->parents = NULL;
276-
return;
277-
}
278-
commit_info->commit = (struct commit *)logobj;
198+
ALLOC_GROW(info->logs, info->nr + 1, info->alloc);
199+
info->logs[info->nr++] = commit_reflog;
279200

280-
commit->parents = xcalloc(1, sizeof(struct commit_list));
281-
commit->parents->item = commit_info->commit;
201+
return 0;
282202
}
283203

284204
void get_reflog_selector(struct strbuf *sb,
@@ -368,5 +288,50 @@ void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
368288

369289
int reflog_walk_empty(struct reflog_walk_info *info)
370290
{
371-
return !info || !info->reflogs.nr;
291+
return !info || !info->nr;
292+
}
293+
294+
static struct commit *next_reflog_commit(struct commit_reflog *log)
295+
{
296+
for (; log->recno >= 0; log->recno--) {
297+
struct reflog_info *entry = &log->reflogs->items[log->recno];
298+
struct object *obj = parse_object(&entry->noid);
299+
300+
if (obj && obj->type == OBJ_COMMIT)
301+
return (struct commit *)obj;
302+
}
303+
return NULL;
304+
}
305+
306+
static timestamp_t log_timestamp(struct commit_reflog *log)
307+
{
308+
return log->reflogs->items[log->recno].timestamp;
309+
}
310+
311+
struct commit *next_reflog_entry(struct reflog_walk_info *walk)
312+
{
313+
struct commit_reflog *best = NULL;
314+
struct commit *best_commit = NULL;
315+
size_t i;
316+
317+
for (i = 0; i < walk->nr; i++) {
318+
struct commit_reflog *log = walk->logs[i];
319+
struct commit *commit = next_reflog_commit(log);
320+
321+
if (!commit)
322+
continue;
323+
324+
if (!best || log_timestamp(log) > log_timestamp(best)) {
325+
best = log;
326+
best_commit = commit;
327+
}
328+
}
329+
330+
if (best) {
331+
best->recno--;
332+
walk->last_commit_reflog = best;
333+
return best_commit;
334+
}
335+
336+
return NULL;
372337
}

reflog-walk.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ struct reflog_walk_info;
88
extern void init_reflog_walk(struct reflog_walk_info **info);
99
extern int add_reflog_for_walk(struct reflog_walk_info *info,
1010
struct commit *commit, const char *name);
11-
extern void fake_reflog_parent(struct reflog_walk_info *info,
12-
struct commit *commit);
1311
extern void show_reflog_message(struct reflog_walk_info *info, int,
1412
const struct date_mode *, int force_date);
1513
extern void get_reflog_message(struct strbuf *sb,
@@ -22,4 +20,6 @@ extern void get_reflog_selector(struct strbuf *sb,
2220

2321
extern int reflog_walk_empty(struct reflog_walk_info *walk);
2422

23+
struct commit *next_reflog_entry(struct reflog_walk_info *reflog_info);
24+
2525
#endif

revision.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,14 @@ static void add_pending_object_with_path(struct rev_info *revs,
148148
if (revs->reflog_info && obj->type == OBJ_COMMIT) {
149149
struct strbuf buf = STRBUF_INIT;
150150
int len = interpret_branch_name(name, 0, &buf, 0);
151-
int st;
152151

153152
if (0 < len && name[len] && buf.len)
154153
strbuf_addstr(&buf, name + len);
155-
st = add_reflog_for_walk(revs->reflog_info,
156-
(struct commit *)obj,
157-
buf.buf[0] ? buf.buf: name);
154+
add_reflog_for_walk(revs->reflog_info,
155+
(struct commit *)obj,
156+
buf.buf[0] ? buf.buf: name);
158157
strbuf_release(&buf);
159-
if (st)
160-
return;
158+
return; /* do not add the commit itself */
161159
}
162160
add_object_array_with_path(obj, name, &revs->pending, mode, path);
163161
}
@@ -3112,16 +3110,18 @@ static void track_linear(struct rev_info *revs, struct commit *commit)
31123110
static struct commit *get_revision_1(struct rev_info *revs)
31133111
{
31143112
while (1) {
3115-
struct commit *commit = pop_commit(&revs->commits);
3113+
struct commit *commit;
3114+
3115+
if (revs->reflog_info)
3116+
commit = next_reflog_entry(revs->reflog_info);
3117+
else
3118+
commit = pop_commit(&revs->commits);
31163119

31173120
if (!commit)
31183121
return NULL;
31193122

3120-
if (revs->reflog_info) {
3121-
save_parents(revs, commit);
3122-
fake_reflog_parent(revs->reflog_info, commit);
3123+
if (revs->reflog_info)
31233124
commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3124-
}
31253125

31263126
/*
31273127
* If we haven't done the list limiting, we need to look at
@@ -3132,7 +3132,10 @@ static struct commit *get_revision_1(struct rev_info *revs)
31323132
if (revs->max_age != -1 &&
31333133
(commit->date < revs->max_age))
31343134
continue;
3135-
if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3135+
3136+
if (revs->reflog_info)
3137+
try_to_simplify_commit(revs, commit);
3138+
else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
31363139
if (!revs->ignore_missing_links)
31373140
die("Failed to traverse parents of commit %s",
31383141
oid_to_hex(&commit->object.oid));

t/t1414-reflog-walk.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ test_expect_success 'reflog walk shows expected logs' '
3434
test_cmp expect.all actual
3535
'
3636

37-
test_expect_failure 'reflog can limit with --no-merges' '
37+
test_expect_success 'reflog can limit with --no-merges' '
3838
grep -v merge expect.all >expect &&
3939
do_walk --no-merges >actual &&
4040
test_cmp expect actual
4141
'
4242

43-
test_expect_failure 'reflog can limit with pathspecs' '
43+
test_expect_success 'reflog can limit with pathspecs' '
4444
grep two expect.all >expect &&
4545
do_walk -- two.t >actual &&
4646
test_cmp expect actual
4747
'
4848

49-
test_expect_failure 'pathspec limiting handles merges' '
49+
test_expect_success 'pathspec limiting handles merges' '
5050
# we pick up:
5151
# - the initial commit of one
5252
# - the checkout back to commit one
@@ -56,14 +56,14 @@ test_expect_failure 'pathspec limiting handles merges' '
5656
test_cmp expect actual
5757
'
5858

59-
test_expect_failure '--parents shows true parents' '
59+
test_expect_success '--parents shows true parents' '
6060
# convert newlines to spaces
6161
echo $(git rev-parse HEAD HEAD^1 HEAD^2) >expect &&
6262
git rev-list -g --parents -1 HEAD >actual &&
6363
test_cmp expect actual
6464
'
6565

66-
test_expect_failure 'walking multiple reflogs shows all' '
66+
test_expect_success 'walking multiple reflogs shows all' '
6767
# We expect to see all entries for all reflogs, but interleaved by
6868
# date, with order on the command line breaking ties. We
6969
# can use "sort" on the separate lists to generate this,
@@ -91,7 +91,7 @@ test_expect_success 'date-limiting does not interfere with other logs' '
9191
test_cmp expect.all actual
9292
'
9393

94-
test_expect_failure 'walk prefers reflog to ref tip' '
94+
test_expect_success 'walk prefers reflog to ref tip' '
9595
head=$(git rev-parse HEAD) &&
9696
one=$(git rev-parse one) &&
9797
ident="$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE" &&

t/t3200-branch.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,9 @@ test_expect_success 'resulting reflog can be shown by log -g' '
166166
oid=$(git rev-parse HEAD) &&
167167
cat >expect <<-EOF &&
168168
HEAD@{0} $oid $msg
169-
HEAD@{1} $oid $msg
170169
HEAD@{2} $oid checkout: moving from foo to baz
171170
EOF
172-
git log -g --format="%gd %H %gs" -3 HEAD >actual &&
171+
git log -g --format="%gd %H %gs" -2 HEAD >actual &&
173172
test_cmp expect actual
174173
'
175174

0 commit comments

Comments
 (0)