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