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>
This commit is contained in:
Carl Worth 2006-02-22 16:37:27 -08:00 committed by Junio C Hamano
parent d4a1cab541
commit 3844cdc8f1
2 changed files with 55 additions and 34 deletions

@ -4,7 +4,6 @@ USAGE='[-f] [-n] [-v] [--] <file>...'
SUBDIRECTORY_OK='Yes' SUBDIRECTORY_OK='Yes'
. git-sh-setup . git-sh-setup
index_remove_option=--force-remove
remove_files= remove_files=
show_only= show_only=
verbose= verbose=
@ -12,7 +11,6 @@ while : ; do
case "$1" in case "$1" in
-f) -f)
remove_files=true remove_files=true
index_remote_option=--force
;; ;;
-n) -n)
show_only=true show_only=true
@ -45,23 +43,28 @@ case "$#" in
;; ;;
esac esac
files=$( if test -f "$GIT_DIR/info/exclude"
if test -f "$GIT_DIR/info/exclude" ; then then
git-ls-files \ git-ls-files -z \
--exclude-from="$GIT_DIR/info/exclude" \ --exclude-from="$GIT_DIR/info/exclude" \
--exclude-per-directory=.gitignore -- "$@" --exclude-per-directory=.gitignore -- "$@"
else else
git-ls-files \ git-ls-files -z \
--exclude-per-directory=.gitignore -- "$@" --exclude-per-directory=.gitignore -- "$@"
fi | sort | uniq fi |
) case "$show_only,$remove_files" in
true,*)
case "$show_only" in xargs -0 echo
true) ;;
echo $files *,true)
xargs -0 sh -c "
while [ \$# -gt 0 ]; do
file=\$1; shift
rm -- \"\$file\" && git-update-index --remove $verbose \"\$file\"
done
" inline
;; ;;
*) *)
[[ "$remove_files" = "true" ]] && rm -- $files git-update-index --force-remove $verbose -z --stdin
git-update-index $index_remove_option $verbose $files
;; ;;
esac esac

@ -7,36 +7,54 @@ test_description='Test of the various options to git-rm.'
. ./test-lib.sh . ./test-lib.sh
# Setup some files to be removed # Setup some files to be removed, some with funny characters
touch foo bar touch -- foo bar baz 'space embedded' 'tab embedded' 'newline
git-add foo bar embedded' -q
# Need one to test -- git-add -- foo bar baz 'space embedded' 'tab embedded' 'newline
touch -- -q embedded' -q
git update-index --add -- -q git-commit -m "add files"
git-commit -m "add foo, bar, and -q"
test_expect_success \ test_expect_success \
'Pre-check that foo is in index before git-rm foo' \ 'Pre-check that foo exists and is in index before git-rm foo' \
'git-ls-files --error-unmatch foo' '[ -f foo ] && git-ls-files --error-unmatch foo'
test_expect_success \ test_expect_success \
'Test that git-rm foo succeeds' \ 'Test that git-rm foo succeeds' \
'git-rm foo' 'git-rm foo'
test_expect_failure \ test_expect_success \
'Post-check that foo is not in index after git-rm foo' \ 'Post-check that foo exists but is not in index after git-rm foo' \
'git-ls-files --error-unmatch foo' '[ -f foo ] && ! git-ls-files --error-unmatch foo'
test_expect_success \ test_expect_success \
'Test that "git-rm -f bar" works' \ 'Pre-check that bar exists and is in index before "git-rm -f bar"' \
'[ -f bar ] && git-ls-files --error-unmatch bar'
test_expect_success \
'Test that "git-rm -f bar" succeeds' \
'git-rm -f bar' 'git-rm -f bar'
test_expect_failure \ test_expect_success \
'Post-check that bar no longer exists' \ 'Post-check that bar does not exist and is not in index after "git-rm -f bar"' \
'[ -f bar ]' '! [ -f bar ] && ! git-ls-files --error-unmatch bar'
test_expect_success \ test_expect_success \
'Test that "git-rm -- -q" works to delete a file named -q' \ 'Test that "git-rm -- -q" succeeds (remove a file that looks like an option)' \
'git-rm -- -q' 'git-rm -- -q'
test_expect_success \
"Test that \"git-rm -f\" succeeds with embedded space, tab, or newline characters." \
"git-rm -f 'space embedded' 'tab embedded' 'newline
embedded'"
chmod u-w .
test_expect_failure \
'Test that "git-rm -f" fails if its rm fails' \
'git-rm -f baz'
chmod u+w .
test_expect_success \
'When the rm in "git-rm -f" fails, it should not remove the file from the index' \
'git-ls-files --error-unmatch baz'
test_done test_done