Merge branch 'jk/fast-import-history-bugfix'
[git] / t / t1415-worktree-refs.sh
1 #!/bin/sh
2
3 test_description='per-worktree refs'
4
5 . ./test-lib.sh
6
7 test_expect_success 'setup' '
8         test_commit initial &&
9         test_commit wt1 &&
10         test_commit wt2 &&
11         git worktree add wt1 wt1 &&
12         git worktree add wt2 wt2 &&
13         git checkout initial &&
14         git update-ref refs/worktree/foo HEAD &&
15         git -C wt1 update-ref refs/worktree/foo HEAD &&
16         git -C wt2 update-ref refs/worktree/foo HEAD
17 '
18
19 test_expect_success 'refs/worktree must not be packed' '
20         git pack-refs --all &&
21         test_path_is_missing .git/refs/tags/wt1 &&
22         test_path_is_file .git/refs/worktree/foo &&
23         test_path_is_file .git/worktrees/wt1/refs/worktree/foo &&
24         test_path_is_file .git/worktrees/wt2/refs/worktree/foo
25 '
26
27 test_expect_success 'refs/worktree are per-worktree' '
28         test_cmp_rev worktree/foo initial &&
29         ( cd wt1 && test_cmp_rev worktree/foo wt1 ) &&
30         ( cd wt2 && test_cmp_rev worktree/foo wt2 )
31 '
32
33 test_expect_success 'resolve main-worktree/HEAD' '
34         test_cmp_rev main-worktree/HEAD initial &&
35         ( cd wt1 && test_cmp_rev main-worktree/HEAD initial ) &&
36         ( cd wt2 && test_cmp_rev main-worktree/HEAD initial )
37 '
38
39 test_expect_success 'ambiguous main-worktree/HEAD' '
40         mkdir -p .git/refs/heads/main-worktree &&
41         test_when_finished rm -f .git/refs/heads/main-worktree/HEAD &&
42         cp .git/HEAD .git/refs/heads/main-worktree/HEAD &&
43         git rev-parse main-worktree/HEAD 2>warn &&
44         grep "main-worktree/HEAD.*ambiguous" warn
45 '
46
47 test_expect_success 'resolve worktrees/xx/HEAD' '
48         test_cmp_rev worktrees/wt1/HEAD wt1 &&
49         ( cd wt1 && test_cmp_rev worktrees/wt1/HEAD wt1 ) &&
50         ( cd wt2 && test_cmp_rev worktrees/wt1/HEAD wt1 )
51 '
52
53 test_expect_success 'ambiguous worktrees/xx/HEAD' '
54         mkdir -p .git/refs/heads/worktrees/wt1 &&
55         test_when_finished rm -f .git/refs/heads/worktrees/wt1/HEAD &&
56         cp .git/HEAD .git/refs/heads/worktrees/wt1/HEAD &&
57         git rev-parse worktrees/wt1/HEAD 2>warn &&
58         grep "worktrees/wt1/HEAD.*ambiguous" warn
59 '
60
61 test_expect_success 'reflog of main-worktree/HEAD' '
62         git reflog HEAD | sed "s/HEAD/main-worktree\/HEAD/" >expected &&
63         git reflog main-worktree/HEAD >actual &&
64         test_cmp expected actual &&
65         git -C wt1 reflog main-worktree/HEAD >actual.wt1 &&
66         test_cmp expected actual.wt1
67 '
68
69 test_expect_success 'reflog of worktrees/xx/HEAD' '
70         git -C wt2 reflog HEAD | sed "s/HEAD/worktrees\/wt2\/HEAD/" >expected &&
71         git reflog worktrees/wt2/HEAD >actual &&
72         test_cmp expected actual &&
73         git -C wt1 reflog worktrees/wt2/HEAD >actual.wt1 &&
74         test_cmp expected actual.wt1 &&
75         git -C wt2 reflog worktrees/wt2/HEAD >actual.wt2 &&
76         test_cmp expected actual.wt2
77 '
78
79 test_expect_success 'for-each-ref from main repo' '
80         mkdir fer1 &&
81         git -C fer1 init repo &&
82         test_commit -C fer1/repo initial &&
83         git -C fer1/repo worktree add ../second &&
84         git -C fer1/repo update-ref refs/bisect/main HEAD &&
85         git -C fer1/repo update-ref refs/rewritten/main HEAD &&
86         git -C fer1/repo update-ref refs/worktree/main HEAD &&
87         git -C fer1/repo for-each-ref --format="%(refname)" | grep main >actual &&
88         cat >expected <<-\EOF &&
89         refs/bisect/main
90         refs/rewritten/main
91         refs/worktree/main
92         EOF
93         test_cmp expected actual
94 '
95
96 test_expect_success 'for-each-ref from linked repo' '
97         mkdir fer2 &&
98         git -C fer2 init repo &&
99         test_commit -C fer2/repo initial &&
100         git -C fer2/repo worktree add ../second &&
101         git -C fer2/second update-ref refs/bisect/second HEAD &&
102         git -C fer2/second update-ref refs/rewritten/second HEAD &&
103         git -C fer2/second update-ref refs/worktree/second HEAD &&
104         git -C fer2/second for-each-ref --format="%(refname)" | grep second >actual &&
105         cat >expected <<-\EOF &&
106         refs/bisect/second
107         refs/heads/second
108         refs/rewritten/second
109         refs/worktree/second
110         EOF
111         test_cmp expected actual
112 '
113
114 test_done