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