3 # Copyright (c) 2005 Junio C Hamano
6 test_description
='Test rename detection in diff engine.'
8 TEST_PASSES_SANITIZE_LEAK
=true
10 .
"$TEST_DIRECTORY"/lib-diff.sh
12 test_expect_success
'setup' '
30 cat >expected <<-\EOF &&
31 diff --git a/path0 b/path1
36 @@ -8,7 +8,7 @@ Line 7
46 cat >no-rename <<-\EOF
47 diff --git a/path0 b/path0
48 deleted file mode 100644
49 index fdbec44..0000000
68 diff --git a/path1 b/path1
70 index 0000000..752c50e
93 'update-index --add a file.' \
94 'git update-index --add path0'
98 'tree=$(git write-tree) && echo $tree'
100 sed -e 's/line/Line/' <path0
>path1
102 test_expect_success \
103 'renamed and edited the file.' \
104 'git update-index --add --remove path0 path1'
106 test_expect_success \
107 'git diff-index -p -M after rename and editing.' \
108 'git diff-index -p -M $tree >current'
111 test_expect_success \
112 'validate the output.' \
113 'compare_diff_patch current expected'
115 test_expect_success
'test diff.renames=true' '
116 git -c diff.renames=true diff --cached $tree >current &&
117 compare_diff_patch current expected
120 test_expect_success
'test diff.renames=false' '
121 git -c diff.renames=false diff --cached $tree >current &&
122 compare_diff_patch current no-rename
125 test_expect_success
'test diff.renames unset' '
126 git diff --cached $tree >current &&
127 compare_diff_patch current expected
130 test_expect_success
'favour same basenames over different ones' '
131 cp path1 another-path &&
132 git add another-path &&
136 git mv another-path subdir/path1 &&
138 test_grep "renamed: .*path1 -> subdir/path1" out
141 test_expect_success
'test diff.renames=true for git status' '
142 git -c diff.renames=true status >out &&
143 test_grep "renamed: .*path1 -> subdir/path1" out
146 test_expect_success
'test diff.renames=false for git status' '
147 git -c diff.renames=false status >out &&
148 test_grep ! "renamed: .*path1 -> subdir/path1" out &&
149 test_grep "new file: .*subdir/path1" out &&
150 test_grep "deleted: .*[^/]path1" out
153 test_expect_success
'favour same basenames even with minor differences' '
154 git show HEAD:path1 | sed "s/15/16/" > subdir/path1 &&
156 test_grep "renamed: .*path1 -> subdir/path1" out
159 test_expect_success
'two files with same basename and same content' '
161 mkdir -p dir/A dir/B &&
162 cp path1 dir/A/file &&
163 cp path1 dir/B/file &&
166 git mv dir other-dir &&
168 test_grep "renamed: .*dir/A/file -> other-dir/A/file" out
171 test_expect_success
'setup for many rename source candidates' '
173 for i in 0 1 2 3 4 5 6 7 8 9;
175 for j in 0 1 2 3 4 5 6 7 8 9;
177 echo "$i$j" >"path$i$j" || return 1
182 git commit -m "hundred" &&
183 (cat path1 && echo new) >new-path &&
185 git add new-path path1 &&
186 git diff -l 4 -C -C --cached --name-status >actual 2>actual.err &&
187 sed -e "s/^\([CM]\)[0-9]* /\1 /" actual >actual.munged &&
188 cat >expect <<-EOF &&
192 test_cmp expect actual.munged &&
193 grep warning actual.err
196 test_expect_success
'rename pretty print with nothing in common' '
200 git commit -m "create a/b/c" &&
202 git mv a/b/c c/b/a &&
203 git commit -m "a/b/c -> c/b/a" &&
204 git diff -M --summary HEAD^ HEAD >output &&
205 test_grep " a/b/c => c/b/a " output &&
206 git diff -M --stat HEAD^ HEAD >output &&
207 test_grep " a/b/c => c/b/a " output
210 test_expect_success
'rename pretty print with common prefix' '
212 git mv c/b/a c/d/e &&
213 git commit -m "c/b/a -> c/d/e" &&
214 git diff -M --summary HEAD^ HEAD >output &&
215 test_grep " c/{b/a => d/e} " output &&
216 git diff -M --stat HEAD^ HEAD >output &&
217 test_grep " c/{b/a => d/e} " output
220 test_expect_success
'rename pretty print with common suffix' '
223 git commit -m "c/d/e -> d/e" &&
224 git diff -M --summary HEAD^ HEAD >output &&
225 test_grep " {c/d => d}/e " output &&
226 git diff -M --stat HEAD^ HEAD >output &&
227 test_grep " {c/d => d}/e " output
230 test_expect_success
'rename pretty print with common prefix and suffix' '
233 git commit -m "d/e -> d/f/e" &&
234 git diff -M --summary HEAD^ HEAD >output &&
235 test_grep " d/{ => f}/e " output &&
236 git diff -M --stat HEAD^ HEAD >output &&
237 test_grep " d/{ => f}/e " output
240 test_expect_success
'rename pretty print common prefix and suffix overlap' '
242 git mv d/f/e d/f/f/e &&
243 git commit -m "d/f/e d/f/f/e" &&
244 git diff -M --summary HEAD^ HEAD >output &&
245 test_grep " d/f/{ => f}/e " output &&
246 git diff -M --stat HEAD^ HEAD >output &&
247 test_grep " d/f/{ => f}/e " output
250 test_expect_success
'diff-tree -l0 defaults to a big rename limit, not zero' '
251 test_write_lines line1 line2 line3 >myfile &&
255 test_write_lines line1 line2 line4 >myotherfile &&
257 git add myotherfile &&
260 git diff-tree -M -l0 HEAD HEAD^ >actual &&
261 # Verify that a rename from myotherfile to myfile was detected
262 grep "myotherfile.*myfile" actual
265 test_expect_success
'basename similarity vs best similarity' '
267 test_write_lines line1 line2 line3 line4 line5 \
268 line6 line7 line8 line9 line10 >subdir/file.txt &&
269 git add subdir/file.txt &&
270 git commit -m "base txt" &&
272 git rm subdir/file.txt &&
273 test_write_lines line1 line2 line3 line4 line5 \
274 line6 line7 line8 >file.txt &&
275 test_write_lines line1 line2 line3 line4 line5 \
276 line6 line7 line8 line9 >file.md &&
277 git add file.txt file.md &&
278 git commit -a -m "rename" &&
279 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
280 # subdir/file.txt is 88% similar to file.md, 78% similar to file.txt,
281 # but since same basenames are checked first...
282 cat >expected <<-\EOF &&
284 R078 subdir/file.txt file.txt
286 test_cmp expected actual
289 test_expect_success
'last line matters too' '
291 test_write_lines a 0 1 2 3 4 5 6 7 8 9 &&
292 printf "git ignores final up to 63 characters if not newline terminated"
294 git add no-final-lf &&
295 git commit -m "original version of file with no final newline" &&
297 # Change ONLY the first character of the whole file
299 test_write_lines b 0 1 2 3 4 5 6 7 8 9 &&
300 printf "git ignores final up to 63 characters if not newline terminated"
302 git add no-final-lf &&
303 git mv no-final-lf still-absent-final-lf &&
304 git commit -a -m "rename no-final-lf -> still-absent-final-lf" &&
305 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
306 sed -e "s/^R[0-9]* /R /" actual >actual.munged &&
307 cat >expected <<-\EOF &&
308 R no-final-lf still-absent-final-lf
310 test_cmp expected actual.munged