2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
9 #include "cache-tree.h"
10 #include "tree-walk.h"
11 #include "parse-options.h"
13 static const char * const builtin_rm_usage
[] = {
14 "git-rm [options] [--] <file>...",
23 static void add_list(const char *name
)
25 if (list
.nr
>= list
.alloc
) {
26 list
.alloc
= alloc_nr(list
.alloc
);
27 list
.name
= xrealloc(list
.name
, list
.alloc
* sizeof(const char *));
29 list
.name
[list
.nr
++] = name
;
32 static int remove_file(const char *name
)
38 if (ret
&& errno
== ENOENT
)
39 /* The user has removed it from the filesystem by hand */
42 if (!ret
&& (slash
= strrchr(name
, '/'))) {
43 char *n
= xstrdup(name
);
47 } while (!rmdir(name
) && (slash
= strrchr(name
, '/')));
52 static int check_local_mod(unsigned char *head
, int index_only
)
54 /* items in list are already sorted in the cache order,
55 * so we could do this a lot more efficiently by using
56 * tree_desc based traversal if we wanted to, but I am
57 * lazy, and who cares if removal of files is a tad
58 * slower than the theoretical maximum speed?
63 no_head
= is_null_sha1(head
);
64 for (i
= 0; i
< list
.nr
; i
++) {
67 struct cache_entry
*ce
;
68 const char *name
= list
.name
[i
];
69 unsigned char sha1
[20];
71 int local_changes
= 0;
72 int staged_changes
= 0;
74 pos
= cache_name_pos(name
, strlen(name
));
76 continue; /* removing unmerged entry */
77 ce
= active_cache
[pos
];
79 if (lstat(ce
->name
, &st
) < 0) {
81 fprintf(stderr
, "warning: '%s': %s",
82 ce
->name
, strerror(errno
));
83 /* It already vanished from the working tree */
86 else if (S_ISDIR(st
.st_mode
)) {
87 /* if a file was removed and it is now a
88 * directory, that is the same as ENOENT as
89 * far as git is concerned; we do not track
94 if (ce_match_stat(ce
, &st
, 0))
97 || get_tree_entry(head
, name
, sha1
, &mode
)
98 || ce
->ce_mode
!= create_ce_mode(mode
)
99 || hashcmp(ce
->sha1
, sha1
))
102 if (local_changes
&& staged_changes
)
103 errs
= error("'%s' has staged content different "
104 "from both the file and the HEAD\n"
105 "(use -f to force removal)", name
);
106 else if (!index_only
) {
107 /* It's not dangerous to git-rm --cached a
108 * file if the index matches the file or the
109 * HEAD, since it means the deleted content is
110 * still available somewhere.
113 errs
= error("'%s' has changes staged in the index\n"
114 "(use --cached to keep the file, "
115 "or -f to force removal)", name
);
117 errs
= error("'%s' has local modifications\n"
118 "(use --cached to keep the file, "
119 "or -f to force removal)", name
);
125 static struct lock_file lock_file
;
127 static int show_only
= 0, force
= 0, index_only
= 0, recursive
= 0, quiet
= 0;
128 static int ignore_unmatch
= 0;
130 static struct option builtin_rm_options
[] = {
131 OPT__DRY_RUN(&show_only
),
133 OPT_BOOLEAN( 0 , "cached", &index_only
, "only remove from the index"),
134 OPT_BOOLEAN('f', NULL
, &force
, "override the up-to-date check"),
135 OPT_BOOLEAN('r', NULL
, &recursive
, "allow recursive removal"),
136 OPT_BOOLEAN( 0 , "ignore-unmatch", &ignore_unmatch
,
137 "exit with a zero status even if nothing matched"),
141 int cmd_rm(int argc
, const char **argv
, const char *prefix
)
144 const char **pathspec
;
147 git_config(git_default_config
);
149 newfd
= hold_locked_index(&lock_file
, 1);
151 if (read_cache() < 0)
152 die("index file corrupt");
154 argc
= parse_options(argc
, argv
, builtin_rm_options
, builtin_rm_usage
, 0);
156 usage_with_options(builtin_rm_usage
, builtin_rm_options
);
158 pathspec
= get_pathspec(prefix
, argv
);
160 for (i
= 0; pathspec
[i
] ; i
++)
162 seen
= xcalloc(i
, 1);
164 for (i
= 0; i
< active_nr
; i
++) {
165 struct cache_entry
*ce
= active_cache
[i
];
166 if (!match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
), 0, seen
))
174 for (i
= 0; (match
= pathspec
[i
]) != NULL
; i
++) {
176 if (!ignore_unmatch
) {
177 die("pathspec '%s' did not match any files",
184 if (!recursive
&& seen
[i
] == MATCHED_RECURSIVELY
)
185 die("not removing '%s' recursively without -r",
186 *match
? match
: ".");
194 * If not forced, the file, the index and the HEAD (if exists)
195 * must match; but the file can already been removed, since
196 * this sequence is a natural "novice" way:
200 * Further, if HEAD commit exists, "diff-index --cached" must
201 * report no changes unless forced.
204 unsigned char sha1
[20];
205 if (get_sha1("HEAD", sha1
))
207 if (check_local_mod(sha1
, index_only
))
212 * First remove the names from the index: we won't commit
213 * the index unless all of them succeed.
215 for (i
= 0; i
< list
.nr
; i
++) {
216 const char *path
= list
.name
[i
];
218 printf("rm '%s'\n", path
);
220 if (remove_file_from_cache(path
))
221 die("git-rm: unable to remove %s", path
);
228 * Then, unless we used "--cached", remove the filenames from
229 * the workspace. If we fail to remove the first one, we
230 * abort the "git rm" (but once we've successfully removed
231 * any file at all, we'll go ahead and commit to it all:
232 * by then we've already committed ourselves and can't fail
237 for (i
= 0; i
< list
.nr
; i
++) {
238 const char *path
= list
.name
[i
];
239 if (!remove_file(path
)) {
244 die("git-rm: %s: %s", path
, strerror(errno
));
248 if (active_cache_changed
) {
249 if (write_cache(newfd
, active_cache
, active_nr
) ||
250 close(newfd
) || commit_locked_index(&lock_file
))
251 die("Unable to write new index file");