3 # Copyright (c) 2012 Avery Pennaraum
4 # Copyright (c) 2015 Alexey Shumkin
6 test_description='Basic porcelain support for subtrees
8 This test verifies the basic operation of the merge, pull, add
9 and split subcommands of git subtree.
12 TEST_DIRECTORY=$(pwd)/../../../t
15 . ../../../t/test-lib.sh
27 test_debug "echo \"check a:\" \"{$1}\""
28 test_debug "echo \" b:\" \"{$2}\""
29 if [ "$1" = "$2" ]; then
38 git reset --hard HEAD~
43 git log --pretty=format:%s -1
46 test_expect_success 'init subproj' '
47 test_create_repo "sub proj"
53 test_expect_success 'add sub1' '
55 git commit -m "sub1" &&
57 git branch -m master subproj
60 # Save this hash for testing later.
62 subdir_hash=$(git rev-parse HEAD)
64 test_expect_success 'add sub2' '
66 git commit -m "sub2" &&
70 test_expect_success 'add sub3' '
72 git commit -m "sub3" &&
79 test_expect_success 'enable log.date=relative to catch errors' '
80 git config log.date relative
83 test_expect_success 'add main4' '
85 git commit -m "main4" &&
86 git branch -m master mainline &&
90 test_expect_success 'fetch subproj history' '
91 git fetch ./"sub proj" sub1 &&
92 git branch sub1 FETCH_HEAD
95 test_expect_success 'no subtree exists in main tree' '
96 test_must_fail git subtree merge --prefix="sub dir" sub1
99 test_expect_success 'no pull from non-existant subtree' '
100 test_must_fail git subtree pull --prefix="sub dir" ./"sub proj" sub1
103 test_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" &&
109 test_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" &&
115 test_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" &&
121 test_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)"'''"'"'"
126 # this shouldn't actually do anything, since FETCH_HEAD is already a parent
127 test_expect_success 'merge fetched subproj' '
128 git merge -m "merge -s -ours" -s ours FETCH_HEAD
131 test_expect_success 'add main-sub5' '
132 create "sub dir/main-sub5" &&
133 git commit -m "main-sub5"
136 test_expect_success 'add main6' '
138 git commit -m "main6 boring"
141 test_expect_success 'add main-sub7' '
142 create "sub dir/main-sub7" &&
143 git commit -m "main-sub7"
146 test_expect_success 'fetch new subproj history' '
147 git fetch ./"sub proj" sub2 &&
148 git branch sub2 FETCH_HEAD
151 test_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" &&
157 test_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" &&
163 test_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" &&
170 test_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
181 test_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
192 test_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" &&
199 test_expect_success 'check split with --branch' '
200 spl1=$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin) &&
202 git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --branch splitbr1 &&
203 check_equal ''"$(git rev-parse splitbr1)"'' "$spl1"
206 test_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"
214 test_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)"'' &&
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"
222 test_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
226 test_expect_success 'check split+rejoin' '
227 spl1=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --message "Split & rejoin" --rejoin)"'' &&
229 git subtree split --annotate='"'*'"' --prefix "sub dir" --onto FETCH_HEAD --rejoin &&
230 check_equal ''"$(last_commit_message)"'' "Split '"'"'sub dir/'"'"' into commit '"'"'"$spl1"'"'"'"
233 test_expect_success 'add main-sub8' '
234 create "sub dir/main-sub8" &&
235 git commit -m "main-sub8"
241 test_expect_success 'merge split into subproj' '
243 git branch spl1 FETCH_HEAD &&
247 test_expect_success 'add sub9' '
255 test_expect_success 'split for sub8' '
256 split2=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir/" --rejoin)"'' &&
257 git branch split2 "$split2"
260 test_expect_success 'add main-sub10' '
261 create "sub dir/main-sub10" &&
262 git commit -m "main-sub10"
265 test_expect_success 'split for sub10' '
266 spl3=''"$(git subtree split --annotate='"'*'"' --prefix "sub dir" --rejoin)"'' &&
267 git branch spl3 "$spl3"
273 test_expect_success 'merge split into subproj' '
275 git branch spl3 FETCH_HEAD &&
276 git merge FETCH_HEAD &&
277 git branch subproj-merge-spl3
286 chkms_sub=$(cat <<TXT | sed 's,^,sub dir/,'
294 chks_sub=$(cat <<TXT | sed 's,^,sub dir/,'
299 test_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
304 test_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
313 test_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
319 test_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
326 test_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
340 test_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)"'' ""
344 test_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.*:")"'' ""
349 # prepare second pair of repositories
353 test_expect_success 'init main' '
354 test_create_repo main
359 test_expect_success 'add main1' '
361 git commit -m "main1"
366 test_expect_success 'init sub' '
372 test_expect_success 'add sub2' '
379 # check if split can find proper base without --onto
381 test_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
389 test_expect_success 'add sub3' '
396 test_expect_success 'merge from sub' '
397 git fetch ../sub master &&
398 git branch sub3 FETCH_HEAD &&
399 git subtree merge --prefix "sub dir" sub3
402 test_expect_success 'add main-sub4' '
403 create "sub dir/main-sub4" &&
404 git commit -m "main-sub4"
407 test_expect_success 'split for main-sub4 without --onto' '
408 git subtree split --prefix "sub dir" --branch mainsub4
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
416 test_expect_success 'check that the commit parent is sub3' '
417 check_equal ''"$(git log --pretty=format:%P -1 mainsub4)"'' ''"$(git rev-parse sub3)"''
420 test_expect_success 'add main-sub5' '
422 create subdir2/main-sub5 &&
423 git commit -m "main-sub5"
426 test_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)"'' ""
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.
445 elif [ "$x" = "commit:" ]; then
446 if [ -n "$commit" ]; then
458 test_expect_success 'verify one file change per commit' '
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"''
483 test_expect_success 'init main' '
484 test_create_repo main
487 test_expect_success 'init sub' '
488 test_create_repo "sub project"
493 test_expect_success 'add subproject' '
494 create "sub project" &&
495 git commit -m "Sub project: 1" &&
496 git branch sub-branch-1
501 test_expect_success 'make first commit and add subproject' '
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"
508 test_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
516 test_expect_success 'Test second commit is pushed' '
517 git checkout sub-branch-1 &&
518 check_equal "$(last_commit_message)" "Sub project: 2"