Code

Merge branch 'sg/maint-intrebase-msghook' into maint
[git.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"
13 static const char * const builtin_rm_usage[] = {
14         "git rm [options] [--] <file>...",
15         NULL
16 };
18 static struct {
19         int nr, alloc;
20         const char **name;
21 } list;
23 static void add_list(const char *name)
24 {
25         if (list.nr >= list.alloc) {
26                 list.alloc = alloc_nr(list.alloc);
27                 list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
28         }
29         list.name[list.nr++] = name;
30 }
32 static int check_local_mod(unsigned char *head, int index_only)
33 {
34         /* items in list are already sorted in the cache order,
35          * so we could do this a lot more efficiently by using
36          * tree_desc based traversal if we wanted to, but I am
37          * lazy, and who cares if removal of files is a tad
38          * slower than the theoretical maximum speed?
39          */
40         int i, no_head;
41         int errs = 0;
43         no_head = is_null_sha1(head);
44         for (i = 0; i < list.nr; i++) {
45                 struct stat st;
46                 int pos;
47                 struct cache_entry *ce;
48                 const char *name = list.name[i];
49                 unsigned char sha1[20];
50                 unsigned mode;
51                 int local_changes = 0;
52                 int staged_changes = 0;
54                 pos = cache_name_pos(name, strlen(name));
55                 if (pos < 0)
56                         continue; /* removing unmerged entry */
57                 ce = active_cache[pos];
59                 if (lstat(ce->name, &st) < 0) {
60                         if (errno != ENOENT)
61                                 fprintf(stderr, "warning: '%s': %s",
62                                         ce->name, strerror(errno));
63                         /* It already vanished from the working tree */
64                         continue;
65                 }
66                 else if (S_ISDIR(st.st_mode)) {
67                         /* if a file was removed and it is now a
68                          * directory, that is the same as ENOENT as
69                          * far as git is concerned; we do not track
70                          * directories.
71                          */
72                         continue;
73                 }
74                 if (ce_match_stat(ce, &st, 0))
75                         local_changes = 1;
76                 if (no_head
77                      || get_tree_entry(head, name, sha1, &mode)
78                      || ce->ce_mode != create_ce_mode(mode)
79                      || hashcmp(ce->sha1, sha1))
80                         staged_changes = 1;
82                 if (local_changes && staged_changes)
83                         errs = error("'%s' has staged content different "
84                                      "from both the file and the HEAD\n"
85                                      "(use -f to force removal)", name);
86                 else if (!index_only) {
87                         /* It's not dangerous to "git rm --cached" a
88                          * file if the index matches the file or the
89                          * HEAD, since it means the deleted content is
90                          * still available somewhere.
91                          */
92                         if (staged_changes)
93                                 errs = error("'%s' has changes staged in the index\n"
94                                              "(use --cached to keep the file, "
95                                              "or -f to force removal)", name);
96                         if (local_changes)
97                                 errs = error("'%s' has local modifications\n"
98                                              "(use --cached to keep the file, "
99                                              "or -f to force removal)", name);
100                 }
101         }
102         return errs;
105 static struct lock_file lock_file;
107 static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
108 static int ignore_unmatch = 0;
110 static struct option builtin_rm_options[] = {
111         OPT__DRY_RUN(&show_only),
112         OPT__QUIET(&quiet),
113         OPT_BOOLEAN( 0 , "cached",         &index_only, "only remove from the index"),
114         OPT_BOOLEAN('f', "force",          &force,      "override the up-to-date check"),
115         OPT_BOOLEAN('r', NULL,             &recursive,  "allow recursive removal"),
116         OPT_BOOLEAN( 0 , "ignore-unmatch", &ignore_unmatch,
117                                 "exit with a zero status even if nothing matched"),
118         OPT_END(),
119 };
121 int cmd_rm(int argc, const char **argv, const char *prefix)
123         int i, newfd;
124         const char **pathspec;
125         char *seen;
127         git_config(git_default_config, NULL);
129         argc = parse_options(argc, argv, builtin_rm_options, builtin_rm_usage, 0);
130         if (!argc)
131                 usage_with_options(builtin_rm_usage, builtin_rm_options);
133         if (!index_only)
134                 setup_work_tree();
136         newfd = hold_locked_index(&lock_file, 1);
138         if (read_cache() < 0)
139                 die("index file corrupt");
140         refresh_cache(REFRESH_QUIET);
142         pathspec = get_pathspec(prefix, argv);
143         seen = NULL;
144         for (i = 0; pathspec[i] ; i++)
145                 /* nothing */;
146         seen = xcalloc(i, 1);
148         for (i = 0; i < active_nr; i++) {
149                 struct cache_entry *ce = active_cache[i];
150                 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
151                         continue;
152                 add_list(ce->name);
153         }
155         if (pathspec) {
156                 const char *match;
157                 int seen_any = 0;
158                 for (i = 0; (match = pathspec[i]) != NULL ; i++) {
159                         if (!seen[i]) {
160                                 if (!ignore_unmatch) {
161                                         die("pathspec '%s' did not match any files",
162                                             match);
163                                 }
164                         }
165                         else {
166                                 seen_any = 1;
167                         }
168                         if (!recursive && seen[i] == MATCHED_RECURSIVELY)
169                                 die("not removing '%s' recursively without -r",
170                                     *match ? match : ".");
171                 }
173                 if (! seen_any)
174                         exit(0);
175         }
177         /*
178          * If not forced, the file, the index and the HEAD (if exists)
179          * must match; but the file can already been removed, since
180          * this sequence is a natural "novice" way:
181          *
182          *      rm F; git rm F
183          *
184          * Further, if HEAD commit exists, "diff-index --cached" must
185          * report no changes unless forced.
186          */
187         if (!force) {
188                 unsigned char sha1[20];
189                 if (get_sha1("HEAD", sha1))
190                         hashclr(sha1);
191                 if (check_local_mod(sha1, index_only))
192                         exit(1);
193         }
195         /*
196          * First remove the names from the index: we won't commit
197          * the index unless all of them succeed.
198          */
199         for (i = 0; i < list.nr; i++) {
200                 const char *path = list.name[i];
201                 if (!quiet)
202                         printf("rm '%s'\n", path);
204                 if (remove_file_from_cache(path))
205                         die("git rm: unable to remove %s", path);
206         }
208         if (show_only)
209                 return 0;
211         /*
212          * Then, unless we used "--cached", remove the filenames from
213          * the workspace. If we fail to remove the first one, we
214          * abort the "git rm" (but once we've successfully removed
215          * any file at all, we'll go ahead and commit to it all:
216          * by then we've already committed ourselves and can't fail
217          * in the middle)
218          */
219         if (!index_only) {
220                 int removed = 0;
221                 for (i = 0; i < list.nr; i++) {
222                         const char *path = list.name[i];
223                         if (!remove_path(path)) {
224                                 removed = 1;
225                                 continue;
226                         }
227                         if (!removed)
228                                 die("git rm: %s: %s", path, strerror(errno));
229                 }
230         }
232         if (active_cache_changed) {
233                 if (write_cache(newfd, active_cache, active_nr) ||
234                     commit_locked_index(&lock_file))
235                         die("Unable to write new index file");
236         }
238         return 0;