Merge branch 'dl/cat-file-doc-cleanup'
[git] / t / t2401-worktree-prune.sh
1 #!/bin/sh
2
3 test_description='prune $GIT_DIR/worktrees'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 test_expect_success initialize '
11         git commit --allow-empty -m init
12 '
13
14 test_expect_success 'worktree prune on normal repo' '
15         git worktree prune &&
16         test_must_fail git worktree prune abc
17 '
18
19 test_expect_success 'prune files inside $GIT_DIR/worktrees' '
20         mkdir .git/worktrees &&
21         : >.git/worktrees/abc &&
22         git worktree prune --verbose >actual &&
23         cat >expect <<EOF &&
24 Removing worktrees/abc: not a valid directory
25 EOF
26         test_cmp expect actual &&
27         ! test -f .git/worktrees/abc &&
28         ! test -d .git/worktrees
29 '
30
31 test_expect_success 'prune directories without gitdir' '
32         mkdir -p .git/worktrees/def/abc &&
33         : >.git/worktrees/def/def &&
34         cat >expect <<EOF &&
35 Removing worktrees/def: gitdir file does not exist
36 EOF
37         git worktree prune --verbose >actual &&
38         test_cmp expect actual &&
39         ! test -d .git/worktrees/def &&
40         ! test -d .git/worktrees
41 '
42
43 test_expect_success SANITY 'prune directories with unreadable gitdir' '
44         mkdir -p .git/worktrees/def/abc &&
45         : >.git/worktrees/def/def &&
46         : >.git/worktrees/def/gitdir &&
47         chmod u-r .git/worktrees/def/gitdir &&
48         git worktree prune --verbose >actual &&
49         test_i18ngrep "Removing worktrees/def: unable to read gitdir file" actual &&
50         ! test -d .git/worktrees/def &&
51         ! test -d .git/worktrees
52 '
53
54 test_expect_success 'prune directories with invalid gitdir' '
55         mkdir -p .git/worktrees/def/abc &&
56         : >.git/worktrees/def/def &&
57         : >.git/worktrees/def/gitdir &&
58         git worktree prune --verbose >actual &&
59         test_i18ngrep "Removing worktrees/def: invalid gitdir file" actual &&
60         ! test -d .git/worktrees/def &&
61         ! test -d .git/worktrees
62 '
63
64 test_expect_success 'prune directories with gitdir pointing to nowhere' '
65         mkdir -p .git/worktrees/def/abc &&
66         : >.git/worktrees/def/def &&
67         echo "$(pwd)"/nowhere >.git/worktrees/def/gitdir &&
68         git worktree prune --verbose >actual &&
69         test_i18ngrep "Removing worktrees/def: gitdir file points to non-existent location" actual &&
70         ! test -d .git/worktrees/def &&
71         ! test -d .git/worktrees
72 '
73
74 test_expect_success 'not prune locked checkout' '
75         test_when_finished rm -r .git/worktrees &&
76         mkdir -p .git/worktrees/ghi &&
77         : >.git/worktrees/ghi/locked &&
78         git worktree prune &&
79         test -d .git/worktrees/ghi
80 '
81
82 test_expect_success 'not prune recent checkouts' '
83         test_when_finished rm -r .git/worktrees &&
84         git worktree add jlm HEAD &&
85         test -d .git/worktrees/jlm &&
86         rm -rf jlm &&
87         git worktree prune --verbose --expire=2.days.ago &&
88         test -d .git/worktrees/jlm
89 '
90
91 test_expect_success 'not prune proper checkouts' '
92         test_when_finished rm -r .git/worktrees &&
93         git worktree add --detach "$PWD/nop" main &&
94         git worktree prune &&
95         test -d .git/worktrees/nop
96 '
97
98 test_expect_success 'prune duplicate (linked/linked)' '
99         test_when_finished rm -fr .git/worktrees w1 w2 &&
100         git worktree add --detach w1 &&
101         git worktree add --detach w2 &&
102         sed "s/w2/w1/" .git/worktrees/w2/gitdir >.git/worktrees/w2/gitdir.new &&
103         mv .git/worktrees/w2/gitdir.new .git/worktrees/w2/gitdir &&
104         git worktree prune --verbose >actual &&
105         test_i18ngrep "duplicate entry" actual &&
106         test -d .git/worktrees/w1 &&
107         ! test -d .git/worktrees/w2
108 '
109
110 test_expect_success 'prune duplicate (main/linked)' '
111         test_when_finished rm -fr repo wt &&
112         test_create_repo repo &&
113         test_commit -C repo x &&
114         git -C repo worktree add --detach ../wt &&
115         rm -fr wt &&
116         mv repo wt &&
117         git -C wt worktree prune --verbose >actual &&
118         test_i18ngrep "duplicate entry" actual &&
119         ! test -d .git/worktrees/wt
120 '
121
122 test_done