Merge branch 'rj/cygwin-has-dev-tty'
[git/gitster.git] / t / t4017-diff-retval.sh
blobd644310e22a5d17b7ba0b57d6bf308e777be1882
1 #!/bin/sh
3 test_description='Return value of diffs'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 test_expect_success 'setup' '
12 echo "1 " >a &&
13 git add . &&
14 git commit -m zeroth &&
15 echo 1 >a &&
16 git add . &&
17 git commit -m first &&
18 echo 2 >b &&
19 git add . &&
20 git commit -a -m second
23 test_expect_success 'git diff --quiet -w HEAD^^ HEAD^' '
24 git diff --quiet -w HEAD^^ HEAD^
27 test_expect_success 'git diff --quiet HEAD^^ HEAD^' '
28 test_must_fail git diff --quiet HEAD^^ HEAD^
31 test_expect_success 'git diff --quiet -w HEAD^ HEAD' '
32 test_must_fail git diff --quiet -w HEAD^ HEAD
35 test_expect_success 'git diff-tree HEAD^ HEAD' '
36 test_expect_code 1 git diff-tree --exit-code HEAD^ HEAD
38 test_expect_success 'git diff-tree HEAD^ HEAD -- a' '
39 git diff-tree --exit-code HEAD^ HEAD -- a
41 test_expect_success 'git diff-tree HEAD^ HEAD -- b' '
42 test_expect_code 1 git diff-tree --exit-code HEAD^ HEAD -- b
44 test_expect_success 'echo HEAD | git diff-tree --stdin' '
45 echo $(git rev-parse HEAD) | test_expect_code 1 git diff-tree --exit-code --stdin
47 test_expect_success 'git diff-tree HEAD HEAD' '
48 git diff-tree --exit-code HEAD HEAD
50 test_expect_success 'git diff-files' '
51 git diff-files --exit-code
53 test_expect_success 'git diff-index --cached HEAD' '
54 git diff-index --exit-code --cached HEAD
56 test_expect_success 'git diff-index --cached HEAD^' '
57 test_expect_code 1 git diff-index --exit-code --cached HEAD^
59 test_expect_success 'git diff-index --cached HEAD^' '
60 echo text >>b &&
61 echo 3 >c &&
62 git add . &&
63 test_expect_code 1 git diff-index --exit-code --cached HEAD^
65 test_expect_success 'git diff-tree -Stext HEAD^ HEAD -- b' '
66 git commit -m "text in b" &&
67 test_expect_code 1 git diff-tree -p --exit-code -Stext HEAD^ HEAD -- b
69 test_expect_success 'git diff-tree -Snot-found HEAD^ HEAD -- b' '
70 git diff-tree -p --exit-code -Snot-found HEAD^ HEAD -- b
72 test_expect_success 'git diff-files' '
73 echo 3 >>c &&
74 test_expect_code 1 git diff-files --exit-code
76 test_expect_success 'git diff-index --cached HEAD' '
77 git update-index c &&
78 test_expect_code 1 git diff-index --exit-code --cached HEAD
81 test_expect_success '--check --exit-code returns 0 for no difference' '
83 git diff --check --exit-code
87 test_expect_success '--check --exit-code returns 1 for a clean difference' '
89 echo "good" > a &&
90 test_expect_code 1 git diff --check --exit-code
94 test_expect_success '--check --exit-code returns 3 for a dirty difference' '
96 echo "bad " >> a &&
97 test_expect_code 3 git diff --check --exit-code
101 test_expect_success '--check with --no-pager returns 2 for dirty difference' '
103 test_expect_code 2 git --no-pager diff --check
107 test_expect_success 'check should test not just the last line' '
108 echo "" >>a &&
109 test_expect_code 2 git --no-pager diff --check
113 test_expect_success 'check detects leftover conflict markers' '
114 git reset --hard &&
115 git checkout HEAD^ &&
116 echo binary >>b &&
117 git commit -m "side" b &&
118 test_must_fail git merge main &&
119 git add b &&
120 test_expect_code 2 git --no-pager diff --cached --check >test.out &&
121 test 3 = $(grep "conflict marker" test.out | wc -l) &&
122 git reset --hard
125 test_expect_success 'check honors conflict marker length' '
126 git reset --hard &&
127 echo ">>>>>>> boo" >>b &&
128 echo "======" >>a &&
129 git diff --check a &&
130 test_expect_code 2 git diff --check b &&
131 git reset --hard &&
132 echo ">>>>>>>> boo" >>b &&
133 echo "========" >>a &&
134 git diff --check &&
135 echo "b conflict-marker-size=8" >.gitattributes &&
136 test_expect_code 2 git diff --check b &&
137 git diff --check a &&
138 git reset --hard
141 test_expect_success 'option errors are not confused by --exit-code' '
142 test_must_fail git diff --exit-code --nonsense 2>err &&
143 grep '^usage:' err
146 for option in --exit-code --quiet
148 test_expect_success "git diff $option returns 1 for copied file" "
149 git reset --hard &&
150 cp a copy &&
151 git add copy &&
152 test_expect_code 1 git diff $option --cached --find-copies-harder
155 test_expect_success "git diff $option returns 1 for renamed file" "
156 git reset --hard &&
157 git mv a renamed &&
158 test_expect_code 1 git diff $option --cached
160 done
162 test_expect_success 'setup dirty subrepo' '
163 git reset --hard &&
164 test_create_repo subrepo &&
165 test_commit -C subrepo subrepo-file &&
166 test_tick &&
167 git add subrepo &&
168 git commit -m subrepo &&
169 test_commit -C subrepo another-subrepo-file
172 for option in --exit-code --quiet
174 for submodule_format in diff log short
176 opts="$option --submodule=$submodule_format" &&
177 test_expect_success "git diff $opts returns 1 for dirty subrepo" "
178 test_expect_code 1 git diff $opts
180 done
181 done
183 test_done