Skip to content

Commit 9d21a96

Browse files
committed
Merge branch 'jk/reflog-walk-maint' into maint
After "git branch --move" of the currently checked out branch, the code to walk the reflog of HEAD via "log -g" and friends incorrectly stopped at the reflog entry that records the renaming of the branch. * jk/reflog-walk-maint: reflog-walk: include all fields when freeing complete_reflogs reflog-walk: don't free reflogs added to cache reflog-walk: duplicate strings in complete_reflogs list reflog-walk: skip over double-null oid due to HEAD rename
2 parents 699d47e + e30d463 commit 9d21a96

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

reflog-walk.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ static int read_one_reflog(struct object_id *ooid, struct object_id *noid,
3838
return 0;
3939
}
4040

41+
static void free_complete_reflog(struct complete_reflogs *array)
42+
{
43+
int i;
44+
45+
if (!array)
46+
return;
47+
48+
for (i = 0; i < array->nr; i++) {
49+
free(array->items[i].email);
50+
free(array->items[i].message);
51+
}
52+
free(array->items);
53+
free(array->ref);
54+
free(array);
55+
}
56+
4157
static struct complete_reflogs *read_complete_reflog(const char *ref)
4258
{
4359
struct complete_reflogs *reflogs =
@@ -136,6 +152,7 @@ struct reflog_walk_info {
136152
void init_reflog_walk(struct reflog_walk_info **info)
137153
{
138154
*info = xcalloc(1, sizeof(struct reflog_walk_info));
155+
(*info)->complete_reflogs.strdup_strings = 1;
139156
}
140157

141158
int add_reflog_for_walk(struct reflog_walk_info *info,
@@ -188,20 +205,14 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
188205
if (ret > 1)
189206
free(b);
190207
else if (ret == 1) {
191-
if (reflogs) {
192-
free(reflogs->ref);
193-
free(reflogs);
194-
}
208+
free_complete_reflog(reflogs);
195209
free(branch);
196210
branch = b;
197211
reflogs = read_complete_reflog(branch);
198212
}
199213
}
200214
if (!reflogs || reflogs->nr == 0) {
201-
if (reflogs) {
202-
free(reflogs->ref);
203-
free(reflogs);
204-
}
215+
free_complete_reflog(reflogs);
205216
free(branch);
206217
return -1;
207218
}
@@ -214,10 +225,6 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
214225
if (recno < 0) {
215226
commit_reflog->recno = get_reflog_recno_by_time(reflogs, timestamp);
216227
if (commit_reflog->recno < 0) {
217-
if (reflogs) {
218-
free(reflogs->ref);
219-
free(reflogs);
220-
}
221228
free(commit_reflog);
222229
return -1;
223230
}
@@ -259,6 +266,8 @@ void fake_reflog_parent(struct reflog_walk_info *info, struct commit *commit)
259266
/* a root commit, but there are still more entries to show */
260267
reflog = &commit_reflog->reflogs->items[commit_reflog->recno];
261268
logobj = parse_object(reflog->noid.hash);
269+
if (!logobj)
270+
logobj = parse_object(reflog->ooid.hash);
262271
}
263272

264273
if (!logobj || logobj->type != OBJ_COMMIT) {

t/t1411-reflog-show.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,14 @@ test_expect_success 'reflog exists works' '
171171
! git reflog exists refs/heads/nonexistent
172172
'
173173

174+
# The behavior with two reflogs is buggy and the output is in flux; for now
175+
# we're just checking that the program works at all without segfaulting.
176+
test_expect_success 'showing multiple reflogs works' '
177+
git log -g HEAD HEAD >actual
178+
'
179+
180+
test_expect_success 'showing multiple reflogs with an old date' '
181+
git log -g HEAD@{1979-01-01} HEAD >actual
182+
'
183+
174184
test_done

t/t3200-branch.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD'
162162
grep "^0\{40\}.*$msg$" .git/logs/HEAD
163163
'
164164

165+
test_expect_success 'resulting reflog can be shown by log -g' '
166+
oid=$(git rev-parse HEAD) &&
167+
cat >expect <<-EOF &&
168+
HEAD@{0} $oid $msg
169+
HEAD@{1} $oid $msg
170+
HEAD@{2} $oid checkout: moving from foo to baz
171+
EOF
172+
git log -g --format="%gd %H %gs" -3 HEAD >actual &&
173+
test_cmp expect actual
174+
'
175+
165176
test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
166177
git checkout master &&
167178
git worktree add -b baz bazdir &&

0 commit comments

Comments
 (0)