]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7400-submodule-basic.sh
git-fast-import: rename cmd_*() functions to parse_*()
[thirdparty/git.git] / t / t7400-submodule-basic.sh
CommitLineData
88961ef2
LH
1#!/bin/sh
2#
3# Copyright (c) 2007 Lars Hjemli
4#
5
6test_description='Basic porcelain support for submodules
7
8This test tries to verify basic sanity of the init, update and status
9subcommands of git-submodule.
10'
11
12. ./test-lib.sh
13
14#
15# Test setup:
a2d93aea 16# -create a repository in directory init
88961ef2 17# -add a couple of files
a2d93aea 18# -add directory init to 'superproject', this creates a DIRLINK entry
88961ef2 19# -add a couple of regular files to enable testing of submodule filtering
a2d93aea 20# -mv init subrepo
941987a5 21# -add an entry to .gitmodules for submodule 'example'
88961ef2
LH
22#
23test_expect_success 'Prepare submodule testing' '
0cf73755
SV
24 : > t &&
25 git-add t &&
26 git-commit -m "initial commit" &&
27 git branch initial HEAD &&
a2d93aea
JH
28 mkdir init &&
29 cd init &&
5be60078 30 git init &&
88961ef2 31 echo a >a &&
5be60078 32 git add a &&
88961ef2
LH
33 git-commit -m "submodule commit 1" &&
34 git-tag -a -m "rev-1" rev-1 &&
5be60078 35 rev1=$(git rev-parse HEAD) &&
88961ef2
LH
36 if test -z "$rev1"
37 then
5be60078 38 echo "[OOPS] submodule git rev-parse returned nothing"
88961ef2
LH
39 false
40 fi &&
41 cd .. &&
42 echo a >a &&
43 echo z >z &&
a2d93aea 44 git add a init z &&
88961ef2 45 git-commit -m "super commit 1" &&
a2d93aea
JH
46 mv init .subrepo &&
47 GIT_CONFIG=.gitmodules git config submodule.example.url git://example.com/init.git
941987a5
LH
48'
49
50test_expect_success 'status should fail for unmapped paths' '
51 if git-submodule status
52 then
53 echo "[OOPS] submodule status succeeded"
54 false
a2d93aea 55 elif ! GIT_CONFIG=.gitmodules git config submodule.example.path init
941987a5 56 then
5be60078 57 echo "[OOPS] git config failed to update .gitmodules"
941987a5
LH
58 false
59 fi
88961ef2
LH
60'
61
62test_expect_success 'status should only print one line' '
63 lines=$(git-submodule status | wc -l) &&
64 test $lines = 1
65'
66
67test_expect_success 'status should initially be "missing"' '
68 git-submodule status | grep "^-$rev1"
69'
70
211b7f19
LH
71test_expect_success 'init should register submodule url in .git/config' '
72 git-submodule init &&
5be60078 73 url=$(git config submodule.example.url) &&
a2d93aea 74 if test "$url" != "git://example.com/init.git"
211b7f19
LH
75 then
76 echo "[OOPS] init succeeded but submodule url is wrong"
77 false
5be60078 78 elif ! git config submodule.example.url ./.subrepo
211b7f19
LH
79 then
80 echo "[OOPS] init succeeded but update of url failed"
81 false
82 fi
83'
84
85test_expect_success 'update should fail when path is used by a file' '
a2d93aea 86 echo "hello" >init &&
211b7f19 87 if git-submodule update
88961ef2 88 then
211b7f19 89 echo "[OOPS] update should have failed"
88961ef2 90 false
a2d93aea 91 elif test "$(cat init)" != "hello"
88961ef2 92 then
a2d93aea 93 echo "[OOPS] update failed but init file was molested"
88961ef2
LH
94 false
95 else
a2d93aea 96 rm init
88961ef2
LH
97 fi
98'
99
211b7f19 100test_expect_success 'update should fail when path is used by a nonempty directory' '
a2d93aea
JH
101 mkdir init &&
102 echo "hello" >init/a &&
211b7f19 103 if git-submodule update
88961ef2 104 then
211b7f19 105 echo "[OOPS] update should have failed"
88961ef2 106 false
a2d93aea 107 elif test "$(cat init/a)" != "hello"
88961ef2 108 then
a2d93aea 109 echo "[OOPS] update failed but init/a was molested"
88961ef2
LH
110 false
111 else
a2d93aea 112 rm init/a
88961ef2
LH
113 fi
114'
115
211b7f19 116test_expect_success 'update should work when path is an empty dir' '
a2d93aea
JH
117 rm -rf init &&
118 mkdir init &&
211b7f19 119 git-submodule update &&
a2d93aea 120 head=$(cd init && git rev-parse HEAD) &&
88961ef2
LH
121 if test -z "$head"
122 then
123 echo "[OOPS] Failed to obtain submodule head"
124 false
125 elif test "$head" != "$rev1"
126 then
127 echo "[OOPS] Submodule head is $head but should have been $rev1"
128 false
129 fi
130'
131
211b7f19 132test_expect_success 'status should be "up-to-date" after update' '
88961ef2
LH
133 git-submodule status | grep "^ $rev1"
134'
135
136test_expect_success 'status should be "modified" after submodule commit' '
a2d93aea 137 cd init &&
88961ef2 138 echo b >b &&
5be60078 139 git add b &&
88961ef2 140 git-commit -m "submodule commit 2" &&
5be60078 141 rev2=$(git rev-parse HEAD) &&
88961ef2
LH
142 cd .. &&
143 if test -z "$rev2"
144 then
5be60078 145 echo "[OOPS] submodule git rev-parse returned nothing"
88961ef2
LH
146 false
147 fi &&
148 git-submodule status | grep "^+$rev2"
149'
150
151test_expect_success 'the --cached sha1 should be rev1' '
152 git-submodule --cached status | grep "^+$rev1"
153'
154
5701115a
SV
155test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
156 git-diff | grep "^+Subproject commit $rev2"
157'
158
88961ef2 159test_expect_success 'update should checkout rev1' '
a2d93aea
JH
160 git-submodule update init &&
161 head=$(cd init && git rev-parse HEAD) &&
88961ef2
LH
162 if test -z "$head"
163 then
5be60078 164 echo "[OOPS] submodule git rev-parse returned nothing"
88961ef2
LH
165 false
166 elif test "$head" != "$rev1"
167 then
168 echo "[OOPS] init did not checkout correct head"
169 false
170 fi
171'
172
173test_expect_success 'status should be "up-to-date" after update' '
174 git-submodule status | grep "^ $rev1"
175'
176
0cf73755
SV
177test_expect_success 'checkout superproject with subproject already present' '
178 git-checkout initial &&
179 git-checkout master
180'
181
e06c5a6c
SV
182test_expect_success 'apply submodule diff' '
183 git branch second &&
184 (
a2d93aea 185 cd init &&
e06c5a6c
SV
186 echo s >s &&
187 git add s &&
188 git commit -m "change subproject"
189 ) &&
a2d93aea
JH
190 git update-index --add init &&
191 git-commit -m "change init" &&
e06c5a6c
SV
192 git-format-patch -1 --stdout >P.diff &&
193 git checkout second &&
194 git apply --index P.diff &&
195 D=$(git diff --cached master) &&
196 test -z "$D"
197'
198
88961ef2 199test_done