d0a2c86c243ae6e68b4c6cb76f60d61339be859c
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 that prefix argument is required for split (exits with warning and exit status = 1)
144! result=$(git subtree split 2>&1)
145check_equal "You must provide the --prefix option." "$result"
146
147# Check that the <prefix> exists for a split.
148! result=$(git subtree split --prefix=non-existent-directory 2>&1)
149check_equal "non-existent-directory does not exist." "$result"
150
151# check if --message works for split+rejoin
152spl1=$(git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --message "Split & rejoin" --rejoin)
153echo "spl1={$spl1}"
154git branch spl1 "$spl1"
155check_equal "$(last_commit_message)" "Split & rejoin"
156undo
157
158# check split with --branch
159git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr1
160check_equal "$(git rev-parse splitbr1)" "$spl1"
161
162# check split with --branch for an existing branch
163git branch splitbr2 sub1
164git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --branch splitbr2
165check_equal "$(git rev-parse splitbr2)" "$spl1"
166
167# check split with --branch for an incompatible branch
168result=$(git subtree split --prefix subdir --onto FETCH_HEAD --branch subdir || echo "caught error")
169check_equal "$result" "caught error"
170
171
172git subtree split --annotate='*' --prefix subdir --onto FETCH_HEAD --rejoin
173check_equal "$(last_commit_message)" "Split 'subdir/' into commit '$spl1'"
174
175create subdir/main-sub8
176git commit -m 'main-sub8'
177
178cd ../subproj
179git fetch ../mainline spl1
180git branch spl1 FETCH_HEAD
181git merge FETCH_HEAD
182
183create sub9
184git commit -m 'sub9'
185
186cd ../mainline
187split2=$(git subtree split --annotate='*' --prefix subdir/ --rejoin)
188git branch split2 "$split2"
189
190create subdir/main-sub10
191git commit -m 'main-sub10'
192
193spl3=$(git subtree split --annotate='*' --prefix subdir --rejoin)
194git branch spl3 "$spl3"
195
196cd ../subproj
197git fetch ../mainline spl3
198git branch spl3 FETCH_HEAD
199git merge FETCH_HEAD
200git branch subproj-merge-spl3
201
202chkm="main4 main6"
203chkms="main-sub10 main-sub5 main-sub7 main-sub8"
204chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
205chks="sub1 sub2 sub3 sub9"
206chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
207
208# make sure exactly the right set of files ends up in the subproj
209subfiles=$(git ls-files | fixnl)
210check_equal "$subfiles" "$chkms $chks"
211
212# make sure the subproj history *only* contains commits that affect the subdir.
213allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl)
214check_equal "$allchanges" "$chkms $chks"
215
216cd ../mainline
217git fetch ../subproj subproj-merge-spl3
218git branch subproj-merge-spl3 FETCH_HEAD
219git subtree pull --prefix=subdir ../subproj subproj-merge-spl3
220
221# make sure exactly the right set of files ends up in the mainline
222mainfiles=$(git ls-files | fixnl)
223check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
224
225# make sure each filename changed exactly once in the entire history.
226# 'main-sub??' and '/subdir/main-sub??' both change, because those are the
227# changes that were split into their own history. And 'subdir/sub??' never
228# change, since they were *only* changed in the subtree branch.
229allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl)
230check_equal "$allchanges" "$(echo $chkms $chkm $chks $chkms_sub | multiline | sort | fixnl)"
231
232# make sure the --rejoin commits never make it into subproj
233check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" ""
234
235# make sure no 'git subtree' tagged commits make it into subproj. (They're
236# meaningless to subproj since one side of the merge refers to the mainline)
237check_equal "$(git log --pretty=format:'%s%n%b' HEAD^2 | grep 'git-subtree.*:')" ""
238
239
240# check if split can find proper base without --onto
241# prepare second pair of repositories
242mkdir test2
243cd test2
244
245mkdir main
246cd main
247git init
248create main1
249git commit -m "main1"
250
251cd ..
252mkdir sub
253cd sub
254git init
255create sub2
256git commit -m "sub2"
257
258cd ../main
259git fetch ../sub master
260git branch sub2 FETCH_HEAD
261git subtree add --prefix subdir sub2
262
263cd ../sub
264create sub3
265git commit -m "sub3"
266
267cd ../main
268git fetch ../sub master
269git branch sub3 FETCH_HEAD
270git subtree merge --prefix subdir sub3
271
272create subdir/main-sub4
273git commit -m "main-sub4"
274git subtree split --prefix subdir --branch mainsub4
275
276# at this point, the new commit's parent should be sub3
277# if it's not, something went wrong (the "newparent" of "master~" commit should have been sub3,
278# but it wasn't, because it's cache was not set to itself)
279check_equal "$(git log --format=%P -1 mainsub4)" "$(git rev-parse sub3)"
280
281
282
283# make sure no patch changes more than one file. The original set of commits
284# changed only one file each. A multi-file change would imply that we pruned
285# commits too aggressively.
286joincommits()
287{
288 commit=
289 all=
290 while read x y; do
291 echo "{$x}" >&2
292 if [ -z "$x" ]; then
293 continue
294 elif [ "$x" = "commit:" ]; then
295 if [ -n "$commit" ]; then
296 echo "$commit $all"
297 all=
298 fi
299 commit="$y"
300 else
301 all="$all $y"
302 fi
303 done
304 echo "$commit $all"
305}
306x=
307git log --pretty=format:'commit: %H' | joincommits |
308( while read commit a b; do
309 echo "Verifying commit $commit"
310 check_equal "$b" ""
311 x=1
312 done
313 check_equal "$x" 1
314) || exit 1
315
316echo
317echo 'ok'