3 test_description
='various tests of reflog walk (log -g) behavior'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK
=true
10 test_expect_success
'set up some reflog entries' '
13 git checkout -b side HEAD^ &&
15 git merge --no-commit main &&
16 echo evil-merge-content >>one.t &&
18 git commit --no-edit -a
22 git log
-g --format="%gd %gs" "$@"
25 test_expect_success
'set up expected reflog' '
26 cat >expect.all <<-EOF
27 HEAD@{0} commit (merge): Merge branch ${SQ}main${SQ} into side
28 HEAD@{1} commit: three
29 HEAD@{2} checkout: moving from main to side
31 HEAD@{4} commit (initial): one
35 test_expect_success
'reflog walk shows expected logs' '
37 test_cmp expect.all actual
40 test_expect_success
'reflog can limit with --no-merges' '
41 grep -v merge expect.all >expect &&
42 do_walk --no-merges >actual &&
43 test_cmp expect actual
46 test_expect_success
'reflog can limit with pathspecs' '
47 grep two expect.all >expect &&
48 do_walk -- two.t >actual &&
49 test_cmp expect actual
52 test_expect_success
'pathspec limiting handles merges' '
54 # - the initial commit of one
55 # - the checkout back to commit one
56 # - the evil merge which touched one
57 sed -n "1p;3p;5p" expect.all >expect &&
58 do_walk -- one.t >actual &&
59 test_cmp expect actual
62 test_expect_success
'--parents shows true parents' '
63 # convert newlines to spaces
64 echo $(git rev-parse HEAD HEAD^1 HEAD^2) >expect &&
65 git rev-list -g --parents -1 HEAD >actual &&
66 test_cmp expect actual
69 test_expect_success
'walking multiple reflogs shows all' '
70 # We expect to see all entries for all reflogs, but interleaved by
71 # date, with order on the command line breaking ties. We
72 # can use "sort" on the separate lists to generate this,
73 # but note two tricks:
75 # 1. We use "{" as the delimiter, which lets us skip to the reflog
76 # date specifier as our second field, and then our "-n" numeric
77 # sort ignores the bits after the timestamp.
79 # 2. POSIX leaves undefined whether this is a stable sort or not. So
80 # we use "-k 1" to ensure that we see HEAD before main before
81 # side when breaking ties.
83 do_walk --date=unix HEAD &&
84 do_walk --date=unix side &&
85 do_walk --date=unix main
87 sort -t "{" -k 2nr -k 1 <expect.raw >expect &&
88 do_walk --date=unix HEAD main side >actual &&
89 test_cmp expect actual
92 test_expect_success
'date-limiting does not interfere with other logs' '
93 do_walk HEAD@{1979-01-01} HEAD >actual &&
94 test_cmp expect.all actual
97 test_expect_success
'min/max age uses entry date to limit' '
98 # Flip between commits one and two so each ref update actually
99 # does something (and does not get optimized out). We know
100 # that the timestamps of those commits will be before our "min".
102 git update-ref -m before refs/heads/minmax one &&
106 git update-ref -m min refs/heads/minmax two &&
110 git update-ref -m max refs/heads/minmax one &&
113 git update-ref -m after refs/heads/minmax two &&
115 cat >expect <<-\EOF &&
119 git log -g --since=$min --until=$max --format=%gs minmax >actual &&
120 test_cmp expect actual
123 # Create a situation where the reflog and ref database disagree about the latest
125 test_expect_success
'walk prefers reflog to ref tip' '
128 git reflog delete HEAD@{0} &&
129 head=$(git rev-parse HEAD) &&
130 git rev-parse A >expect &&
131 git log -g --format=%H -1 >actual &&
132 test_cmp expect actual
135 test_expect_success
'rev-list -g complains when there are no reflogs' '
136 test_must_fail git rev-list -g