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