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