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