blob: 0cc491271846214d73d55a4f528c8854e0460c8f [file] [log] [blame]
Linus Torvaldsd9b814c2006-05-19 16:19:34 -07001/*
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"
Junio C Hamano3fb3cc62006-05-23 01:31:38 -07009#include "cache-tree.h"
Junio C Hamano9f950692006-12-25 03:11:00 -080010#include "tree-walk.h"
Pierre Habouzitf09985c2007-10-05 21:09:19 +020011#include "parse-options.h"
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070012
Pierre Habouzitf09985c2007-10-05 21:09:19 +020013static const char * const builtin_rm_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020014 "git rm [options] [--] <file>...",
Pierre Habouzitf09985c2007-10-05 21:09:19 +020015 NULL
16};
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070017
18static struct {
19 int nr, alloc;
20 const char **name;
21} list;
22
23static void add_list(const char *name)
24{
25 if (list.nr >= list.alloc) {
26 list.alloc = alloc_nr(list.alloc);
27 list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
28 }
29 list.name[list.nr++] = name;
30}
31
Matthieu Moybdecd9d2007-07-13 19:41:38 +020032static int check_local_mod(unsigned char *head, int index_only)
Junio C Hamano9f950692006-12-25 03:11:00 -080033{
Junio C Hamano69530cb2008-11-28 18:41:21 -080034 /*
35 * Items in list are already sorted in the cache order,
Junio C Hamano9f950692006-12-25 03:11:00 -080036 * so we could do this a lot more efficiently by using
37 * tree_desc based traversal if we wanted to, but I am
38 * lazy, and who cares if removal of files is a tad
39 * slower than the theoretical maximum speed?
40 */
41 int i, no_head;
42 int errs = 0;
43
44 no_head = is_null_sha1(head);
45 for (i = 0; i < list.nr; i++) {
46 struct stat st;
47 int pos;
48 struct cache_entry *ce;
49 const char *name = list.name[i];
50 unsigned char sha1[20];
51 unsigned mode;
Matthieu Moybdecd9d2007-07-13 19:41:38 +020052 int local_changes = 0;
53 int staged_changes = 0;
Junio C Hamano9f950692006-12-25 03:11:00 -080054
55 pos = cache_name_pos(name, strlen(name));
56 if (pos < 0)
57 continue; /* removing unmerged entry */
58 ce = active_cache[pos];
59
60 if (lstat(ce->name, &st) < 0) {
61 if (errno != ENOENT)
Miklos Vajnac36d7852009-03-24 02:09:14 +010062 warning("'%s': %s", ce->name, strerror(errno));
Junio C Hamano9f950692006-12-25 03:11:00 -080063 /* It already vanished from the working tree */
64 continue;
65 }
66 else if (S_ISDIR(st.st_mode)) {
67 /* if a file was removed and it is now a
68 * directory, that is the same as ENOENT as
69 * far as git is concerned; we do not track
70 * directories.
71 */
72 continue;
73 }
Junio C Hamano69530cb2008-11-28 18:41:21 -080074
75 /*
76 * "rm" of a path that has changes need to be treated
77 * carefully not to allow losing local changes
78 * accidentally. A local change could be (1) file in
79 * work tree is different since the index; and/or (2)
80 * the user staged a content that is different from
81 * the current commit in the index.
82 *
83 * In such a case, you would need to --force the
84 * removal. However, "rm --cached" (remove only from
85 * the index) is safe if the index matches the file in
86 * the work tree or the HEAD commit, as it means that
87 * the content being removed is available elsewhere.
88 */
89
90 /*
91 * Is the index different from the file in the work tree?
92 */
Junio C Hamano9f950692006-12-25 03:11:00 -080093 if (ce_match_stat(ce, &st, 0))
Matthieu Moybdecd9d2007-07-13 19:41:38 +020094 local_changes = 1;
Junio C Hamano69530cb2008-11-28 18:41:21 -080095
96 /*
97 * Is the index different from the HEAD commit? By
98 * definition, before the very initial commit,
99 * anything staged in the index is treated by the same
100 * way as changed from the HEAD.
101 */
Jeff Kinge4d95162007-03-26 14:55:39 -0400102 if (no_head
103 || get_tree_entry(head, name, sha1, &mode)
104 || ce->ce_mode != create_ce_mode(mode)
105 || hashcmp(ce->sha1, sha1))
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200106 staged_changes = 1;
107
Junio C Hamano69530cb2008-11-28 18:41:21 -0800108 /*
109 * If the index does not match the file in the work
110 * tree and if it does not match the HEAD commit
111 * either, (1) "git rm" without --cached definitely
112 * will lose information; (2) "git rm --cached" will
113 * lose information unless it is about removing an
114 * "intent to add" entry.
115 */
116 if (local_changes && staged_changes) {
Junio C Hamano388b2ac2008-11-28 19:55:25 -0800117 if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD))
Junio C Hamano69530cb2008-11-28 18:41:21 -0800118 errs = error("'%s' has staged content different "
119 "from both the file and the HEAD\n"
120 "(use -f to force removal)", name);
121 }
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200122 else if (!index_only) {
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200123 if (staged_changes)
124 errs = error("'%s' has changes staged in the index\n"
125 "(use --cached to keep the file, "
126 "or -f to force removal)", name);
127 if (local_changes)
128 errs = error("'%s' has local modifications\n"
129 "(use --cached to keep the file, "
130 "or -f to force removal)", name);
131 }
Junio C Hamano9f950692006-12-25 03:11:00 -0800132 }
133 return errs;
134}
135
Junio C Hamano021b6e42006-06-06 12:51:49 -0700136static struct lock_file lock_file;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700137
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200138static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
139static int ignore_unmatch = 0;
140
141static struct option builtin_rm_options[] = {
142 OPT__DRY_RUN(&show_only),
143 OPT__QUIET(&quiet),
144 OPT_BOOLEAN( 0 , "cached", &index_only, "only remove from the index"),
Pieter de Bie01144f22008-08-09 00:37:02 +0200145 OPT_BOOLEAN('f', "force", &force, "override the up-to-date check"),
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200146 OPT_BOOLEAN('r', NULL, &recursive, "allow recursive removal"),
147 OPT_BOOLEAN( 0 , "ignore-unmatch", &ignore_unmatch,
148 "exit with a zero status even if nothing matched"),
149 OPT_END(),
150};
151
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700152int cmd_rm(int argc, const char **argv, const char *prefix)
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700153{
154 int i, newfd;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700155 const char **pathspec;
156 char *seen;
157
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100158 git_config(git_default_config, NULL);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700159
Stephen Boyd37782922009-05-23 11:53:12 -0700160 argc = parse_options(argc, argv, prefix, builtin_rm_options,
161 builtin_rm_usage, 0);
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200162 if (!argc)
163 usage_with_options(builtin_rm_usage, builtin_rm_options);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700164
Mike Hommey271bb082007-11-03 12:23:13 +0100165 if (!index_only)
166 setup_work_tree();
167
Olivier Marin4d264672008-07-19 18:24:46 +0200168 newfd = hold_locked_index(&lock_file, 1);
169
170 if (read_cache() < 0)
171 die("index file corrupt");
Johannes Schindelincced48a2008-10-07 18:08:21 +0200172 refresh_cache(REFRESH_QUIET);
Olivier Marin4d264672008-07-19 18:24:46 +0200173
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200174 pathspec = get_pathspec(prefix, argv);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700175 seen = NULL;
Junio C Hamano7612a1e2006-06-08 21:11:25 -0700176 for (i = 0; pathspec[i] ; i++)
177 /* nothing */;
Peter Eriksen28f75812006-07-25 09:30:18 +0200178 seen = xcalloc(i, 1);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700179
180 for (i = 0; i < active_nr; i++) {
181 struct cache_entry *ce = active_cache[i];
182 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
183 continue;
184 add_list(ce->name);
185 }
186
187 if (pathspec) {
188 const char *match;
Steven Grimmbb1faf02007-04-16 00:53:24 -0700189 int seen_any = 0;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700190 for (i = 0; (match = pathspec[i]) != NULL ; i++) {
Steven Grimmbb1faf02007-04-16 00:53:24 -0700191 if (!seen[i]) {
192 if (!ignore_unmatch) {
193 die("pathspec '%s' did not match any files",
194 match);
195 }
196 }
197 else {
198 seen_any = 1;
199 }
Junio C Hamano9f950692006-12-25 03:11:00 -0800200 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
201 die("not removing '%s' recursively without -r",
202 *match ? match : ".");
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700203 }
Steven Grimmbb1faf02007-04-16 00:53:24 -0700204
205 if (! seen_any)
206 exit(0);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700207 }
208
209 /*
Junio C Hamano9f950692006-12-25 03:11:00 -0800210 * If not forced, the file, the index and the HEAD (if exists)
211 * must match; but the file can already been removed, since
212 * this sequence is a natural "novice" way:
213 *
Steven Grimm9474eda2007-04-16 01:17:32 -0700214 * rm F; git rm F
Junio C Hamano9f950692006-12-25 03:11:00 -0800215 *
216 * Further, if HEAD commit exists, "diff-index --cached" must
217 * report no changes unless forced.
218 */
219 if (!force) {
220 unsigned char sha1[20];
221 if (get_sha1("HEAD", sha1))
222 hashclr(sha1);
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200223 if (check_local_mod(sha1, index_only))
Junio C Hamano9f950692006-12-25 03:11:00 -0800224 exit(1);
225 }
226
227 /*
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700228 * First remove the names from the index: we won't commit
Junio C Hamano9f950692006-12-25 03:11:00 -0800229 * the index unless all of them succeed.
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700230 */
231 for (i = 0; i < list.nr; i++) {
232 const char *path = list.name[i];
Steven Grimmb48caa22007-04-16 00:46:48 -0700233 if (!quiet)
234 printf("rm '%s'\n", path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700235
236 if (remove_file_from_cache(path))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700237 die("git rm: unable to remove %s", path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700238 }
239
Junio C Hamano7612a1e2006-06-08 21:11:25 -0700240 if (show_only)
241 return 0;
242
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700243 /*
Junio C Hamano646ac222007-01-11 14:58:47 -0800244 * Then, unless we used "--cached", remove the filenames from
Junio C Hamano9f950692006-12-25 03:11:00 -0800245 * the workspace. If we fail to remove the first one, we
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700246 * abort the "git rm" (but once we've successfully removed
247 * any file at all, we'll go ahead and commit to it all:
Pavel Roskin82e5a822006-07-10 01:50:18 -0400248 * by then we've already committed ourselves and can't fail
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700249 * in the middle)
250 */
Junio C Hamano9f950692006-12-25 03:11:00 -0800251 if (!index_only) {
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700252 int removed = 0;
253 for (i = 0; i < list.nr; i++) {
254 const char *path = list.name[i];
Alex Riesen175a4942008-09-27 00:59:14 +0200255 if (!remove_path(path)) {
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700256 removed = 1;
257 continue;
258 }
259 if (!removed)
Junio C Hamano7e44c932008-08-31 09:39:19 -0700260 die("git rm: %s: %s", path, strerror(errno));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700261 }
262 }
263
264 if (active_cache_changed) {
265 if (write_cache(newfd, active_cache, active_nr) ||
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600266 commit_locked_index(&lock_file))
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700267 die("Unable to write new index file");
268 }
269
270 return 0;
271}