t / t7607-merge-overwrite.shon commit Merge branch 'pw/rebase-i-internal' (7ba06bc)
   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        mkdir sub2 &&
  19        echo "sub2/f" > sub2/f &&
  20        git add sub/f sub2/f &&
  21        git commit -m sub &&
  22        git tag sub &&
  23        echo "VERY IMPORTANT CHANGES" > important
  24'
  25
  26test_expect_success 'will not overwrite untracked file' '
  27        git reset --hard c1 &&
  28        cp important c2.c &&
  29        test_must_fail git merge c2 &&
  30        test_path_is_missing .git/MERGE_HEAD &&
  31        test_cmp important c2.c
  32'
  33
  34test_expect_success 'will overwrite tracked file' '
  35        git reset --hard c1 &&
  36        cp important c2.c &&
  37        git add c2.c &&
  38        git commit -m important &&
  39        git checkout c2
  40'
  41
  42test_expect_success 'will not overwrite new file' '
  43        git reset --hard c1 &&
  44        cp important c2.c &&
  45        git add c2.c &&
  46        test_must_fail git merge c2 &&
  47        test_path_is_missing .git/MERGE_HEAD &&
  48        test_cmp important c2.c
  49'
  50
  51test_expect_success 'will not overwrite staged changes' '
  52        git reset --hard c1 &&
  53        cp important c2.c &&
  54        git add c2.c &&
  55        rm c2.c &&
  56        test_must_fail git merge c2 &&
  57        test_path_is_missing .git/MERGE_HEAD &&
  58        git checkout c2.c &&
  59        test_cmp important c2.c
  60'
  61
  62test_expect_success 'will not overwrite removed file' '
  63        git reset --hard c1 &&
  64        git rm c1.c &&
  65        git commit -m "rm c1.c" &&
  66        cp important c1.c &&
  67        test_must_fail git merge c1a &&
  68        test_cmp important c1.c
  69'
  70
  71test_expect_success 'will not overwrite re-added file' '
  72        git reset --hard c1 &&
  73        git rm c1.c &&
  74        git commit -m "rm c1.c" &&
  75        cp important c1.c &&
  76        git add c1.c &&
  77        test_must_fail git merge c1a &&
  78        test_path_is_missing .git/MERGE_HEAD &&
  79        test_cmp important c1.c
  80'
  81
  82test_expect_success 'will not overwrite removed file with staged changes' '
  83        git reset --hard c1 &&
  84        git rm c1.c &&
  85        git commit -m "rm c1.c" &&
  86        cp important c1.c &&
  87        git add c1.c &&
  88        rm c1.c &&
  89        test_must_fail git merge c1a &&
  90        test_path_is_missing .git/MERGE_HEAD &&
  91        git checkout c1.c &&
  92        test_cmp important c1.c
  93'
  94
  95test_expect_success 'will not overwrite unstaged changes in renamed file' '
  96        git reset --hard c1 &&
  97        git mv c1.c other.c &&
  98        git commit -m rename &&
  99        cp important other.c &&
 100        test_must_fail git merge c1a >out &&
 101        test_i18ngrep "Refusing to lose dirty file at other.c" out &&
 102        test_path_is_file other.c~HEAD &&
 103        test $(git hash-object other.c~HEAD) = $(git rev-parse c1a:c1.c) &&
 104        test_cmp important other.c
 105'
 106
 107test_expect_success 'will not overwrite untracked subtree' '
 108        git reset --hard c0 &&
 109        rm -rf sub &&
 110        mkdir -p sub/f &&
 111        cp important sub/f/important &&
 112        test_must_fail git merge sub &&
 113        test_path_is_missing .git/MERGE_HEAD &&
 114        test_cmp important sub/f/important
 115'
 116
 117cat >expect <<\EOF
 118error: The following untracked working tree files would be overwritten by merge:
 119        sub
 120        sub2
 121Please move or remove them before you merge.
 122Aborting
 123EOF
 124
 125test_expect_success 'will not overwrite untracked file in leading path' '
 126        git reset --hard c0 &&
 127        rm -rf sub &&
 128        cp important sub &&
 129        cp important sub2 &&
 130        test_must_fail git merge sub 2>out &&
 131        test_i18ncmp out expect &&
 132        test_path_is_missing .git/MERGE_HEAD &&
 133        test_cmp important sub &&
 134        test_cmp important sub2 &&
 135        rm -f sub sub2
 136'
 137
 138test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
 139        git reset --hard c0 &&
 140        rm -rf sub &&
 141        mkdir sub2 &&
 142        ln -s sub2 sub &&
 143        test_must_fail git merge sub &&
 144        test_path_is_missing .git/MERGE_HEAD
 145'
 146
 147test_expect_success 'will not be confused by symlink in leading path' '
 148        git reset --hard c0 &&
 149        rm -rf sub &&
 150        test_ln_s_add sub2 sub &&
 151        git commit -m ln &&
 152        git checkout sub
 153'
 154
 155cat >expect <<\EOF
 156error: Untracked working tree file 'c0.c' would be overwritten by merge.
 157fatal: read-tree failed
 158EOF
 159
 160test_expect_success 'will not overwrite untracked file on unborn branch' '
 161        git reset --hard c0 &&
 162        git rm -fr . &&
 163        git checkout --orphan new &&
 164        cp important c0.c &&
 165        test_must_fail git merge c0 2>out &&
 166        test_i18ncmp out expect
 167'
 168
 169test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
 170        test_when_finished "rm c0.c" &&
 171        test_path_is_missing .git/MERGE_HEAD &&
 172        test_cmp important c0.c
 173'
 174
 175test_expect_success 'failed merge leaves unborn branch in the womb' '
 176        test_must_fail git rev-parse --verify HEAD
 177'
 178
 179test_expect_success 'set up unborn branch and content' '
 180        git symbolic-ref HEAD refs/heads/unborn &&
 181        rm -f .git/index &&
 182        echo foo > tracked-file &&
 183        git add tracked-file &&
 184        echo bar > untracked-file
 185'
 186
 187test_expect_success 'will not clobber WT/index when merging into unborn' '
 188        git merge master &&
 189        grep foo tracked-file &&
 190        git show :tracked-file >expect &&
 191        grep foo expect &&
 192        grep bar untracked-file
 193'
 194
 195test_done