t / t6050-replace.shon commit Convert many resolve_ref() calls to read_ref*() and ref_exists() (c689332)
   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
  21HASH1=
  22HASH2=
  23HASH3=
  24HASH4=
  25HASH5=
  26HASH6=
  27HASH7=
  28
  29test_expect_success 'set up buggy branch' '
  30     echo "line 1" >> hello &&
  31     echo "line 2" >> hello &&
  32     echo "line 3" >> hello &&
  33     echo "line 4" >> hello &&
  34     add_and_commit_file hello "4 lines" &&
  35     HASH1=$(git rev-parse --verify HEAD) &&
  36     echo "line BUG" >> hello &&
  37     echo "line 6" >> hello &&
  38     echo "line 7" >> hello &&
  39     echo "line 8" >> hello &&
  40     add_and_commit_file hello "4 more lines with a BUG" &&
  41     HASH2=$(git rev-parse --verify HEAD) &&
  42     echo "line 9" >> hello &&
  43     echo "line 10" >> hello &&
  44     add_and_commit_file hello "2 more lines" &&
  45     HASH3=$(git rev-parse --verify HEAD) &&
  46     echo "line 11" >> hello &&
  47     add_and_commit_file hello "1 more line" &&
  48     HASH4=$(git rev-parse --verify HEAD) &&
  49     sed -e "s/BUG/5/" hello > hello.new &&
  50     mv hello.new hello &&
  51     add_and_commit_file hello "BUG fixed" &&
  52     HASH5=$(git rev-parse --verify HEAD) &&
  53     echo "line 12" >> hello &&
  54     echo "line 13" >> hello &&
  55     add_and_commit_file hello "2 more lines" &&
  56     HASH6=$(git rev-parse --verify HEAD) &&
  57     echo "line 14" >> hello &&
  58     echo "line 15" >> hello &&
  59     echo "line 16" >> hello &&
  60     add_and_commit_file hello "again 3 more lines" &&
  61     HASH7=$(git rev-parse --verify HEAD)
  62'
  63
  64test_expect_success 'replace the author' '
  65     git cat-file commit $HASH2 | grep "author A U Thor" &&
  66     R=$(git cat-file commit $HASH2 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
  67     git cat-file commit $R | grep "author O Thor" &&
  68     git update-ref refs/replace/$HASH2 $R &&
  69     git show HEAD~5 | grep "O Thor" &&
  70     git show $HASH2 | grep "O Thor"
  71'
  72
  73test_expect_success 'test --no-replace-objects option' '
  74     git cat-file commit $HASH2 | grep "author O Thor" &&
  75     git --no-replace-objects cat-file commit $HASH2 | grep "author A U Thor" &&
  76     git show $HASH2 | grep "O Thor" &&
  77     git --no-replace-objects show $HASH2 | grep "A U Thor"
  78'
  79
  80test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
  81     GIT_NO_REPLACE_OBJECTS=1 git cat-file commit $HASH2 | grep "author A U Thor" &&
  82     GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 | grep "A U Thor"
  83'
  84
  85cat >tag.sig <<EOF
  86object $HASH2
  87type commit
  88tag mytag
  89tagger T A Gger <> 0 +0000
  90
  91EOF
  92
  93test_expect_success 'tag replaced commit' '
  94     git mktag <tag.sig >.git/refs/tags/mytag 2>message
  95'
  96
  97test_expect_success '"git fsck" works' '
  98     git fsck master > fsck_master.out &&
  99     grep "dangling commit $R" fsck_master.out &&
 100     grep "dangling tag $(cat .git/refs/tags/mytag)" fsck_master.out &&
 101     test -z "$(git fsck)"
 102'
 103
 104test_expect_success 'repack, clone and fetch work' '
 105     git repack -a -d &&
 106     git clone --no-hardlinks . clone_dir &&
 107     (
 108          cd clone_dir &&
 109          git show HEAD~5 | grep "A U Thor" &&
 110          git show $HASH2 | grep "A U Thor" &&
 111          git cat-file commit $R &&
 112          git repack -a -d &&
 113          test_must_fail git cat-file commit $R &&
 114          git fetch ../ "refs/replace/*:refs/replace/*" &&
 115          git show HEAD~5 | grep "O Thor" &&
 116          git show $HASH2 | grep "O Thor" &&
 117          git cat-file commit $R
 118     )
 119'
 120
 121test_expect_success '"git replace" listing and deleting' '
 122     test "$HASH2" = "$(git replace -l)" &&
 123     test "$HASH2" = "$(git replace)" &&
 124     aa=${HASH2%??????????????????????????????????????} &&
 125     test "$HASH2" = "$(git replace -l "$aa*")" &&
 126     test_must_fail git replace -d $R &&
 127     test_must_fail git replace -d &&
 128     test_must_fail git replace -l -d $HASH2 &&
 129     git replace -d $HASH2 &&
 130     git show $HASH2 | grep "A U Thor" &&
 131     test -z "$(git replace -l)"
 132'
 133
 134test_expect_success '"git replace" replacing' '
 135     git replace $HASH2 $R &&
 136     git show $HASH2 | grep "O Thor" &&
 137     test_must_fail git replace $HASH2 $R &&
 138     git replace -f $HASH2 $R &&
 139     test_must_fail git replace -f &&
 140     test "$HASH2" = "$(git replace)"
 141'
 142
 143# This creates a side branch where the bug in H2
 144# does not appear because P2 is created by applying
 145# H2 and squashing H5 into it.
 146# P3, P4 and P6 are created by cherry-picking H3, H4
 147# and H6 respectively.
 148#
 149# At this point, we should have the following:
 150#
 151#    P2--P3--P4--P6
 152#   /
 153# H1-H2-H3-H4-H5-H6-H7
 154#
 155# Then we replace H6 with P6.
 156#
 157test_expect_success 'create parallel branch without the bug' '
 158     git replace -d $HASH2 &&
 159     git show $HASH2 | grep "A U Thor" &&
 160     git checkout $HASH1 &&
 161     git cherry-pick $HASH2 &&
 162     git show $HASH5 | git apply &&
 163     git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
 164     PARA2=$(git rev-parse --verify HEAD) &&
 165     git cherry-pick $HASH3 &&
 166     PARA3=$(git rev-parse --verify HEAD) &&
 167     git cherry-pick $HASH4 &&
 168     PARA4=$(git rev-parse --verify HEAD) &&
 169     git cherry-pick $HASH6 &&
 170     PARA6=$(git rev-parse --verify HEAD) &&
 171     git replace $HASH6 $PARA6 &&
 172     git checkout master &&
 173     cur=$(git rev-parse --verify HEAD) &&
 174     test "$cur" = "$HASH7" &&
 175     git log --pretty=oneline | grep $PARA2 &&
 176     git remote add cloned ./clone_dir
 177'
 178
 179test_expect_success 'push to cloned repo' '
 180     git push cloned $HASH6^:refs/heads/parallel &&
 181     (
 182          cd clone_dir &&
 183          git checkout parallel &&
 184          git log --pretty=oneline | grep $PARA2
 185     )
 186'
 187
 188test_expect_success 'push branch with replacement' '
 189     git cat-file commit $PARA3 | grep "author A U Thor" &&
 190     S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
 191     git cat-file commit $S | grep "author O Thor" &&
 192     git replace $PARA3 $S &&
 193     git show $HASH6~2 | grep "O Thor" &&
 194     git show $PARA3 | grep "O Thor" &&
 195     git push cloned $HASH6^:refs/heads/parallel2 &&
 196     (
 197          cd clone_dir &&
 198          git checkout parallel2 &&
 199          git log --pretty=oneline | grep $PARA3 &&
 200          git show $PARA3 | grep "A U Thor"
 201     )
 202'
 203
 204test_expect_success 'fetch branch with replacement' '
 205     git branch tofetch $HASH6 &&
 206     (
 207          cd clone_dir &&
 208          git fetch origin refs/heads/tofetch:refs/heads/parallel3 &&
 209          git log --pretty=oneline parallel3 > output.txt &&
 210          ! grep $PARA3 output.txt &&
 211          git show $PARA3 > para3.txt &&
 212          grep "A U Thor" para3.txt &&
 213          git fetch origin "refs/replace/*:refs/replace/*" &&
 214          git log --pretty=oneline parallel3 > output.txt &&
 215          grep $PARA3 output.txt &&
 216          git show $PARA3 > para3.txt &&
 217          grep "O Thor" para3.txt
 218     )
 219'
 220
 221test_expect_success 'bisect and replacements' '
 222     git bisect start $HASH7 $HASH1 &&
 223     test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
 224     git bisect reset &&
 225     GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
 226     test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
 227     git bisect reset &&
 228     git --no-replace-objects bisect start $HASH7 $HASH1 &&
 229     test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
 230     git bisect reset
 231'
 232
 233test_expect_success 'index-pack and replacements' '
 234        git --no-replace-objects rev-list --objects HEAD |
 235        git --no-replace-objects pack-objects test- &&
 236        git index-pack test-*.pack
 237'
 238
 239test_expect_success 'not just commits' '
 240        echo replaced >file &&
 241        git add file &&
 242        REPLACED=$(git rev-parse :file) &&
 243        mv file file.replaced &&
 244
 245        echo original >file &&
 246        git add file &&
 247        ORIGINAL=$(git rev-parse :file) &&
 248        git update-ref refs/replace/$ORIGINAL $REPLACED &&
 249        mv file file.original &&
 250
 251        git checkout file &&
 252        test_cmp file.replaced file
 253'
 254
 255test_done