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
56rm -rf mainline subproj
57mkdir mainline subproj
58
59cd subproj
60git init
61
62create sub1
63git commit -m 'sub1'
64git branch sub1
65git branch -m master subproj
66check true
67
68create sub2
69git commit -m 'sub2'
70git branch sub2
71
72create sub3
73git commit -m 'sub3'
74git branch sub3
75
76cd ../mainline
77git init
78create main4
79git commit -m 'main4'
80git branch -m master mainline
81
82git fetch ../subproj sub1
83git branch sub1 FETCH_HEAD
84git subtree add --prefix=subdir FETCH_HEAD
85
86# this shouldn't actually do anything, since FETCH_HEAD is already a parent
87git merge -m 'merge -s -ours' -s ours FETCH_HEAD
88
89create subdir/main-sub5
90git commit -m 'main-sub5'
91
92create main6
93git commit -m 'main6 boring'
94
95create subdir/main-sub7
96git commit -m 'main-sub7'
97
98git fetch ../subproj sub2
99git branch sub2 FETCH_HEAD
100git subtree merge --prefix=subdir FETCH_HEAD
101git branch pre-split
102
103spl1=$(git subtree split --annotate='*' \
104 --prefix subdir --onto FETCH_HEAD --rejoin)
105echo "spl1={$spl1}"
106git branch spl1 "$spl1"
107
108create subdir/main-sub8
109git commit -m 'main-sub8'
110
111cd ../subproj
112git fetch ../mainline spl1
113git branch spl1 FETCH_HEAD
114git merge FETCH_HEAD
115
116create sub9
117git commit -m 'sub9'
118
119cd ../mainline
120split2=$(git subtree split --annotate='*' --prefix subdir --rejoin)
121git branch split2 "$split2"
122
123create subdir/main-sub10
124git commit -m 'main-sub10'
125
126spl3=$(git subtree split --annotate='*' --prefix subdir --rejoin)
127git branch spl3 "$spl3"
128
129cd ../subproj
130git fetch ../mainline spl3
131git branch spl3 FETCH_HEAD
132git merge FETCH_HEAD
133git branch subproj-merge-spl3
134
135chkm="main4 main6"
136chkms="main-sub10 main-sub5 main-sub7 main-sub8"
137chkms_sub=$(echo $chkms | multiline | sed 's,^,subdir/,' | fixnl)
138chks="sub1 sub2 sub3 sub9"
139chks_sub=$(echo $chks | multiline | sed 's,^,subdir/,' | fixnl)
140
141# make sure exactly the right set of files ends up in the subproj
142subfiles=$(git ls-files | fixnl)
143check_equal "$subfiles" "$chkms $chks"
144
145# make sure the subproj history *only* contains commits that affect the subdir.
146allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl)
147check_equal "$allchanges" "$chkms $chks"
148
149cd ../mainline
150git fetch ../subproj subproj-merge-spl3
151git branch subproj-merge-spl3 FETCH_HEAD
152git subtree pull --prefix=subdir ../subproj subproj-merge-spl3
153
154# make sure exactly the right set of files ends up in the mainline
155mainfiles=$(git ls-files | fixnl)
156check_equal "$mainfiles" "$chkm $chkms_sub $chks_sub"
157
158# make sure each filename changed exactly once in the entire history.
159# 'main-sub??' and '/subdir/main-sub??' both change, because those are the
160# changes that were split into their own history. And 'subdir/sub??' never
161# change, since they were *only* changed in the subtree branch.
162allchanges=$(git log --name-only --pretty=format:'' | sort | fixnl)
163check_equal "$allchanges" "$chkm $chkms $chks $chkms_sub"
164
165# make sure the --rejoin commits never make it into subproj
166check_equal "$(git log --pretty=format:'%s' HEAD^2 | grep -i split)" ""
167
168# make sure no 'git subtree' tagged commits make it into subproj. (They're
169# meaningless to subproj since one side of the merge refers to the mainline)
170check_equal "$(git log --pretty=format:'%s%n%b' HEAD^2 | grep 'git-subtree.*:')" ""
171
172# make sure no patch changes more than one file. The original set of commits
173# changed only one file each. A multi-file change would imply that we pruned
174# commits too aggressively.
175joincommits()
176{
177 commit=
178 all=
179 while read x y; do
180 echo "{$x}" >&2
181 if [ -z "$x" ]; then
182 continue
183 elif [ "$x" = "commit:" ]; then
184 if [ -n "$commit" ]; then
185 echo "$commit $all"
186 all=
187 fi
188 commit="$y"
189 else
190 all="$all $y"
191 fi
192 done
193 echo "$commit $all"
194}
195x=
196git log --pretty=format:'commit: %H' | joincommits |
197( while read commit a b; do
198 echo "Verifying commit $commit"
199 check_equal "$b" ""
200 x=1
201 done
202 check_equal "$x" 1
203) || exit 1
204
205echo
206echo 'ok'