]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/rm.c
config: remove git_config_iter
[thirdparty/git.git] / builtin / rm.c
CommitLineData
d9b814cc
LT
1/*
2 * "git rm" builtin command
3 *
4 * Copyright (C) Linus Torvalds 2006
5 */
d9b814cc 6#include "builtin.h"
697cc8ef 7#include "lockfile.h"
d9b814cc 8#include "dir.h"
3fb3cc69 9#include "cache-tree.h"
9f95069b 10#include "tree-walk.h"
f09985c2 11#include "parse-options.h"
914dc028 12#include "string-list.h"
293ab15e 13#include "submodule.h"
29211a93 14#include "pathspec.h"
d9b814cc 15
f09985c2 16static const char * const builtin_rm_usage[] = {
9c9b4f2f 17 N_("git rm [<options>] [--] <file>..."),
f09985c2
PH
18 NULL
19};
d9b814cc
LT
20
21static struct {
22 int nr, alloc;
293ab15e
JL
23 struct {
24 const char *name;
25 char is_submodule;
26 } *entry;
d9b814cc
LT
27} list;
28
293ab15e
JL
29static int get_ours_cache_pos(const char *path, int pos)
30{
31 int i = -pos - 1;
32
33 while ((i < active_nr) && !strcmp(active_cache[i]->name, path)) {
34 if (ce_stage(active_cache[i]) == 2)
35 return i;
36 i++;
37 }
38 return -1;
39}
40
914dc028
MLM
41static void print_error_files(struct string_list *files_list,
42 const char *main_msg,
43 const char *hints_msg,
44 int *errs)
45{
46 if (files_list->nr) {
47 int i;
48 struct strbuf err_msg = STRBUF_INIT;
49
50 strbuf_addstr(&err_msg, main_msg);
51 for (i = 0; i < files_list->nr; i++)
52 strbuf_addf(&err_msg,
53 "\n %s",
54 files_list->items[i].string);
7e309446
MLM
55 if (advice_rm_hints)
56 strbuf_addstr(&err_msg, hints_msg);
914dc028
MLM
57 *errs = error("%s", err_msg.buf);
58 strbuf_release(&err_msg);
59 }
60}
61
55856a35 62static void submodules_absorb_gitdir_if_needed(const char *prefix)
293ab15e
JL
63{
64 int i;
293ab15e
JL
65 for (i = 0; i < list.nr; i++) {
66 const char *name = list.entry[i].name;
67 int pos;
9c5e6c80 68 const struct cache_entry *ce;
293ab15e
JL
69
70 pos = cache_name_pos(name, strlen(name));
71 if (pos < 0) {
72 pos = get_ours_cache_pos(name, pos);
73 if (pos < 0)
74 continue;
75 }
76 ce = active_cache[pos];
77
78 if (!S_ISGITLINK(ce->ce_mode) ||
dbe44faa 79 !file_exists(ce->name) ||
293ab15e
JL
80 is_empty_dir(name))
81 continue;
82
83 if (!submodule_uses_gitfile(name))
55856a35
SB
84 absorb_git_dir_into_superproject(prefix, name,
85 ABSORB_GITDIR_RECURSE_SUBMODULES);
293ab15e 86 }
293ab15e
JL
87}
88
8ec46d7e 89static int check_local_mod(struct object_id *head, int index_only)
9f95069b 90{
69530cb0
JH
91 /*
92 * Items in list are already sorted in the cache order,
9f95069b
JH
93 * so we could do this a lot more efficiently by using
94 * tree_desc based traversal if we wanted to, but I am
95 * lazy, and who cares if removal of files is a tad
96 * slower than the theoretical maximum speed?
97 */
98 int i, no_head;
99 int errs = 0;
914dc028
MLM
100 struct string_list files_staged = STRING_LIST_INIT_NODUP;
101 struct string_list files_cached = STRING_LIST_INIT_NODUP;
914dc028 102 struct string_list files_local = STRING_LIST_INIT_NODUP;
9f95069b 103
8ec46d7e 104 no_head = is_null_oid(head);
9f95069b
JH
105 for (i = 0; i < list.nr; i++) {
106 struct stat st;
107 int pos;
9c5e6c80 108 const struct cache_entry *ce;
293ab15e 109 const char *name = list.entry[i].name;
8ec46d7e 110 struct object_id oid;
9f95069b 111 unsigned mode;
bdecd9d4
MM
112 int local_changes = 0;
113 int staged_changes = 0;
9f95069b
JH
114
115 pos = cache_name_pos(name, strlen(name));
293ab15e
JL
116 if (pos < 0) {
117 /*
118 * Skip unmerged entries except for populated submodules
119 * that could lose history when removed.
120 */
121 pos = get_ours_cache_pos(name, pos);
122 if (pos < 0)
123 continue;
124
125 if (!S_ISGITLINK(active_cache[pos]->ce_mode) ||
126 is_empty_dir(name))
127 continue;
128 }
9f95069b
JH
129 ce = active_cache[pos];
130
131 if (lstat(ce->name, &st) < 0) {
9a6728d4 132 if (errno != ENOENT && errno != ENOTDIR)
7dcf3d97 133 warning_errno(_("failed to stat '%s'"), ce->name);
9f95069b
JH
134 /* It already vanished from the working tree */
135 continue;
136 }
137 else if (S_ISDIR(st.st_mode)) {
138 /* if a file was removed and it is now a
139 * directory, that is the same as ENOENT as
140 * far as git is concerned; we do not track
293ab15e 141 * directories unless they are submodules.
9f95069b 142 */
293ab15e
JL
143 if (!S_ISGITLINK(ce->ce_mode))
144 continue;
9f95069b 145 }
69530cb0
JH
146
147 /*
148 * "rm" of a path that has changes need to be treated
149 * carefully not to allow losing local changes
150 * accidentally. A local change could be (1) file in
151 * work tree is different since the index; and/or (2)
152 * the user staged a content that is different from
153 * the current commit in the index.
154 *
155 * In such a case, you would need to --force the
156 * removal. However, "rm --cached" (remove only from
157 * the index) is safe if the index matches the file in
158 * the work tree or the HEAD commit, as it means that
159 * the content being removed is available elsewhere.
160 */
161
162 /*
163 * Is the index different from the file in the work tree?
293ab15e 164 * If it's a submodule, is its work tree modified?
69530cb0 165 */
293ab15e
JL
166 if (ce_match_stat(ce, &st, 0) ||
167 (S_ISGITLINK(ce->ce_mode) &&
83b76966
SB
168 bad_to_remove_submodule(ce->name,
169 SUBMODULE_REMOVAL_DIE_ON_ERROR |
170 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)))
bdecd9d4 171 local_changes = 1;
69530cb0
JH
172
173 /*
174 * Is the index different from the HEAD commit? By
175 * definition, before the very initial commit,
176 * anything staged in the index is treated by the same
177 * way as changed from the HEAD.
178 */
e4d9516b 179 if (no_head
8ec46d7e 180 || get_tree_entry(head->hash, name, oid.hash, &mode)
e4d9516b 181 || ce->ce_mode != create_ce_mode(mode)
8ec46d7e 182 || oidcmp(&ce->oid, &oid))
bdecd9d4
MM
183 staged_changes = 1;
184
69530cb0
JH
185 /*
186 * If the index does not match the file in the work
187 * tree and if it does not match the HEAD commit
188 * either, (1) "git rm" without --cached definitely
189 * will lose information; (2) "git rm --cached" will
190 * lose information unless it is about removing an
191 * "intent to add" entry.
192 */
193 if (local_changes && staged_changes) {
895ff3b2 194 if (!index_only || !ce_intent_to_add(ce))
914dc028 195 string_list_append(&files_staged, name);
69530cb0 196 }
bdecd9d4 197 else if (!index_only) {
bdecd9d4 198 if (staged_changes)
914dc028 199 string_list_append(&files_cached, name);
55856a35
SB
200 if (local_changes)
201 string_list_append(&files_local, name);
bdecd9d4 202 }
9f95069b 203 }
914dc028
MLM
204 print_error_files(&files_staged,
205 Q_("the following file has staged content different "
206 "from both the\nfile and the HEAD:",
207 "the following files have staged content different"
208 " from both the\nfile and the HEAD:",
209 files_staged.nr),
210 _("\n(use -f to force removal)"),
211 &errs);
212 string_list_clear(&files_staged, 0);
213 print_error_files(&files_cached,
214 Q_("the following file has changes "
215 "staged in the index:",
216 "the following files have changes "
217 "staged in the index:", files_cached.nr),
218 _("\n(use --cached to keep the file,"
219 " or -f to force removal)"),
220 &errs);
221 string_list_clear(&files_cached, 0);
658ff473 222
914dc028
MLM
223 print_error_files(&files_local,
224 Q_("the following file has local modifications:",
225 "the following files have local modifications:",
226 files_local.nr),
227 _("\n(use --cached to keep the file,"
228 " or -f to force removal)"),
229 &errs);
230 string_list_clear(&files_local, 0);
231
9f95069b
JH
232 return errs;
233}
234
021b6e45 235static struct lock_file lock_file;
d9b814cc 236
f09985c2
PH
237static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
238static int ignore_unmatch = 0;
239
240static struct option builtin_rm_options[] = {
72bba2a6
NTND
241 OPT__DRY_RUN(&show_only, N_("dry run")),
242 OPT__QUIET(&quiet, N_("do not list removed files")),
d5d09d47 243 OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")),
72bba2a6 244 OPT__FORCE(&force, N_("override the up-to-date check")),
d5d09d47
SB
245 OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
246 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
72bba2a6 247 N_("exit with a zero status even if nothing matched")),
f09985c2
PH
248 OPT_END(),
249};
250
a633fca0 251int cmd_rm(int argc, const char **argv, const char *prefix)
d9b814cc 252{
03b86647 253 int i;
29211a93 254 struct pathspec pathspec;
d9b814cc
LT
255 char *seen;
256
95c16418 257 gitmodules_config();
ef90d6d4 258 git_config(git_default_config, NULL);
d9b814cc 259
37782920
SB
260 argc = parse_options(argc, argv, prefix, builtin_rm_options,
261 builtin_rm_usage, 0);
f09985c2
PH
262 if (!argc)
263 usage_with_options(builtin_rm_usage, builtin_rm_options);
d9b814cc 264
271bb087
MH
265 if (!index_only)
266 setup_work_tree();
267
b3e83cc7 268 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
4d264672
OM
269
270 if (read_cache() < 0)
b9b537f7 271 die(_("index file corrupt"));
4d264672 272
f8bc2ac3
JK
273 parse_pathspec(&pathspec, 0,
274 PATHSPEC_PREFER_CWD |
275 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
276 prefix, argv);
9b2d6149 277 refresh_index(&the_index, REFRESH_QUIET, &pathspec, NULL, NULL);
4e1a7baa 278
29211a93 279 seen = xcalloc(pathspec.nr, 1);
d9b814cc
LT
280
281 for (i = 0; i < active_nr; i++) {
9c5e6c80 282 const struct cache_entry *ce = active_cache[i];
429bb40a 283 if (!ce_path_match(ce, &pathspec, seen))
d9b814cc 284 continue;
293ab15e 285 ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
5699d17e 286 list.entry[list.nr].name = xstrdup(ce->name);
95c16418
JL
287 list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
288 if (list.entry[list.nr++].is_submodule &&
289 !is_staging_gitmodules_ok())
8ae51c41 290 die (_("Please stage your changes to .gitmodules or stash them to proceed"));
d9b814cc
LT
291 }
292
29211a93
NTND
293 if (pathspec.nr) {
294 const char *original;
bb1faf0d 295 int seen_any = 0;
29211a93
NTND
296 for (i = 0; i < pathspec.nr; i++) {
297 original = pathspec.items[i].original;
bb1faf0d
SG
298 if (!seen[i]) {
299 if (!ignore_unmatch) {
b9b537f7 300 die(_("pathspec '%s' did not match any files"),
29211a93 301 original);
bb1faf0d 302 }
bb1faf0d 303 }
bb1faf0d
SG
304 else {
305 seen_any = 1;
306 }
9f95069b 307 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
b9b537f7 308 die(_("not removing '%s' recursively without -r"),
29211a93 309 *original ? original : ".");
d9b814cc 310 }
bb1faf0d 311
b02f5aed 312 if (!seen_any)
bb1faf0d 313 exit(0);
d9b814cc
LT
314 }
315
55856a35
SB
316 if (!index_only)
317 submodules_absorb_gitdir_if_needed(prefix);
318
9f95069b
JH
319 /*
320 * If not forced, the file, the index and the HEAD (if exists)
321 * must match; but the file can already been removed, since
322 * this sequence is a natural "novice" way:
323 *
9474eda6 324 * rm F; git rm F
9f95069b
JH
325 *
326 * Further, if HEAD commit exists, "diff-index --cached" must
327 * report no changes unless forced.
328 */
329 if (!force) {
8ec46d7e 330 struct object_id oid;
331 if (get_oid("HEAD", &oid))
332 oidclr(&oid);
333 if (check_local_mod(&oid, index_only))
9f95069b
JH
334 exit(1);
335 }
336
d9b814cc
LT
337 /*
338 * First remove the names from the index: we won't commit
9f95069b 339 * the index unless all of them succeed.
d9b814cc
LT
340 */
341 for (i = 0; i < list.nr; i++) {
293ab15e 342 const char *path = list.entry[i].name;
b48caa20
SG
343 if (!quiet)
344 printf("rm '%s'\n", path);
d9b814cc
LT
345
346 if (remove_file_from_cache(path))
b9b537f7 347 die(_("git rm: unable to remove %s"), path);
d9b814cc
LT
348 }
349
7612a1ef
JH
350 if (show_only)
351 return 0;
352
d9b814cc 353 /*
646ac22b 354 * Then, unless we used "--cached", remove the filenames from
9f95069b 355 * the workspace. If we fail to remove the first one, we
d9b814cc
LT
356 * abort the "git rm" (but once we've successfully removed
357 * any file at all, we'll go ahead and commit to it all:
82e5a82f 358 * by then we've already committed ourselves and can't fail
d9b814cc
LT
359 * in the middle)
360 */
9f95069b 361 if (!index_only) {
95c16418 362 int removed = 0, gitmodules_modified = 0;
590fc052 363 struct strbuf buf = STRBUF_INIT;
d9b814cc 364 for (i = 0; i < list.nr; i++) {
293ab15e
JL
365 const char *path = list.entry[i].name;
366 if (list.entry[i].is_submodule) {
590fc052 367 strbuf_reset(&buf);
55856a35
SB
368 strbuf_addstr(&buf, path);
369 if (remove_dir_recursively(&buf, 0))
370 die(_("could not remove '%s'"), path);
55856a35
SB
371
372 removed = 1;
373 if (!remove_path_from_gitmodules(path))
374 gitmodules_modified = 1;
375 continue;
293ab15e 376 }
175a4948 377 if (!remove_path(path)) {
d9b814cc
LT
378 removed = 1;
379 continue;
380 }
381 if (!removed)
d824cbba 382 die_errno("git rm: '%s'", path);
d9b814cc 383 }
590fc052 384 strbuf_release(&buf);
95c16418
JL
385 if (gitmodules_modified)
386 stage_updated_gitmodules();
d9b814cc
LT
387 }
388
389 if (active_cache_changed) {
03b86647 390 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
b9b537f7 391 die(_("Unable to write new index file"));
d9b814cc
LT
392 }
393
394 return 0;
395}