worktree: teach "repair" to fix worktree back-links to main worktree
[git/debian.git] / t / t2406-worktree-repair.sh
blobef59cdce9505c4de9eeec79cdf67ca0289be197a
1 #!/bin/sh
3 test_description='test git worktree repair'
5 . ./test-lib.sh
7 test_expect_success setup '
8 test_commit init
11 test_expect_success 'skip missing worktree' '
12 test_when_finished "git worktree prune" &&
13 git worktree add --detach missing &&
14 rm -rf missing &&
15 git worktree repair >out 2>err &&
16 test_must_be_empty out &&
17 test_must_be_empty err
20 test_expect_success 'worktree path not directory' '
21 test_when_finished "git worktree prune" &&
22 git worktree add --detach notdir &&
23 rm -rf notdir &&
24 >notdir &&
25 test_must_fail git worktree repair >out 2>err &&
26 test_must_be_empty out &&
27 test_i18ngrep "not a directory" err
30 test_expect_success "don't clobber .git repo" '
31 test_when_finished "rm -rf repo && git worktree prune" &&
32 git worktree add --detach repo &&
33 rm -rf repo &&
34 test_create_repo repo &&
35 test_must_fail git worktree repair >out 2>err &&
36 test_must_be_empty out &&
37 test_i18ngrep ".git is not a file" err
40 test_corrupt_gitfile () {
41 butcher=$1 &&
42 problem=$2 &&
43 repairdir=${3:-.} &&
44 test_when_finished 'rm -rf corrupt && git worktree prune' &&
45 git worktree add --detach corrupt &&
46 git -C corrupt rev-parse --absolute-git-dir >expect &&
47 eval "$butcher" &&
48 git -C "$repairdir" worktree repair >out 2>err &&
49 test_i18ngrep "$problem" out &&
50 test_must_be_empty err &&
51 git -C corrupt rev-parse --absolute-git-dir >actual &&
52 test_cmp expect actual
55 test_expect_success 'repair missing .git file' '
56 test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken"
59 test_expect_success 'repair bogus .git file' '
60 test_corrupt_gitfile "echo \"gitdir: /nowhere\" >corrupt/.git" \
61 ".git file broken"
64 test_expect_success 'repair incorrect .git file' '
65 test_when_finished "rm -rf other && git worktree prune" &&
66 test_create_repo other &&
67 other=$(git -C other rev-parse --absolute-git-dir) &&
68 test_corrupt_gitfile "echo \"gitdir: $other\" >corrupt/.git" \
69 ".git file incorrect"
72 test_expect_success 'repair .git file from main/.git' '
73 test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" .git
76 test_expect_success 'repair .git file from linked worktree' '
77 test_when_finished "rm -rf other && git worktree prune" &&
78 git worktree add --detach other &&
79 test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" other
82 test_expect_success 'repair .git file from bare.git' '
83 test_when_finished "rm -rf bare.git corrupt && git worktree prune" &&
84 git clone --bare . bare.git &&
85 git -C bare.git worktree add --detach ../corrupt &&
86 git -C corrupt rev-parse --absolute-git-dir >expect &&
87 rm -f corrupt/.git &&
88 git -C bare.git worktree repair &&
89 git -C corrupt rev-parse --absolute-git-dir >actual &&
90 test_cmp expect actual
93 test_done