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

Add builtin "git rm" command

This changes semantics very subtly, because it adds a new atomicity guarantee. In particular, if you "git rm" several files, it will now do all or nothing. The old shell-script really looped over the removed files one by one, and would basically randomly fail in the middle if "-f" was used and one of the files didn't exist in the working directory. This C builtin one will not re-write the index after each remove, but instead remove all files at once. However, that means that if "-f" is used (to also force removal of the file from the working directory), and some files have already been removed from the workspace, it won't stop in the middle in some half-way state like the old one did. So what happens is that if the _first_ file fails to be removed with "-f", we abort the whole "git rm". But once we've started removing, we don't leave anything half done. If some of the other files don't exist, we'll just ignore errors of removal from the working tree. This is only an issue with "-f", of course. I think the new behaviour is strictly an improvement, but perhaps more importantly, it is _different_. As a special case, the semantics are identical for the single-file case (which is the only one our test-suite seems to test). The other question is what to do with leading directories. The old "git rm" script didn't do anything, which is somewhat inconsistent. This one will actually clean up directories that have become empty as a result of removing the last file, but maybe we want to have a flag to decide the behaviour? Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 19, 2006 at 16:19 UTC d9b814cc97f16daac06566a5340121c446136d22
5 files changed +157 -73
Makefile
+5 -3
@@ -120,7 +120,7 @@ SCRIPT_SH = \
120 git-merge-one-file.sh git-parse-remote.sh \
121 git-prune.sh git-pull.sh git-rebase.sh \
122 git-repack.sh git-request-pull.sh git-reset.sh \
123 - git-resolve.sh git-revert.sh git-rm.sh git-sh-setup.sh \
123 + git-resolve.sh git-revert.sh git-sh-setup.sh \
124 git-tag.sh git-verify-tag.sh \
125 git-applymbox.sh git-applypatch.sh git-am.sh \
126 git-merge.sh git-merge-stupid.sh git-merge-octopus.sh \
@@ -170,7 +170,8 @@ PROGRAMS = \
170
171 BUILT_INS = git-log$X git-whatchanged$X git-show$X \
172 git-count-objects$X git-diff$X git-push$X \
173 - git-grep$X git-add$X
173 + git-grep$X git-add$X git-rm$X git-rev-list$X \
174 + git-check-ref-format$X
175
176 # what 'all' will build and 'install' will install, in gitexecdir
177 ALL_PROGRAMS = $(PROGRAMS) $(SIMPLE_PROGRAMS) $(SCRIPTS)
@@ -218,7 +219,8 @@ LIB_OBJS = \
219
220 BUILTIN_OBJS = \
221 builtin-log.o builtin-help.o builtin-count.o builtin-diff.o builtin-push.o \
221 - builtin-grep.o builtin-add.o
222 + builtin-grep.o builtin-add.o builtin-rev-list.o builtin-check-ref-format.o \
223 + builtin-rm.o
224
225 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
226 LIBS = $(GITLIBS) -lz
builtin-rm.c new
+150
@@ -0,0 +1,150 @@
1 +/*
2 + * "git rm" builtin command
3 + *
4 + * Copyright (C) Linus Torvalds 2006
5 + */
6 +#include "cache.h"
7 +#include "builtin.h"
8 +#include "dir.h"
9 +
10 +static const char builtin_rm_usage[] =
11 +"git-rm [-n] [-v] [-f] <filepattern>...";
12 +
13 +static struct {
14 + int nr, alloc;
15 + const char **name;
16 +} list;
17 +
18 +static void add_list(const char *name)
19 +{
20 + if (list.nr >= list.alloc) {
21 + list.alloc = alloc_nr(list.alloc);
22 + list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
23 + }
24 + list.name[list.nr++] = name;
25 +}
26 +
27 +static int remove_file(const char *name)
28 +{
29 + int ret;
30 + char *slash;
31 +
32 + ret = unlink(name);
33 + if (!ret && (slash = strrchr(name, '/'))) {
34 + char *n = strdup(name);
35 + do {
36 + n[slash - name] = 0;
37 + name = n;
38 + } while (!rmdir(name) && (slash = strrchr(name, '/')));
39 + }
40 + return ret;
41 +}
42 +
43 +static struct cache_file cache_file;
44 +
45 +int cmd_rm(int argc, const char **argv, char **envp)
46 +{
47 + int i, newfd;
48 + int verbose = 0, show_only = 0, force = 0;
49 + const char *prefix = setup_git_directory();
50 + const char **pathspec;
51 + char *seen;
52 +
53 + git_config(git_default_config);
54 +
55 + newfd = hold_index_file_for_update(&cache_file, get_index_file());
56 + if (newfd < 0)
57 + die("unable to create new index file");
58 +
59 + if (read_cache() < 0)
60 + die("index file corrupt");
61 +
62 + for (i = 1 ; i < argc ; i++) {
63 + const char *arg = argv[i];
64 +
65 + if (*arg != '-')
66 + break;
67 + if (!strcmp(arg, "--")) {
68 + i++;
69 + break;
70 + }
71 + if (!strcmp(arg, "-n")) {
72 + show_only = 1;
73 + continue;
74 + }
75 + if (!strcmp(arg, "-v")) {
76 + verbose = 1;
77 + continue;
78 + }
79 + if (!strcmp(arg, "-f")) {
80 + force = 1;
81 + continue;
82 + }
83 + die(builtin_rm_usage);
84 + }
85 + pathspec = get_pathspec(prefix, argv + i);
86 +
87 + seen = NULL;
88 + if (pathspec) {
89 + for (i = 0; pathspec[i] ; i++)
90 + /* nothing */;
91 + seen = xmalloc(i);
92 + memset(seen, 0, i);
93 + }
94 +
95 + for (i = 0; i < active_nr; i++) {
96 + struct cache_entry *ce = active_cache[i];
97 + if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
98 + continue;
99 + add_list(ce->name);
100 + }
101 +
102 + if (pathspec) {
103 + const char *match;
104 + for (i = 0; (match = pathspec[i]) != NULL ; i++) {
105 + if (*match && !seen[i])
106 + die("pathspec '%s' did not match any files", match);
107 + }
108 + }
109 +
110 + /*
111 + * First remove the names from the index: we won't commit
112 + * the index unless all of them succeed
113 + */
114 + for (i = 0; i < list.nr; i++) {
115 + const char *path = list.name[i];
116 + printf("rm '%s'\n", path);
117 +
118 + if (remove_file_from_cache(path))
119 + die("git rm: unable to remove %s", path);
120 + }
121 +
122 + /*
123 + * Then, if we used "-f", remove the filenames from the
124 + * workspace. If we fail to remove the first one, we
125 + * abort the "git rm" (but once we've successfully removed
126 + * any file at all, we'll go ahead and commit to it all:
127 + * by then we've already committed ourself and can't fail
128 + * in the middle)
129 + */
130 + if (force) {
131 + int removed = 0;
132 + for (i = 0; i < list.nr; i++) {
133 + const char *path = list.name[i];
134 + if (!remove_file(path)) {
135 + removed = 1;
136 + continue;
137 + }
138 + if (!removed)
139 + die("git rm: %s: %s", path, strerror(errno));
140 + }
141 + }
142 +
143 + if (active_cache_changed) {
144 + if (write_cache(newfd, active_cache, active_nr) ||
145 + commit_index_file(&cache_file))
146 + die("Unable to write new index file");
147 + }
148 +
149 + return 0;
150 +}
builtin.h
+1
@@ -24,6 +24,7 @@ extern int cmd_count_objects(int argc, const char **argv, char **envp);
24
25 extern int cmd_push(int argc, const char **argv, char **envp);
26 extern int cmd_grep(int argc, const char **argv, char **envp);
27 +extern int cmd_rm(int argc, const char **argv, char **envp);
28 extern int cmd_add(int argc, const char **argv, char **envp);
29
30 #endif
git-rm.sh deleted
-70
@@ -1,70 +0,0 @@
1 -#!/bin/sh
2 -
3 -USAGE='[-f] [-n] [-v] [--] <file>...'
4 -SUBDIRECTORY_OK='Yes'
5 -. git-sh-setup
6 -
7 -remove_files=
8 -show_only=
9 -verbose=
10 -while : ; do
11 - case "$1" in
12 - -f)
13 - remove_files=true
14 - ;;
15 - -n)
16 - show_only=true
17 - ;;
18 - -v)
19 - verbose=--verbose
20 - ;;
21 - --)
22 - shift; break
23 - ;;
24 - -*)
25 - usage
26 - ;;
27 - *)
28 - break
29 - ;;
30 - esac
31 - shift
32 -done
33 -
34 -# This is typo-proofing. If some paths match and some do not, we want
35 -# to do nothing.
36 -case "$#" in
37 -0) ;;
38 -*)
39 - git-ls-files --error-unmatch -- "$@" >/dev/null || {
40 - echo >&2 "Maybe you misspelled it?"
41 - exit 1
42 - }
43 - ;;
44 -esac
45 -
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 -- "$@"
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 -*)
68 - git-update-index --force-remove $verbose -z --stdin
69 - ;;
70 -esac
git.c
+1
@@ -50,6 +50,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
50 { "count-objects", cmd_count_objects },
51 { "diff", cmd_diff },
52 { "grep", cmd_grep },
53 + { "rm", cmd_rm },
54 { "add", cmd_add },
55 };
56 int i;