test.shon commit Jakub's changes broke the progress message slightly. (e2d0a45)
   1#!/bin/bash
   2. shellopts.sh
   3set -e
   4
   5create()
   6{
   7        echo "$1" >"$1"
   8        git add "$1"
   9}
  10
  11check()
  12{
  13        echo
  14        echo "check:" "$@"
  15        if "$@"; then
  16                echo ok
  17                return 0
  18        else
  19                echo FAILED
  20                exit 1
  21        fi
  22}
  23
  24check_equal()
  25{
  26        echo
  27        echo "check a:" "{$1}"
  28        echo "      b:" "{$2}"
  29        if [ "$1" = "$2" ]; then
  30                return 0
  31        else
  32                echo FAILED
  33                exit 1
  34        fi
  35}
  36
  37fixnl()
  38{       
  39        t=""
  40        while read x; do
  41                t="$t$x "
  42        done
  43        echo $t
  44}
  45
  46multiline()
  47{
  48        while read x; do
  49                set -- $x
  50                for d in "$@"; do
  51                        echo "$d"
  52                done
  53        done
  54}
  55
  56undo()
  57{
  58        git reset --hard HEAD~
  59}
  60
  61last_commit_message()
  62{
  63        git log --format=%s -1
  64}
  65
  66rm -rf mainline subproj
  67mkdir mainline subproj
  68
  69cd subproj
  70git init
  71
  72create sub1
  73git commit -m 'sub1'
  74git branch sub1
  75git branch -m master subproj
  76check true
  77
  78create sub2
  79git commit -m 'sub2'
  80git branch sub2
  81
  82create sub3
  83git commit -m 'sub3'
  84git branch sub3
  85
  86cd ../mainline
  87git init
  88create main4
  89git commit -m 'main4'
  90git branch -m master mainline
  91git branch subdir
  92
  93git fetch ../subproj sub1
  94git branch sub1 FETCH_HEAD
  95
  96# check if --message works for add
  97git subtree add --prefix=subdir --message="Added subproject" sub1
  98check_equal "$(last_commit_message)" "Added subproject"
  99undo
 100
 101# check if --message works as -m and --prefix as -P
 102git subtree add -P subdir -m "Added subproject using git subtree" sub1
 103check_equal "$(last_commit_message)" "Added subproject using git subtree"
 104undo
 105
 106# check if --message works with squash too
 107git subtree add -P subdir -m "Added subproject with squash" --squash sub1
 108check_equal "$(last_commit_message)" "Added subproject with squash"
 109undo
 110
 111git subtree add --prefix=subdir/ FETCH_HEAD
 112check_equal "$(last_commit_message)" "Add 'subdir/' from commit '$(git rev-parse sub1)'"
 113
 114# this shouldn't actually do anything, since FETCH_HEAD is already a parent
 115git merge -m 'merge -s -ours' -s ours FETCH_HEAD
 116
 117create subdir/main-sub5
 118git commit -m 'main-sub5'
 119
 120create main6
 121git commit -m 'main6 boring'
 122
 123create subdir/main-sub7
 124git commit -m 'main-sub7'
 125
 126git fetch ../subproj sub2
 127git branch sub2 FETCH_HEAD
 128
 129# check if --message works for merge
 130git subtree merge --prefix=subdir -m "Merged changes from subproject" sub2
 131check_equal "$(last_commit_message)" "Merged changes from subproject"
 132undo
 133
 134# check if --message for merge works with squash too
 135git subtree merge --prefix subdir -m "Merged changes from subproject using squash" --squash sub2
 136check_equal "$(last_commit_message)" "Merged changes from subproject using squash"
 137undo
 138
 139git subtree merge --prefix=subdir FETCH_HEAD
 140git branch pre-split
 141check_equal "$(last_commit_message)" "Merge commit '$(git rev-parse sub2)' into mainline"
 142
 143# check if --message works for split+rejoin
 144spl1=$(git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)
 145echo "spl1={$spl1}"
 146git branch spl1 "$spl1"
 147check_equal "$(last_commit_message)" "Split & rejoin"
 148undo
 149
 150# check split with --branch
 151git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr1
 152check_equal "$(git rev-parse splitbr1)" "$spl1"
 153
 154# check split with --branch for an existing branch
 155git branch splitbr2 sub1
 156git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr2
 157check_equal "$(git rev-parse splitbr2)" "$spl1"
 158
 159# check split with --branch for an incompatible branch
 160result=$(git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir || echo "caught error")
 161check_equal "$result" "caught error"
 162
 163
 164git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --rejoin
 165check_equal "$(last_commit_message)" "Split 'subdir/' into commit '$spl1'"
 166
 167create subdir/main-sub8
 168git commit -m 'main-sub8'
 169
 170cd ../subproj
 171git fetch ../mainline spl1
 172git branch spl1 FETCH_HEAD
 173git merge FETCH_HEAD
 174
 175create sub9
 176git commit -m 'sub9'
 177
 178cd ../mainline
 179split2=$(git subtree split --annotate='*' --prefix subdir/ --rejoin)
 180git branch split2 "$split2"
 181
 182create subdir/main-sub10
 183git commit -m 'main-sub10'
 184
 185spl3=$(git subtree split --annotate='*' --prefix subdir --rejoin)
 186git branch spl3 "$spl3"
 187
 188cd ../subproj
 189git fetch ../mainline spl3
 190git branch spl3 FETCH_HEAD
 191git merge FETCH_HEAD
 192git branch subproj-merge-spl3
 193
 194chkm="main4 main6"
 195chkms="main-sub10 main-sub5 main-sub7 main-sub8"
 196chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
 197chks="sub1 sub2 sub3 sub9"
 198chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
 199
 200# make sure exactly the right set of files ends up in the subproj
 201subfiles=$(git ls-files | fixnl)
 202check_equal "$subfiles" "$chkms $chks"
 203
 204# make sure the subproj history *only* contains commits that affect the subdir.
 205allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl)
 206check_equal "$allchanges" "$chkms $chks"
 207
 208cd ../mainline
 209git fetch ../subproj subproj-merge-spl3
 210git branch subproj-merge-spl3 FETCH_HEAD
 211git subtree pull --prefix=subdir ../subproj subproj-merge-spl3
 212
 213# make sure exactly the right set of files ends up in the mainline
 214mainfiles=$(git ls-files | fixnl)
 215check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
 216
 217# make sure each filename changed exactly once in the entire history.
 218# 'main-sub??' and '/subdir/main-sub??' both change, because those are the
 219# changes that were split into their own history.  And 'subdir/sub??' never
 220# change, since they were *only* changed in the subtree branch.
 221allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl)
 222check_equal "$allchanges" "$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"
 223
 224# make sure the --rejoin commits never make it into subproj
 225check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" ""
 226
 227# make sure no 'git subtree' tagged commits make it into subproj. (They're
 228# meaningless to subproj since one side of the merge refers to the mainline)
 229check_equal "$(git log --pretty=format:'%s%n%b' HEAD^2 | grep 'git-subtree.*:')" ""
 230
 231
 232# check if split can find proper base without --onto
 233# prepare second pair of repositories
 234mkdir test2
 235cd test2
 236
 237mkdir main
 238cd main
 239git init
 240create main1
 241git commit -m "main1"
 242
 243cd ..
 244mkdir sub
 245cd sub
 246git init
 247create sub2
 248git commit -m "sub2"
 249
 250cd ../main
 251git fetch ../sub master
 252git branch sub2 FETCH_HEAD
 253git subtree add --prefix subdir sub2
 254
 255cd ../sub
 256create sub3
 257git commit -m "sub3"
 258
 259cd ../main
 260git fetch ../sub master
 261git branch sub3 FETCH_HEAD
 262git subtree merge --prefix subdir sub3
 263
 264create subdir/main-sub4
 265git commit -m "main-sub4"
 266git subtree split --prefix subdir --branch mainsub4
 267
 268# at this point, the new commit's parent should be sub3
 269# if it's not, something went wrong (the "newparent" of "master~" commit should have been sub3,
 270# but it wasn't, because it's cache was not set to itself)
 271check_equal "$(git log --format=%P -1 mainsub4)" "$(git rev-parse sub3)"
 272
 273
 274
 275# make sure no patch changes more than one file.  The original set of commits
 276# changed only one file each.  A multi-file change would imply that we pruned
 277# commits too aggressively.
 278joincommits()
 279{
 280        commit=
 281        all=
 282        while read x y; do
 283                echo "{$x}" >&2
 284                if [ -z "$x" ]; then
 285                        continue
 286                elif [ "$x" = "commit:" ]; then
 287                        if [ -n "$commit" ]; then
 288                                echo "$commit $all"
 289                                all=
 290                        fi
 291                        commit="$y"
 292                else
 293                        all="$all $y"
 294                fi
 295        done
 296        echo "$commit $all"
 297}
 298x=
 299git log --pretty=format:'commit: %H' | joincommits |
 300(       while read commit a b; do
 301                echo "Verifying commit $commit"
 302                check_equal "$b" ""
 303                x=1
 304        done
 305        check_equal "$x" 1
 306) || exit 1
 307
 308echo
 309echo 'ok'