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
10static const char builtin_rm_usage[] =
11"git-rm [-n] [-v] [-f] <filepattern>...";
12
13static struct {
14 int nr, alloc;
15 const char **name;
16} list;
17
18static void add_list(const char *name)
19{
20 if (list.nr >= list.alloc) {
21 list.alloc = alloc_nr(list.alloc);
22 list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
23 }
24 list.name[list.nr++] = name;
25}
26
27static int remove_file(const char *name)
28{
29 int ret;
30 char *slash;
31
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, '/')));
39 }
40 return ret;
41}
42
43static struct cache_file cache_file;
44
45int cmd_rm(int argc, const char **argv, char **envp)
46{
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;
52
53 git_config(git_default_config);
54
55 newfd = hold_index_file_for_update(&cache_file, get_index_file());
56 if (newfd < 0)
57 die("unable to create new index file");
58
59 if (read_cache() < 0)
60 die("index file corrupt");
61
62 for (i = 1 ; i < argc ; i++) {
63 const char *arg = argv[i];
64
65 if (*arg != '-')
66 break;
67 if (!strcmp(arg, "--")) {
68 i++;
69 break;
70 }
71 if (!strcmp(arg, "-n")) {
72 show_only = 1;
73 continue;
74 }
75 if (!strcmp(arg, "-v")) {
76 verbose = 1;
77 continue;
78 }
79 if (!strcmp(arg, "-f")) {
80 force = 1;
81 continue;
82 }
83 die(builtin_rm_usage);
84 }
85 pathspec = get_pathspec(prefix, argv + i);
86
87 seen = NULL;
88 if (pathspec) {
89 for (i = 0; pathspec[i] ; i++)
90 /* nothing */;
91 seen = xmalloc(i);
92 memset(seen, 0, i);
93 }
94
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);
100 }
101
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);
107 }
108 }
109
110 /*
111 * First remove the names from the index: we won't commit
112 * the index unless all of them succeed
113 */
114 for (i = 0; i < list.nr; i++) {
115 const char *path = list.name[i];
116 printf("rm '%s'\n", path);
117
118 if (remove_file_from_cache(path))
119 die("git rm: unable to remove %s", path);
120 }
121
122 /*
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)
129 */
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;
137 }
138 if (!removed)
139 die("git rm: %s: %s", path, strerror(errno));
140 }
141 }
142
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");
147 }
148
149 return 0;
150}