1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
6test_description='Break and then rename
7
8We have two very different files, file0 and file1, registered in a tree.
9
10We update file1 so drastically that it is more similar to file0, and
11then remove file0. With -B, changes to file1 should be broken into
12separate delete and create, resulting in removal of file0, removal of
13original file1 and creation of completely rewritten file1. The latter
14two are then merged back into a single "complete rewrite".
15
16Further, with -B and -M together, these three modifications should
17turn into rename-edit of file0 into file1.
18
19Starting from the same two files in the tree, we swap file0 and file1.
20With -B, this should be detected as two complete rewrites.
21
22Further, with -B and -M together, these should turn into two renames.
23'
24. ./test-lib.sh
25. "$TEST_DIRECTORY"/diff-lib.sh ;# test-lib chdir's into trash
26
27test_expect_success setup '
28 cat "$TEST_DIRECTORY"/diff-lib/README >file0 &&
29 cat "$TEST_DIRECTORY"/diff-lib/COPYING >file1 &&
30 blob0_id=$(git hash-object file0) &&
31 blob1_id=$(git hash-object file1) &&
32 git update-index --add file0 file1 &&
33 git tag reference $(git write-tree)
34'
35
36test_expect_success 'change file1 with copy-edit of file0 and remove file0' '
37 sed -e "s/git/GIT/" file0 >file1 &&
38 blob2_id=$(git hash-object file1) &&
39 rm -f file0 &&
40 git update-index --remove file0 file1
41'
42
43test_expect_success 'run diff with -B (#1)' '
44 git diff-index -B --cached reference >current &&
45 cat >expect <<-EOF &&
46 :100644 000000 $blob0_id $ZERO_OID D file0
47 :100644 100644 $blob1_id $blob2_id M100 file1
48 EOF
49 compare_diff_raw expect current
50'
51
52test_expect_success 'run diff with -B and -M (#2)' '
53 git diff-index -B -M reference >current &&
54 cat >expect <<-EOF &&
55 :100644 100644 $blob0_id $blob2_id R100 file0 file1
56 EOF
57 compare_diff_raw expect current
58'
59
60test_expect_success 'swap file0 and file1' '
61 rm -f file0 file1 &&
62 git read-tree -m reference &&
63 git checkout-index -f -u -a &&
64 mv file0 tmp &&
65 mv file1 file0 &&
66 mv tmp file1 &&
67 git update-index file0 file1
68'
69
70test_expect_success 'run diff with -B (#3)' '
71 git diff-index -B reference >current &&
72 cat >expect <<-EOF &&
73 :100644 100644 $blob0_id $blob1_id M100 file0
74 :100644 100644 $blob1_id $blob0_id M100 file1
75 EOF
76 compare_diff_raw expect current
77'
78
79test_expect_success 'run diff with -B and -M (#4)' '
80 git diff-index -B -M reference >current &&
81 cat >expect <<-EOF &&
82 :100644 100644 $blob1_id $blob1_id R100 file1 file0
83 :100644 100644 $blob0_id $blob0_id R100 file0 file1
84 EOF
85 compare_diff_raw expect current
86'
87
88test_expect_success 'make file0 into something completely different' '
89 rm -f file0 &&
90 test_ln_s_add frotz file0 &&
91 slink_id=$(printf frotz | git hash-object --stdin) &&
92 git update-index file1
93'
94
95test_expect_success 'run diff with -B (#5)' '
96 git diff-index -B reference >current &&
97 cat >expect <<-EOF &&
98 :100644 120000 $blob0_id $slink_id T file0
99 :100644 100644 $blob1_id $blob0_id M100 file1
100 EOF
101 compare_diff_raw expect current
102'
103
104test_expect_success 'run diff with -B -M (#6)' '
105 git diff-index -B -M reference >current &&
106
107 # file0 changed from regular to symlink. file1 is the same as the preimage
108 # of file0. Because the change does not make file0 disappear, file1 is
109 # denoted as a copy of file0
110 cat >expect <<-EOF &&
111 :100644 120000 $blob0_id $slink_id T file0
112 :100644 100644 $blob0_id $blob0_id C file0 file1
113 EOF
114 compare_diff_raw expect current
115'
116
117test_expect_success 'run diff with -M (#7)' '
118 git diff-index -M reference >current &&
119
120 # This should not mistake file0 as the copy source of new file1
121 # due to type differences.
122 cat >expect <<-EOF &&
123 :100644 120000 $blob0_id $slink_id T file0
124 :100644 100644 $blob1_id $blob0_id M file1
125 EOF
126 compare_diff_raw expect current
127'
128
129test_expect_success 'file1 edited to look like file0 and file0 rename-edited to file2' '
130 rm -f file0 file1 &&
131 git read-tree -m reference &&
132 git checkout-index -f -u -a &&
133 sed -e "s/git/GIT/" file0 >file1 &&
134 sed -e "s/git/GET/" file0 >file2 &&
135 blob3_id=$(git hash-object file2) &&
136 rm -f file0 &&
137 git update-index --add --remove file0 file1 file2
138'
139
140test_expect_success 'run diff with -B (#8)' '
141 git diff-index -B reference >current &&
142 cat >expect <<-EOF &&
143 :100644 000000 $blob0_id $ZERO_OID D file0
144 :100644 100644 $blob1_id $blob2_id M100 file1
145 :000000 100644 $ZERO_OID $blob3_id A file2
146 EOF
147 compare_diff_raw expect current
148'
149
150test_expect_success 'run diff with -B -C (#9)' '
151 git diff-index -B -C reference >current &&
152 cat >expect <<-EOF &&
153 :100644 100644 $blob0_id $blob2_id C095 file0 file1
154 :100644 100644 $blob0_id $blob3_id R095 file0 file2
155 EOF
156 compare_diff_raw expect current
157'
158
159test_done