t / t6050-replace.shon commit pull --rebase=<type>: allow single-letter abbreviations for the type (46af44b)
   1#!/bin/sh
   2#
   3# Copyright (c) 2008 Christian Couder
   4#
   5test_description='Tests replace refs functionality'
   6
   7. ./test-lib.sh
   8. "$TEST_DIRECTORY/lib-gpg.sh"
   9
  10add_and_commit_file ()
  11{
  12    _file="$1"
  13    _msg="$2"
  14
  15    git add $_file || return $?
  16    test_tick || return $?
  17    git commit --quiet -m "$_file: $_msg"
  18}
  19
  20commit_buffer_contains_parents ()
  21{
  22    git cat-file commit "$1" >payload &&
  23    sed -n -e '/^$/q' -e '/^parent /p' <payload >actual &&
  24    shift &&
  25    for _parent
  26    do
  27        echo "parent $_parent"
  28    done >expected &&
  29    test_cmp expected actual
  30}
  31
  32commit_peeling_shows_parents ()
  33{
  34    _parent_number=1
  35    _commit="$1"
  36    shift &&
  37    for _parent
  38    do
  39        _found=$(git rev-parse --verify $_commit^$_parent_number) || return 1
  40        test "$_found" = "$_parent" || return 1
  41        _parent_number=$(( $_parent_number + 1 ))
  42    done &&
  43    test_must_fail git rev-parse --verify $_commit^$_parent_number
  44}
  45
  46commit_has_parents ()
  47{
  48    commit_buffer_contains_parents "$@" &&
  49    commit_peeling_shows_parents "$@"
  50}
  51
  52HASH1=
  53HASH2=
  54HASH3=
  55HASH4=
  56HASH5=
  57HASH6=
  58HASH7=
  59
  60test_expect_success 'set up buggy branch' '
  61     echo "line 1" >>hello &&
  62     echo "line 2" >>hello &&
  63     echo "line 3" >>hello &&
  64     echo "line 4" >>hello &&
  65     add_and_commit_file hello "4 lines" &&
  66     HASH1=$(git rev-parse --verify HEAD) &&
  67     echo "line BUG" >>hello &&
  68     echo "line 6" >>hello &&
  69     echo "line 7" >>hello &&
  70     echo "line 8" >>hello &&
  71     add_and_commit_file hello "4 more lines with a BUG" &&
  72     HASH2=$(git rev-parse --verify HEAD) &&
  73     echo "line 9" >>hello &&
  74     echo "line 10" >>hello &&
  75     add_and_commit_file hello "2 more lines" &&
  76     HASH3=$(git rev-parse --verify HEAD) &&
  77     echo "line 11" >>hello &&
  78     add_and_commit_file hello "1 more line" &&
  79     HASH4=$(git rev-parse --verify HEAD) &&
  80     sed -e "s/BUG/5/" hello >hello.new &&
  81     mv hello.new hello &&
  82     add_and_commit_file hello "BUG fixed" &&
  83     HASH5=$(git rev-parse --verify HEAD) &&
  84     echo "line 12" >>hello &&
  85     echo "line 13" >>hello &&
  86     add_and_commit_file hello "2 more lines" &&
  87     HASH6=$(git rev-parse --verify HEAD) &&
  88     echo "line 14" >>hello &&
  89     echo "line 15" >>hello &&
  90     echo "line 16" >>hello &&
  91     add_and_commit_file hello "again 3 more lines" &&
  92     HASH7=$(git rev-parse --verify HEAD)
  93'
  94
  95test_expect_success 'replace the author' '
  96     git cat-file commit $HASH2 | grep "author A U Thor" &&
  97     R=$(git cat-file commit $HASH2 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
  98     git cat-file commit $R | grep "author O Thor" &&
  99     git update-ref refs/replace/$HASH2 $R &&
 100     git show HEAD~5 | grep "O Thor" &&
 101     git show $HASH2 | grep "O Thor"
 102'
 103
 104test_expect_success 'test --no-replace-objects option' '
 105     git cat-file commit $HASH2 | grep "author O Thor" &&
 106     git --no-replace-objects cat-file commit $HASH2 | grep "author A U Thor" &&
 107     git show $HASH2 | grep "O Thor" &&
 108     git --no-replace-objects show $HASH2 | grep "A U Thor"
 109'
 110
 111test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
 112     GIT_NO_REPLACE_OBJECTS=1 git cat-file commit $HASH2 | grep "author A U Thor" &&
 113     GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 | grep "A U Thor"
 114'
 115
 116cat >tag.sig <<EOF
 117object $HASH2
 118type commit
 119tag mytag
 120tagger T A Gger <> 0 +0000
 121
 122EOF
 123
 124test_expect_success 'tag replaced commit' '
 125     git mktag <tag.sig >.git/refs/tags/mytag 2>message
 126'
 127
 128test_expect_success '"git fsck" works' '
 129     git fsck master >fsck_master.out &&
 130     grep "dangling commit $R" fsck_master.out &&
 131     grep "dangling tag $(cat .git/refs/tags/mytag)" fsck_master.out &&
 132     test -z "$(git fsck)"
 133'
 134
 135test_expect_success 'repack, clone and fetch work' '
 136     git repack -a -d &&
 137     git clone --no-hardlinks . clone_dir &&
 138     (
 139          cd clone_dir &&
 140          git show HEAD~5 | grep "A U Thor" &&
 141          git show $HASH2 | grep "A U Thor" &&
 142          git cat-file commit $R &&
 143          git repack -a -d &&
 144          test_must_fail git cat-file commit $R &&
 145          git fetch ../ "refs/replace/*:refs/replace/*" &&
 146          git show HEAD~5 | grep "O Thor" &&
 147          git show $HASH2 | grep "O Thor" &&
 148          git cat-file commit $R
 149     )
 150'
 151
 152test_expect_success '"git replace" listing and deleting' '
 153     test "$HASH2" = "$(git replace -l)" &&
 154     test "$HASH2" = "$(git replace)" &&
 155     aa=${HASH2%??????????????????????????????????????} &&
 156     test "$HASH2" = "$(git replace --list "$aa*")" &&
 157     test_must_fail git replace -d $R &&
 158     test_must_fail git replace --delete &&
 159     test_must_fail git replace -l -d $HASH2 &&
 160     git replace -d $HASH2 &&
 161     git show $HASH2 | grep "A U Thor" &&
 162     test -z "$(git replace -l)"
 163'
 164
 165test_expect_success '"git replace" replacing' '
 166     git replace $HASH2 $R &&
 167     git show $HASH2 | grep "O Thor" &&
 168     test_must_fail git replace $HASH2 $R &&
 169     git replace -f $HASH2 $R &&
 170     test_must_fail git replace -f &&
 171     test "$HASH2" = "$(git replace)"
 172'
 173
 174test_expect_success '"git replace" resolves sha1' '
 175     SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
 176     git replace -d $SHORTHASH2 &&
 177     git replace $SHORTHASH2 $R &&
 178     git show $HASH2 | grep "O Thor" &&
 179     test_must_fail git replace $HASH2 $R &&
 180     git replace -f $HASH2 $R &&
 181     test_must_fail git replace --force &&
 182     test "$HASH2" = "$(git replace)"
 183'
 184
 185# This creates a side branch where the bug in H2
 186# does not appear because P2 is created by applying
 187# H2 and squashing H5 into it.
 188# P3, P4 and P6 are created by cherry-picking H3, H4
 189# and H6 respectively.
 190#
 191# At this point, we should have the following:
 192#
 193#    P2--P3--P4--P6
 194#   /
 195# H1-H2-H3-H4-H5-H6-H7
 196#
 197# Then we replace H6 with P6.
 198#
 199test_expect_success 'create parallel branch without the bug' '
 200     git replace -d $HASH2 &&
 201     git show $HASH2 | grep "A U Thor" &&
 202     git checkout $HASH1 &&
 203     git cherry-pick $HASH2 &&
 204     git show $HASH5 | git apply &&
 205     git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
 206     PARA2=$(git rev-parse --verify HEAD) &&
 207     git cherry-pick $HASH3 &&
 208     PARA3=$(git rev-parse --verify HEAD) &&
 209     git cherry-pick $HASH4 &&
 210     PARA4=$(git rev-parse --verify HEAD) &&
 211     git cherry-pick $HASH6 &&
 212     PARA6=$(git rev-parse --verify HEAD) &&
 213     git replace $HASH6 $PARA6 &&
 214     git checkout master &&
 215     cur=$(git rev-parse --verify HEAD) &&
 216     test "$cur" = "$HASH7" &&
 217     git log --pretty=oneline | grep $PARA2 &&
 218     git remote add cloned ./clone_dir
 219'
 220
 221test_expect_success 'push to cloned repo' '
 222     git push cloned $HASH6^:refs/heads/parallel &&
 223     (
 224          cd clone_dir &&
 225          git checkout parallel &&
 226          git log --pretty=oneline | grep $PARA2
 227     )
 228'
 229
 230test_expect_success 'push branch with replacement' '
 231     git cat-file commit $PARA3 | grep "author A U Thor" &&
 232     S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
 233     git cat-file commit $S | grep "author O Thor" &&
 234     git replace $PARA3 $S &&
 235     git show $HASH6~2 | grep "O Thor" &&
 236     git show $PARA3 | grep "O Thor" &&
 237     git push cloned $HASH6^:refs/heads/parallel2 &&
 238     (
 239          cd clone_dir &&
 240          git checkout parallel2 &&
 241          git log --pretty=oneline | grep $PARA3 &&
 242          git show $PARA3 | grep "A U Thor"
 243     )
 244'
 245
 246test_expect_success 'fetch branch with replacement' '
 247     git branch tofetch $HASH6 &&
 248     (
 249          cd clone_dir &&
 250          git fetch origin refs/heads/tofetch:refs/heads/parallel3 &&
 251          git log --pretty=oneline parallel3 >output.txt &&
 252          ! grep $PARA3 output.txt &&
 253          git show $PARA3 >para3.txt &&
 254          grep "A U Thor" para3.txt &&
 255          git fetch origin "refs/replace/*:refs/replace/*" &&
 256          git log --pretty=oneline parallel3 >output.txt &&
 257          grep $PARA3 output.txt &&
 258          git show $PARA3 >para3.txt &&
 259          grep "O Thor" para3.txt
 260     )
 261'
 262
 263test_expect_success 'bisect and replacements' '
 264     git bisect start $HASH7 $HASH1 &&
 265     test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
 266     git bisect reset &&
 267     GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
 268     test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
 269     git bisect reset &&
 270     git --no-replace-objects bisect start $HASH7 $HASH1 &&
 271     test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
 272     git bisect reset
 273'
 274
 275test_expect_success 'index-pack and replacements' '
 276        git --no-replace-objects rev-list --objects HEAD |
 277        git --no-replace-objects pack-objects test- &&
 278        git index-pack test-*.pack
 279'
 280
 281test_expect_success 'not just commits' '
 282        echo replaced >file &&
 283        git add file &&
 284        REPLACED=$(git rev-parse :file) &&
 285        mv file file.replaced &&
 286
 287        echo original >file &&
 288        git add file &&
 289        ORIGINAL=$(git rev-parse :file) &&
 290        git update-ref refs/replace/$ORIGINAL $REPLACED &&
 291        mv file file.original &&
 292
 293        git checkout file &&
 294        test_cmp file.replaced file
 295'
 296
 297test_expect_success 'replaced and replacement objects must be of the same type' '
 298        test_must_fail git replace mytag $HASH1 &&
 299        test_must_fail git replace HEAD^{tree} HEAD~1 &&
 300        BLOB=$(git rev-parse :file) &&
 301        test_must_fail git replace HEAD^ $BLOB
 302'
 303
 304test_expect_success '-f option bypasses the type check' '
 305        git replace -f mytag $HASH1 &&
 306        git replace --force HEAD^{tree} HEAD~1 &&
 307        git replace -f HEAD^ $BLOB
 308'
 309
 310test_expect_success 'git cat-file --batch works on replace objects' '
 311        git replace | grep $PARA3 &&
 312        echo $PARA3 | git cat-file --batch
 313'
 314
 315test_expect_success 'test --format bogus' '
 316        test_must_fail git replace --format bogus >/dev/null 2>&1
 317'
 318
 319test_expect_success 'test --format short' '
 320        git replace --format=short >actual &&
 321        git replace >expected &&
 322        test_cmp expected actual
 323'
 324
 325test_expect_success 'test --format medium' '
 326        H1=$(git --no-replace-objects rev-parse HEAD~1) &&
 327        HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
 328        MYTAG=$(git --no-replace-objects rev-parse mytag) &&
 329        {
 330                echo "$H1 -> $BLOB" &&
 331                echo "$BLOB -> $REPLACED" &&
 332                echo "$HT -> $H1" &&
 333                echo "$PARA3 -> $S" &&
 334                echo "$MYTAG -> $HASH1"
 335        } | sort >expected &&
 336        git replace -l --format medium | sort >actual &&
 337        test_cmp expected actual
 338'
 339
 340test_expect_success 'test --format long' '
 341        {
 342                echo "$H1 (commit) -> $BLOB (blob)" &&
 343                echo "$BLOB (blob) -> $REPLACED (blob)" &&
 344                echo "$HT (tree) -> $H1 (commit)" &&
 345                echo "$PARA3 (commit) -> $S (commit)" &&
 346                echo "$MYTAG (tag) -> $HASH1 (commit)"
 347        } | sort >expected &&
 348        git replace --format=long | sort >actual &&
 349        test_cmp expected actual
 350'
 351
 352test_expect_success 'setup fake editors' '
 353        write_script fakeeditor <<-\EOF &&
 354                sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
 355                mv "$1.new" "$1"
 356        EOF
 357        write_script failingfakeeditor <<-\EOF
 358                ./fakeeditor "$@"
 359                false
 360        EOF
 361'
 362
 363test_expect_success '--edit with and without already replaced object' '
 364        test_must_fail env GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
 365        GIT_EDITOR=./fakeeditor git replace --force --edit "$PARA3" &&
 366        git replace -l | grep "$PARA3" &&
 367        git cat-file commit "$PARA3" | grep "A fake Thor" &&
 368        git replace -d "$PARA3" &&
 369        GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
 370        git replace -l | grep "$PARA3" &&
 371        git cat-file commit "$PARA3" | grep "A fake Thor"
 372'
 373
 374test_expect_success '--edit and change nothing or command failed' '
 375        git replace -d "$PARA3" &&
 376        test_must_fail env GIT_EDITOR=true git replace --edit "$PARA3" &&
 377        test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
 378        GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
 379        git replace -l | grep "$PARA3" &&
 380        git cat-file commit "$PARA3" | grep "A fake Thor"
 381'
 382
 383test_expect_success 'replace ref cleanup' '
 384        test -n "$(git replace)" &&
 385        git replace -d $(git replace) &&
 386        test -z "$(git replace)"
 387'
 388
 389test_expect_success '--graft with and without already replaced object' '
 390        test $(git log --oneline | wc -l) = 7 &&
 391        git replace --graft $HASH5 &&
 392        test $(git log --oneline | wc -l) = 3 &&
 393        commit_has_parents $HASH5 &&
 394        test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
 395        git replace --force -g $HASH5 $HASH4 $HASH3 &&
 396        commit_has_parents $HASH5 $HASH4 $HASH3 &&
 397        git replace -d $HASH5
 398'
 399
 400test_expect_success GPG 'set up a signed commit' '
 401        echo "line 17" >>hello &&
 402        echo "line 18" >>hello &&
 403        git add hello &&
 404        test_tick &&
 405        git commit --quiet -S -m "hello: 2 more lines in a signed commit" &&
 406        HASH8=$(git rev-parse --verify HEAD) &&
 407        git verify-commit $HASH8
 408'
 409
 410test_expect_success GPG '--graft with a signed commit' '
 411        git cat-file commit $HASH8 >orig &&
 412        git replace --graft $HASH8 &&
 413        git cat-file commit $HASH8 >repl &&
 414        commit_has_parents $HASH8 &&
 415        test_must_fail git verify-commit $HASH8 &&
 416        sed -n -e "/^tree /p" -e "/^author /p" -e "/^committer /p" orig >expected &&
 417        echo >>expected &&
 418        sed -e "/^$/q" repl >actual &&
 419        test_cmp expected actual &&
 420        git replace -d $HASH8
 421'
 422
 423test_expect_success GPG 'set up a merge commit with a mergetag' '
 424        git reset --hard HEAD &&
 425        git checkout -b test_branch HEAD~2 &&
 426        echo "line 1 from test branch" >>hello &&
 427        echo "line 2 from test branch" >>hello &&
 428        git add hello &&
 429        test_tick &&
 430        git commit -m "hello: 2 more lines from a test branch" &&
 431        HASH9=$(git rev-parse --verify HEAD) &&
 432        git tag -s -m "tag for testing with a mergetag" test_tag HEAD &&
 433        git checkout master &&
 434        git merge -s ours test_tag &&
 435        HASH10=$(git rev-parse --verify HEAD) &&
 436        git cat-file commit $HASH10 | grep "^mergetag object"
 437'
 438
 439test_expect_success GPG '--graft on a commit with a mergetag' '
 440        test_must_fail git replace --graft $HASH10 $HASH8^1 &&
 441        git replace --graft $HASH10 $HASH8^1 $HASH9 &&
 442        git replace -d $HASH10
 443'
 444
 445test_expect_success '--convert-graft-file' '
 446        git checkout -b with-graft-file &&
 447        test_commit root2 &&
 448        git reset --hard root2^ &&
 449        test_commit root1 &&
 450        test_commit after-root1 &&
 451        test_tick &&
 452        git merge -m merge-root2 root2 &&
 453
 454        : add and convert graft file &&
 455        printf "%s\n%s %s\n\n# comment\n%s\n" \
 456                $(git rev-parse HEAD^^ HEAD^ HEAD^^ HEAD^2) \
 457                >.git/info/grafts &&
 458        git replace --convert-graft-file &&
 459        test_path_is_missing .git/info/grafts &&
 460
 461        : verify that the history is now "grafted" &&
 462        git rev-list HEAD >out &&
 463        test_line_count = 4 out &&
 464
 465        : create invalid graft file and verify that it is not deleted &&
 466        test_when_finished "rm -f .git/info/grafts" &&
 467        echo $EMPTY_BLOB $EMPTY_TREE >.git/info/grafts &&
 468        test_must_fail git replace --convert-graft-file 2>err &&
 469        test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" err &&
 470        test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" .git/info/grafts
 471'
 472
 473test_done