t / t3412-rebase-root.shon commit Merge branch 'kk/maint-commit-tree' (a7fe78a)
   1#!/bin/sh
   2
   3test_description='git rebase --root
   4
   5Tests if git rebase --root --onto <newparent> can rebase the root commit.
   6'
   7. ./test-lib.sh
   8
   9log_with_names () {
  10        git rev-list --topo-order --parents --pretty="tformat:%s" HEAD |
  11        git name-rev --stdin --name-only --refs=refs/heads/$1
  12}
  13
  14
  15test_expect_success 'prepare repository' '
  16        test_commit 1 A &&
  17        test_commit 2 A &&
  18        git symbolic-ref HEAD refs/heads/other &&
  19        rm .git/index &&
  20        test_commit 3 B &&
  21        test_commit 1b A 1 &&
  22        test_commit 4 B
  23'
  24
  25test_expect_success 'rebase --root fails with too many args' '
  26        git checkout -B fail other &&
  27        test_must_fail git rebase --onto master --root fail fail
  28'
  29
  30test_expect_success 'setup pre-rebase hook' '
  31        mkdir -p .git/hooks &&
  32        cat >.git/hooks/pre-rebase <<EOF &&
  33#!$SHELL_PATH
  34echo "\$1,\$2" >.git/PRE-REBASE-INPUT
  35EOF
  36        chmod +x .git/hooks/pre-rebase
  37'
  38cat > expect <<EOF
  394
  403
  412
  421
  43EOF
  44
  45test_expect_success 'rebase --root --onto <newbase>' '
  46        git checkout -b work other &&
  47        git rebase --root --onto master &&
  48        git log --pretty=tformat:"%s" > rebased &&
  49        test_cmp expect rebased
  50'
  51
  52test_expect_success 'pre-rebase got correct input (1)' '
  53        test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
  54'
  55
  56test_expect_success 'rebase --root --onto <newbase> <branch>' '
  57        git branch work2 other &&
  58        git rebase --root --onto master work2 &&
  59        git log --pretty=tformat:"%s" > rebased2 &&
  60        test_cmp expect rebased2
  61'
  62
  63test_expect_success 'pre-rebase got correct input (2)' '
  64        test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work2
  65'
  66
  67test_expect_success 'rebase -i --root --onto <newbase>' '
  68        git checkout -b work3 other &&
  69        git rebase -i --root --onto master &&
  70        git log --pretty=tformat:"%s" > rebased3 &&
  71        test_cmp expect rebased3
  72'
  73
  74test_expect_success 'pre-rebase got correct input (3)' '
  75        test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
  76'
  77
  78test_expect_success 'rebase -i --root --onto <newbase> <branch>' '
  79        git branch work4 other &&
  80        git rebase -i --root --onto master work4 &&
  81        git log --pretty=tformat:"%s" > rebased4 &&
  82        test_cmp expect rebased4
  83'
  84
  85test_expect_success 'pre-rebase got correct input (4)' '
  86        test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work4
  87'
  88
  89test_expect_success 'rebase -i -p with linear history' '
  90        git checkout -b work5 other &&
  91        git rebase -i -p --root --onto master &&
  92        git log --pretty=tformat:"%s" > rebased5 &&
  93        test_cmp expect rebased5
  94'
  95
  96test_expect_success 'pre-rebase got correct input (5)' '
  97        test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
  98'
  99
 100test_expect_success 'set up merge history' '
 101        git checkout other^ &&
 102        git checkout -b side &&
 103        test_commit 5 C &&
 104        git checkout other &&
 105        git merge side
 106'
 107
 108cat > expect-side <<'EOF'
 109commit work6 work6~1 work6^2
 110Merge branch 'side' into other
 111commit work6^2 work6~2
 1125
 113commit work6~1 work6~2
 1144
 115commit work6~2 work6~3
 1163
 117commit work6~3 work6~4
 1182
 119commit work6~4
 1201
 121EOF
 122
 123test_expect_success 'rebase -i -p with merge' '
 124        git checkout -b work6 other &&
 125        git rebase -i -p --root --onto master &&
 126        log_with_names work6 > rebased6 &&
 127        test_cmp expect-side rebased6
 128'
 129
 130test_expect_success 'set up second root and merge' '
 131        git symbolic-ref HEAD refs/heads/third &&
 132        rm .git/index &&
 133        rm A B C &&
 134        test_commit 6 D &&
 135        git checkout other &&
 136        git merge third
 137'
 138
 139cat > expect-third <<'EOF'
 140commit work7 work7~1 work7^2
 141Merge branch 'third' into other
 142commit work7^2 work7~4
 1436
 144commit work7~1 work7~2 work7~1^2
 145Merge branch 'side' into other
 146commit work7~1^2 work7~3
 1475
 148commit work7~2 work7~3
 1494
 150commit work7~3 work7~4
 1513
 152commit work7~4 work7~5
 1532
 154commit work7~5
 1551
 156EOF
 157
 158test_expect_success 'rebase -i -p with two roots' '
 159        git checkout -b work7 other &&
 160        git rebase -i -p --root --onto master &&
 161        log_with_names work7 > rebased7 &&
 162        test_cmp expect-third rebased7
 163'
 164
 165test_expect_success 'setup pre-rebase hook that fails' '
 166        mkdir -p .git/hooks &&
 167        cat >.git/hooks/pre-rebase <<EOF &&
 168#!$SHELL_PATH
 169false
 170EOF
 171        chmod +x .git/hooks/pre-rebase
 172'
 173
 174test_expect_success 'pre-rebase hook stops rebase' '
 175        git checkout -b stops1 other &&
 176        test_must_fail git rebase --root --onto master &&
 177        test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops1 &&
 178        test 0 = $(git rev-list other...stops1 | wc -l)
 179'
 180
 181test_expect_success 'pre-rebase hook stops rebase -i' '
 182        git checkout -b stops2 other &&
 183        test_must_fail git rebase --root --onto master &&
 184        test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops2 &&
 185        test 0 = $(git rev-list other...stops2 | wc -l)
 186'
 187
 188test_expect_success 'remove pre-rebase hook' '
 189        rm -f .git/hooks/pre-rebase
 190'
 191
 192test_expect_success 'set up a conflict' '
 193        git checkout master &&
 194        echo conflict > B &&
 195        git add B &&
 196        git commit -m conflict
 197'
 198
 199test_expect_success 'rebase --root with conflict (first part)' '
 200        git checkout -b conflict1 other &&
 201        test_must_fail git rebase --root --onto master &&
 202        git ls-files -u | grep "B$"
 203'
 204
 205test_expect_success 'fix the conflict' '
 206        echo 3 > B &&
 207        git add B
 208'
 209
 210cat > expect-conflict <<EOF
 2116
 2125
 2134
 2143
 215conflict
 2162
 2171
 218EOF
 219
 220test_expect_success 'rebase --root with conflict (second part)' '
 221        git rebase --continue &&
 222        git log --pretty=tformat:"%s" > conflict1 &&
 223        test_cmp expect-conflict conflict1
 224'
 225
 226test_expect_success 'rebase -i --root with conflict (first part)' '
 227        git checkout -b conflict2 other &&
 228        test_must_fail git rebase -i --root --onto master &&
 229        git ls-files -u | grep "B$"
 230'
 231
 232test_expect_success 'fix the conflict' '
 233        echo 3 > B &&
 234        git add B
 235'
 236
 237test_expect_success 'rebase -i --root with conflict (second part)' '
 238        git rebase --continue &&
 239        git log --pretty=tformat:"%s" > conflict2 &&
 240        test_cmp expect-conflict conflict2
 241'
 242
 243cat >expect-conflict-p <<\EOF
 244commit conflict3 conflict3~1 conflict3^2
 245Merge branch 'third' into other
 246commit conflict3^2 conflict3~4
 2476
 248commit conflict3~1 conflict3~2 conflict3~1^2
 249Merge branch 'side' into other
 250commit conflict3~1^2 conflict3~3
 2515
 252commit conflict3~2 conflict3~3
 2534
 254commit conflict3~3 conflict3~4
 2553
 256commit conflict3~4 conflict3~5
 257conflict
 258commit conflict3~5 conflict3~6
 2592
 260commit conflict3~6
 2611
 262EOF
 263
 264test_expect_success 'rebase -i -p --root with conflict (first part)' '
 265        git checkout -b conflict3 other &&
 266        test_must_fail git rebase -i -p --root --onto master &&
 267        git ls-files -u | grep "B$"
 268'
 269
 270test_expect_success 'fix the conflict' '
 271        echo 3 > B &&
 272        git add B
 273'
 274
 275test_expect_success 'rebase -i -p --root with conflict (second part)' '
 276        git rebase --continue &&
 277        log_with_names conflict3 >out &&
 278        test_cmp expect-conflict-p out
 279'
 280
 281test_done