2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 static const char builtin_rm_usage[] =
13 "git-rm [-n] [-f] [--cached] <filepattern>...";
20 static void add_list(const char *name)
22 if (list.nr >= list.alloc) {
23 list.alloc = alloc_nr(list.alloc);
24 list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
26 list.name[list.nr++] = name;
29 static int remove_file(const char *name)
35 if (!ret && (slash = strrchr(name, '/'))) {
36 char *n = xstrdup(name);
40 } while (!rmdir(name) && (slash = strrchr(name, '/')));
45 static int check_local_mod(unsigned char *head)
47 /* items in list are already sorted in the cache order,
48 * so we could do this a lot more efficiently by using
49 * tree_desc based traversal if we wanted to, but I am
50 * lazy, and who cares if removal of files is a tad
51 * slower than the theoretical maximum speed?
56 no_head = is_null_sha1(head);
57 for (i = 0; i < list.nr; i++) {
60 struct cache_entry *ce;
61 const char *name = list.name[i];
62 unsigned char sha1[20];
65 pos = cache_name_pos(name, strlen(name));
67 continue; /* removing unmerged entry */
68 ce = active_cache[pos];
70 if (lstat(ce->name, &st) < 0) {
72 fprintf(stderr, "warning: '%s': %s",
73 ce->name, strerror(errno));
74 /* It already vanished from the working tree */
77 else if (S_ISDIR(st.st_mode)) {
78 /* if a file was removed and it is now a
79 * directory, that is the same as ENOENT as
80 * far as git is concerned; we do not track
85 if (ce_match_stat(ce, &st, 0))
86 errs = error("'%s' has local modifications "
87 "(hint: try -f)", ce->name);
91 * It is Ok to remove a newly added path, as long as
94 if (get_tree_entry(head, name, sha1, &mode))
97 * Otherwise make sure the version from the HEAD
100 if (ce->ce_mode != create_ce_mode(mode) ||
101 hashcmp(ce->sha1, sha1))
102 errs = error("'%s' has changes staged in the index "
103 "(hint: try -f)", name);
108 static struct lock_file lock_file;
110 int cmd_rm(int argc, const char **argv, const char *prefix)
113 int show_only = 0, force = 0, index_only = 0, recursive = 0;
114 const char **pathspec;
117 git_config(git_default_config);
119 newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
121 if (read_cache() < 0)
122 die("index file corrupt");
124 for (i = 1 ; i < argc ; i++) {
125 const char *arg = argv[i];
129 else if (!strcmp(arg, "--")) {
133 else if (!strcmp(arg, "-n"))
135 else if (!strcmp(arg, "--cached"))
137 else if (!strcmp(arg, "-f"))
139 else if (!strcmp(arg, "-r"))
142 usage(builtin_rm_usage);
145 usage(builtin_rm_usage);
147 pathspec = get_pathspec(prefix, argv + i);
149 for (i = 0; pathspec[i] ; i++)
151 seen = xcalloc(i, 1);
153 for (i = 0; i < active_nr; i++) {
154 struct cache_entry *ce = active_cache[i];
155 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
162 for (i = 0; (match = pathspec[i]) != NULL ; i++) {
164 die("pathspec '%s' did not match any files",
166 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
167 die("not removing '%s' recursively without -r",
168 *match ? match : ".");
173 * If not forced, the file, the index and the HEAD (if exists)
174 * must match; but the file can already been removed, since
175 * this sequence is a natural "novice" way:
179 * Further, if HEAD commit exists, "diff-index --cached" must
180 * report no changes unless forced.
183 unsigned char sha1[20];
184 if (get_sha1("HEAD", sha1))
186 if (check_local_mod(sha1))
191 * First remove the names from the index: we won't commit
192 * the index unless all of them succeed.
194 for (i = 0; i < list.nr; i++) {
195 const char *path = list.name[i];
196 printf("rm '%s'\n", path);
198 if (remove_file_from_cache(path))
199 die("git-rm: unable to remove %s", path);
200 cache_tree_invalidate_path(active_cache_tree, path);
207 * Then, unless we used "--cache", remove the filenames from
208 * the workspace. If we fail to remove the first one, we
209 * abort the "git rm" (but once we've successfully removed
210 * any file at all, we'll go ahead and commit to it all:
211 * by then we've already committed ourselves and can't fail
216 for (i = 0; i < list.nr; i++) {
217 const char *path = list.name[i];
218 if (!remove_file(path)) {
223 die("git-rm: %s: %s", path, strerror(errno));
227 if (active_cache_changed) {
228 if (write_cache(newfd, active_cache, active_nr) ||
229 close(newfd) || commit_lock_file(&lock_file))
230 die("Unable to write new index file");