diff --check: honor conflict-marker-size attribute
[git] / t / t4017-diff-retval.sh
1 #!/bin/sh
2
3 test_description='Return value of diffs'
4
5 . ./test-lib.sh
6
7 test_expect_success 'setup' '
8         echo 1 >a &&
9         git add . &&
10         git commit -m first &&
11         echo 2 >b &&
12         git add . &&
13         git commit -a -m second
14 '
15
16 test_expect_success 'git diff-tree HEAD^ HEAD' '
17         git diff-tree --exit-code HEAD^ HEAD
18         test $? = 1
19 '
20 test_expect_success 'git diff-tree HEAD^ HEAD -- a' '
21         git diff-tree --exit-code HEAD^ HEAD -- a
22         test $? = 0
23 '
24 test_expect_success 'git diff-tree HEAD^ HEAD -- b' '
25         git diff-tree --exit-code HEAD^ HEAD -- b
26         test $? = 1
27 '
28 test_expect_success 'echo HEAD | git diff-tree --stdin' '
29         echo $(git rev-parse HEAD) | git diff-tree --exit-code --stdin
30         test $? = 1
31 '
32 test_expect_success 'git diff-tree HEAD HEAD' '
33         git diff-tree --exit-code HEAD HEAD
34         test $? = 0
35 '
36 test_expect_success 'git diff-files' '
37         git diff-files --exit-code
38         test $? = 0
39 '
40 test_expect_success 'git diff-index --cached HEAD' '
41         git diff-index --exit-code --cached HEAD
42         test $? = 0
43 '
44 test_expect_success 'git diff-index --cached HEAD^' '
45         git diff-index --exit-code --cached HEAD^
46         test $? = 1
47 '
48 test_expect_success 'git diff-index --cached HEAD^' '
49         echo text >>b &&
50         echo 3 >c &&
51         git add . && {
52                 git diff-index --exit-code --cached HEAD^
53                 test $? = 1
54         }
55 '
56 test_expect_success 'git diff-tree -Stext HEAD^ HEAD -- b' '
57         git commit -m "text in b" && {
58                 git diff-tree -p --exit-code -Stext HEAD^ HEAD -- b
59                 test $? = 1
60         }
61 '
62 test_expect_success 'git diff-tree -Snot-found HEAD^ HEAD -- b' '
63         git diff-tree -p --exit-code -Snot-found HEAD^ HEAD -- b
64         test $? = 0
65 '
66 test_expect_success 'git diff-files' '
67         echo 3 >>c && {
68                 git diff-files --exit-code
69                 test $? = 1
70         }
71 '
72 test_expect_success 'git diff-index --cached HEAD' '
73         git update-index c && {
74                 git diff-index --exit-code --cached HEAD
75                 test $? = 1
76         }
77 '
78
79 test_expect_success '--check --exit-code returns 0 for no difference' '
80
81         git diff --check --exit-code
82
83 '
84
85 test_expect_success '--check --exit-code returns 1 for a clean difference' '
86
87         echo "good" > a &&
88         git diff --check --exit-code
89         test $? = 1
90
91 '
92
93 test_expect_success '--check --exit-code returns 3 for a dirty difference' '
94
95         echo "bad   " >> a &&
96         git diff --check --exit-code
97         test $? = 3
98
99 '
100
101 test_expect_success '--check with --no-pager returns 2 for dirty difference' '
102
103         git --no-pager diff --check
104         test $? = 2
105
106 '
107
108 test_expect_success 'check should test not just the last line' '
109         echo "" >>a &&
110         git --no-pager diff --check
111         test $? = 2
112
113 '
114
115 test_expect_success 'check detects leftover conflict markers' '
116         git reset --hard &&
117         git checkout HEAD^ &&
118         echo binary >>b &&
119         git commit -m "side" b &&
120         test_must_fail git merge master &&
121         git add b && (
122                 git --no-pager diff --cached --check >test.out
123                 test $? = 2
124         ) &&
125         test 3 = $(grep "conflict marker" test.out | wc -l) &&
126         git reset --hard
127 '
128
129 test_expect_success 'check honors conflict marker length' '
130         git reset --hard &&
131         echo ">>>>>>> boo" >>b &&
132         echo "======" >>a &&
133         git diff --check a &&
134         (
135                 git diff --check b
136                 test $? = 2
137         ) &&
138         git reset --hard &&
139         echo ">>>>>>>> boo" >>b &&
140         echo "========" >>a &&
141         git diff --check &&
142         echo "b conflict-marker-size=8" >.gitattributes &&
143         (
144                 git diff --check b
145                 test $? = 2
146         ) &&
147         git diff --check a &&
148         git reset --hard
149 '
150
151 test_done