2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
10 static const char builtin_rm_usage[] =
11 "git-rm [-n] [-v] [-f] <filepattern>...";
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)
33 if (!ret && (slash = strrchr(name, '/'))) {
34 char *n = strdup(name);
38 } while (!rmdir(name) && (slash = strrchr(name, '/')));
43 static struct cache_file cache_file;
45 int cmd_rm(int argc, const char **argv, char **envp)
48 int verbose = 0, show_only = 0, force = 0;
49 const char *prefix = setup_git_directory();
50 const char **pathspec;
53 git_config(git_default_config);
55 newfd = hold_index_file_for_update(&cache_file, get_index_file());
57 die("unable to create new index file");
60 die("index file corrupt");
62 for (i = 1 ; i < argc ; i++) {
63 const char *arg = argv[i];
67 if (!strcmp(arg, "--")) {
71 if (!strcmp(arg, "-n")) {
75 if (!strcmp(arg, "-v")) {
79 if (!strcmp(arg, "-f")) {
83 die(builtin_rm_usage);
85 pathspec = get_pathspec(prefix, argv + i);
89 for (i = 0; pathspec[i] ; 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))
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
132 for (i = 0; i < list.nr; i++) {
133 const char *path = list.name[i];
134 if (!remove_file(path)) {
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");