18e7ff5d3a38c8ee20b9850b89799fe77c867298
   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        echo "VERY IMPORTANT CHANGES" > important
  16'
  17
  18test_expect_success 'will not overwrite untracked file' '
  19        git reset --hard c1 &&
  20        cp important c2.c &&
  21        test_must_fail git merge c2 &&
  22        test_path_is_missing .git/MERGE_HEAD &&
  23        test_cmp important c2.c
  24'
  25
  26test_expect_success 'will not overwrite new file' '
  27        git reset --hard c1 &&
  28        cp important c2.c &&
  29        git add c2.c &&
  30        test_must_fail git merge c2 &&
  31        test_path_is_missing .git/MERGE_HEAD &&
  32        test_cmp important c2.c
  33'
  34
  35test_expect_success 'will not overwrite staged changes' '
  36        git reset --hard c1 &&
  37        cp important c2.c &&
  38        git add c2.c &&
  39        rm c2.c &&
  40        test_must_fail git merge c2 &&
  41        test_path_is_missing .git/MERGE_HEAD &&
  42        git checkout c2.c &&
  43        test_cmp important c2.c
  44'
  45
  46test_expect_success 'will not overwrite removed file' '
  47        git reset --hard c1 &&
  48        git rm c1.c &&
  49        git commit -m "rm c1.c" &&
  50        cp important c1.c &&
  51        test_must_fail git merge c1a &&
  52        test_cmp important c1.c
  53'
  54
  55test_expect_success 'will not overwrite re-added file' '
  56        git reset --hard c1 &&
  57        git rm c1.c &&
  58        git commit -m "rm c1.c" &&
  59        cp important c1.c &&
  60        git add c1.c &&
  61        test_must_fail git merge c1a &&
  62        test_path_is_missing .git/MERGE_HEAD &&
  63        test_cmp important c1.c
  64'
  65
  66test_expect_success 'will not overwrite removed file with staged changes' '
  67        git reset --hard c1 &&
  68        git rm c1.c &&
  69        git commit -m "rm c1.c" &&
  70        cp important c1.c &&
  71        git add c1.c &&
  72        rm c1.c &&
  73        test_must_fail git merge c1a &&
  74        test_path_is_missing .git/MERGE_HEAD &&
  75        git checkout c1.c &&
  76        test_cmp important c1.c
  77'
  78
  79cat >expect <<\EOF
  80error: Untracked working tree file 'c0.c' would be overwritten by merge.
  81fatal: read-tree failed
  82EOF
  83
  84test_expect_success 'will not overwrite untracked file on unborn branch' '
  85        git reset --hard c0 &&
  86        git rm -fr . &&
  87        git checkout --orphan new &&
  88        cp important c0.c &&
  89        test_must_fail git merge c0 2>out &&
  90        test_cmp out expect &&
  91        test_path_is_missing .git/MERGE_HEAD &&
  92        test_cmp important c0.c
  93'
  94
  95test_done