1#!/bin/sh
   2test_description='read-tree -m -u checks working tree files'
   4. ./test-lib.sh
   6. "$TEST_DIRECTORY"/lib-read-tree.sh
   7# two-tree test
   9test_expect_success 'two-way setup' '
  11        mkdir subdir &&
  13        echo >file1 file one &&
  14        echo >file2 file two &&
  15        echo >subdir/file1 file one in subdirectory &&
  16        echo >subdir/file2 file two in subdirectory &&
  17        git update-index --add file1 file2 subdir/file1 subdir/file2 &&
  18        git commit -m initial &&
  19        git branch side &&
  21        git tag -f branch-point &&
  22        echo file2 is not tracked on the master anymore &&
  24        rm -f file2 subdir/file2 &&
  25        git update-index --remove file2 subdir/file2 &&
  26        git commit -a -m "master removes file2 and subdir/file2"
  27'
  28test_expect_success 'two-way not clobbering' '
  30        echo >file2 master creates untracked file2 &&
  32        echo >subdir/file2 master creates untracked subdir/file2 &&
  33        if err=$(read_tree_u_must_succeed -m -u master side 2>&1)
  34        then
  35                echo should have complained
  36                false
  37        else
  38                echo "happy to see $err"
  39        fi
  40'
  41echo file2 >.gitignore
  43test_expect_success 'two-way with incorrect --exclude-per-directory (1)' '
  45        if err=$(read_tree_u_must_succeed -m --exclude-per-directory=.gitignore master side 2>&1)
  47        then
  48                echo should have complained
  49                false
  50        else
  51                echo "happy to see $err"
  52        fi
  53'
  54test_expect_success 'two-way with incorrect --exclude-per-directory (2)' '
  56        if err=$(read_tree_u_must_succeed -m -u --exclude-per-directory=foo --exclude-per-directory=.gitignore master side 2>&1)
  58        then
  59                echo should have complained
  60                false
  61        else
  62                echo "happy to see $err"
  63        fi
  64'
  65test_expect_success 'two-way clobbering a ignored file' '
  67        read_tree_u_must_succeed -m -u --exclude-per-directory=.gitignore master side
  69'
  70rm -f .gitignore
  72# three-tree test
  74test_expect_success 'three-way not complaining on an untracked path in both' '
  76        rm -f file2 subdir/file2 &&
  78        git checkout side &&
  79        echo >file3 file three &&
  80        echo >subdir/file3 file three &&
  81        git update-index --add file3 subdir/file3 &&
  82        git commit -a -m "side adds file3 and removes file2" &&
  83        git checkout master &&
  85        echo >file2 file two is untracked on the master side &&
  86        echo >subdir/file2 file two is untracked on the master side &&
  87        read_tree_u_must_succeed -m -u branch-point master side
  89'
  90test_expect_success 'three-way not clobbering a working tree file' '
  92        git reset --hard &&
  94        rm -f file2 subdir/file2 file3 subdir/file3 &&
  95        git checkout master &&
  96        echo >file3 file three created in master, untracked &&
  97        echo >subdir/file3 file three created in master, untracked &&
  98        if err=$(read_tree_u_must_succeed -m -u branch-point master side 2>&1)
  99        then
 100                echo should have complained
 101                false
 102        else
 103                echo "happy to see $err"
 104        fi
 105'
 106echo >.gitignore file3
 108test_expect_success 'three-way not complaining on an untracked file' '
 110        git reset --hard &&
 112        rm -f file2 subdir/file2 file3 subdir/file3 &&
 113        git checkout master &&
 114        echo >file3 file three created in master, untracked &&
 115        echo >subdir/file3 file three created in master, untracked &&
 116        read_tree_u_must_succeed -m -u --exclude-per-directory=.gitignore branch-point master side
 118'
 119test_expect_success '3-way not overwriting local changes (setup)' '
 121        git reset --hard &&
 123        git checkout -b side-a branch-point &&
 124        echo >>file1 "new line to be kept in the merge result" &&
 125        git commit -a -m "side-a changes file1" &&
 126        git checkout -b side-b branch-point &&
 127        echo >>file2 "new line to be kept in the merge result" &&
 128        git commit -a -m "side-b changes file2" &&
 129        git checkout side-a
 130'
 132test_expect_success '3-way not overwriting local changes (our side)' '
 134        # At this point, file1 from side-a should be kept as side-b
 136        # did not touch it.
 137        git reset --hard &&
 139        echo >>file1 "local changes" &&
 141        read_tree_u_must_succeed -m -u branch-point side-a side-b &&
 142        grep "new line to be kept" file1 &&
 143        grep "local changes" file1
 144'
 146test_expect_success '3-way not overwriting local changes (their side)' '
 148        # At this point, file2 from side-b should be taken as side-a
 150        # did not touch it.
 151        git reset --hard &&
 153        echo >>file2 "local changes" &&
 155        read_tree_u_must_fail -m -u branch-point side-a side-b &&
 156        ! grep "new line to be kept" file2 &&
 157        grep "local changes" file2
 158'
 160test_expect_success 'funny symlink in work tree' '
 162        git reset --hard &&
 164        git checkout -b sym-b side-b &&
 165        mkdir -p a &&
 166        >a/b &&
 167        git add a/b &&
 168        git commit -m "side adds a/b" &&
 169        rm -fr a &&
 171        git checkout -b sym-a side-a &&
 172        mkdir -p a &&
 173        test_ln_s_add ../b a/b &&
 174        git commit -m "we add a/b" &&
 175        read_tree_u_must_succeed -m -u sym-a sym-a sym-b
 177'
 179test_expect_success SANITY 'funny symlink in work tree, un-unlink-able' '
 181        rm -fr a b &&
 183        git reset --hard &&
 184        git checkout sym-a &&
 186        chmod a-w a &&
 187        test_must_fail git read-tree -m -u sym-a sym-a sym-b
 188'
 190# clean-up from the above test
 192chmod a+w a 2>/dev/null
 193rm -fr a b
 194test_expect_success 'D/F setup' '
 196        git reset --hard &&
 198        git checkout side-a &&
 200        rm -f subdir/file2 &&
 201        mkdir subdir/file2 &&
 202        echo qfwfq >subdir/file2/another &&
 203        git add subdir/file2/another &&
 204        test_tick &&
 205        git commit -m "side-a changes file2 to directory"
 206'
 208test_expect_success 'D/F' '
 210        git checkout side-b &&
 212        read_tree_u_must_succeed -m -u branch-point side-b side-a &&
 213        git ls-files -u >actual &&
 214        (
 215                a=$(git rev-parse branch-point:subdir/file2)
 216                b=$(git rev-parse side-a:subdir/file2/another)
 217                echo "100644 $a 1       subdir/file2"
 218                echo "100644 $a 2       subdir/file2"
 219                echo "100644 $b 3       subdir/file2/another"
 220        ) >expect &&
 221        test_cmp actual expect
 222'
 224test_expect_success 'D/F resolve' '
 226        git reset --hard &&
 228        git checkout side-b &&
 229        git merge-resolve branch-point -- side-b side-a
 230'
 232test_expect_success 'D/F recursive' '
 234        git reset --hard &&
 236        git checkout side-b &&
 237        git merge-recursive branch-point -- side-b side-a
 238'
 240test_done