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
11static const char builtin_rm_usage[] =
12"git-rm [-n] [-v] [-f] <filepattern>...";
13
14static struct {
15 int nr, alloc;
16 const char **name;
17} list;
18
19static void add_list(const char *name)
20{
21 if (list.nr >= list.alloc) {
22 list.alloc = alloc_nr(list.alloc);
23 list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
24 }
25 list.name[list.nr++] = name;
26}
27
28static int remove_file(const char *name)
29{
30 int ret;
31 char *slash;
32
33 ret = unlink(name);
34 if (!ret && (slash = strrchr(name, '/'))) {
35 char *n = strdup(name);
36 do {
37 n[slash - name] = 0;
38 name = n;
39 } while (!rmdir(name) && (slash = strrchr(name, '/')));
40 }
41 return ret;
42}
43
44static struct lock_file lock_file;
45
46int cmd_rm(int argc, const char **argv, char **envp)
47{
48 int i, newfd;
49 int verbose = 0, show_only = 0, force = 0;
50 const char *prefix = setup_git_directory();
51 const char **pathspec;
52 char *seen;
53
54 git_config(git_default_config);
55
56 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
57 if (newfd < 0)
58 die("unable to create new index file");
59
60 if (read_cache() < 0)
61 die("index file corrupt");
62
63 for (i = 1 ; i < argc ; i++) {
64 const char *arg = argv[i];
65
66 if (*arg != '-')
67 break;
68 if (!strcmp(arg, "--")) {
69 i++;
70 break;
71 }
72 if (!strcmp(arg, "-n")) {
73 show_only = 1;
74 continue;
75 }
76 if (!strcmp(arg, "-v")) {
77 verbose = 1;
78 continue;
79 }
80 if (!strcmp(arg, "-f")) {
81 force = 1;
82 continue;
83 }
84 die(builtin_rm_usage);
85 }
86 if (argc <= i)
87 usage(builtin_rm_usage);
88
89 pathspec = get_pathspec(prefix, argv + i);
90 seen = NULL;
91 for (i = 0; pathspec[i] ; i++)
92 /* nothing */;
93 seen = xcalloc(i, 1);
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 cache_tree_invalidate_path(active_cache_tree, path);
121 }
122
123 if (show_only)
124 return 0;
125
126 /*
127 * Then, if we used "-f", remove the filenames from the
128 * workspace. If we fail to remove the first one, we
129 * abort the "git rm" (but once we've successfully removed
130 * any file at all, we'll go ahead and commit to it all:
131 * by then we've already committed ourselves and can't fail
132 * in the middle)
133 */
134 if (force) {
135 int removed = 0;
136 for (i = 0; i < list.nr; i++) {
137 const char *path = list.name[i];
138 if (!remove_file(path)) {
139 removed = 1;
140 continue;
141 }
142 if (!removed)
143 die("git rm: %s: %s", path, strerror(errno));
144 }
145 }
146
147 if (active_cache_changed) {
148 if (write_cache(newfd, active_cache, active_nr) ||
149 close(newfd) || commit_lock_file(&lock_file))
150 die("Unable to write new index file");
151 }
152
153 return 0;
154}