@matheusbsbs / matheus-bagliano / commits / d4a1cab541
Fork 0

Add new git-rm command with documentation

This adds a git-rm command which provides convenience similar to git-add, (and a bit more since it takes care of the rm as well if given -f). Like git-add, git-rm expands the given path names through git-ls-files. This means it only acts on files listed in the index. And it does act recursively on directories by default, (no -r needed as in the case of rm itself). When it recurses, it does not remove empty directories that are left behind. Signed-off-by: Junio C Hamano <junkio@cox.net>

Carl Worth committed Feb 21, 2006 at 15:04 UTC d4a1cab541be0c276b38285c8b33050ea411eacf
5 files changed +200 -1
.gitignore
+1
@@ -84,6 +84,7 @@ git-resolve
84 git-rev-list
85 git-rev-parse
86 git-revert
87 +git-rm
88 git-send-email
89 git-send-pack
90 git-sh-setup
Documentation/git-rm.txt new
+89
@@ -0,0 +1,89 @@
1 +git-rm(1)
2 +=========
3 +
4 +NAME
5 +----
6 +git-rm - Remove files from the working tree and from the index.
7 +
8 +SYNOPSIS
9 +--------
10 +'git-rm' [-f] [-n] [-v] [--] <file>...
11 +
12 +DESCRIPTION
13 +-----------
14 +A convenience wrapper for git-update-index --remove. For those coming
15 +from cvs, git-rm provides an operation similar to "cvs rm" or "cvs
16 +remove".
17 +
18 +
19 +OPTIONS
20 +-------
21 +<file>...::
22 + Files to remove from the index and optionally, from the
23 + working tree as well.
24 +
25 +-f::
26 + Remove files from the working tree as well as from the index.
27 +
28 +-n::
29 + Don't actually remove the file(s), just show if they exist in
30 + the index.
31 +
32 +-v::
33 + Be verbose.
34 +
35 +--::
36 + This option can be used to separate command-line options from
37 + the list of files, (useful when filenames might be mistaken
38 + for command-line options).
39 +
40 +
41 +DISCUSSION
42 +----------
43 +
44 +The list of <file> given to the command is fed to `git-ls-files`
45 +command to list files that are registered in the index and
46 +are not ignored/excluded by `$GIT_DIR/info/exclude` file or
47 +`.gitignore` file in each directory. This means two things:
48 +
49 +. You can put the name of a directory on the command line, and the
50 + command will remove all files in it and its subdirectories (the
51 + directories themselves are never removed from the working tree);
52 +
53 +. Giving the name of a file that is not in the index does not
54 + remove that file.
55 +
56 +
57 +EXAMPLES
58 +--------
59 +git-rm Documentation/\\*.txt::
60 +
61 + Removes all `\*.txt` files from the index that are under the
62 + `Documentation` directory and any of its subdirectories. The
63 + files are not removed from the working tree.
64 ++
65 +Note that the asterisk `\*` is quoted from the shell in this
66 +example; this lets the command include the files from
67 +subdirectories of `Documentation/` directory.
68 +
69 +git-rm -f git-*.sh::
70 +
71 + Remove all git-*.sh scripts that are in the index. The files
72 + are removed from the index, and (because of the -f option),
73 + from the working tree as well. Because this example lets the
74 + shell expand the asterisk (i.e. you are listing the files
75 + explicitly), it does not remove `subdir/git-foo.sh`.
76 +
77 +
78 +Author
79 +------
80 +Written by Linus Torvalds <torvalds@osdl.org>
81 +
82 +Documentation
83 +--------------
84 +Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
85 +
86 +GIT
87 +---
88 +Part of the gitlink:git[7] suite
89 +
Makefile
+1 -1
@@ -120,7 +120,7 @@ SCRIPT_SH = \
120 git-merge-one-file.sh git-parse-remote.sh \
121 git-prune.sh git-pull.sh git-push.sh git-rebase.sh \
122 git-repack.sh git-request-pull.sh git-reset.sh \
123 - git-resolve.sh git-revert.sh git-sh-setup.sh \
123 + git-resolve.sh git-revert.sh git-rm.sh git-sh-setup.sh \
124 git-tag.sh git-verify-tag.sh git-whatchanged.sh \
125 git-applymbox.sh git-applypatch.sh git-am.sh \
126 git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \
git-rm.sh new
+67
@@ -0,0 +1,67 @@
1 +#!/bin/sh
2 +
3 +USAGE='[-f] [-n] [-v] [--] <file>...'
4 +SUBDIRECTORY_OK='Yes'
5 +. git-sh-setup
6 +
7 +index_remove_option=--force-remove
8 +remove_files=
9 +show_only=
10 +verbose=
11 +while : ; do
12 + case "$1" in
13 + -f)
14 + remove_files=true
15 + index_remote_option=--force
16 + ;;
17 + -n)
18 + show_only=true
19 + ;;
20 + -v)
21 + verbose=--verbose
22 + ;;
23 + --)
24 + shift; break
25 + ;;
26 + -*)
27 + usage
28 + ;;
29 + *)
30 + break
31 + ;;
32 + esac
33 + shift
34 +done
35 +
36 +# This is typo-proofing. If some paths match and some do not, we want
37 +# to do nothing.
38 +case "$#" in
39 +0) ;;
40 +*)
41 + git-ls-files --error-unmatch -- "$@" >/dev/null || {
42 + echo >&2 "Maybe you misspelled it?"
43 + exit 1
44 + }
45 + ;;
46 +esac
47 +
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 \
55 + --exclude-per-directory=.gitignore -- "$@"
56 + fi | sort | uniq
57 +)
58 +
59 +case "$show_only" in
60 +true)
61 + echo $files
62 + ;;
63 +*)
64 + [[ "$remove_files" = "true" ]] && rm -- $files
65 + git-update-index $index_remove_option $verbose $files
66 + ;;
67 +esac
t/t3600-rm.sh new
+42
@@ -0,0 +1,42 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2006 Carl D. Worth
4 +#
5 +
6 +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"
17 +
18 +test_expect_success \
19 + 'Pre-check that foo is in index before git-rm foo' \
20 + 'git-ls-files --error-unmatch foo'
21 +
22 +test_expect_success \
23 + 'Test that git-rm foo succeeds' \
24 + 'git-rm foo'
25 +
26 +test_expect_failure \
27 + 'Post-check that foo is not in index after git-rm foo' \
28 + 'git-ls-files --error-unmatch foo'
29 +
30 +test_expect_success \
31 + 'Test that "git-rm -f bar" works' \
32 + 'git-rm -f bar'
33 +
34 +test_expect_failure \
35 + 'Post-check that bar no longer exists' \
36 + '[ -f bar ]'
37 +
38 +test_expect_success \
39 + 'Test that "git-rm -- -q" works to delete a file named -q' \
40 + 'git-rm -- -q'
41 +
42 +test_done