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