contrib / subtree / t / t7900-subtree.shon commit Merge branch 'nd/fixup-linked-gitdir' (91d5469)
   1#!/bin/sh
   2#
   3# Copyright (c) 2012 Avery Pennaraum
   4#
   5test_description='Basic porcelain support for subtrees
   6
   7This test verifies the basic operation of the merge, pull, add
   8and split subcommands of git subtree.
   9'
  10
  11TEST_DIRECTORY=$(pwd)/../../../t
  12export TEST_DIRECTORY
  13
  14. ../../../t/test-lib.sh
  15
  16create()
  17{
  18        echo "$1" >"$1"
  19        git add "$1"
  20}
  21
  22
  23check_equal()
  24{
  25        test_debug 'echo'
  26        test_debug "echo \"check a:\" \"{$1}\""
  27        test_debug "echo \"      b:\" \"{$2}\""
  28        if [ "$1" = "$2" ]; then
  29                return 0
  30        else
  31                return 1
  32        fi
  33}
  34
  35fixnl()
  36{
  37        t=""
  38        while read x; do
  39                t="$t$x "
  40        done
  41        echo $t
  42}
  43
  44multiline()
  45{
  46        while read x; do
  47                set -- $x
  48                for d in "$@"; do
  49                        echo "$d"
  50                done
  51        done
  52}
  53
  54undo()
  55{
  56        git reset --hard HEAD~
  57}
  58
  59last_commit_message()
  60{
  61        git log --pretty=format:%s -1
  62}
  63
  64test_expect_success 'init subproj' '
  65        test_create_repo subproj
  66'
  67
  68# To the subproject!
  69cd subproj
  70
  71test_expect_success 'add sub1' '
  72        create sub1 &&
  73        git commit -m "sub1" &&
  74        git branch sub1 &&
  75        git branch -m master subproj
  76'
  77
  78# Save this hash for testing later.
  79
  80subdir_hash=$(git rev-parse HEAD)
  81
  82test_expect_success 'add sub2' '
  83        create sub2 &&
  84        git commit -m "sub2" &&
  85        git branch sub2
  86'
  87
  88test_expect_success 'add sub3' '
  89        create sub3 &&
  90        git commit -m "sub3" &&
  91        git branch sub3
  92'
  93
  94# Back to mainline
  95cd ..
  96
  97test_expect_success 'enable log.date=relative to catch errors' '
  98        git config log.date relative
  99'
 100
 101test_expect_success 'add main4' '
 102        create main4 &&
 103        git commit -m "main4" &&
 104        git branch -m master mainline &&
 105        git branch subdir
 106'
 107
 108test_expect_success 'fetch subproj history' '
 109        git fetch ./subproj sub1 &&
 110        git branch sub1 FETCH_HEAD
 111'
 112
 113test_expect_success 'no subtree exists in main tree' '
 114        test_must_fail git subtree merge --prefix=subdir sub1
 115'
 116
 117test_expect_success 'no pull from non-existant subtree' '
 118        test_must_fail git subtree pull --prefix=subdir ./subproj sub1
 119'
 120
 121test_expect_success 'check if --message works for add' '
 122        git subtree add --prefix=subdir --message="Added subproject" sub1 &&
 123        check_equal ''"$(last_commit_message)"'' "Added subproject" &&
 124        undo
 125'
 126
 127test_expect_success 'check if --message works as -m and --prefix as -P' '
 128        git subtree add -P subdir -m "Added subproject using git subtree" sub1 &&
 129        check_equal ''"$(last_commit_message)"'' "Added subproject using git subtree" &&
 130        undo
 131'
 132
 133test_expect_success 'check if --message works with squash too' '
 134        git subtree add -P subdir -m "Added subproject with squash" --squash sub1 &&
 135        check_equal ''"$(last_commit_message)"'' "Added subproject with squash" &&
 136        undo
 137'
 138
 139test_expect_success 'add subproj to mainline' '
 140        git subtree add --prefix=subdir/ FETCH_HEAD &&
 141        check_equal ''"$(last_commit_message)"'' "Add '"'subdir/'"' from commit '"'"'''"$(git rev-parse sub1)"'''"'"'"
 142'
 143
 144# this shouldn't actually do anything, since FETCH_HEAD is already a parent
 145test_expect_success 'merge fetched subproj' '
 146        git merge -m "merge -s -ours" -s ours FETCH_HEAD
 147'
 148
 149test_expect_success 'add main-sub5' '
 150        create subdir/main-sub5 &&
 151        git commit -m "main-sub5"
 152'
 153
 154test_expect_success 'add main6' '
 155        create main6 &&
 156        git commit -m "main6 boring"
 157'
 158
 159test_expect_success 'add main-sub7' '
 160        create subdir/main-sub7 &&
 161        git commit -m "main-sub7"
 162'
 163
 164test_expect_success 'fetch new subproj history' '
 165        git fetch ./subproj sub2 &&
 166        git branch sub2 FETCH_HEAD
 167'
 168
 169test_expect_success 'check if --message works for merge' '
 170        git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2 &&
 171        check_equal ''"$(last_commit_message)"'' "Merged changes from subproject" &&
 172        undo
 173'
 174
 175test_expect_success 'check if --message for merge works with squash too' '
 176        git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2 &&
 177        check_equal ''"$(last_commit_message)"'' "Merged changes from subproject using squash" &&
 178        undo
 179'
 180
 181test_expect_success 'merge new subproj history into subdir' '
 182        git subtree merge --prefix=subdir FETCH_HEAD &&
 183        git branch pre-split &&
 184        check_equal ''"$(last_commit_message)"'' "Merge commit '"'"'"$(git rev-parse sub2)"'"'"' into mainline" &&
 185        undo
 186'
 187
 188test_expect_success 'Check that prefix argument is required for split' '
 189        echo "You must provide the --prefix option." > expected &&
 190        test_must_fail git subtree split > actual 2>&1 &&
 191        test_debug "printf '"'"'expected: '"'"'" &&
 192        test_debug "cat expected" &&
 193        test_debug "printf '"'"'actual: '"'"'" &&
 194        test_debug "cat actual" &&
 195        test_cmp expected actual &&
 196        rm -f expected actual
 197'
 198
 199test_expect_success 'Check that the <prefix> exists for a split' '
 200        echo "'"'"'non-existent-directory'"'"'" does not exist\; use "'"'"'git subtree add'"'"'" > expected &&
 201        test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
 202        test_debug "printf '"'"'expected: '"'"'" &&
 203        test_debug "cat expected" &&
 204        test_debug "printf '"'"'actual: '"'"'" &&
 205        test_debug "cat actual" &&
 206        test_cmp expected actual
 207#       rm -f expected actual
 208'
 209
 210test_expect_success 'check if --message works for split+rejoin' '
 211        spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
 212        git branch spl1 "$spl1" &&
 213        check_equal ''"$(last_commit_message)"'' "Split & rejoin" &&
 214        undo
 215'
 216
 217test_expect_success 'check split with --branch' '
 218        spl1=$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
 219        undo &&
 220        git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr1 &&
 221        check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
 222'
 223
 224test_expect_success 'check hash of split' '
 225        spl1=$(git subtree split --prefix subdir) &&
 226        git subtree split --prefix subdir --branch splitbr1test &&
 227        check_equal ''"$(git rev-parse splitbr1test)"'' "$spl1" &&
 228        new_hash=$(git rev-parse splitbr1test~2) &&
 229        check_equal ''"$new_hash"'' "$subdir_hash"
 230'
 231
 232test_expect_success 'check split with --branch for an existing branch' '
 233        spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
 234        undo &&
 235        git branch splitbr2 sub1 &&
 236        git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --branch splitbr2 &&
 237        check_equal ''"$(git rev-parse splitbr2)"'' "$spl1"
 238'
 239
 240test_expect_success 'check split with --branch for an incompatible branch' '
 241        test_must_fail git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir
 242'
 243
 244test_expect_success 'check split+rejoin' '
 245        spl1=''"$(git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
 246        undo &&
 247        git subtree split --annotate='"'*'"' --prefix subdir --onto FETCH_HEAD --rejoin &&
 248        check_equal ''"$(last_commit_message)"'' "Split '"'"'subdir/'"'"' into commit '"'"'"$spl1"'"'"'"
 249'
 250
 251test_expect_success 'add main-sub8' '
 252        create subdir/main-sub8 &&
 253        git commit -m "main-sub8"
 254'
 255
 256# To the subproject!
 257cd ./subproj
 258
 259test_expect_success 'merge split into subproj' '
 260        git fetch .. spl1 &&
 261        git branch spl1 FETCH_HEAD &&
 262        git merge FETCH_HEAD
 263'
 264
 265test_expect_success 'add sub9' '
 266        create sub9 &&
 267        git commit -m "sub9"
 268'
 269
 270# Back to mainline
 271cd ..
 272
 273test_expect_success 'split for sub8' '
 274        split2=''"$(git subtree split --annotate='"'*'"' --prefix subdir/ --rejoin)"'' &&
 275        git branch split2 "$split2"
 276'
 277
 278test_expect_success 'add main-sub10' '
 279        create subdir/main-sub10 &&
 280        git commit -m "main-sub10"
 281'
 282
 283test_expect_success 'split for sub10' '
 284        spl3=''"$(git subtree split --annotate='"'*'"' --prefix subdir --rejoin)"'' &&
 285        git branch spl3 "$spl3"
 286'
 287
 288# To the subproject!
 289cd ./subproj
 290
 291test_expect_success 'merge split into subproj' '
 292        git fetch .. spl3 &&
 293        git branch spl3 FETCH_HEAD &&
 294        git merge FETCH_HEAD &&
 295        git branch subproj-merge-spl3
 296'
 297
 298chkm="main4 main6"
 299chkms="main-sub10 main-sub5 main-sub7 main-sub8"
 300chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
 301chks="sub1 sub2 sub3 sub9"
 302chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
 303
 304test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
 305        subfiles=''"$(git ls-files | fixnl)"'' &&
 306        check_equal "$subfiles" "$chkms $chks"
 307'
 308
 309test_expect_success 'make sure the subproj history *only* contains commits that affect the subdir' '
 310        allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
 311        check_equal "$allchanges" "$chkms $chks"
 312'
 313
 314# Back to mainline
 315cd ..
 316
 317test_expect_success 'pull from subproj' '
 318        git fetch ./subproj subproj-merge-spl3 &&
 319        git branch subproj-merge-spl3 FETCH_HEAD &&
 320        git subtree pull --prefix=subdir ./subproj subproj-merge-spl3
 321'
 322
 323test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
 324        mainfiles=''"$(git ls-files | fixnl)"'' &&
 325        check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
 326'
 327
 328test_expect_success 'make sure each filename changed exactly once in the entire history' '
 329        # main-sub?? and /subdir/main-sub?? both change, because those are the
 330        # changes that were split into their own history.  And subdir/sub?? never
 331        # change, since they were *only* changed in the subtree branch.
 332        allchanges=''"$(git log --name-only --pretty=format:'"''"' | sort | fixnl)"'' &&
 333        check_equal "$allchanges" ''"$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"''
 334'
 335
 336test_expect_success 'make sure the --rejoin commits never make it into subproj' '
 337        check_equal ''"$(git log --pretty=format:'"'%s'"' HEAD^2 | grep -i split)"'' ""
 338'
 339
 340test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
 341        # They are meaningless to subproj since one side of the merge refers to the mainline
 342        check_equal ''"$(git log --pretty=format:'"'%s%n%b'"' HEAD^2 | grep "git-subtree.*:")"'' ""
 343'
 344
 345# prepare second pair of repositories
 346mkdir test2
 347cd test2
 348
 349test_expect_success 'init main' '
 350        test_create_repo main
 351'
 352
 353cd main
 354
 355test_expect_success 'add main1' '
 356        create main1 &&
 357        git commit -m "main1"
 358'
 359
 360cd ..
 361
 362test_expect_success 'init sub' '
 363        test_create_repo sub
 364'
 365
 366cd sub
 367
 368test_expect_success 'add sub2' '
 369        create sub2 &&
 370        git commit -m "sub2"
 371'
 372
 373cd ../main
 374
 375# check if split can find proper base without --onto
 376
 377test_expect_success 'add sub as subdir in main' '
 378        git fetch ../sub master &&
 379        git branch sub2 FETCH_HEAD &&
 380        git subtree add --prefix subdir sub2
 381'
 382
 383cd ../sub
 384
 385test_expect_success 'add sub3' '
 386        create sub3 &&
 387        git commit -m "sub3"
 388'
 389
 390cd ../main
 391
 392test_expect_success 'merge from sub' '
 393        git fetch ../sub master &&
 394        git branch sub3 FETCH_HEAD &&
 395        git subtree merge --prefix subdir sub3
 396'
 397
 398test_expect_success 'add main-sub4' '
 399        create subdir/main-sub4 &&
 400        git commit -m "main-sub4"
 401'
 402
 403test_expect_success 'split for main-sub4 without --onto' '
 404        git subtree split --prefix subdir --branch mainsub4
 405'
 406
 407# at this point, the new commit parent should be sub3 if it is not,
 408# something went wrong (the "newparent" of "master~" commit should
 409# have been sub3, but it was not, because its cache was not set to
 410# itself)
 411
 412test_expect_success 'check that the commit parent is sub3' '
 413        check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
 414'
 415
 416test_expect_success 'add main-sub5' '
 417        mkdir subdir2 &&
 418        create subdir2/main-sub5 &&
 419        git commit -m "main-sub5"
 420'
 421
 422test_expect_success 'split for main-sub5 without --onto' '
 423        # also test that we still can split out an entirely new subtree
 424        # if the parent of the first commit in the tree is not empty,
 425        # then the new subtree has accidentally been attached to something
 426        git subtree split --prefix subdir2 --branch mainsub5 &&
 427        check_equal ''"$(git log --pretty=format:%P -1 mainsub5)"'' ""
 428'
 429
 430# make sure no patch changes more than one file.  The original set of commits
 431# changed only one file each.  A multi-file change would imply that we pruned
 432# commits too aggressively.
 433joincommits()
 434{
 435        commit=
 436        all=
 437        while read x y; do
 438                #echo "{$x}" >&2
 439                if [ -z "$x" ]; then
 440                        continue
 441                elif [ "$x" = "commit:" ]; then
 442                        if [ -n "$commit" ]; then
 443                                echo "$commit $all"
 444                                all=
 445                        fi
 446                        commit="$y"
 447                else
 448                        all="$all $y"
 449                fi
 450        done
 451        echo "$commit $all"
 452}
 453
 454test_expect_success 'verify one file change per commit' '
 455        x= &&
 456        list=''"$(git log --pretty=format:'"'commit: %H'"' | joincommits)"'' &&
 457#       test_debug "echo HERE" &&
 458#       test_debug "echo ''"$list"''" &&
 459        (git log --pretty=format:'"'commit: %H'"' | joincommits |
 460        (       while read commit a b; do
 461                        test_debug "echo Verifying commit "''"$commit"''
 462                        test_debug "echo a: "''"$a"''
 463                        test_debug "echo b: "''"$b"''
 464                        check_equal "$b" ""
 465                        x=1
 466                done
 467                check_equal "$x" 1
 468        ))
 469'
 470
 471test_done