t / t2027-worktree-list.shon commit Merge branch 'bw/realpath-wo-chdir' (1c16df2)
   1#!/bin/sh
   2
   3test_description='test git worktree list'
   4
   5. ./test-lib.sh
   6
   7test_expect_success 'setup' '
   8        test_commit init
   9'
  10
  11test_expect_success 'rev-parse --git-common-dir on main worktree' '
  12        git rev-parse --git-common-dir >actual &&
  13        echo .git >expected &&
  14        test_cmp expected actual &&
  15        mkdir sub &&
  16        git -C sub rev-parse --git-common-dir >actual2 &&
  17        echo sub/.git >expected2 &&
  18        test_cmp expected2 actual2
  19'
  20
  21test_expect_success '"list" all worktrees from main' '
  22        echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
  23        test_when_finished "rm -rf here && git worktree prune" &&
  24        git worktree add --detach here master &&
  25        echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
  26        git worktree list | sed "s/  */ /g" >actual &&
  27        test_cmp expect actual
  28'
  29
  30test_expect_success '"list" all worktrees from linked' '
  31        echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
  32        test_when_finished "rm -rf here && git worktree prune" &&
  33        git worktree add --detach here master &&
  34        echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
  35        git -C here worktree list | sed "s/  */ /g" >actual &&
  36        test_cmp expect actual
  37'
  38
  39test_expect_success '"list" all worktrees --porcelain' '
  40        echo "worktree $(git rev-parse --show-toplevel)" >expect &&
  41        echo "HEAD $(git rev-parse HEAD)" >>expect &&
  42        echo "branch $(git symbolic-ref HEAD)" >>expect &&
  43        echo >>expect &&
  44        test_when_finished "rm -rf here && git worktree prune" &&
  45        git worktree add --detach here master &&
  46        echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect &&
  47        echo "HEAD $(git rev-parse HEAD)" >>expect &&
  48        echo "detached" >>expect &&
  49        echo >>expect &&
  50        git worktree list --porcelain >actual &&
  51        test_cmp expect actual
  52'
  53
  54test_expect_success 'bare repo setup' '
  55        git init --bare bare1 &&
  56        echo "data" >file1 &&
  57        git add file1 &&
  58        git commit -m"File1: add data" &&
  59        git push bare1 master &&
  60        git reset --hard HEAD^
  61'
  62
  63test_expect_success '"list" all worktrees from bare main' '
  64        test_when_finished "rm -rf there && git -C bare1 worktree prune" &&
  65        git -C bare1 worktree add --detach ../there master &&
  66        echo "$(pwd)/bare1 (bare)" >expect &&
  67        echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
  68        git -C bare1 worktree list | sed "s/  */ /g" >actual &&
  69        test_cmp expect actual
  70'
  71
  72test_expect_success '"list" all worktrees --porcelain from bare main' '
  73        test_when_finished "rm -rf there && git -C bare1 worktree prune" &&
  74        git -C bare1 worktree add --detach ../there master &&
  75        echo "worktree $(pwd)/bare1" >expect &&
  76        echo "bare" >>expect &&
  77        echo >>expect &&
  78        echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect &&
  79        echo "HEAD $(git -C there rev-parse HEAD)" >>expect &&
  80        echo "detached" >>expect &&
  81        echo >>expect &&
  82        git -C bare1 worktree list --porcelain >actual &&
  83        test_cmp expect actual
  84'
  85
  86test_expect_success '"list" all worktrees from linked with a bare main' '
  87        test_when_finished "rm -rf there && git -C bare1 worktree prune" &&
  88        git -C bare1 worktree add --detach ../there master &&
  89        echo "$(pwd)/bare1 (bare)" >expect &&
  90        echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
  91        git -C there worktree list | sed "s/  */ /g" >actual &&
  92        test_cmp expect actual
  93'
  94
  95test_expect_success 'bare repo cleanup' '
  96        rm -rf bare1
  97'
  98
  99test_expect_success 'broken main worktree still at the top' '
 100        git init broken-main &&
 101        (
 102                cd broken-main &&
 103                test_commit new &&
 104                git worktree add linked &&
 105                cat >expected <<-EOF &&
 106                worktree $(pwd)
 107                HEAD $_z40
 108
 109                EOF
 110                cd linked &&
 111                echo "worktree $(pwd)" >expected &&
 112                echo "ref: .broken" >../.git/HEAD &&
 113                git worktree list --porcelain | head -n 3 >actual &&
 114                test_cmp ../expected actual &&
 115                git worktree list | head -n 1 >actual.2 &&
 116                grep -F "(error)" actual.2
 117        )
 118'
 119
 120test_expect_success 'linked worktrees are sorted' '
 121        mkdir sorted &&
 122        git init sorted/main &&
 123        (
 124                cd sorted/main &&
 125                test_tick &&
 126                test_commit new &&
 127                git worktree add ../first &&
 128                git worktree add ../second &&
 129                git worktree list --porcelain | grep ^worktree >actual
 130        ) &&
 131        cat >expected <<-EOF &&
 132        worktree $(pwd)/sorted/main
 133        worktree $(pwd)/sorted/first
 134        worktree $(pwd)/sorted/second
 135        EOF
 136        test_cmp expected sorted/main/actual
 137'
 138
 139test_done