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