]>
Commit | Line | Data |
---|---|---|
d9b814cc LT |
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" | |
3fb3cc69 | 9 | #include "cache-tree.h" |
d9b814cc LT |
10 | |
11 | static const char builtin_rm_usage[] = | |
12 | "git-rm [-n] [-v] [-f] <filepattern>..."; | |
13 | ||
14 | static struct { | |
15 | int nr, alloc; | |
16 | const char **name; | |
17 | } list; | |
18 | ||
19 | static 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 | ||
28 | static 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 | ||
021b6e45 | 44 | static struct lock_file lock_file; |
d9b814cc | 45 | |
a633fca0 | 46 | int cmd_rm(int argc, const char **argv, const char *prefix) |
d9b814cc LT |
47 | { |
48 | int i, newfd; | |
49 | int verbose = 0, show_only = 0, force = 0; | |
d9b814cc LT |
50 | const char **pathspec; |
51 | char *seen; | |
52 | ||
53 | git_config(git_default_config); | |
54 | ||
021b6e45 | 55 | newfd = hold_lock_file_for_update(&lock_file, get_index_file()); |
d9b814cc LT |
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 | } | |
8cdf3364 | 83 | usage(builtin_rm_usage); |
d9b814cc | 84 | } |
7612a1ef JH |
85 | if (argc <= i) |
86 | usage(builtin_rm_usage); | |
d9b814cc | 87 | |
7612a1ef | 88 | pathspec = get_pathspec(prefix, argv + i); |
d9b814cc | 89 | seen = NULL; |
7612a1ef JH |
90 | for (i = 0; pathspec[i] ; i++) |
91 | /* nothing */; | |
28f75818 | 92 | seen = xcalloc(i, 1); |
d9b814cc LT |
93 | |
94 | for (i = 0; i < active_nr; i++) { | |
95 | struct cache_entry *ce = active_cache[i]; | |
96 | if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen)) | |
97 | continue; | |
98 | add_list(ce->name); | |
99 | } | |
100 | ||
101 | if (pathspec) { | |
102 | const char *match; | |
103 | for (i = 0; (match = pathspec[i]) != NULL ; i++) { | |
104 | if (*match && !seen[i]) | |
105 | die("pathspec '%s' did not match any files", match); | |
106 | } | |
107 | } | |
108 | ||
109 | /* | |
110 | * First remove the names from the index: we won't commit | |
111 | * the index unless all of them succeed | |
112 | */ | |
113 | for (i = 0; i < list.nr; i++) { | |
114 | const char *path = list.name[i]; | |
115 | printf("rm '%s'\n", path); | |
116 | ||
117 | if (remove_file_from_cache(path)) | |
cba05fa8 | 118 | die("git-rm: unable to remove %s", path); |
93872e07 | 119 | cache_tree_invalidate_path(active_cache_tree, path); |
d9b814cc LT |
120 | } |
121 | ||
7612a1ef JH |
122 | if (show_only) |
123 | return 0; | |
124 | ||
d9b814cc LT |
125 | /* |
126 | * Then, if we used "-f", remove the filenames from the | |
127 | * workspace. If we fail to remove the first one, we | |
128 | * abort the "git rm" (but once we've successfully removed | |
129 | * any file at all, we'll go ahead and commit to it all: | |
82e5a82f | 130 | * by then we've already committed ourselves and can't fail |
d9b814cc LT |
131 | * in the middle) |
132 | */ | |
133 | if (force) { | |
134 | int removed = 0; | |
135 | for (i = 0; i < list.nr; i++) { | |
136 | const char *path = list.name[i]; | |
137 | if (!remove_file(path)) { | |
138 | removed = 1; | |
139 | continue; | |
140 | } | |
141 | if (!removed) | |
cba05fa8 | 142 | die("git-rm: %s: %s", path, strerror(errno)); |
d9b814cc LT |
143 | } |
144 | } | |
145 | ||
146 | if (active_cache_changed) { | |
147 | if (write_cache(newfd, active_cache, active_nr) || | |
6244b249 | 148 | close(newfd) || commit_lock_file(&lock_file)) |
d9b814cc LT |
149 | die("Unable to write new index file"); |
150 | } | |
151 | ||
152 | return 0; | |
153 | } |