Code

Documentation: add git-remote man page
[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"
12 static const char builtin_rm_usage[] =
13 "git-rm [-n] [-f] [--cached] <filepattern>...";
15 static struct {
16         int nr, alloc;
17         const char **name;
18 } list;
20 static void add_list(const char *name)
21 {
22         if (list.nr >= list.alloc) {
23                 list.alloc = alloc_nr(list.alloc);
24                 list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
25         }
26         list.name[list.nr++] = name;
27 }
29 static int remove_file(const char *name)
30 {
31         int ret;
32         char *slash;
34         ret = unlink(name);
35         if (!ret && (slash = strrchr(name, '/'))) {
36                 char *n = xstrdup(name);
37                 do {
38                         n[slash - name] = 0;
39                         name = n;
40                 } while (!rmdir(name) && (slash = strrchr(name, '/')));
41         }
42         return ret;
43 }
45 static int check_local_mod(unsigned char *head)
46 {
47         /* items in list are already sorted in the cache order,
48          * so we could do this a lot more efficiently by using
49          * tree_desc based traversal if we wanted to, but I am
50          * lazy, and who cares if removal of files is a tad
51          * slower than the theoretical maximum speed?
52          */
53         int i, no_head;
54         int errs = 0;
56         no_head = is_null_sha1(head);
57         for (i = 0; i < list.nr; i++) {
58                 struct stat st;
59                 int pos;
60                 struct cache_entry *ce;
61                 const char *name = list.name[i];
62                 unsigned char sha1[20];
63                 unsigned mode;
65                 pos = cache_name_pos(name, strlen(name));
66                 if (pos < 0)
67                         continue; /* removing unmerged entry */
68                 ce = active_cache[pos];
70                 if (lstat(ce->name, &st) < 0) {
71                         if (errno != ENOENT)
72                                 fprintf(stderr, "warning: '%s': %s",
73                                         ce->name, strerror(errno));
74                         /* It already vanished from the working tree */
75                         continue;
76                 }
77                 else if (S_ISDIR(st.st_mode)) {
78                         /* if a file was removed and it is now a
79                          * directory, that is the same as ENOENT as
80                          * far as git is concerned; we do not track
81                          * directories.
82                          */
83                         continue;
84                 }
85                 if (ce_match_stat(ce, &st, 0))
86                         errs = error("'%s' has local modifications "
87                                      "(hint: try -f)", ce->name);
88                 if (no_head)
89                         continue;
90                 /*
91                  * It is Ok to remove a newly added path, as long as
92                  * it is cache-clean.
93                  */
94                 if (get_tree_entry(head, name, sha1, &mode))
95                         continue;
96                 /*
97                  * Otherwise make sure the version from the HEAD
98                  * matches the index.
99                  */
100                 if (ce->ce_mode != create_ce_mode(mode) ||
101                     hashcmp(ce->sha1, sha1))
102                         errs = error("'%s' has changes staged in the index "
103                                      "(hint: try -f)", name);
104         }
105         return errs;
108 static struct lock_file lock_file;
110 int cmd_rm(int argc, const char **argv, const char *prefix)
112         int i, newfd;
113         int show_only = 0, force = 0, index_only = 0, recursive = 0;
114         const char **pathspec;
115         char *seen;
117         git_config(git_default_config);
119         newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
121         if (read_cache() < 0)
122                 die("index file corrupt");
124         for (i = 1 ; i < argc ; i++) {
125                 const char *arg = argv[i];
127                 if (*arg != '-')
128                         break;
129                 else if (!strcmp(arg, "--")) {
130                         i++;
131                         break;
132                 }
133                 else if (!strcmp(arg, "-n"))
134                         show_only = 1;
135                 else if (!strcmp(arg, "--cached"))
136                         index_only = 1;
137                 else if (!strcmp(arg, "-f"))
138                         force = 1;
139                 else if (!strcmp(arg, "-r"))
140                         recursive = 1;
141                 else
142                         usage(builtin_rm_usage);
143         }
144         if (argc <= i)
145                 usage(builtin_rm_usage);
147         pathspec = get_pathspec(prefix, argv + i);
148         seen = NULL;
149         for (i = 0; pathspec[i] ; i++)
150                 /* nothing */;
151         seen = xcalloc(i, 1);
153         for (i = 0; i < active_nr; i++) {
154                 struct cache_entry *ce = active_cache[i];
155                 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
156                         continue;
157                 add_list(ce->name);
158         }
160         if (pathspec) {
161                 const char *match;
162                 for (i = 0; (match = pathspec[i]) != NULL ; i++) {
163                         if (!seen[i])
164                                 die("pathspec '%s' did not match any files",
165                                     match);
166                         if (!recursive && seen[i] == MATCHED_RECURSIVELY)
167                                 die("not removing '%s' recursively without -r",
168                                     *match ? match : ".");
169                 }
170         }
172         /*
173          * If not forced, the file, the index and the HEAD (if exists)
174          * must match; but the file can already been removed, since
175          * this sequence is a natural "novice" way:
176          *
177          *      rm F; git fm F
178          *
179          * Further, if HEAD commit exists, "diff-index --cached" must
180          * report no changes unless forced.
181          */
182         if (!force) {
183                 unsigned char sha1[20];
184                 if (get_sha1("HEAD", sha1))
185                         hashclr(sha1);
186                 if (check_local_mod(sha1))
187                         exit(1);
188         }
190         /*
191          * First remove the names from the index: we won't commit
192          * the index unless all of them succeed.
193          */
194         for (i = 0; i < list.nr; i++) {
195                 const char *path = list.name[i];
196                 printf("rm '%s'\n", path);
198                 if (remove_file_from_cache(path))
199                         die("git-rm: unable to remove %s", path);
200                 cache_tree_invalidate_path(active_cache_tree, path);
201         }
203         if (show_only)
204                 return 0;
206         /*
207          * Then, unless we used "--cache", remove the filenames from
208          * the workspace. If we fail to remove the first one, we
209          * abort the "git rm" (but once we've successfully removed
210          * any file at all, we'll go ahead and commit to it all:
211          * by then we've already committed ourselves and can't fail
212          * in the middle)
213          */
214         if (!index_only) {
215                 int removed = 0;
216                 for (i = 0; i < list.nr; i++) {
217                         const char *path = list.name[i];
218                         if (!remove_file(path)) {
219                                 removed = 1;
220                                 continue;
221                         }
222                         if (!removed)
223                                 die("git-rm: %s: %s", path, strerror(errno));
224                 }
225         }
227         if (active_cache_changed) {
228                 if (write_cache(newfd, active_cache, active_nr) ||
229                     close(newfd) || commit_lock_file(&lock_file))
230                         die("Unable to write new index file");
231         }
233         return 0;