refs/files: sort reflogs returned by the reflog iterator

We use a directory iterator to return reflogs via the reflog iterator. This iterator returns entries in the same order as readdir(3P) would and will thus yield reflogs with no discernible order. Set the new `DIR_ITERATOR_SORTED` flag that was introduced in the preceding commit so that the order is deterministic. While the effect of this can only been observed in a test tool, a subsequent commit will start to expose this functionality to users via a new `git reflog list` subcommand. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 21, 2024 at 13:37 UTC e69e8ffef7369ca0d24c570d2657e41cf5e45936
4 files changed +6 -6
refs/files-backend.c
+2 -2
@@ -2193,7 +2193,7 @@ static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
2193
2194 strbuf_addf(&sb, "%s/logs", gitdir);
2195
2196 - diter = dir_iterator_begin(sb.buf, 0);
2196 + diter = dir_iterator_begin(sb.buf, DIR_ITERATOR_SORTED);
2197 if (!diter) {
2198 strbuf_release(&sb);
2199 return empty_ref_iterator_begin();
@@ -2202,7 +2202,7 @@ static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
2202 CALLOC_ARRAY(iter, 1);
2203 ref_iterator = &iter->base;
2204
2205 - base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0);
2205 + base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 1);
2206 iter->dir_iterator = diter;
2207 iter->ref_store = ref_store;
2208 strbuf_release(&sb);
t/t0600-reffiles-backend.sh
+2 -2
@@ -287,7 +287,7 @@ test_expect_success 'for_each_reflog()' '
287 mkdir -p .git/worktrees/wt/logs/refs/bisect &&
288 echo $ZERO_OID > .git/worktrees/wt/logs/refs/bisect/wt-random &&
289
290 - $RWT for-each-reflog | cut -d" " -f 2- | sort >actual &&
290 + $RWT for-each-reflog | cut -d" " -f 2- >actual &&
291 cat >expected <<-\EOF &&
292 HEAD 0x1
293 PSEUDO-WT 0x0
@@ -297,7 +297,7 @@ test_expect_success 'for_each_reflog()' '
297 EOF
298 test_cmp expected actual &&
299
300 - $RMAIN for-each-reflog | cut -d" " -f 2- | sort >actual &&
300 + $RMAIN for-each-reflog | cut -d" " -f 2- >actual &&
301 cat >expected <<-\EOF &&
302 HEAD 0x1
303 PSEUDO-MAIN 0x0
t/t1405-main-ref-store.sh
+1 -1
@@ -74,7 +74,7 @@ test_expect_success 'verify_ref(new-main)' '
74 '
75
76 test_expect_success 'for_each_reflog()' '
77 - $RUN for-each-reflog | sort -k2 | cut -d" " -f 2- >actual &&
77 + $RUN for-each-reflog | cut -d" " -f 2- >actual &&
78 cat >expected <<-\EOF &&
79 HEAD 0x1
80 refs/heads/main 0x0
t/t1406-submodule-ref-store.sh
+1 -1
@@ -63,7 +63,7 @@ test_expect_success 'verify_ref(new-main)' '
63 '
64
65 test_expect_success 'for_each_reflog()' '
66 - $RUN for-each-reflog | sort | cut -d" " -f 2- >actual &&
66 + $RUN for-each-reflog | cut -d" " -f 2- >actual &&
67 cat >expected <<-\EOF &&
68 HEAD 0x1
69 refs/heads/main 0x0