aboutsummaryrefslogtreecommitdiff
path: root/t/t7400-submodule-basic.sh
blob: cbc0c34ce2487959ef0e8f89f7c2a5d4a68be826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/sh
#
# Copyright (c) 2007 Lars Hjemli
#

test_description='Basic porcelain support for submodules

This test tries to verify basic sanity of the init, update and status
subcommands of git-submodule.
'

. ./test-lib.sh

#
# Test setup:
#  -create a repository in directory init
#  -add a couple of files
#  -add directory init to 'superproject', this creates a DIRLINK entry
#  -add a couple of regular files to enable testing of submodule filtering
#  -mv init subrepo
#  -add an entry to .gitmodules for submodule 'example'
#
test_expect_success 'Prepare submodule testing' '
	: > t &&
	git-add t &&
	git-commit -m "initial commit" &&
	git branch initial HEAD &&
	mkdir init &&
	cd init &&
	git init &&
	echo a >a &&
	git add a &&
	git-commit -m "submodule commit 1" &&
	git-tag -a -m "rev-1" rev-1 &&
	rev1=$(git rev-parse HEAD) &&
	if test -z "$rev1"
	then
		echo "[OOPS] submodule git rev-parse returned nothing"
		false
	fi &&
	cd .. &&
	echo a >a &&
	echo z >z &&
	git add a init z &&
	git-commit -m "super commit 1" &&
	mv init .subrepo &&
	GIT_CONFIG=.gitmodules git config submodule.example.url git://example.com/init.git
'

test_expect_success 'status should fail for unmapped paths' '
	if git-submodule status
	then
		echo "[OOPS] submodule status succeeded"
		false
	elif ! GIT_CONFIG=.gitmodules git config submodule.example.path init
	then
		echo "[OOPS] git config failed to update .gitmodules"
		false
	fi
'

test_expect_success 'status should only print one line' '
	lines=$(git-submodule status | wc -l) &&
	test $lines = 1
'

test_expect_success 'status should initially be "missing"' '
	git-submodule status | grep "^-$rev1"
'

test_expect_success 'init should register submodule url in .git/config' '
	git-submodule init &&
	url=$(git config submodule.example.url) &&
	if test "$url" != "git://example.com/init.git"
	then
		echo "[OOPS] init succeeded but submodule url is wrong"
		false
	elif test_must_fail git config submodule.example.url ./.subrepo
	then
		echo "[OOPS] init succeeded but update of url failed"
		false
	fi
'

test_expect_success 'update should fail when path is used by a file' '
	echo "hello" >init &&
	if git-submodule update
	then
		echo "[OOPS] update should have failed"
		false
	elif test "$(cat init)" != "hello"
	then
		echo "[OOPS] update failed but init file was molested"
		false
	else
		rm init
	fi
'

test_expect_success 'update should fail when path is used by a nonempty directory' '
	mkdir init &&
	echo "hello" >init/a &&
	if git-submodule update
	then
		echo "[OOPS] update should have failed"
		false
	elif test "$(cat init/a)" != "hello"
	then
		echo "[OOPS] update failed but init/a was molested"
		false
	else
		rm init/a
	fi
'

test_expect_success 'update should work when path is an empty dir' '
	rm -rf init &&
	mkdir init &&
	git-submodule update &&
	head=$(cd init && git rev-parse HEAD) &&
	if test -z "$head"
	then
		echo "[OOPS] Failed to obtain submodule head"
		false
	elif test "$head" != "$rev1"
	then
		echo "[OOPS] Submodule head is $head but should have been $rev1"
		false
	fi
'

test_expect_success 'status should be "up-to-date" after update' '
	git-submodule status | grep "^ $rev1"
'

test_expect_success 'status should be "modified" after submodule commit' '
	cd init &&
	echo b >b &&
	git add b &&
	git-commit -m "submodule commit 2" &&
	rev2=$(git rev-parse HEAD) &&
	cd .. &&
	if test -z "$rev2"
	then
		echo "[OOPS] submodule git rev-parse returned nothing"
		false
	fi &&
	git-submodule status | grep "^+$rev2"
'

test_expect_success 'the --cached sha1 should be rev1' '
	git-submodule --cached status | grep "^+$rev1"
'

test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
	git-diff | grep "^+Subproject commit $rev2"
'

test_expect_success 'update should checkout rev1' '
	git-submodule update init &&
	head=$(cd init && git rev-parse HEAD) &&
	if test -z "$head"
	then
		echo "[OOPS] submodule git rev-parse returned nothing"
		false
	elif test "$head" != "$rev1"
	then
		echo "[OOPS] init did not checkout correct head"
		false
	fi
'

test_expect_success 'status should be "up-to-date" after update' '
	git-submodule status | grep "^ $rev1"
'

test_expect_success 'checkout superproject with subproject already present' '
	git-checkout initial &&
	git-checkout master
'

test_expect_success 'apply submodule diff' '
	git branch second &&
	(
		cd init &&
		echo s >s &&
		git add s &&
		git commit -m "change subproject"
	) &&
	git update-index --add init &&
	git-commit -m "change init" &&
	git-format-patch -1 --stdout >P.diff &&
	git checkout second &&
	git apply --index P.diff &&
	D=$(git diff --cached master) &&
	test -z "$D"
'

test_expect_success 'update --init' '

	mv init init2 &&
	git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
	git config --remove-section submodule.example
	git submodule update init > update.out &&
	grep "not initialized" update.out &&
	test ! -d init/.git &&
	git submodule update --init init &&
	test -d init/.git

'

test_done