aboutsummaryrefslogtreecommitdiff
path: root/t/t4041-diff-submodule.sh
blob: 019acb926d6765bf24e1dfe4bea2a9dba28ca797 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/sh
#
# Copyright (c) 2009 Jens Lehmann, based on t7401 by Ping Yin
#

test_description='Support for verbose submodule differences in git diff

This test tries to verify the sanity of the --submodule option of git diff.
'

. ./test-lib.sh

add_file () {
	sm=$1
	shift
	owd=$(pwd)
	cd "$sm"
	for name; do
		echo "$name" > "$name" &&
		git add "$name" &&
		test_tick &&
		git commit -m "Add $name"
	done >/dev/null
	git rev-parse --verify HEAD | cut -c1-7
	cd "$owd"
}
commit_file () {
	test_tick &&
	git commit "$@" -m "Commit $*" >/dev/null
}

test_create_repo sm1 &&
add_file . foo >/dev/null

head1=$(add_file sm1 foo1 foo2)

test_expect_success 'added submodule' "
	git add sm1 &&
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 0000000...$head1 (new submodule)
EOF
"

commit_file sm1 &&
head2=$(add_file sm1 foo3)

test_expect_success 'modified submodule(forward)' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 $head1..$head2:
  > Add foo3
EOF
"

test_expect_success 'modified submodule(forward)' "
	git diff --submodule=log >actual &&
	diff actual - <<-EOF
Submodule sm1 $head1..$head2:
  > Add foo3
EOF
"

test_expect_success 'modified submodule(forward) --submodule' "
	git diff --submodule >actual &&
	diff actual - <<-EOF
Submodule sm1 $head1..$head2:
  > Add foo3
EOF
"

fullhead1=$(cd sm1; git rev-list --max-count=1 $head1)
fullhead2=$(cd sm1; git rev-list --max-count=1 $head2)
test_expect_success 'modified submodule(forward) --submodule=short' "
	git diff --submodule=short >actual &&
	diff actual - <<-EOF
diff --git a/sm1 b/sm1
index $head1..$head2 160000
--- a/sm1
+++ b/sm1
@@ -1 +1 @@
-Subproject commit $fullhead1
+Subproject commit $fullhead2
EOF
"

commit_file sm1 &&
cd sm1 &&
git reset --hard HEAD~2 >/dev/null &&
head3=$(git rev-parse --verify HEAD | cut -c1-7) &&
cd ..

test_expect_success 'modified submodule(backward)' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 $head2..$head3 (rewind):
  < Add foo3
  < Add foo2
EOF
"

head4=$(add_file sm1 foo4 foo5) &&
head4_full=$(GIT_DIR=sm1/.git git rev-parse --verify HEAD)
test_expect_success 'modified submodule(backward and forward)' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 $head2...$head4:
  > Add foo5
  > Add foo4
  < Add foo3
  < Add foo2
EOF
"

commit_file sm1 &&
mv sm1 sm1-bak &&
echo sm1 >sm1 &&
head5=$(git hash-object sm1 | cut -c1-7) &&
git add sm1 &&
rm -f sm1 &&
mv sm1-bak sm1

test_expect_success 'typechanged submodule(submodule->blob), --cached' "
	git diff --submodule=log --cached >actual &&
	diff actual - <<-EOF
Submodule sm1 41fbea9...0000000 (submodule deleted)
diff --git a/sm1 b/sm1
new file mode 100644
index 0000000..9da5fb8
--- /dev/null
+++ b/sm1
@@ -0,0 +1 @@
+sm1
EOF
"

test_expect_success 'typechanged submodule(submodule->blob)' "
	git diff --submodule=log >actual &&
	diff actual - <<-EOF
diff --git a/sm1 b/sm1
deleted file mode 100644
index 9da5fb8..0000000
--- a/sm1
+++ /dev/null
@@ -1 +0,0 @@
-sm1
Submodule sm1 0000000...$head4 (new submodule)
EOF
"

rm -rf sm1 &&
git checkout-index sm1
test_expect_success 'typechanged submodule(submodule->blob)' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 $head4...0000000 (submodule deleted)
diff --git a/sm1 b/sm1
new file mode 100644
index 0000000..$head5
--- /dev/null
+++ b/sm1
@@ -0,0 +1 @@
+sm1
EOF
"

