git-rm: Fix to properly handle files with spaces, tabs, newlines, etc.
New tests are added to the git-rm test case to cover this as well. Signed-off-by: Carl Worth <cworth@cworth.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Carl Worth committed
Feb 22, 2006 at 16:37 UTC
3844cdc8f19dcd848003586d6b98c9f2bd36a7d0
2 files changed
+55
-34
git-rm.sh
+20
-17
@@ -4,7 +4,6 @@ USAGE='[-f] [-n] [-v] [--] <file>...'
4
SUBDIRECTORY_OK='Yes'
5
. git-sh-setup
6
7
-index_remove_option=--force-remove
7
remove_files=
8
show_only=
9
verbose=
@@ -12,7 +11,6 @@ while : ; do
11
case "$1" in
12
-f)
13
remove_files=true
15
- index_remote_option=--force
14
;;
15
-n)
16
show_only=true
@@ -45,23 +43,28 @@ case "$#" in
43
;;
44
esac
45
48
-files=$(
49
- if test -f "$GIT_DIR/info/exclude" ; then
50
- git-ls-files \
51
- --exclude-from="$GIT_DIR/info/exclude" \
52
- --exclude-per-directory=.gitignore -- "$@"
53
- else
54
- git-ls-files \
46
+if test -f "$GIT_DIR/info/exclude"
47
+then
48
+ git-ls-files -z \
49
+ --exclude-from="$GIT_DIR/info/exclude" \
50
--exclude-per-directory=.gitignore -- "$@"
56
- fi | sort | uniq
57
-)
58
-
59
-case "$show_only" in
60
-true)
61
- echo $files
51
+else
52
+ git-ls-files -z \
53
+ --exclude-per-directory=.gitignore -- "$@"
54
+fi |
55
+case "$show_only,$remove_files" in
56
+true,*)
57
+ xargs -0 echo
58
+ ;;
59
+*,true)
60
+ xargs -0 sh -c "
61
+ while [ \$# -gt 0 ]; do
62
+ file=\$1; shift
63
+ rm -- \"\$file\" && git-update-index --remove $verbose \"\$file\"
64
+ done
65
+ " inline
66
;;
67
*)
64
- [[ "$remove_files" = "true" ]] && rm -- $files
65
- git-update-index $index_remove_option $verbose $files
68
+ git-update-index --force-remove $verbose -z --stdin
69
;;
70
esac
t/t3600-rm.sh
+35
-17
@@ -7,36 +7,54 @@ test_description='Test of the various options to git-rm.'
7
8
. ./test-lib.sh
9
10
-# Setup some files to be removed
11
-touch foo bar
12
-git-add foo bar
13
-# Need one to test --
14
-touch -- -q
15
-git update-index --add -- -q
16
-git-commit -m "add foo, bar, and -q"
10
+# Setup some files to be removed, some with funny characters
11
+touch -- foo bar baz 'space embedded' 'tab embedded' 'newline
12
+embedded' -q
13
+git-add -- foo bar baz 'space embedded' 'tab embedded' 'newline
14
+embedded' -q
15
+git-commit -m "add files"
16
17
test_expect_success \
19
- 'Pre-check that foo is in index before git-rm foo' \
20
- 'git-ls-files --error-unmatch foo'
18
+ 'Pre-check that foo exists and is in index before git-rm foo' \
19
+ '[ -f foo ] && git-ls-files --error-unmatch foo'
20
21
test_expect_success \
22
'Test that git-rm foo succeeds' \
23
'git-rm foo'
24
26
-test_expect_failure \
27
- 'Post-check that foo is not in index after git-rm foo' \
28
- 'git-ls-files --error-unmatch foo'
25
+test_expect_success \
26
+ 'Post-check that foo exists but is not in index after git-rm foo' \
27
+ '[ -f foo ] && ! git-ls-files --error-unmatch foo'
28
+
29
+test_expect_success \
30
+ 'Pre-check that bar exists and is in index before "git-rm -f bar"' \
31
+ '[ -f bar ] && git-ls-files --error-unmatch bar'
32
33
test_expect_success \
31
- 'Test that "git-rm -f bar" works' \
34
+ 'Test that "git-rm -f bar" succeeds' \
35
'git-rm -f bar'
36
34
-test_expect_failure \
35
- 'Post-check that bar no longer exists' \
36
- '[ -f bar ]'
37
+test_expect_success \
38
+ 'Post-check that bar does not exist and is not in index after "git-rm -f bar"' \
39
+ '! [ -f bar ] && ! git-ls-files --error-unmatch bar'
40
41
test_expect_success \
39
- 'Test that "git-rm -- -q" works to delete a file named -q' \
42
+ 'Test that "git-rm -- -q" succeeds (remove a file that looks like an option)' \
43
'git-rm -- -q'
44
45
+test_expect_success \
46
+ "Test that \"git-rm -f\" succeeds with embedded space, tab, or newline characters." \
47
+ "git-rm -f 'space embedded' 'tab embedded' 'newline
48
+embedded'"
49
+
50
+chmod u-w .
51
+test_expect_failure \
52
+ 'Test that "git-rm -f" fails if its rm fails' \
53
+ 'git-rm -f baz'
54
+chmod u+w .
55
+
56
+test_expect_success \
57
+ 'When the rm in "git-rm -f" fails, it should not remove the file from the index' \
58
+ 'git-ls-files --error-unmatch baz'
59
+
60
test_done