Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2006 Carl D. Worth |
| 4 | # |
| 5 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 6 | test_description='Test of the various options to git rm.' |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
Carl Worth | 3844cdc | 2006-02-22 16:37:27 -0800 | [diff] [blame] | 10 | # Setup some files to be removed, some with funny characters |
Shawn Pearce | 22669a0 | 2006-05-29 04:06:48 -0400 | [diff] [blame] | 11 | test_expect_success \ |
| 12 | 'Initialize test directory' \ |
| 13 | "touch -- foo bar baz 'space embedded' -q && |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 14 | git add -- foo bar baz 'space embedded' -q && |
Johannes Sixt | 56e78bf | 2009-02-26 23:09:00 +0100 | [diff] [blame] | 15 | git commit -m 'add normal files'" |
| 16 | |
| 17 | if touch -- 'tab embedded' 'newline |
| 18 | embedded' 2>/dev/null |
| 19 | then |
| 20 | test_set_prereq FUNNYNAMES |
| 21 | else |
| 22 | say 'Your filesystem does not allow tabs in filenames.' |
| 23 | fi |
| 24 | |
| 25 | test_expect_success FUNNYNAMES 'add files with funny names' " |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 26 | git add -- 'tab embedded' 'newline |
Shawn Pearce | 22669a0 | 2006-05-29 04:06:48 -0400 | [diff] [blame] | 27 | embedded' && |
Nanako Shiraishi | 3604e7c | 2008-09-03 17:59:29 +0900 | [diff] [blame] | 28 | git commit -m 'add files with tabs and newlines' |
Johannes Sixt | 56e78bf | 2009-02-26 23:09:00 +0100 | [diff] [blame] | 29 | " |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 30 | |
Johannes Sixt | 56e78bf | 2009-02-26 23:09:00 +0100 | [diff] [blame] | 31 | # Determine rm behavior |
Junio C Hamano | 2283645 | 2006-04-13 11:57:57 -0700 | [diff] [blame] | 32 | # Later we will try removing an unremovable path to make sure |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 33 | # git rm barfs, but if the test is run as root that cannot be |
Junio C Hamano | 2283645 | 2006-04-13 11:57:57 -0700 | [diff] [blame] | 34 | # arranged. |
Johannes Sixt | 56e78bf | 2009-02-26 23:09:00 +0100 | [diff] [blame] | 35 | : >test-file |
| 36 | chmod a-w . |
| 37 | rm -f test-file 2>/dev/null |
| 38 | if test -f test-file |
| 39 | then |
| 40 | test_set_prereq RO_DIR |
| 41 | else |
| 42 | say 'skipping removal failure test (perhaps running as root?)' |
| 43 | fi |
| 44 | chmod 775 . |
| 45 | rm -f test-file |
Junio C Hamano | 2283645 | 2006-04-13 11:57:57 -0700 | [diff] [blame] | 46 | |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 47 | test_expect_success \ |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 48 | 'Pre-check that foo exists and is in index before git rm foo' \ |
| 49 | '[ -f foo ] && git ls-files --error-unmatch foo' |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 50 | |
| 51 | test_expect_success \ |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 52 | 'Test that git rm foo succeeds' \ |
| 53 | 'git rm --cached foo' |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 54 | |
Carl Worth | 3844cdc | 2006-02-22 16:37:27 -0800 | [diff] [blame] | 55 | test_expect_success \ |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 56 | 'Test that git rm --cached foo succeeds if the index matches the file' \ |
| 57 | 'echo content > foo |
| 58 | git add foo |
| 59 | git rm --cached foo' |
| 60 | |
| 61 | test_expect_success \ |
| 62 | 'Test that git rm --cached foo succeeds if the index matches the file' \ |
| 63 | 'echo content > foo |
| 64 | git add foo |
| 65 | git commit -m foo |
| 66 | echo "other content" > foo |
| 67 | git rm --cached foo' |
| 68 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 69 | test_expect_success \ |
| 70 | 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' ' |
| 71 | echo content > foo |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 72 | git add foo |
| 73 | git commit -m foo |
| 74 | echo "other content" > foo |
| 75 | git add foo |
| 76 | echo "yet another content" > foo |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 77 | test_must_fail git rm --cached foo |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 78 | ' |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 79 | |
| 80 | test_expect_success \ |
| 81 | 'Test that git rm --cached -f foo works in case where --cached only did not' \ |
| 82 | 'echo content > foo |
| 83 | git add foo |
| 84 | git commit -m foo |
| 85 | echo "other content" > foo |
| 86 | git add foo |
| 87 | echo "yet another content" > foo |
| 88 | git rm --cached -f foo' |
| 89 | |
| 90 | test_expect_success \ |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 91 | 'Post-check that foo exists but is not in index after git rm foo' \ |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 92 | '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo' |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 93 | |
| 94 | test_expect_success \ |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 95 | 'Pre-check that bar exists and is in index before "git rm bar"' \ |
| 96 | '[ -f bar ] && git ls-files --error-unmatch bar' |
Carl Worth | 3844cdc | 2006-02-22 16:37:27 -0800 | [diff] [blame] | 97 | |
| 98 | test_expect_success \ |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 99 | 'Test that "git rm bar" succeeds' \ |
| 100 | 'git rm bar' |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 101 | |
Carl Worth | 3844cdc | 2006-02-22 16:37:27 -0800 | [diff] [blame] | 102 | test_expect_success \ |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 103 | 'Post-check that bar does not exist and is not in index after "git rm -f bar"' \ |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 104 | '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar' |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 105 | |
| 106 | test_expect_success \ |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 107 | 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \ |
| 108 | 'git rm -- -q' |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 109 | |
Johannes Sixt | 56e78bf | 2009-02-26 23:09:00 +0100 | [diff] [blame] | 110 | test_expect_success FUNNYNAMES \ |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 111 | "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \ |
| 112 | "git rm -f 'space embedded' 'tab embedded' 'newline |
Carl Worth | 3844cdc | 2006-02-22 16:37:27 -0800 | [diff] [blame] | 113 | embedded'" |
| 114 | |
Johannes Sixt | 56e78bf | 2009-02-26 23:09:00 +0100 | [diff] [blame] | 115 | test_expect_success RO_DIR 'Test that "git rm -f" fails if its rm fails' ' |
| 116 | chmod a-w . && |
| 117 | test_must_fail git rm -f baz && |
| 118 | chmod 775 . |
| 119 | ' |
Carl Worth | 3844cdc | 2006-02-22 16:37:27 -0800 | [diff] [blame] | 120 | |
| 121 | test_expect_success \ |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 122 | 'When the rm in "git rm -f" fails, it should not remove the file from the index' \ |
| 123 | 'git ls-files --error-unmatch baz' |
Carl Worth | 3844cdc | 2006-02-22 16:37:27 -0800 | [diff] [blame] | 124 | |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 125 | test_expect_success 'Remove nonexistent file with --ignore-unmatch' ' |
| 126 | git rm --ignore-unmatch nonexistent |
| 127 | ' |
| 128 | |
Steven Grimm | b48caa2 | 2007-04-16 00:46:48 -0700 | [diff] [blame] | 129 | test_expect_success '"rm" command printed' ' |
| 130 | echo frotz > test-file && |
| 131 | git add test-file && |
| 132 | git commit -m "add file for rm test" && |
| 133 | git rm test-file > rm-output && |
Junio C Hamano | 0feb4d1 | 2008-01-04 22:28:13 -0800 | [diff] [blame] | 134 | test `grep "^rm " rm-output | wc -l` = 1 && |
Steven Grimm | b48caa2 | 2007-04-16 00:46:48 -0700 | [diff] [blame] | 135 | rm -f test-file rm-output && |
| 136 | git commit -m "remove file from rm test" |
| 137 | ' |
| 138 | |
| 139 | test_expect_success '"rm" command suppressed with --quiet' ' |
| 140 | echo frotz > test-file && |
| 141 | git add test-file && |
| 142 | git commit -m "add file for rm --quiet test" && |
| 143 | git rm --quiet test-file > rm-output && |
| 144 | test `wc -l < rm-output` = 0 && |
| 145 | rm -f test-file rm-output && |
| 146 | git commit -m "remove file from rm --quiet test" |
| 147 | ' |
| 148 | |
Junio C Hamano | 467e1b5 | 2006-12-25 03:11:17 -0800 | [diff] [blame] | 149 | # Now, failure cases. |
| 150 | test_expect_success 'Re-add foo and baz' ' |
| 151 | git add foo baz && |
| 152 | git ls-files --error-unmatch foo baz |
| 153 | ' |
| 154 | |
| 155 | test_expect_success 'Modify foo -- rm should refuse' ' |
| 156 | echo >>foo && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 157 | test_must_fail git rm foo baz && |
Junio C Hamano | 467e1b5 | 2006-12-25 03:11:17 -0800 | [diff] [blame] | 158 | test -f foo && |
| 159 | test -f baz && |
| 160 | git ls-files --error-unmatch foo baz |
| 161 | ' |
| 162 | |
| 163 | test_expect_success 'Modified foo -- rm -f should work' ' |
| 164 | git rm -f foo baz && |
| 165 | test ! -f foo && |
| 166 | test ! -f baz && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 167 | test_must_fail git ls-files --error-unmatch foo && |
| 168 | test_must_fail git ls-files --error-unmatch bar |
Junio C Hamano | 467e1b5 | 2006-12-25 03:11:17 -0800 | [diff] [blame] | 169 | ' |
| 170 | |
| 171 | test_expect_success 'Re-add foo and baz for HEAD tests' ' |
| 172 | echo frotz >foo && |
| 173 | git checkout HEAD -- baz && |
| 174 | git add foo baz && |
| 175 | git ls-files --error-unmatch foo baz |
| 176 | ' |
| 177 | |
| 178 | test_expect_success 'foo is different in index from HEAD -- rm should refuse' ' |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 179 | test_must_fail git rm foo baz && |
Junio C Hamano | 467e1b5 | 2006-12-25 03:11:17 -0800 | [diff] [blame] | 180 | test -f foo && |
| 181 | test -f baz && |
| 182 | git ls-files --error-unmatch foo baz |
| 183 | ' |
| 184 | |
| 185 | test_expect_success 'but with -f it should work.' ' |
| 186 | git rm -f foo baz && |
| 187 | test ! -f foo && |
| 188 | test ! -f baz && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 189 | test_must_fail git ls-files --error-unmatch foo |
| 190 | test_must_fail git ls-files --error-unmatch baz |
Junio C Hamano | 467e1b5 | 2006-12-25 03:11:17 -0800 | [diff] [blame] | 191 | ' |
| 192 | |
Junio C Hamano | 388b2ac | 2008-11-28 19:55:25 -0800 | [diff] [blame] | 193 | test_expect_success 'refuse to remove cached empty file with modifications' ' |
| 194 | >empty && |
Jeff King | f55527f | 2008-10-21 09:54:19 -0400 | [diff] [blame] | 195 | git add empty && |
| 196 | echo content >empty && |
| 197 | test_must_fail git rm --cached empty |
| 198 | ' |
| 199 | |
| 200 | test_expect_success 'remove intent-to-add file without --force' ' |
| 201 | echo content >intent-to-add && |
| 202 | git add -N intent-to-add |
| 203 | git rm --cached intent-to-add |
| 204 | ' |
| 205 | |
Junio C Hamano | 467e1b5 | 2006-12-25 03:11:17 -0800 | [diff] [blame] | 206 | test_expect_success 'Recursive test setup' ' |
| 207 | mkdir -p frotz && |
| 208 | echo qfwfq >frotz/nitfol && |
| 209 | git add frotz && |
| 210 | git commit -m "subdir test" |
| 211 | ' |
| 212 | |
| 213 | test_expect_success 'Recursive without -r fails' ' |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 214 | test_must_fail git rm frotz && |
Junio C Hamano | 467e1b5 | 2006-12-25 03:11:17 -0800 | [diff] [blame] | 215 | test -d frotz && |
| 216 | test -f frotz/nitfol |
| 217 | ' |
| 218 | |
| 219 | test_expect_success 'Recursive with -r but dirty' ' |
| 220 | echo qfwfq >>frotz/nitfol |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 221 | test_must_fail git rm -r frotz && |
Junio C Hamano | 467e1b5 | 2006-12-25 03:11:17 -0800 | [diff] [blame] | 222 | test -d frotz && |
| 223 | test -f frotz/nitfol |
| 224 | ' |
| 225 | |
| 226 | test_expect_success 'Recursive with -r -f' ' |
| 227 | git rm -f -r frotz && |
| 228 | ! test -f frotz/nitfol && |
| 229 | ! test -d frotz |
| 230 | ' |
| 231 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 232 | test_expect_success 'Remove nonexistent file returns nonzero exit status' ' |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 233 | test_must_fail git rm nonexistent |
Steven Grimm | b48caa2 | 2007-04-16 00:46:48 -0700 | [diff] [blame] | 234 | ' |
| 235 | |
Olivier Marin | 4d26467 | 2008-07-19 18:24:46 +0200 | [diff] [blame] | 236 | test_expect_success 'Call "rm" from outside the work tree' ' |
| 237 | mkdir repo && |
Johannes Schindelin | cced48a | 2008-10-07 18:08:21 +0200 | [diff] [blame] | 238 | (cd repo && |
| 239 | git init && |
| 240 | echo something > somefile && |
| 241 | git add somefile && |
| 242 | git commit -m "add a file" && |
| 243 | (cd .. && |
| 244 | git --git-dir=repo/.git --work-tree=repo rm somefile) && |
| 245 | test_must_fail git ls-files --error-unmatch somefile) |
| 246 | ' |
| 247 | |
| 248 | test_expect_success 'refresh index before checking if it is up-to-date' ' |
| 249 | |
| 250 | git reset --hard && |
| 251 | test-chmtime -86400 frotz/nitfol && |
| 252 | git rm frotz/nitfol && |
| 253 | test ! -f frotz/nitfol |
| 254 | |
Olivier Marin | 4d26467 | 2008-07-19 18:24:46 +0200 | [diff] [blame] | 255 | ' |
| 256 | |
Junio C Hamano | 0693f9d | 2008-12-18 17:31:57 -0800 | [diff] [blame] | 257 | test_expect_success 'choking "git rm" should not let it die with cruft' ' |
| 258 | git reset -q --hard && |
| 259 | H=0000000000000000000000000000000000000000 && |
| 260 | i=0 && |
| 261 | while test $i -lt 12000 |
| 262 | do |
| 263 | echo "100644 $H 0 some-file-$i" |
| 264 | i=$(( $i + 1 )) |
| 265 | done | git update-index --index-info && |
| 266 | git rm -n "some-file-*" | :; |
| 267 | test -f .git/index.lock |
| 268 | status=$? |
| 269 | rm -f .git/index.lock |
| 270 | git reset -q --hard |
| 271 | test "$status" != 0 |
| 272 | ' |
| 273 | |
Carl Worth | d4a1cab | 2006-02-21 15:04:51 -0800 | [diff] [blame] | 274 | test_done |