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