contrib / subtree / t / t7900-subtree.shon commit contrib/subtree: Make each test self-contained (4371174)
   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 add, pull, merge
   9and split subcommands of git subtree.
  10'
  11
  12TEST_DIRECTORY=$(pwd)/../../../t
  13export TEST_DIRECTORY
  14
  15. ../../../t/test-lib.sh
  16
  17subtree_test_create_repo()
  18{
  19        test_create_repo "$1"
  20        (
  21                cd $1
  22                git config log.date relative
  23        )
  24}
  25
  26create()
  27{
  28        echo "$1" >"$1"
  29        git add "$1"
  30}
  31
  32check_equal()
  33{
  34        test_debug 'echo'
  35        test_debug "echo \"check a:\" \"{$1}\""
  36        test_debug "echo \"      b:\" \"{$2}\""
  37        if [ "$1" = "$2" ]; then
  38                return 0
  39        else
  40                return 1
  41        fi
  42}
  43
  44undo()
  45{
  46        git reset --hard HEAD~
  47}
  48
  49# Make sure no patch changes more than one file.
  50# The original set of commits changed only one file each.
  51# A multi-file change would imply that we pruned commits
  52# too aggressively.
  53join_commits()
  54{
  55        commit=
  56        all=
  57        while read x y; do
  58                if [ -z "$x" ]; then
  59                        continue
  60                elif [ "$x" = "commit:" ]; then
  61                        if [ -n "$commit" ]; then
  62                                echo "$commit $all"
  63                                all=
  64                        fi
  65                        commit="$y"
  66                else
  67                        all="$all $y"
  68                fi
  69        done
  70        echo "$commit $all"
  71}
  72
  73test_create_commit() (
  74        repo=$1
  75        commit=$2
  76        cd "$repo"
  77        mkdir -p $(dirname "$commit") \
  78        || error "Could not create directory for commit"
  79        echo "$commit" >"$commit"
  80        git add "$commit" || error "Could not add commit"
  81        git commit -m "$commit" || error "Could not commit"
  82)
  83
  84last_commit_message()
  85{
  86        git log --pretty=format:%s -1
  87}
  88
  89subtree_test_count=0
  90next_test() {
  91        subtree_test_count=$(($subtree_test_count+1))
  92}
  93
  94#
  95# Tests for 'git subtree add'
  96#
  97
  98next_test
  99test_expect_success 'no merge from non-existent subtree' '
 100        subtree_test_create_repo "$subtree_test_count" &&
 101        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 102        test_create_commit "$subtree_test_count" main1 &&
 103        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 104        (
 105                cd "$subtree_test_count" &&
 106                git fetch ./"sub proj" master &&
 107                test_must_fail git subtree merge --prefix="sub dir" FETCH_HEAD
 108        )
 109'
 110
 111next_test
 112test_expect_success 'no pull from non-existent subtree' '
 113        subtree_test_create_repo "$subtree_test_count" &&
 114        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 115        test_create_commit "$subtree_test_count" main1 &&
 116        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 117        (
 118                cd "$subtree_test_count" &&
 119                git fetch ./"sub proj" master &&
 120                test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" master
 121        )'
 122
 123next_test
 124test_expect_success 'add subproj as subtree into sub dir/ with --prefix' '
 125        subtree_test_create_repo "$subtree_test_count" &&
 126        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 127        test_create_commit "$subtree_test_count" main1 &&
 128        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 129        (
 130                cd "$subtree_test_count" &&
 131                git fetch ./"sub proj" master &&
 132                git subtree add --prefix="sub dir" FETCH_HEAD &&
 133                check_equal "$(last_commit_message)" "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
 134        )
 135'
 136
 137next_test
 138test_expect_success 'add subproj as subtree into sub dir/ with --prefix and --message' '
 139        subtree_test_create_repo "$subtree_test_count" &&
 140        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 141        test_create_commit "$subtree_test_count" main1 &&
 142        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 143        (
 144                cd "$subtree_test_count" &&
 145                git fetch ./"sub proj" master &&
 146                git subtree add --prefix="sub dir" --message="Added subproject" FETCH_HEAD &&
 147                check_equal "$(last_commit_message)" "Added subproject"
 148        )
 149'
 150
 151next_test
 152test_expect_success 'add subproj as subtree into sub dir/ with --prefix as -P and --message as -m' '
 153        subtree_test_create_repo "$subtree_test_count" &&
 154        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 155        test_create_commit "$subtree_test_count" main1 &&
 156        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 157        (
 158                cd "$subtree_test_count" &&
 159                git fetch ./"sub proj" master &&
 160                git subtree add -P "sub dir" -m "Added subproject" FETCH_HEAD &&
 161                check_equal "$(last_commit_message)" "Added subproject"
 162        )
 163'
 164
 165next_test
 166test_expect_success 'add subproj as subtree into sub dir/ with --squash and --prefix and --message' '
 167        subtree_test_create_repo "$subtree_test_count" &&
 168        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 169        test_create_commit "$subtree_test_count" main1 &&
 170        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 171        (
 172                cd "$subtree_test_count" &&
 173                git fetch ./"sub proj" master &&
 174                git subtree add --prefix="sub dir" --message="Added subproject with squash" --squash FETCH_HEAD &&
 175                check_equal "$(last_commit_message)" "Added subproject with squash"
 176        )
 177'
 178
 179#
 180# Tests for 'git subtree merge'
 181#
 182
 183next_test
 184test_expect_success 'merge new subproj history into sub dir/ with --prefix' '
 185        subtree_test_create_repo "$subtree_test_count" &&
 186        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 187        test_create_commit "$subtree_test_count" main1 &&
 188        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 189        (
 190                cd "$subtree_test_count" &&
 191                git fetch ./"sub proj" master &&
 192                git subtree add --prefix="sub dir" FETCH_HEAD
 193        ) &&
 194        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 195        (
 196                cd "$subtree_test_count" &&
 197                git fetch ./"sub proj" master &&
 198                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 199                check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
 200        )
 201'
 202
 203next_test
 204test_expect_success 'merge new subproj history into sub dir/ with --prefix and --message' '
 205        subtree_test_create_repo "$subtree_test_count" &&
 206        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 207        test_create_commit "$subtree_test_count" main1 &&
 208        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 209        (
 210                cd "$subtree_test_count" &&
 211                git fetch ./"sub proj" master &&
 212                git subtree add --prefix="sub dir" FETCH_HEAD
 213        ) &&
 214        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 215        (
 216                cd "$subtree_test_count" &&
 217                git fetch ./"sub proj" master &&
 218                git subtree merge --prefix="sub dir" --message="Merged changes from subproject" FETCH_HEAD &&
 219                check_equal "$(last_commit_message)" "Merged changes from subproject"
 220        )
 221'
 222
 223next_test
 224test_expect_success 'merge new subproj history into sub dir/ with --squash and --prefix and --message' '
 225        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 226        subtree_test_create_repo "$subtree_test_count" &&
 227        test_create_commit "$subtree_test_count" main1 &&
 228        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 229        (
 230                cd "$subtree_test_count" &&
 231                git fetch ./"sub proj" master &&
 232                git subtree add --prefix="sub dir" FETCH_HEAD
 233        ) &&
 234        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 235        (
 236                cd "$subtree_test_count" &&
 237                git fetch ./"sub proj" master &&
 238                git subtree merge --prefix="sub dir" --message="Merged changes from subproject using squash" --squash FETCH_HEAD &&
 239                check_equal "$(last_commit_message)" "Merged changes from subproject using squash"
 240        )
 241'
 242
 243next_test
 244test_expect_success 'merge the added subproj again, should do nothing' '
 245        subtree_test_create_repo "$subtree_test_count" &&
 246        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 247        test_create_commit "$subtree_test_count" main1 &&
 248        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 249        (
 250                cd "$subtree_test_count" &&
 251                git fetch ./"sub proj" master &&
 252                git subtree add --prefix="sub dir" FETCH_HEAD &&
 253                # this shouldn not actually do anything, since FETCH_HEAD
 254                # is already a parent
 255                result=$(git merge -s ours -m "merge -s -ours" FETCH_HEAD) &&
 256                check_equal "${result}" "Already up-to-date."
 257        )
 258'
 259
 260#
 261# Tests for 'git subtree split'
 262#
 263
 264next_test
 265test_expect_success 'split requires option --prefix' '
 266        subtree_test_create_repo "$subtree_test_count" &&
 267        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 268        test_create_commit "$subtree_test_count" main1 &&
 269        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 270        (
 271                cd "$subtree_test_count" &&
 272                git fetch ./"sub proj" master &&
 273                git subtree add --prefix="sub dir" FETCH_HEAD &&
 274                echo "You must provide the --prefix option." > expected &&
 275                test_must_fail git subtree split > actual 2>&1 &&
 276                test_debug "printf '"expected: "'" &&
 277                test_debug "cat expected" &&
 278                test_debug "printf '"actual: "'" &&
 279                test_debug "cat actual" &&
 280                test_cmp expected actual
 281        )
 282'
 283
 284next_test
 285test_expect_success 'split requires path given by option --prefix must exist' '
 286        subtree_test_create_repo "$subtree_test_count" &&
 287        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 288        test_create_commit "$subtree_test_count" main1 &&
 289        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 290        (
 291                cd "$subtree_test_count" &&
 292                git fetch ./"sub proj" master &&
 293                git subtree add --prefix="sub dir" FETCH_HEAD &&
 294                echo "'\''non-existent-directory'\'' does not exist; use '\''git subtree add'\''" > expected &&
 295                test_must_fail git subtree split --prefix=non-existent-directory > actual 2>&1 &&
 296                test_debug "printf '"expected: "'" &&
 297                test_debug "cat expected" &&
 298                test_debug "printf '"actual: "'" &&
 299                test_debug "cat actual" &&
 300                test_cmp expected actual
 301        )
 302'
 303
 304next_test
 305test_expect_success 'split sub dir/ with --rejoin' '
 306        subtree_test_create_repo "$subtree_test_count" &&
 307        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 308        test_create_commit "$subtree_test_count" main1 &&
 309        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 310        (
 311                cd "$subtree_test_count" &&
 312                git fetch ./"sub proj" master &&
 313                git subtree add --prefix="sub dir" FETCH_HEAD
 314        ) &&
 315        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 316        test_create_commit "$subtree_test_count" main2 &&
 317        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 318        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 319        (
 320                cd "$subtree_test_count" &&
 321                git fetch ./"sub proj" master &&
 322                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 323                split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
 324                git subtree split --prefix="sub dir" --annotate="*" --rejoin &&
 325                check_equal "$(last_commit_message)" "Split '\''sub dir/'\'' into commit '\''$split_hash'\''"
 326        )
 327 '
 328
 329next_test
 330test_expect_success 'split sub dir/ with --rejoin and --message' '
 331        subtree_test_create_repo "$subtree_test_count" &&
 332        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 333        test_create_commit "$subtree_test_count" main1 &&
 334        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 335        (
 336                cd "$subtree_test_count" &&
 337                git fetch ./"sub proj" master &&
 338                git subtree add --prefix="sub dir" FETCH_HEAD
 339        ) &&
 340        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 341        test_create_commit "$subtree_test_count" main2 &&
 342        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 343        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 344        (
 345                cd "$subtree_test_count" &&
 346                git fetch ./"sub proj" master &&
 347                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 348                git subtree split --prefix="sub dir" --message="Split & rejoin" --annotate="*" --rejoin &&
 349                check_equal "$(last_commit_message)" "Split & rejoin"
 350        )
 351'
 352
 353next_test
 354test_expect_success 'split "sub dir"/ with --branch' '
 355        subtree_test_create_repo "$subtree_test_count" &&
 356        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 357        test_create_commit "$subtree_test_count" main1 &&
 358        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 359        (
 360                cd "$subtree_test_count" &&
 361                git fetch ./"sub proj" master &&
 362                git subtree add --prefix="sub dir" FETCH_HEAD
 363        ) &&
 364        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 365        test_create_commit "$subtree_test_count" main2 &&
 366        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 367        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 368        (
 369                cd "$subtree_test_count" &&
 370                git fetch ./"sub proj" master &&
 371                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 372                split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
 373                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
 374                check_equal "$(git rev-parse subproj-br)" "$split_hash"
 375        )
 376'
 377
 378next_test
 379test_expect_success 'check hash of split' '
 380        subtree_test_create_repo "$subtree_test_count" &&
 381        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 382        test_create_commit "$subtree_test_count" main1 &&
 383        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 384        (
 385                cd "$subtree_test_count" &&
 386                git fetch ./"sub proj" master &&
 387                git subtree add --prefix="sub dir" FETCH_HEAD
 388        ) &&
 389        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 390        test_create_commit "$subtree_test_count" main2 &&
 391        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 392        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 393        (
 394                cd "$subtree_test_count" &&
 395                git fetch ./"sub proj" master &&
 396                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 397                split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
 398                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
 399                check_equal "$(git rev-parse subproj-br)" "$split_hash" &&
 400                # Check hash of split
 401                new_hash=$(git rev-parse subproj-br^2) &&
 402                (
 403                        cd ./"sub proj" &&
 404                        subdir_hash=$(git rev-parse HEAD) &&
 405                        check_equal ''"$new_hash"'' "$subdir_hash"
 406                )
 407        )
 408'
 409
 410next_test
 411test_expect_success 'split "sub dir"/ with --branch for an existing branch' '
 412        subtree_test_create_repo "$subtree_test_count" &&
 413        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 414        test_create_commit "$subtree_test_count" main1 &&
 415        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 416        (
 417                cd "$subtree_test_count" &&
 418                git fetch ./"sub proj" master &&
 419                git branch subproj-br FETCH_HEAD &&
 420                git subtree add --prefix="sub dir" FETCH_HEAD
 421        ) &&
 422        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 423        test_create_commit "$subtree_test_count" main2 &&
 424        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 425        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 426        (
 427                cd "$subtree_test_count" &&
 428                git fetch ./"sub proj" master &&
 429                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 430                split_hash=$(git subtree split --prefix="sub dir" --annotate="*") &&
 431                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br &&
 432                check_equal "$(git rev-parse subproj-br)" "$split_hash"
 433        )
 434'
 435
 436next_test
 437test_expect_success 'split "sub dir"/ with --branch for an incompatible branch' '
 438        subtree_test_create_repo "$subtree_test_count" &&
 439        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 440        test_create_commit "$subtree_test_count" main1 &&
 441        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 442        (
 443                cd "$subtree_test_count" &&
 444                git branch init HEAD &&
 445                git fetch ./"sub proj" master &&
 446                git subtree add --prefix="sub dir" FETCH_HEAD
 447        ) &&
 448        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 449        test_create_commit "$subtree_test_count" main2 &&
 450        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 451        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 452        (
 453                cd "$subtree_test_count" &&
 454                git fetch ./"sub proj" master &&
 455                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 456                test_must_fail git subtree split --prefix="sub dir" --branch init
 457        )
 458'
 459
 460#
 461# Validity checking
 462#
 463
 464next_test
 465test_expect_success 'make sure exactly the right set of files ends up in the subproj' '
 466        subtree_test_create_repo "$subtree_test_count" &&
 467        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 468        test_create_commit "$subtree_test_count" main1 &&
 469        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 470        (
 471                cd "$subtree_test_count" &&
 472                git fetch ./"sub proj" master &&
 473                git subtree add --prefix="sub dir" FETCH_HEAD
 474        ) &&
 475        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 476        test_create_commit "$subtree_test_count" main2 &&
 477        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 478        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 479        (
 480                cd "$subtree_test_count" &&
 481                git fetch ./"sub proj" master &&
 482                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 483                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 484        ) &&
 485        test_create_commit "$subtree_test_count/sub proj" sub3 &&
 486        test_create_commit "$subtree_test_count" "sub dir"/main-sub3 &&
 487        (
 488                cd "$subtree_test_count/sub proj" &&
 489                git fetch .. subproj-br &&
 490                git merge FETCH_HEAD
 491        ) &&
 492        test_create_commit "$subtree_test_count/sub proj" sub4 &&
 493        (
 494                cd "$subtree_test_count" &&
 495                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 496        ) &&
 497        test_create_commit "$subtree_test_count" "sub dir"/main-sub4 &&
 498        (
 499                cd "$subtree_test_count" &&
 500                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 501        ) &&
 502        (
 503                cd "$subtree_test_count/sub proj" &&
 504                git fetch .. subproj-br &&
 505                git merge FETCH_HEAD &&
 506
 507                chks="sub1
 508sub2
 509sub3
 510sub4" &&
 511                chks_sub=$(cat <<TXT | sed '\''s,^,sub dir/,'\''
 512$chks
 513TXT
 514) &&
 515                chkms="main-sub1
 516main-sub2
 517main-sub3
 518main-sub4" &&
 519                chkms_sub=$(cat <<TXT | sed '\''s,^,sub dir/,'\''
 520$chkms
 521TXT
 522) &&
 523
 524                subfiles=$(git ls-files) &&
 525                check_equal "$subfiles" "$chkms
 526$chks"
 527        )
 528'
 529
 530next_test
 531test_expect_success 'make sure the subproj *only* contains commits that affect the "sub dir"' '
 532        subtree_test_create_repo "$subtree_test_count" &&
 533        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 534        test_create_commit "$subtree_test_count" main1 &&
 535        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 536        (
 537                cd "$subtree_test_count" &&
 538                git fetch ./"sub proj" master &&
 539                git subtree add --prefix="sub dir" FETCH_HEAD
 540        ) &&
 541        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 542        test_create_commit "$subtree_test_count" main2 &&
 543        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 544        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 545        (
 546                cd "$subtree_test_count" &&
 547                git fetch ./"sub proj" master &&
 548                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 549                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 550        ) &&
 551        test_create_commit "$subtree_test_count/sub proj" sub3 &&
 552        test_create_commit "$subtree_test_count" "sub dir"/main-sub3 &&
 553        (
 554                cd "$subtree_test_count/sub proj" &&
 555                git fetch .. subproj-br &&
 556                git merge FETCH_HEAD
 557        ) &&
 558        test_create_commit "$subtree_test_count/sub proj" sub4 &&
 559        (
 560                cd "$subtree_test_count" &&
 561                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 562        ) &&
 563        test_create_commit "$subtree_test_count" "sub dir"/main-sub4 &&
 564        (
 565                cd "$subtree_test_count" &&
 566                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 567        ) &&
 568        (
 569                cd "$subtree_test_count/sub proj" &&
 570                git fetch .. subproj-br &&
 571                git merge FETCH_HEAD &&
 572
 573                chks="sub1
 574sub2
 575sub3
 576sub4" &&
 577                chks_sub=$(cat <<TXT | sed '\''s,^,sub dir/,'\''
 578$chks
 579TXT
 580) &&
 581                chkms="main-sub1
 582main-sub2
 583main-sub3
 584main-sub4" &&
 585                chkms_sub=$(cat <<TXT | sed '\''s,^,sub dir/,'\''
 586$chkms
 587TXT
 588) &&
 589                allchanges=$(git log --name-only --pretty=format:"" | sort | sed "/^$/d") &&
 590                check_equal "$allchanges" "$chkms
 591$chks"
 592        )
 593'
 594
 595next_test
 596test_expect_success 'make sure exactly the right set of files ends up in the mainline' '
 597        subtree_test_create_repo "$subtree_test_count" &&
 598        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 599        test_create_commit "$subtree_test_count" main1 &&
 600        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 601        (
 602                cd "$subtree_test_count" &&
 603                git fetch ./"sub proj" master &&
 604                git subtree add --prefix="sub dir" FETCH_HEAD
 605        ) &&
 606        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 607        test_create_commit "$subtree_test_count" main2 &&
 608        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 609        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 610        (
 611                cd "$subtree_test_count" &&
 612                git fetch ./"sub proj" master &&
 613                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 614                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 615        ) &&
 616        test_create_commit "$subtree_test_count/sub proj" sub3 &&
 617        test_create_commit "$subtree_test_count" "sub dir"/main-sub3 &&
 618        (
 619                cd "$subtree_test_count/sub proj" &&
 620                git fetch .. subproj-br &&
 621                git merge FETCH_HEAD
 622        ) &&
 623        test_create_commit "$subtree_test_count/sub proj" sub4 &&
 624        (
 625                cd "$subtree_test_count" &&
 626                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 627        ) &&
 628        test_create_commit "$subtree_test_count" "sub dir"/main-sub4 &&
 629        (
 630                cd "$subtree_test_count" &&
 631                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 632        ) &&
 633        (
 634                cd "$subtree_test_count/sub proj" &&
 635                git fetch .. subproj-br &&
 636                git merge FETCH_HEAD
 637        ) &&
 638        (
 639                cd "$subtree_test_count" &&
 640                git subtree pull --prefix="sub dir" ./"sub proj" master &&
 641
 642                chkm="main1
 643main2" &&
 644                chks="sub1
 645sub2
 646sub3
 647sub4" &&
 648                chks_sub=$(cat <<TXT | sed '\''s,^,sub dir/,'\''
 649$chks
 650TXT
 651) &&
 652                chkms="main-sub1
 653main-sub2
 654main-sub3
 655main-sub4" &&
 656                chkms_sub=$(cat <<TXT | sed '\''s,^,sub dir/,'\''
 657$chkms
 658TXT
 659) &&
 660                mainfiles=$(git ls-files) &&
 661                check_equal "$mainfiles" "$chkm
 662$chkms_sub
 663$chks_sub"
 664)
 665'
 666
 667next_test
 668test_expect_success 'make sure each filename changed exactly once in the entire history' '
 669        subtree_test_create_repo "$subtree_test_count" &&
 670        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 671        test_create_commit "$subtree_test_count" main1 &&
 672        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 673        (
 674                cd "$subtree_test_count" &&
 675                git config log.date relative
 676                git fetch ./"sub proj" master &&
 677                git subtree add --prefix="sub dir" FETCH_HEAD
 678        ) &&
 679        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 680        test_create_commit "$subtree_test_count" main2 &&
 681        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 682        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 683        (
 684                cd "$subtree_test_count" &&
 685                git fetch ./"sub proj" master &&
 686                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 687                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 688        ) &&
 689        test_create_commit "$subtree_test_count/sub proj" sub3 &&
 690        test_create_commit "$subtree_test_count" "sub dir"/main-sub3 &&
 691        (
 692                cd "$subtree_test_count/sub proj" &&
 693                git fetch .. subproj-br &&
 694                git merge FETCH_HEAD
 695        ) &&
 696        test_create_commit "$subtree_test_count/sub proj" sub4 &&
 697        (
 698                cd "$subtree_test_count" &&
 699                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 700        ) &&
 701        test_create_commit "$subtree_test_count" "sub dir"/main-sub4 &&
 702        (
 703                cd "$subtree_test_count" &&
 704                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 705        ) &&
 706        (
 707                cd "$subtree_test_count/sub proj" &&
 708                git fetch .. subproj-br &&
 709                git merge FETCH_HEAD
 710        ) &&
 711        (
 712                cd "$subtree_test_count" &&
 713                git subtree pull --prefix="sub dir" ./"sub proj" master &&
 714
 715                chkm="main1
 716main2" &&
 717                chks="sub1
 718sub2
 719sub3
 720sub4" &&
 721                chks_sub=$(cat <<TXT | sed '\''s,^,sub dir/,'\''
 722$chks
 723TXT
 724) &&
 725                chkms="main-sub1
 726main-sub2
 727main-sub3
 728main-sub4" &&
 729                chkms_sub=$(cat <<TXT | sed '\''s,^,sub dir/,'\''
 730$chkms
 731TXT
 732) &&
 733
 734                # main-sub?? and /"sub dir"/main-sub?? both change, because those are the
 735                # changes that were split into their own history.  And "sub dir"/sub?? never
 736                # change, since they were *only* changed in the subtree branch.
 737                allchanges=$(git log --name-only --pretty=format:"" | sort | sed "/^$/d") &&
 738                expected=''"$(cat <<TXT | sort
 739$chkms
 740$chkm
 741$chks
 742$chkms_sub
 743TXT
 744)"'' &&
 745                check_equal "$allchanges" "$expected"
 746        )
 747'
 748
 749next_test
 750test_expect_success 'make sure the --rejoin commits never make it into subproj' '
 751        subtree_test_create_repo "$subtree_test_count" &&
 752        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 753        test_create_commit "$subtree_test_count" main1 &&
 754        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 755        (
 756                cd "$subtree_test_count" &&
 757                git fetch ./"sub proj" master &&
 758                git subtree add --prefix="sub dir" FETCH_HEAD
 759        ) &&
 760        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 761        test_create_commit "$subtree_test_count" main2 &&
 762        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 763        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 764        (
 765                cd "$subtree_test_count" &&
 766                git fetch ./"sub proj" master &&
 767                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 768                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 769        ) &&
 770        test_create_commit "$subtree_test_count/sub proj" sub3 &&
 771        test_create_commit "$subtree_test_count" "sub dir"/main-sub3 &&
 772        (
 773                cd "$subtree_test_count/sub proj" &&
 774                git fetch .. subproj-br &&
 775                git merge FETCH_HEAD
 776        ) &&
 777        test_create_commit "$subtree_test_count/sub proj" sub4 &&
 778        (
 779                cd "$subtree_test_count" &&
 780                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 781        ) &&
 782        test_create_commit "$subtree_test_count" "sub dir"/main-sub4 &&
 783        (
 784                cd "$subtree_test_count" &&
 785                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 786        ) &&
 787        (
 788                cd "$subtree_test_count/sub proj" &&
 789                git fetch .. subproj-br &&
 790                git merge FETCH_HEAD
 791        ) &&
 792        (
 793                cd "$subtree_test_count" &&
 794                git subtree pull --prefix="sub dir" ./"sub proj" master &&
 795                check_equal "$(git log --pretty=format:"%s" HEAD^2 | grep -i split)" ""
 796        )
 797'
 798
 799next_test
 800test_expect_success 'make sure no "git subtree" tagged commits make it into subproj' '
 801        subtree_test_create_repo "$subtree_test_count" &&
 802        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 803        test_create_commit "$subtree_test_count" main1 &&
 804        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 805        (
 806                cd "$subtree_test_count" &&
 807                git fetch ./"sub proj" master &&
 808                git subtree add --prefix="sub dir" FETCH_HEAD
 809        ) &&
 810        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 811        test_create_commit "$subtree_test_count" main2 &&
 812        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 813        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 814        (
 815                cd "$subtree_test_count" &&
 816                git fetch ./"sub proj" master &&
 817                git subtree merge --prefix="sub dir" FETCH_HEAD &&
 818                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 819        ) &&
 820        test_create_commit "$subtree_test_count/sub proj" sub3 &&
 821        test_create_commit "$subtree_test_count" "sub dir"/main-sub3 &&
 822        (
 823                cd "$subtree_test_count/sub proj" &&
 824                git fetch .. subproj-br &&
 825                 git merge FETCH_HEAD
 826        ) &&
 827        test_create_commit "$subtree_test_count/sub proj" sub4 &&
 828        (
 829                cd "$subtree_test_count" &&
 830                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 831        ) &&
 832        test_create_commit "$subtree_test_count" "sub dir"/main-sub4 &&
 833        (
 834                cd "$subtree_test_count" &&
 835                git subtree split --prefix="sub dir" --annotate="*" --branch subproj-br --rejoin
 836        ) &&
 837        (
 838                cd "$subtree_test_count/sub proj" &&
 839                git fetch .. subproj-br &&
 840                git merge FETCH_HEAD
 841        ) &&
 842        (
 843                cd "$subtree_test_count" &&
 844                git subtree pull --prefix="sub dir" ./"sub proj" master &&
 845
 846                # They are meaningless to subproj since one side of the merge refers to the mainline
 847                check_equal "$(git log --pretty=format:"%s%n%b" HEAD^2 | grep "git-subtree.*:")" ""
 848        )
 849'
 850
 851#
 852# A new set of tests
 853#
 854
 855next_test
 856test_expect_success 'make sure "git subtree split" find the correct parent' '
 857        subtree_test_create_repo "$subtree_test_count" &&
 858        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 859        test_create_commit "$subtree_test_count" main1 &&
 860        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 861        (
 862                cd "$subtree_test_count" &&
 863                git fetch ./"sub proj" master &&
 864                git subtree add --prefix="sub dir" FETCH_HEAD
 865        ) &&
 866        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 867        (
 868                cd "$subtree_test_count" &&
 869                git fetch ./"sub proj" master &&
 870                git branch subproj-ref FETCH_HEAD &&
 871                git subtree merge --prefix="sub dir" FETCH_HEAD
 872        ) &&
 873        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 874        (
 875                cd "$subtree_test_count" &&
 876                git subtree split --prefix="sub dir" --branch subproj-br &&
 877
 878                # at this point, the new commit parent should be subproj-ref, if it is
 879                # not, something went wrong (the "newparent" of "master~" commit should
 880                # have been sub2, but it was not, because its cache was not set to
 881                # itself)
 882                check_equal "$(git log --pretty=format:%P -1 subproj-br)" "$(git rev-parse subproj-ref)"
 883        )
 884'
 885
 886next_test
 887test_expect_success 'split a new subtree without --onto option' '
 888        subtree_test_create_repo "$subtree_test_count" &&
 889        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 890        test_create_commit "$subtree_test_count" main1 &&
 891        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 892        (
 893                cd "$subtree_test_count" &&
 894                git fetch ./"sub proj" master &&
 895                git subtree add --prefix="sub dir" FETCH_HEAD
 896        ) &&
 897        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 898        (
 899                cd "$subtree_test_count" &&
 900                git fetch ./"sub proj" master &&
 901                git subtree merge --prefix="sub dir" FETCH_HEAD
 902        ) &&
 903        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 904        (
 905                cd "$subtree_test_count" &&
 906                git subtree split --prefix="sub dir" --branch subproj-br
 907        ) &&
 908        mkdir "$subtree_test_count"/"sub dir2" &&
 909        test_create_commit "$subtree_test_count" "sub dir2"/main-sub2 &&
 910        (
 911                cd "$subtree_test_count" &&
 912
 913                # also test that we still can split out an entirely new subtree
 914                # if the parent of the first commit in the tree is not empty,
 915                # then the new subtree has accidently been attached to something
 916                git subtree split --prefix="sub dir2" --branch subproj2-br &&
 917                check_equal "$(git log --pretty=format:%P -1 subproj2-br)" ""
 918        )
 919'
 920
 921next_test
 922test_expect_success 'verify one file change per commit' '
 923        subtree_test_create_repo "$subtree_test_count" &&
 924        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 925        test_create_commit "$subtree_test_count" main1 &&
 926        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 927        (
 928                cd "$subtree_test_count" &&
 929                git fetch ./"sub proj" master &&
 930                git branch sub1 FETCH_HEAD &&
 931                git subtree add --prefix="sub dir" sub1
 932        ) &&
 933        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 934        (
 935                cd "$subtree_test_count" &&
 936                git fetch ./"sub proj" master &&
 937                git subtree merge --prefix="sub dir" FETCH_HEAD
 938        ) &&
 939        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 940        (
 941                cd "$subtree_test_count" &&
 942                git subtree split --prefix="sub dir" --branch subproj-br
 943        ) &&
 944        mkdir "$subtree_test_count"/"sub dir2" &&
 945        test_create_commit "$subtree_test_count" "sub dir2"/main-sub2 &&
 946        (
 947                cd "$subtree_test_count" &&
 948                git subtree split --prefix="sub dir2" --branch subproj2-br &&
 949
 950                x= &&
 951                git log --pretty=format:"commit: %H" | join_commits |
 952                (
 953                        while read commit a b; do
 954                                test_debug "echo Verifying commit $commit"
 955                                test_debug "echo a: $a"
 956                                test_debug "echo b: $b"
 957                                check_equal "$b" ""
 958                                x=1
 959                        done
 960                        check_equal "$x" 1
 961                )
 962        )
 963'
 964
 965next_test
 966test_expect_success 'push split to subproj' '
 967        subtree_test_create_repo "$subtree_test_count" &&
 968        subtree_test_create_repo "$subtree_test_count/sub proj" &&
 969        test_create_commit "$subtree_test_count" main1 &&
 970        test_create_commit "$subtree_test_count/sub proj" sub1 &&
 971        (
 972                cd "$subtree_test_count" &&
 973                git fetch ./"sub proj" master &&
 974                git subtree add --prefix="sub dir" FETCH_HEAD
 975        ) &&
 976        test_create_commit "$subtree_test_count" "sub dir"/main-sub1 &&
 977        test_create_commit "$subtree_test_count" main2 &&
 978        test_create_commit "$subtree_test_count/sub proj" sub2 &&
 979        test_create_commit "$subtree_test_count" "sub dir"/main-sub2 &&
 980        (
 981                cd $subtree_test_count/"sub proj" &&
 982                git branch sub-branch-1 &&
 983                cd .. &&
 984                git fetch ./"sub proj" master &&
 985                git subtree merge --prefix="sub dir" FETCH_HEAD
 986        ) &&
 987        test_create_commit "$subtree_test_count" "sub dir"/main-sub3 &&
 988        (
 989                cd "$subtree_test_count" &&
 990                git subtree push ./"sub proj" --prefix "sub dir" sub-branch-1 &&
 991                cd ./"sub proj" &&
 992                git checkout sub-branch-1 &&
 993                check_equal "$(last_commit_message)" "sub dir/main-sub3"
 994        )
 995'
 996
 997test_done