Code

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