ff2875c00b856fa6b4badbcbb87e45d7fd0b48b7
   1#!/bin/sh
   2#
   3# Copyright (c) 2009 Christian Couder
   4#
   5
   6test_description='Tests for "git reset --merge"'
   7
   8. ./test-lib.sh
   9
  10test_expect_success setup '
  11    for i in 1 2 3; do echo line $i; done >file1 &&
  12    cat file1 >file2 &&
  13    git add file1 file2 &&
  14    test_tick &&
  15    git commit -m "Initial commit" &&
  16    git tag initial &&
  17    echo line 4 >>file1 &&
  18    cat file1 >file2 &&
  19    test_tick &&
  20    git commit -m "add line 4 to file1" file1 &&
  21    git tag second
  22'
  23
  24# The next test will test the following:
  25#
  26#           working index HEAD target         working index HEAD
  27#           ----------------------------------------------------
  28# file1:     C       C     C    D     --merge  D       D     D
  29# file2:     C       D     D    D     --merge  C       D     D
  30test_expect_success 'reset --merge is ok with changes in file it does not touch' '
  31    git reset --merge HEAD^ &&
  32    ! grep 4 file1 &&
  33    grep 4 file2 &&
  34    test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
  35    test -z "$(git diff --cached)"
  36'
  37
  38test_expect_success 'reset --merge is ok when switching back' '
  39    git reset --merge second &&
  40    grep 4 file1 &&
  41    grep 4 file2 &&
  42    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
  43    test -z "$(git diff --cached)"
  44'
  45
  46# The next test will test the following:
  47#
  48#           working index HEAD target         working index HEAD
  49#           ----------------------------------------------------
  50# file1:     B       B     C    D     --merge  D       D     D
  51# file2:     C       D     D    D     --merge  C       D     D
  52test_expect_success 'reset --merge discards changes added to index (1)' '
  53    git reset --hard second &&
  54    cat file1 >file2 &&
  55    echo "line 5" >> file1 &&
  56    git add file1 &&
  57    git reset --merge HEAD^ &&
  58    ! grep 4 file1 &&
  59    ! grep 5 file1 &&
  60    grep 4 file2 &&
  61    test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
  62    test -z "$(git diff --cached)"
  63'
  64
  65test_expect_success 'reset --merge is ok again when switching back (1)' '
  66    git reset --hard initial &&
  67    echo "line 5" >> file2 &&
  68    git add file2 &&
  69    git reset --merge second &&
  70    ! grep 4 file2 &&
  71    ! grep 5 file1 &&
  72    grep 4 file1 &&
  73    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
  74    test -z "$(git diff --cached)"
  75'
  76
  77# The next test will test the following:
  78#
  79#           working index HEAD target         working index HEAD
  80#           ----------------------------------------------------
  81# file1:     C       C     C    D     --merge  D       D     D
  82# file2:     C       C     D    D     --merge  D       D     D
  83test_expect_success 'reset --merge discards changes added to index (2)' '
  84    git reset --hard second &&
  85    echo "line 4" >> file2 &&
  86    git add file2 &&
  87    git reset --merge HEAD^ &&
  88    ! grep 4 file2 &&
  89    test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
  90    test -z "$(git diff)" &&
  91    test -z "$(git diff --cached)"
  92'
  93
  94test_expect_success 'reset --merge is ok again when switching back (2)' '
  95    git reset --hard initial &&
  96    git reset --merge second &&
  97    ! grep 4 file2 &&
  98    grep 4 file1 &&
  99    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
 100    test -z "$(git diff --cached)"
 101'
 102
 103# The next test will test the following:
 104#
 105#           working index HEAD target         working index HEAD
 106#           ----------------------------------------------------
 107# file1:     A       B     B    C     --merge  (disallowed)
 108test_expect_success 'reset --merge fails with changes in file it touches' '
 109    git reset --hard second &&
 110    echo "line 5" >> file1 &&
 111    test_tick &&
 112    git commit -m "add line 5" file1 &&
 113    sed -e "s/line 1/changed line 1/" <file1 >file3 &&
 114    mv file3 file1 &&
 115    test_must_fail git reset --merge HEAD^ 2>err.log &&
 116    grep file1 err.log | grep "not uptodate"
 117'
 118
 119test_expect_success 'setup 2 different branches' '
 120    git reset --hard second &&
 121    git branch branch1 &&
 122    git branch branch2 &&
 123    git checkout branch1 &&
 124    echo "line 5 in branch1" >> file1 &&
 125    test_tick &&
 126    git commit -a -m "change in branch1" &&
 127    git checkout branch2 &&
 128    echo "line 5 in branch2" >> file1 &&
 129    test_tick &&
 130    git commit -a -m "change in branch2" &&
 131    git tag third
 132'
 133
 134# The next test will test the following:
 135#
 136#           working index HEAD target         working index HEAD
 137#           ----------------------------------------------------
 138# file1:     X       U     B    C     --merge  X       C     C
 139test_expect_success '"reset --merge HEAD^" is ok with pending merge' '
 140    test_must_fail git merge branch1 &&
 141    cat file1 >orig_file1 &&
 142    git reset --merge HEAD^ &&
 143    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
 144    test -z "$(git diff --cached)" &&
 145    test_cmp file1 orig_file1
 146'
 147
 148# The next test will test the following:
 149#
 150#           working index HEAD target         working index HEAD
 151#           ----------------------------------------------------
 152# file1:     X       U     B    B     --merge  (disallowed)
 153test_expect_success '"reset --merge HEAD" fails with pending merge' '
 154    git reset --hard third &&
 155    test_must_fail git merge branch1 &&
 156    test_must_fail git reset --merge HEAD &&
 157    test "$(git rev-parse HEAD)" = "$(git rev-parse third)" &&
 158    test -n "$(git diff --cached)"
 159'
 160
 161test_done