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
9 test_expect_success
'set up some reflog entries' '
12 git checkout -b side HEAD^ &&
14 git merge --no-commit main &&
15 echo evil-merge-content >>one.t &&
17 git commit --no-edit -a
21 git log
-g --format="%gd %gs" "$@"
24 test_expect_success
'set up expected reflog' '
25 cat >expect.all <<-EOF
26 HEAD@{0} commit (merge): Merge branch ${SQ}main${SQ} into side
27 HEAD@{1} commit: three
28 HEAD@{2} checkout: moving from main to side
30 HEAD@{4} commit (initial): one
34 test_expect_success
'reflog walk shows expected logs' '
36 test_cmp expect.all actual
39 test_expect_success
'reflog can limit with --no-merges' '
40 grep -v merge expect.all >expect &&
41 do_walk --no-merges >actual &&
42 test_cmp expect actual
45 test_expect_success
'reflog can limit with pathspecs' '
46 grep two expect.all >expect &&
47 do_walk -- two.t >actual &&
48 test_cmp expect actual
51 test_expect_success
'pathspec limiting handles merges' '
53 # - the initial commit of one
54 # - the checkout back to commit one
55 # - the evil merge which touched one
56 sed -n "1p;3p;5p" expect.all >expect &&
57 do_walk -- one.t >actual &&
58 test_cmp expect actual
61 test_expect_success
'--parents shows true parents' '
62 # convert newlines to spaces
63 echo $(git rev-parse HEAD HEAD^1 HEAD^2) >expect &&
64 git rev-list -g --parents -1 HEAD >actual &&
65 test_cmp expect actual
68 test_expect_success
'walking multiple reflogs shows all' '
69 # We expect to see all entries for all reflogs, but interleaved by
70 # date, with order on the command line breaking ties. We
71 # can use "sort" on the separate lists to generate this,
72 # but note two tricks:
74 # 1. We use "{" as the delimiter, which lets us skip to the reflog
75 # date specifier as our second field, and then our "-n" numeric
76 # sort ignores the bits after the timestamp.
78 # 2. POSIX leaves undefined whether this is a stable sort or not. So
79 # we use "-k 1" to ensure that we see HEAD before main before
80 # side when breaking ties.
82 do_walk --date=unix HEAD &&
83 do_walk --date=unix side &&
84 do_walk --date=unix main
86 sort -t "{" -k 2nr -k 1 <expect.raw >expect &&
87 do_walk --date=unix HEAD main side >actual &&
88 test_cmp expect actual
91 test_expect_success
'date-limiting does not interfere with other logs' '
92 do_walk HEAD@{1979-01-01} HEAD >actual &&
93 test_cmp expect.all actual
96 test_expect_success
'min/max age uses entry date to limit' '
97 # Flip between commits one and two so each ref update actually
98 # does something (and does not get optimized out). We know
99 # that the timestamps of those commits will be before our "min".
101 git update-ref -m before refs/heads/minmax one &&
105 git update-ref -m min refs/heads/minmax two &&
109 git update-ref -m max refs/heads/minmax one &&
112 git update-ref -m after refs/heads/minmax two &&
114 cat >expect <<-\EOF &&
118 git log -g --since=$min --until=$max --format=%gs minmax >actual &&
119 test_cmp expect actual
122 # Create a situation where the reflog and ref database disagree about the latest
124 test_expect_success
'walk prefers reflog to ref tip' '
127 git reflog delete HEAD@{0} &&
128 head=$(git rev-parse HEAD) &&
129 git rev-parse A >expect &&
130 git log -g --format=%H -1 >actual &&
131 test_cmp expect actual
134 test_expect_success
'rev-list -g complains when there are no reflogs' '
135 test_must_fail git rev-list -g