t0410: make test description clearer
[git/gitster.git] / builtin / rm.c
blobeaff027258db4d7e7d1ca73f914d4bf756aa691d
1 /*
2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
5 */
6 #define USE_THE_REPOSITORY_VARIABLE
7 #include "builtin.h"
8 #include "advice.h"
9 #include "config.h"
10 #include "lockfile.h"
11 #include "dir.h"
12 #include "gettext.h"
13 #include "hash.h"
14 #include "tree-walk.h"
15 #include "object-name.h"
16 #include "parse-options.h"
17 #include "read-cache.h"
19 #include "string-list.h"
20 #include "setup.h"
21 #include "sparse-index.h"
22 #include "submodule.h"
23 #include "pathspec.h"
25 static const char * const builtin_rm_usage[] = {
26 N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n"
27 " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
28 " [--] [<pathspec>...]"),
29 NULL
32 static struct {
33 int nr, alloc;
34 struct {
35 const char *name;
36 char is_submodule;
37 } *entry;
38 } list;
40 static int get_ours_cache_pos(const char *path, int pos)
42 int i = -pos - 1;
44 while ((i < the_repository->index->cache_nr) && !strcmp(the_repository->index->cache[i]->name, path)) {
45 if (ce_stage(the_repository->index->cache[i]) == 2)
46 return i;
47 i++;
49 return -1;
52 static void print_error_files(struct string_list *files_list,
53 const char *main_msg,
54 const char *hints_msg,
55 int *errs)
57 if (files_list->nr) {
58 int i;
59 struct strbuf err_msg = STRBUF_INIT;
61 strbuf_addstr(&err_msg, main_msg);
62 for (i = 0; i < files_list->nr; i++)
63 strbuf_addf(&err_msg,
64 "\n %s",
65 files_list->items[i].string);
66 if (advice_enabled(ADVICE_RM_HINTS))
67 strbuf_addstr(&err_msg, hints_msg);
68 *errs = error("%s", err_msg.buf);
69 strbuf_release(&err_msg);
73 static void submodules_absorb_gitdir_if_needed(void)
75 int i;
76 for (i = 0; i < list.nr; i++) {
77 const char *name = list.entry[i].name;
78 int pos;
79 const struct cache_entry *ce;
81 pos = index_name_pos(the_repository->index, name, strlen(name));
82 if (pos < 0) {
83 pos = get_ours_cache_pos(name, pos);
84 if (pos < 0)
85 continue;
87 ce = the_repository->index->cache[pos];
89 if (!S_ISGITLINK(ce->ce_mode) ||
90 !file_exists(ce->name) ||
91 is_empty_dir(name))
92 continue;
94 if (!submodule_uses_gitfile(name))
95 absorb_git_dir_into_superproject(name, NULL);
99 static int check_local_mod(struct object_id *head, int index_only)
102 * Items in list are already sorted in the cache order,
103 * so we could do this a lot more efficiently by using
104 * tree_desc based traversal if we wanted to, but I am
105 * lazy, and who cares if removal of files is a tad
106 * slower than the theoretical maximum speed?
108 int i, no_head;
109 int errs = 0;
110 struct string_list files_staged = STRING_LIST_INIT_NODUP;
111 struct string_list files_cached = STRING_LIST_INIT_NODUP;
112 struct string_list files_local = STRING_LIST_INIT_NODUP;
114 no_head = is_null_oid(head);
115 for (i = 0; i < list.nr; i++) {
116 struct stat st;
117 int pos;
118 const struct cache_entry *ce;
119 const char *name = list.entry[i].name;
120 struct object_id oid;
121 unsigned short mode;
122 int local_changes = 0;
123 int staged_changes = 0;
125 pos = index_name_pos(the_repository->index, name, strlen(name));
126 if (pos < 0) {
128 * Skip unmerged entries except for populated submodules
129 * that could lose history when removed.
131 pos = get_ours_cache_pos(name, pos);
132 if (pos < 0)
133 continue;
135 if (!S_ISGITLINK(the_repository->index->cache[pos]->ce_mode) ||
136 is_empty_dir(name))
137 continue;
139 ce = the_repository->index->cache[pos];
141 if (lstat(ce->name, &st) < 0) {
142 if (!is_missing_file_error(errno))
143 warning_errno(_("failed to stat '%s'"), ce->name);
144 /* It already vanished from the working tree */
145 continue;
147 else if (S_ISDIR(st.st_mode)) {
148 /* if a file was removed and it is now a
149 * directory, that is the same as ENOENT as
150 * far as git is concerned; we do not track
151 * directories unless they are submodules.
153 if (!S_ISGITLINK(ce->ce_mode))
154 continue;
158 * "rm" of a path that has changes need to be treated
159 * carefully not to allow losing local changes
160 * accidentally. A local change could be (1) file in
161 * work tree is different since the index; and/or (2)
162 * the user staged a content that is different from
163 * the current commit in the index.
165 * In such a case, you would need to --force the
166 * removal. However, "rm --cached" (remove only from
167 * the index) is safe if the index matches the file in
168 * the work tree or the HEAD commit, as it means that
169 * the content being removed is available elsewhere.
173 * Is the index different from the file in the work tree?
174 * If it's a submodule, is its work tree modified?
176 if (ie_match_stat(the_repository->index, ce, &st, 0) ||
177 (S_ISGITLINK(ce->ce_mode) &&
178 bad_to_remove_submodule(ce->name,
179 SUBMODULE_REMOVAL_DIE_ON_ERROR |
180 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)))
181 local_changes = 1;
184 * Is the index different from the HEAD commit? By
185 * definition, before the very initial commit,
186 * anything staged in the index is treated by the same
187 * way as changed from the HEAD.
189 if (no_head
190 || get_tree_entry(the_repository, head, name, &oid, &mode)
191 || ce->ce_mode != create_ce_mode(mode)
192 || !oideq(&ce->oid, &oid))
193 staged_changes = 1;
196 * If the index does not match the file in the work
197 * tree and if it does not match the HEAD commit
198 * either, (1) "git rm" without --cached definitely
199 * will lose information; (2) "git rm --cached" will
200 * lose information unless it is about removing an
201 * "intent to add" entry.
203 if (local_changes && staged_changes) {
204 if (!index_only || !ce_intent_to_add(ce))
205 string_list_append(&files_staged, name);
207 else if (!index_only) {
208 if (staged_changes)
209 string_list_append(&files_cached, name);
210 if (local_changes)
211 string_list_append(&files_local, name);
214 print_error_files(&files_staged,
215 Q_("the following file has staged content different "
216 "from both the\nfile and the HEAD:",
217 "the following files have staged content different"
218 " from both the\nfile and the HEAD:",
219 files_staged.nr),
220 _("\n(use -f to force removal)"),
221 &errs);
222 string_list_clear(&files_staged, 0);
223 print_error_files(&files_cached,
224 Q_("the following file has changes "
225 "staged in the index:",
226 "the following files have changes "
227 "staged in the index:", files_cached.nr),
228 _("\n(use --cached to keep the file,"
229 " or -f to force removal)"),
230 &errs);
231 string_list_clear(&files_cached, 0);
233 print_error_files(&files_local,
234 Q_("the following file has local modifications:",
235 "the following files have local modifications:",
236 files_local.nr),
237 _("\n(use --cached to keep the file,"
238 " or -f to force removal)"),
239 &errs);
240 string_list_clear(&files_local, 0);
242 return errs;
245 static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
246 static int ignore_unmatch = 0, pathspec_file_nul;
247 static int include_sparse;
248 static char *pathspec_from_file;
250 static struct option builtin_rm_options[] = {
251 OPT__DRY_RUN(&show_only, N_("dry run")),
252 OPT__QUIET(&quiet, N_("do not list removed files")),
253 OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")),
254 OPT__FORCE(&force, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE),
255 OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
256 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
257 N_("exit with a zero status even if nothing matched")),
258 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
259 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
260 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
261 OPT_END(),
264 int cmd_rm(int argc,
265 const char **argv,
266 const char *prefix,
267 struct repository *repo UNUSED)
269 struct lock_file lock_file = LOCK_INIT;
270 int i, ret = 0;
271 struct pathspec pathspec;
272 char *seen;
274 git_config(git_default_config, NULL);
276 argc = parse_options(argc, argv, prefix, builtin_rm_options,
277 builtin_rm_usage, 0);
279 parse_pathspec(&pathspec, 0,
280 PATHSPEC_PREFER_CWD,
281 prefix, argv);
283 if (pathspec_from_file) {
284 if (pathspec.nr)
285 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
287 parse_pathspec_file(&pathspec, 0,
288 PATHSPEC_PREFER_CWD,
289 prefix, pathspec_from_file, pathspec_file_nul);
290 } else if (pathspec_file_nul) {
291 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
294 if (!pathspec.nr)
295 die(_("No pathspec was given. Which files should I remove?"));
297 if (!index_only)
298 setup_work_tree();
300 prepare_repo_settings(the_repository);
301 the_repository->settings.command_requires_full_index = 0;
302 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
304 if (repo_read_index(the_repository) < 0)
305 die(_("index file corrupt"));
307 refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
309 seen = xcalloc(pathspec.nr, 1);
311 if (pathspec_needs_expanded_index(the_repository->index, &pathspec))
312 ensure_full_index(the_repository->index);
314 for (i = 0; i < the_repository->index->cache_nr; i++) {
315 const struct cache_entry *ce = the_repository->index->cache[i];
317 if (!include_sparse &&
318 (ce_skip_worktree(ce) ||
319 !path_in_sparse_checkout(ce->name, the_repository->index)))
320 continue;
321 if (!ce_path_match(the_repository->index, ce, &pathspec, seen))
322 continue;
323 ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
324 list.entry[list.nr].name = xstrdup(ce->name);
325 list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
326 if (list.entry[list.nr++].is_submodule &&
327 !is_staging_gitmodules_ok(the_repository->index))
328 die(_("please stage your changes to .gitmodules or stash them to proceed"));
331 if (pathspec.nr) {
332 const char *original;
333 int seen_any = 0;
334 char *skip_worktree_seen = NULL;
335 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
337 for (i = 0; i < pathspec.nr; i++) {
338 original = pathspec.items[i].original;
339 if (seen[i])
340 seen_any = 1;
341 else if (ignore_unmatch)
342 continue;
343 else if (!include_sparse &&
344 matches_skip_worktree(&pathspec, i, &skip_worktree_seen))
345 string_list_append(&only_match_skip_worktree, original);
346 else
347 die(_("pathspec '%s' did not match any files"), original);
349 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
350 die(_("not removing '%s' recursively without -r"),
351 *original ? original : ".");
354 if (only_match_skip_worktree.nr) {
355 advise_on_updating_sparse_paths(&only_match_skip_worktree);
356 ret = 1;
358 free(skip_worktree_seen);
359 string_list_clear(&only_match_skip_worktree, 0);
361 if (!seen_any)
362 exit(ret);
364 clear_pathspec(&pathspec);
365 free(seen);
367 if (!index_only)
368 submodules_absorb_gitdir_if_needed();
371 * If not forced, the file, the index and the HEAD (if exists)
372 * must match; but the file can already been removed, since
373 * this sequence is a natural "novice" way:
375 * rm F; git rm F
377 * Further, if HEAD commit exists, "diff-index --cached" must
378 * report no changes unless forced.
380 if (!force) {
381 struct object_id oid;
382 if (repo_get_oid(the_repository, "HEAD", &oid))
383 oidclr(&oid, the_repository->hash_algo);
384 if (check_local_mod(&oid, index_only))
385 exit(1);
389 * First remove the names from the index: we won't commit
390 * the index unless all of them succeed.
392 for (i = 0; i < list.nr; i++) {
393 const char *path = list.entry[i].name;
394 if (!quiet)
395 printf("rm '%s'\n", path);
397 if (remove_file_from_index(the_repository->index, path))
398 die(_("git rm: unable to remove %s"), path);
401 if (show_only)
402 return 0;
405 * Then, unless we used "--cached", remove the filenames from
406 * the workspace. If we fail to remove the first one, we
407 * abort the "git rm" (but once we've successfully removed
408 * any file at all, we'll go ahead and commit to it all:
409 * by then we've already committed ourselves and can't fail
410 * in the middle)
412 if (!index_only) {
413 int removed = 0, gitmodules_modified = 0;
414 struct strbuf buf = STRBUF_INIT;
415 int flag = force ? REMOVE_DIR_PURGE_ORIGINAL_CWD : 0;
416 for (i = 0; i < list.nr; i++) {
417 const char *path = list.entry[i].name;
418 if (list.entry[i].is_submodule) {
419 strbuf_reset(&buf);
420 strbuf_addstr(&buf, path);
421 if (remove_dir_recursively(&buf, flag))
422 die(_("could not remove '%s'"), path);
424 removed = 1;
425 if (!remove_path_from_gitmodules(path))
426 gitmodules_modified = 1;
427 continue;
429 if (!remove_path(path)) {
430 removed = 1;
431 continue;
433 if (!removed)
434 die_errno("git rm: '%s'", path);
436 strbuf_release(&buf);
437 if (gitmodules_modified)
438 stage_updated_gitmodules(the_repository->index);
441 if (write_locked_index(the_repository->index, &lock_file,
442 COMMIT_LOCK | SKIP_IF_UNCHANGED))
443 die(_("Unable to write new index file"));
445 return ret;