t / t7405-submodule-merge.shon commit Merge branch 'np/diff-in-corrupt-repository' (f5a5531)
   1#!/bin/sh
   2
   3test_description='merging with submodules'
   4
   5. ./test-lib.sh
   6
   7#
   8# history
   9#
  10#        a --- c
  11#      /   \ /
  12# root      X
  13#      \   / \
  14#        b --- d
  15#
  16
  17test_expect_success setup '
  18
  19        mkdir sub &&
  20        (cd sub &&
  21         git init &&
  22         echo original > file &&
  23         git add file &&
  24         test_tick &&
  25         git commit -m sub-root) &&
  26        git add sub &&
  27        test_tick &&
  28        git commit -m root &&
  29
  30        git checkout -b a master &&
  31        (cd sub &&
  32         echo A > file &&
  33         git add file &&
  34         test_tick &&
  35         git commit -m sub-a) &&
  36        git add sub &&
  37        test_tick &&
  38        git commit -m a &&
  39
  40        git checkout -b b master &&
  41        (cd sub &&
  42         echo B > file &&
  43         git add file &&
  44         test_tick &&
  45         git commit -m sub-b) &&
  46        git add sub &&
  47        test_tick &&
  48        git commit -m b &&
  49
  50        git checkout -b c a &&
  51        git merge -s ours b &&
  52
  53        git checkout -b d b &&
  54        git merge -s ours a
  55'
  56
  57# History setup
  58#
  59#      b
  60#    /   \
  61#   a     d
  62#    \   /
  63#      c
  64#
  65# a in the main repository records to sub-a in the submodule and
  66# analogous b and c. d should be automatically found by merging c into
  67# b in the main repository.
  68test_expect_success 'setup for merge search' '
  69        mkdir merge-search &&
  70        (cd merge-search &&
  71        git init &&
  72        mkdir sub &&
  73        (cd sub &&
  74         git init &&
  75         echo "file-a" > file-a &&
  76         git add file-a &&
  77         git commit -m "sub-a" &&
  78         git branch sub-a) &&
  79        git add sub &&
  80        git commit -m "a" &&
  81        git branch a &&
  82
  83        git checkout -b b &&
  84        (cd sub &&
  85         git checkout -b sub-b &&
  86         echo "file-b" > file-b &&
  87         git add file-b &&
  88         git commit -m "sub-b") &&
  89        git commit -a -m "b" &&
  90
  91        git checkout -b c a &&
  92        (cd sub &&
  93         git checkout -b sub-c sub-a &&
  94         echo "file-c" > file-c &&
  95         git add file-c &&
  96         git commit -m "sub-c") &&
  97        git commit -a -m "c" &&
  98
  99        git checkout -b d a &&
 100        (cd sub &&
 101         git checkout -b sub-d sub-b &&
 102         git merge sub-c) &&
 103        git commit -a -m "d" &&
 104        git branch test b)
 105'
 106
 107test_expect_success 'merge with one side as a fast-forward of the other' '
 108        (cd merge-search &&
 109         git checkout -b test-forward b &&
 110         git merge d &&
 111         git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
 112         (cd sub &&
 113          git rev-parse sub-d > ../expect) &&
 114         test_cmp actual expect)
 115'
 116
 117test_expect_success 'merging should conflict for non fast-forward' '
 118        (cd merge-search &&
 119         git checkout -b test-nonforward b &&
 120         (cd sub &&
 121          git rev-parse sub-d > ../expect) &&
 122         test_must_fail git merge c 2> actual  &&
 123         grep $(cat expect) actual > /dev/null &&
 124         git reset --hard)
 125'
 126
 127test_expect_success 'merging should fail for ambiguous common parent' '
 128        (cd merge-search &&
 129        git checkout -b test-ambiguous b &&
 130        (cd sub &&
 131         git checkout -b ambiguous sub-b &&
 132         git merge sub-c &&
 133         git rev-parse sub-d > ../expect1 &&
 134         git rev-parse ambiguous > ../expect2) &&
 135        test_must_fail git merge c 2> actual &&
 136        grep $(cat expect1) actual > /dev/null &&
 137        grep $(cat expect2) actual > /dev/null &&
 138        git reset --hard)
 139'
 140
 141# in a situation like this
 142#
 143# submodule tree:
 144#
 145#    sub-a --- sub-b --- sub-d
 146#
 147# main tree:
 148#
 149#    e (sub-a)
 150#   /
 151#  bb (sub-b)
 152#   \
 153#    f (sub-d)
 154#
 155# A merge between e and f should fail because one of the submodule
 156# commits (sub-a) does not descend from the submodule merge-base (sub-b).
 157#
 158test_expect_success 'merging should fail for changes that are backwards' '
 159        (cd merge-search &&
 160        git checkout -b bb a &&
 161        (cd sub &&
 162         git checkout sub-b) &&
 163        git commit -a -m "bb" &&
 164
 165        git checkout -b e bb &&
 166        (cd sub &&
 167         git checkout sub-a) &&
 168        git commit -a -m "e" &&
 169
 170        git checkout -b f bb &&
 171        (cd sub &&
 172         git checkout sub-d) &&
 173        git commit -a -m "f" &&
 174
 175        git checkout -b test-backward e &&
 176        test_must_fail git merge f)
 177'
 178
 179test_expect_success 'merging with a modify/modify conflict between merge bases' '
 180        git reset --hard HEAD &&
 181        git checkout -b test2 c &&
 182        git merge d
 183'
 184
 185test_done