rm -f sm1 &&
test_create_repo sm1 &&
head6=$(add_file sm1 foo6 foo7)
fullhead6=$(cd sm1; git rev-list --max-count=1 $head6)
test_expect_success 'nonexistent commit' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 $head4...$head6 (commits not present)
EOF
"

commit_file
test_expect_success 'typechanged submodule(blob->submodule)' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
diff --git a/sm1 b/sm1
deleted file mode 100644
index $head5..0000000
--- a/sm1
+++ /dev/null
@@ -1 +0,0 @@
-sm1
Submodule sm1 0000000...$head6 (new submodule)
EOF
"

commit_file sm1 &&
test_expect_success 'submodule is up to date' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
EOF
"

test_expect_success 'submodule contains untracked content' "
	echo new > sm1/new-file &&
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 contains untracked content
EOF
"

test_expect_success 'submodule contains untracked and modifed content' "
	echo new > sm1/foo6 &&
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 contains untracked content
Submodule sm1 contains modified content
EOF
"

test_expect_success 'submodule contains modifed content' "
	rm -f sm1/new-file &&
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 contains modified content
EOF
"

(cd sm1; git commit -mchange foo6 >/dev/null) &&
head8=$(cd sm1; git rev-parse --verify HEAD | cut -c1-7) &&
test_expect_success 'submodule is modified' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 $head6..$head8:
  > change
EOF
"

test_expect_success 'modified submodule contains untracked content' "
	echo new > sm1/new-file &&
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 contains untracked content
Submodule sm1 $head6..$head8:
  > change
EOF
"

test_expect_success 'modified submodule contains untracked and modifed content' "
	echo modification >> sm1/foo6 &&
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 contains untracked content
Submodule sm1 contains modified content
Submodule sm1 $head6..$head8:
  > change
EOF
"

test_expect_success 'modified submodule contains modifed content' "
	rm -f sm1/new-file &&
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 contains modified content
Submodule sm1 $head6..$head8:
  > change
EOF
"

rm -rf sm1
test_expect_success 'deleted submodule' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 $head6...0000000 (submodule deleted)
EOF
"

test_create_repo sm2 &&
head7=$(add_file sm2 foo8 foo9) &&
git add sm2

test_expect_success 'multiple submodules' "
	git diff-index -p --submodule=log HEAD >actual &&
	diff actual - <<-EOF
Submodule sm1 $head6...0000000 (submodule deleted)
Submodule sm2 0000000...$head7 (new submodule)
EOF
"

test_expect_success 'path filter' "
	git diff-index -p --submodule=log HEAD sm2 >actual &&
	diff actual - <<-EOF
Submodule sm2 0000000...$head7 (new submodule)
EOF
"

commit_file sm2
test_expect_success 'given commit' "
	git diff-index -p --submodule=log HEAD^ >actual &&
	diff actual - <<-EOF
Submodule sm1 $head6...0000000 (submodule deleted)
Submodule sm2 0000000...$head7 (new submodule)
EOF
"

test_expect_success 'given commit --submodule' "
	git diff-index -p --submodule HEAD^ >actual &&
	diff actual - <<-EOF
Submodule sm1 $head6...0000000 (submodule deleted)
Submodule sm2 0000000...$head7 (new submodule)
EOF
"

fullhead7=$(cd sm2; git rev-list --max-count=1 $head7)

test_expect_success 'given commit --submodule=short' "
	git diff-index -p --submodule=short HEAD^ >actual &&
	diff actual - <<-EOF
diff --git a/sm1 b/sm1
deleted file mode 160000
index $head6..0000000
--- a/sm1
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit $fullhead6
diff --git a/sm2 b/sm2
new file mode 160000
index 0000000..$head7
--- /dev/null
+++ b/sm2
@@ -0,0 +1 @@
+Subproject commit $fullhead7
EOF
"

test_expect_success 'setup .git file for sm2' '
	(cd sm2 &&
	 REAL="$(pwd)/../.real" &&
	 mv .git "$REAL"
	 echo "gitdir: $REAL" >.git)
'

test_expect_success 'diff --submodule with .git file' '
	git diff --submodule HEAD^ >actual &&
	diff actual - <<-EOF
Submodule sm1 $head6...0000000 (submodule deleted)
Submodule sm2 0000000...$head7 (new submodule)
EOF
'

test_done