[PATCH] Add -B flag to diff-* brothers.
[git.git] / t / t4008-diff-break-rewrite.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
6 test_description='Break and then rename
7
8 We have two very different files, file0 and file1, registered in a tree.
9
10 We update file1 so drastically that it is more similar to file0, and
11 then remove file0.  With -B, changes to file1 should be broken into
12 separate delete and create, resulting in removal of file0, removal of
13 original file1 and creation of completely rewritten file1.
14
15 Further, with -B and -M together, these three modifications should
16 turn into rename-edit of file0 into file1.
17
18 Starting from the same two files in the tree, we swap file0 and file1.
19 With -B, this should be detected as two complete rewrites, resulting in
20 four changes in total.
21
22 Further, with -B and -M together, these should turn into two renames.
23 '
24 . ./test-lib.sh
25
26 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
27 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
28 sanitize_diff_raw='s/ '"$_x40"' '"$_x40"' \([CDNR]\)[0-9]*      / X X \1#       /'
29 compare_diff_raw () {
30     # When heuristics are improved, the score numbers would change.
31     # Ignore them while comparing.
32     # Also we do not check SHA1 hash generation in this test, which
33     # is a job for t0000-basic.sh
34
35     sed -e "$sanitize_diff_raw" <"$1" >.tmp-1
36     sed -e "$sanitize_diff_raw" <"$2" >.tmp-2
37     diff -u .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
38 }
39
40 test_expect_success \
41     setup \
42     'cat ../../README >file0 &&
43      cat ../../COPYING >file1 &&
44     git-update-cache --add file0 file1 &&
45     tree=$(git-write-tree) &&
46     echo "$tree"'
47
48 test_expect_success \
49     'change file1 with copy-edit of file0 and remove file0' \
50     'sed -e "s/git/GIT/" file0 >file1 &&
51      rm -f file0 &&
52     git-update-cache --remove file0 file1'
53
54 test_expect_success \
55     'run diff with -B' \
56     'git-diff-cache -B --cached "$tree" >current'
57
58 cat >expected <<\EOF
59 :100644 000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 0000000000000000000000000000000000000000 D      file0
60 :100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
61 :000000 100644 0000000000000000000000000000000000000000 11e331465a89c394dc25c780de230043750c1ec8 N100   file1
62 EOF
63
64 test_expect_success \
65     'validate result of -B (#1)' \
66     'compare_diff_raw current expected'
67
68 test_expect_success \
69     'run diff with -B and -M' \
70     'git-diff-cache -B -M "$tree" >current'
71
72 cat >expected <<\EOF
73 :100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 08bb2fb671deff4c03a4d4a0a1315dff98d5732c R100   file0   file1
74 EOF
75
76 test_expect_success \
77     'validate result of -B -M (#2)' \
78     'compare_diff_raw current expected'
79
80 test_expect_success \
81     'swap file0 and file1' \
82     'rm -f file0 file1 &&
83      git-read-tree -m $tree &&
84      git-checkout-cache -f -u -a &&
85      mv file0 tmp &&
86      mv file1 file0 &&
87      mv tmp file1 &&
88      git-update-cache file0 file1'
89
90 test_expect_success \
91     'run diff with -B' \
92     'git-diff-cache -B "$tree" >current'
93
94 cat >expected <<\EOF
95 :100644 000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 0000000000000000000000000000000000000000 D100   file0
96 :000000 100644 0000000000000000000000000000000000000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 N100   file0
97 :100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
98 :000000 100644 0000000000000000000000000000000000000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 N100   file1
99 EOF
100
101 test_expect_success \
102     'validate result of -B (#3)' \
103     'compare_diff_raw current expected'
104
105 test_expect_success \
106     'run diff with -B and -M' \
107     'git-diff-cache -B -M "$tree" >current'
108
109 cat >expected <<\EOF
110 :100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 6ff87c4664981e4397625791c8ea3bbb5f2279a3 R100   file1   file0
111 :100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 R100   file0   file1
112 EOF
113
114 test_expect_success \
115     'validate result of -B -M (#4)' \
116     'compare_diff_raw current expected'
117
118 test_expect_success \
119     'make file0 into something completely different' \
120     'rm -f file0 &&
121      ln -s frotz file0 &&
122      git-update-cache file0 file1'
123
124 test_expect_success \
125     'run diff with -B' \
126     'git-diff-cache -B "$tree" >current'
127
128 cat >expected <<\EOF
129 :100644 120000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 67be421f88824578857624f7b3dc75e99a8a1481 T      file0
130 :100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
131 :000000 100644 0000000000000000000000000000000000000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 N100   file1
132 EOF
133
134 test_expect_success \
135     'validate result of -B (#5)' \
136     'compare_diff_raw current expected'
137
138 test_expect_success \
139     'run diff with -B' \
140     'git-diff-cache -B -M "$tree" >current'
141
142 # This should not mistake file0 as the copy source of new file1
143 # due to type differences.
144 cat >expected <<\EOF
145 :100644 120000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 67be421f88824578857624f7b3dc75e99a8a1481 T      file0
146 :100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
147 :000000 100644 0000000000000000000000000000000000000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 N100   file1
148 EOF
149
150 test_expect_success \
151     'validate result of -B -M (#6)' \
152     'compare_diff_raw current expected'
153
154 test_expect_success \
155     'run diff with -M' \
156     'git-diff-cache -M "$tree" >current'
157
158 # This should not mistake file0 as the copy source of new file1
159 # due to type differences.
160 cat >expected <<\EOF
161 :100644 120000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 67be421f88824578857624f7b3dc75e99a8a1481 T      file0
162 :100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 M      file1
163 EOF
164
165 test_expect_success \
166     'validate result of -M (#7)' \
167     'compare_diff_raw current expected'
168
169 test_expect_success \
170     'file1 edited to look like file0 and file0 rename-edited to file2' \
171     'rm -f file0 file1 &&
172      git-read-tree -m $tree &&
173      git-checkout-cache -f -u -a &&
174      sed -e "s/git/GIT/" file0 >file1 &&
175      sed -e "s/git/GET/" file0 >file2 &&
176      rm -f file0
177      git-update-cache --add --remove file0 file1 file2'
178
179 test_expect_success \
180     'run diff with -B' \
181     'git-diff-cache -B "$tree" >current'
182
183 cat >expected <<\EOF
184 :100644 000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 0000000000000000000000000000000000000000 D      file0
185 :100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
186 :000000 100644 0000000000000000000000000000000000000000 08bb2fb671deff4c03a4d4a0a1315dff98d5732c N100   file1
187 :000000 100644 0000000000000000000000000000000000000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 N      file2
188 EOF
189
190 test_expect_success \
191     'validate result of -B (#8)' \
192     'compare_diff_raw current expected'
193
194 test_expect_success \
195     'run diff with -B -M' \
196     'git-diff-cache -B -M "$tree" >current'
197
198 cat >expected <<\EOF
199 :100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 08bb2fb671deff4c03a4d4a0a1315dff98d5732c C095   file0   file1
200 :100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 59f832e5c8b3f7e486be15ad0cd3e95ba9af8998 R095   file0   file2
201 EOF
202
203 test_expect_success \
204     'validate result of -B -M (#9)' \
205     'compare_diff_raw current expected'
206
207 test_done