Make cvsexportcommit create parent directories as needed.
[git/dscho.git] / builtin-rm.c
blob9014c61556c80d2d37d7f59e9e9419d3b3ba9884
1 /*
2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "dir.h"
10 static const char builtin_rm_usage[] =
11 "git-rm [-n] [-v] [-f] <filepattern>...";
13 static struct {
14 int nr, alloc;
15 const char **name;
16 } list;
18 static void add_list(const char *name)
20 if (list.nr >= list.alloc) {
21 list.alloc = alloc_nr(list.alloc);
22 list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
24 list.name[list.nr++] = name;
27 static int remove_file(const char *name)
29 int ret;
30 char *slash;
32 ret = unlink(name);
33 if (!ret && (slash = strrchr(name, '/'))) {
34 char *n = strdup(name);
35 do {
36 n[slash - name] = 0;
37 name = n;
38 } while (!rmdir(name) && (slash = strrchr(name, '/')));
40 return ret;
43 static struct cache_file cache_file;
45 int cmd_rm(int argc, const char **argv, char **envp)
47 int i, newfd;
48 int verbose = 0, show_only = 0, force = 0;
49 const char *prefix = setup_git_directory();
50 const char **pathspec;
51 char *seen;
53 git_config(git_default_config);
55 newfd = hold_index_file_for_update(&cache_file, get_index_file());
56 if (newfd < 0)
57 die("unable to create new index file");
59 if (read_cache() < 0)
60 die("index file corrupt");
62 for (i = 1 ; i < argc ; i++) {
63 const char *arg = argv[i];
65 if (*arg != '-')
66 break;
67 if (!strcmp(arg, "--")) {
68 i++;
69 break;
71 if (!strcmp(arg, "-n")) {
72 show_only = 1;
73 continue;
75 if (!strcmp(arg, "-v")) {
76 verbose = 1;
77 continue;
79 if (!strcmp(arg, "-f")) {
80 force = 1;
81 continue;
83 die(builtin_rm_usage);
85 pathspec = get_pathspec(prefix, argv + i);
87 seen = NULL;
88 if (pathspec) {
89 for (i = 0; pathspec[i] ; i++)
90 /* nothing */;
91 seen = xmalloc(i);
92 memset(seen, 0, i);
95 for (i = 0; i < active_nr; i++) {
96 struct cache_entry *ce = active_cache[i];
97 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
98 continue;
99 add_list(ce->name);
102 if (pathspec) {
103 const char *match;
104 for (i = 0; (match = pathspec[i]) != NULL ; i++) {
105 if (*match && !seen[i])
106 die("pathspec '%s' did not match any files", match);
111 * First remove the names from the index: we won't commit
112 * the index unless all of them succeed
114 for (i = 0; i < list.nr; i++) {
115 const char *path = list.name[i];
116 printf("rm '%s'\n", path);
118 if (remove_file_from_cache(path))
119 die("git rm: unable to remove %s", path);
123 * Then, if we used "-f", remove the filenames from the
124 * workspace. If we fail to remove the first one, we
125 * abort the "git rm" (but once we've successfully removed
126 * any file at all, we'll go ahead and commit to it all:
127 * by then we've already committed ourself and can't fail
128 * in the middle)
130 if (force) {
131 int removed = 0;
132 for (i = 0; i < list.nr; i++) {
133 const char *path = list.name[i];
134 if (!remove_file(path)) {
135 removed = 1;
136 continue;
138 if (!removed)
139 die("git rm: %s: %s", path, strerror(errno));
143 if (active_cache_changed) {
144 if (write_cache(newfd, active_cache, active_nr) ||
145 commit_index_file(&cache_file))
146 die("Unable to write new index file");
149 return 0;