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