t / t7607-merge-overwrite.shon commit t7607: add leading-path tests (175659b)
   1#!/bin/sh
   2
   3test_description='git-merge
   4
   5Do not overwrite changes.'
   6
   7. ./test-lib.sh
   8
   9test_expect_success 'setup' '
  10        test_commit c0 c0.c &&
  11        test_commit c1 c1.c &&
  12        test_commit c1a c1.c "c1 a" &&
  13        git reset --hard c0 &&
  14        test_commit c2 c2.c &&
  15        git reset --hard c0 &&
  16        mkdir sub &&
  17        echo "sub/f" > sub/f &&
  18        git add sub/f &&
  19        git commit -m sub &&
  20        git tag sub &&
  21        echo "VERY IMPORTANT CHANGES" > important
  22'
  23
  24test_expect_success 'will not overwrite untracked file' '
  25        git reset --hard c1 &&
  26        cp important c2.c &&
  27        test_must_fail git merge c2 &&
  28        test_path_is_missing .git/MERGE_HEAD &&
  29        test_cmp important c2.c
  30'
  31
  32test_expect_success 'will overwrite tracked file' '
  33        git reset --hard c1 &&
  34        cp important c2.c &&
  35        git add c2.c &&
  36        git commit -m important &&
  37        git checkout c2
  38'
  39
  40test_expect_success 'will not overwrite new file' '
  41        git reset --hard c1 &&
  42        cp important c2.c &&
  43        git add c2.c &&
  44        test_must_fail git merge c2 &&
  45        test_path_is_missing .git/MERGE_HEAD &&
  46        test_cmp important c2.c
  47'
  48
  49test_expect_success 'will not overwrite staged changes' '
  50        git reset --hard c1 &&
  51        cp important c2.c &&
  52        git add c2.c &&
  53        rm c2.c &&
  54        test_must_fail git merge c2 &&
  55        test_path_is_missing .git/MERGE_HEAD &&
  56        git checkout c2.c &&
  57        test_cmp important c2.c
  58'
  59
  60test_expect_success 'will not overwrite removed file' '
  61        git reset --hard c1 &&
  62        git rm c1.c &&
  63        git commit -m "rm c1.c" &&
  64        cp important c1.c &&
  65        test_must_fail git merge c1a &&
  66        test_cmp important c1.c
  67'
  68
  69test_expect_success 'will not overwrite re-added file' '
  70        git reset --hard c1 &&
  71        git rm c1.c &&
  72        git commit -m "rm c1.c" &&
  73        cp important c1.c &&
  74        git add c1.c &&
  75        test_must_fail git merge c1a &&
  76        test_path_is_missing .git/MERGE_HEAD &&
  77        test_cmp important c1.c
  78'
  79
  80test_expect_success 'will not overwrite removed file with staged changes' '
  81        git reset --hard c1 &&
  82        git rm c1.c &&
  83        git commit -m "rm c1.c" &&
  84        cp important c1.c &&
  85        git add c1.c &&
  86        rm c1.c &&
  87        test_must_fail git merge c1a &&
  88        test_path_is_missing .git/MERGE_HEAD &&
  89        git checkout c1.c &&
  90        test_cmp important c1.c
  91'
  92
  93test_expect_success 'will not overwrite untracked subtree' '
  94        git reset --hard c0 &&
  95        rm -rf sub &&
  96        mkdir -p sub/f &&
  97        cp important sub/f/important &&
  98        test_must_fail git merge sub &&
  99        test_path_is_missing .git/MERGE_HEAD &&
 100        test_cmp important sub/f/important
 101'
 102
 103test_expect_failure 'will not overwrite untracked file in leading path' '
 104        git reset --hard c0 &&
 105        rm -rf sub &&
 106        cp important sub &&
 107        test_must_fail git merge sub &&
 108        test_path_is_missing .git/MERGE_HEAD &&
 109        test_cmp important sub
 110'
 111
 112test_expect_failure SYMLINKS 'will not overwrite untracked symlink in leading path' '
 113        git reset --hard c0 &&
 114        rm -rf sub &&
 115        mkdir sub2 &&
 116        ln -s sub2 sub &&
 117        test_must_fail git merge sub &&
 118        test_path_is_missing .git/MERGE_HEAD
 119'
 120
 121test_expect_success SYMLINKS 'will not be confused by symlink in leading path' '
 122        git reset --hard c0 &&
 123        rm -rf sub &&
 124        ln -s sub2 sub &&
 125        git add sub &&
 126        git commit -m ln &&
 127        git checkout sub
 128'
 129
 130cat >expect <<\EOF
 131error: Untracked working tree file 'c0.c' would be overwritten by merge.
 132fatal: read-tree failed
 133EOF
 134
 135test_expect_success 'will not overwrite untracked file on unborn branch' '
 136        git reset --hard c0 &&
 137        git rm -fr . &&
 138        git checkout --orphan new &&
 139        cp important c0.c &&
 140        test_must_fail git merge c0 2>out &&
 141        test_cmp out expect &&
 142        test_path_is_missing .git/MERGE_HEAD &&
 143        test_cmp important c0.c
 144'
 145
 146test_done