summaryrefslogtreecommitdiff
path: root/t/t4011-diff-symlink.sh
blob: 717034bb50b57f3edc5d19989f0b178e912e08e6 (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
#!/bin/sh
#
# Copyright (c) 2005 Johannes Schindelin
#
 
test_description='Test diff of symlinks.
 
'
. ./test-lib.sh
. "$TEST_DIRECTORY"/diff-lib.sh
 
# Print the short OID of a symlink with the given name.
symlink_oid () {
	local oid=$(printf "%s" "$1" | git hash-object --stdin) &&
	git rev-parse --short "$oid"
}
 
# Print the short OID of the given file.
short_oid () {
	local oid=$(git hash-object "$1") &&
	git rev-parse --short "$oid"
}
 
test_expect_success 'diff new symlink and file' '
	symlink=$(symlink_oid xyzzy) &&
	cat >expected <<-EOF &&
	diff --git a/frotz b/frotz
	new file mode 120000
	index 0000000..$symlink
	--- /dev/null
	+++ b/frotz
	@@ -0,0 +1 @@
	+xyzzy
	\ No newline at end of file
	diff --git a/nitfol b/nitfol
	new file mode 100644
	index 0000000..$symlink
	--- /dev/null
	+++ b/nitfol
	@@ -0,0 +1 @@
	+xyzzy
	EOF
 
	# the empty tree
	git update-index &&
	tree=$(git write-tree) &&
 
	test_ln_s_add xyzzy frotz &&
	echo xyzzy >nitfol &&
	git update-index --add nitfol &&
	GIT_DIFF_OPTS=--unified=0 git diff-index -M -p $tree >current &&
	compare_diff_patch expected current
'
 
test_expect_success 'diff unchanged symlink and file'  '
	tree=$(git write-tree) &&
	git update-index frotz nitfol &&
	test -z "$(git diff-index --name-only $tree)"
'
 
test_expect_success 'diff removed symlink and file' '
	cat >expected <<-EOF &&
	diff --git a/frotz b/frotz
	deleted file mode 120000
	index $symlink..0000000
	--- a/frotz
	+++ /dev/null
	@@ -1 +0,0 @@
	-xyzzy
	\ No newline at end of file
	diff --git a/nitfol b/nitfol
	deleted file mode 100644
	index $symlink..0000000
	--- a/nitfol
	+++ /dev/null
	@@ -1 +0,0 @@
	-xyzzy
	EOF
	mv frotz frotz2 &&
	mv nitfol nitfol2 &&
	git diff-index -M -p $tree >current &&
	compare_diff_patch expected current
'
 
test_expect_success 'diff identical, but newly created symlink and file' '
	>expected &&
	rm -f frotz nitfol &&
	echo xyzzy >nitfol &&
	test-tool chmtime +10 nitfol &&
	if test_have_prereq SYMLINKS
	then
		ln -s xyzzy frotz
	else
		printf xyzzy >frotz
		# the symlink property propagates from the index
	fi &&
	git diff-index -M -p $tree >current &&
	compare_diff_patch expected current &&
 
	>expected &&
	git diff-index -M -p -w $tree >current &&
	compare_diff_patch expected current
'
 
test_expect_success 'diff different symlink and file' '
	new=$(symlink_oid yxyyz) &&
	cat >expected <<-EOF &&
	diff --git a/frotz b/frotz
	index $symlink..$new 120000
	--- a/frotz
	+++ b/frotz
	@@ -1 +1 @@
	-xyzzy
	\ No newline at end of file
	+yxyyz
	\ No newline at end of file
	diff --git a/nitfol b/nitfol
	index $symlink..$new 100644
	--- a/nitfol
	+++ b/nitfol
	@@ -1 +1 @@
	-xyzzy
	+yxyyz
	EOF
	rm -f frotz &&
	if test_have_prereq SYMLINKS
	then
		ln -s yxyyz frotz
	else
		printf yxyyz >frotz
		# the symlink property propagates from the index
	fi &&
	echo yxyyz >nitfol &&
	git diff-index -M -p $tree >current &&
	compare_diff_patch expected current
'
 
test_expect_success SYMLINKS 'diff symlinks with non-existing targets' '
	ln -s narf pinky &&
	ln -s take\ over brain &&
	test_must_fail git diff --no-index pinky brain >output 2>output.err &&
	grep narf output &&
	test_must_be_empty output.err
'
 
test_expect_success SYMLINKS 'setup symlinks with attributes' '
	echo "*.bin diff=bin" >>.gitattributes &&
	echo content >file.bin &&
	ln -s file.bin link.bin &&
	git add -N file.bin link.bin
'
 
test_expect_success SYMLINKS 'symlinks do not respect userdiff config by path' '
	file=$(short_oid file.bin) &&
	link=$(symlink_oid file.bin) &&
	cat >expect <<-EOF &&
	diff --git a/file.bin b/file.bin
	new file mode 100644
	index 0000000..$file
	Binary files /dev/null and b/file.bin differ
	diff --git a/link.bin b/link.bin
	new file mode 120000
	index 0000000..$link
	--- /dev/null
	+++ b/link.bin
	@@ -0,0 +1 @@
	+file.bin
	\ No newline at end of file
	EOF
	git config diff.bin.binary true &&
	git diff file.bin link.bin >actual &&
	test_cmp expect actual
'
 
test_done