git-clone: fix duplicated "master" in $GIT_DIR/remotes/origin
[git/spearce.git] / builtin-rm.c
blobef2f8b5d099555c259a39985d8e765cf378e4431
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"
9 #include "cache-tree.h"
11 static const char builtin_rm_usage[] =
12 "git-rm [-n] [-v] [-f] <filepattern>...";
14 static struct {
15 int nr, alloc;
16 const char **name;
17 } list;
19 static void add_list(const char *name)
21 if (list.nr >= list.alloc) {
22 list.alloc = alloc_nr(list.alloc);
23 list.name = xrealloc(list.name, list.alloc * sizeof(const char *));
25 list.name[list.nr++] = name;
28 static int remove_file(const char *name)
30 int ret;
31 char *slash;
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, '/')));
41 return ret;
44 static struct lock_file lock_file;
46 int cmd_rm(int argc, const char **argv, char **envp)
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;
54 git_config(git_default_config);
56 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
57 if (newfd < 0)
58 die("unable to create new index file");
60 if (read_cache() < 0)
61 die("index file corrupt");
63 for (i = 1 ; i < argc ; i++) {
64 const char *arg = argv[i];
66 if (*arg != '-')
67 break;
68 if (!strcmp(arg, "--")) {
69 i++;
70 break;
72 if (!strcmp(arg, "-n")) {
73 show_only = 1;
74 continue;
76 if (!strcmp(arg, "-v")) {
77 verbose = 1;
78 continue;
80 if (!strcmp(arg, "-f")) {
81 force = 1;
82 continue;
84 die(builtin_rm_usage);
86 pathspec = get_pathspec(prefix, argv + i);
88 seen = NULL;
89 if (pathspec) {
90 for (i = 0; pathspec[i] ; i++)
91 /* nothing */;
92 seen = xmalloc(i);
93 memset(seen, 0, i);
96 for (i = 0; i < active_nr; i++) {
97 struct cache_entry *ce = active_cache[i];
98 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
99 continue;
100 add_list(ce->name);
103 if (pathspec) {
104 const char *match;
105 for (i = 0; (match = pathspec[i]) != NULL ; i++) {
106 if (*match && !seen[i])
107 die("pathspec '%s' did not match any files", match);
112 * First remove the names from the index: we won't commit
113 * the index unless all of them succeed
115 for (i = 0; i < list.nr; i++) {
116 const char *path = list.name[i];
117 printf("rm '%s'\n", path);
119 if (remove_file_from_cache(path))
120 die("git rm: unable to remove %s", path);
121 cache_tree_invalidate_path(active_cache_tree, path);
125 * Then, if we used "-f", remove the filenames from the
126 * workspace. If we fail to remove the first one, we
127 * abort the "git rm" (but once we've successfully removed
128 * any file at all, we'll go ahead and commit to it all:
129 * by then we've already committed ourself and can't fail
130 * in the middle)
132 if (force) {
133 int removed = 0;
134 for (i = 0; i < list.nr; i++) {
135 const char *path = list.name[i];
136 if (!remove_file(path)) {
137 removed = 1;
138 continue;
140 if (!removed)
141 die("git rm: %s: %s", path, strerror(errno));
145 if (active_cache_changed) {
146 if (write_cache(newfd, active_cache, active_nr) ||
147 commit_lock_file(&lock_file))
148 die("Unable to write new index file");
151 return 0;