5f7d45b7b7fa91a497d6f8d96a7235fc0024919e
   1#!/bin/sh
   2
   3test_description='test git worktree move, remove, lock and unlock'
   4
   5. ./test-lib.sh
   6
   7test_expect_success 'setup' '
   8        test_commit init &&
   9        git worktree add source &&
  10        git worktree list --porcelain >out &&
  11        grep "^worktree" out >actual &&
  12        cat <<-EOF >expected &&
  13        worktree $(pwd)
  14        worktree $(pwd)/source
  15        EOF
  16        test_cmp expected actual
  17'
  18
  19test_expect_success 'lock main worktree' '
  20        test_must_fail git worktree lock .
  21'
  22
  23test_expect_success 'lock linked worktree' '
  24        git worktree lock --reason hahaha source &&
  25        echo hahaha >expected &&
  26        test_cmp expected .git/worktrees/source/locked
  27'
  28
  29test_expect_success 'lock linked worktree from another worktree' '
  30        rm .git/worktrees/source/locked &&
  31        git worktree add elsewhere &&
  32        git -C elsewhere worktree lock --reason hahaha ../source &&
  33        echo hahaha >expected &&
  34        test_cmp expected .git/worktrees/source/locked
  35'
  36
  37test_expect_success 'lock worktree twice' '
  38        test_must_fail git worktree lock source &&
  39        echo hahaha >expected &&
  40        test_cmp expected .git/worktrees/source/locked
  41'
  42
  43test_expect_success 'lock worktree twice (from the locked worktree)' '
  44        test_must_fail git -C source worktree lock . &&
  45        echo hahaha >expected &&
  46        test_cmp expected .git/worktrees/source/locked
  47'
  48
  49test_expect_success 'unlock main worktree' '
  50        test_must_fail git worktree unlock .
  51'
  52
  53test_expect_success 'unlock linked worktree' '
  54        git worktree unlock source &&
  55        test_path_is_missing .git/worktrees/source/locked
  56'
  57
  58test_expect_success 'unlock worktree twice' '
  59        test_must_fail git worktree unlock source &&
  60        test_path_is_missing .git/worktrees/source/locked
  61'
  62
  63test_expect_success 'move non-worktree' '
  64        mkdir abc &&
  65        test_must_fail git worktree move abc def
  66'
  67
  68test_expect_success 'move locked worktree' '
  69        git worktree lock source &&
  70        test_when_finished "git worktree unlock source" &&
  71        test_must_fail git worktree move source destination
  72'
  73
  74test_expect_success 'move worktree' '
  75        git worktree move source destination &&
  76        test_path_is_missing source &&
  77        git worktree list --porcelain >out &&
  78        grep "^worktree.*/destination$" out &&
  79        ! grep "^worktree.*/source$" out &&
  80        git -C destination log --format=%s >actual2 &&
  81        echo init >expected2 &&
  82        test_cmp expected2 actual2
  83'
  84
  85test_expect_success 'move main worktree' '
  86        test_must_fail git worktree move . def
  87'
  88
  89test_expect_success 'move worktree to another dir' '
  90        mkdir some-dir &&
  91        git worktree move destination some-dir &&
  92        test_when_finished "git worktree move some-dir/destination destination" &&
  93        test_path_is_missing destination &&
  94        git worktree list --porcelain >out &&
  95        grep "^worktree.*/some-dir/destination$" out &&
  96        git -C some-dir/destination log --format=%s >actual2 &&
  97        echo init >expected2 &&
  98        test_cmp expected2 actual2
  99'
 100
 101test_expect_success 'remove main worktree' '
 102        test_must_fail git worktree remove .
 103'
 104
 105test_expect_success 'remove locked worktree' '
 106        git worktree lock destination &&
 107        test_when_finished "git worktree unlock destination" &&
 108        test_must_fail git worktree remove destination
 109'
 110
 111test_expect_success 'remove worktree with dirty tracked file' '
 112        echo dirty >>destination/init.t &&
 113        test_when_finished "git -C destination checkout init.t" &&
 114        test_must_fail git worktree remove destination
 115'
 116
 117test_expect_success 'remove worktree with untracked file' '
 118        : >destination/untracked &&
 119        test_must_fail git worktree remove destination
 120'
 121
 122test_expect_success 'force remove worktree with untracked file' '
 123        git worktree remove --force destination &&
 124        test_path_is_missing destination
 125'
 126
 127test_expect_success 'remove missing worktree' '
 128        git worktree add to-be-gone &&
 129        test -d .git/worktrees/to-be-gone &&
 130        mv to-be-gone gone &&
 131        git worktree remove to-be-gone &&
 132        test_path_is_missing .git/worktrees/to-be-gone
 133'
 134
 135test_expect_success 'NOT remove missing-but-locked worktree' '
 136        git worktree add gone-but-locked &&
 137        git worktree lock gone-but-locked &&
 138        test -d .git/worktrees/gone-but-locked &&
 139        mv gone-but-locked really-gone-now &&
 140        test_must_fail git worktree remove gone-but-locked &&
 141        test_path_is_dir .git/worktrees/gone-but-locked
 142'
 143
 144test_done