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