2 * "git clean" builtin command
4 * Copyright (C) 2007 Shawn Bohrer
6 * Based on git-clean.sh by Pavel Roskin
12 #include "parse-options.h"
13 #include "string-list.h"
16 static int force = -1; /* unset */
18 static const char *const builtin_clean_usage[] = {
19 "git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...",
23 static int git_clean_config(const char *var, const char *value, void *cb)
25 if (!strcmp(var, "clean.requireforce"))
26 force = !git_config_bool(var, value);
27 return git_default_config(var, value, cb);
30 static int exclude_cb(const struct option *opt, const char *arg, int unset)
32 struct string_list *exclude_list = opt->value;
33 string_list_append(exclude_list, arg);
37 int cmd_clean(int argc, const char **argv, const char *prefix)
40 int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
41 int ignored_only = 0, config_set = 0, errors = 0;
42 int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
43 struct strbuf directory = STRBUF_INIT;
44 struct dir_struct dir;
45 static const char **pathspec;
46 struct strbuf buf = STRBUF_INIT;
47 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
50 struct option options[] = {
51 OPT__QUIET(&quiet, "do not print names of files removed"),
52 OPT__DRY_RUN(&show_only, "dry run"),
53 OPT__FORCE(&force, "force"),
54 OPT_BOOLEAN('d', NULL, &remove_directories,
55 "remove whole directories"),
56 { OPTION_CALLBACK, 'e', "exclude", &exclude_list, "pattern",
57 "add <pattern> to ignore rules", PARSE_OPT_NONEG, exclude_cb },
58 OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
59 OPT_BOOLEAN('X', NULL, &ignored_only,
60 "remove only ignored files"),
64 git_config(git_clean_config, NULL);
70 argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
73 memset(&dir, 0, sizeof(dir));
75 dir.flags |= DIR_SHOW_IGNORED;
77 if (ignored && ignored_only)
78 die(_("-x and -X cannot be used together"));
80 if (!show_only && !force) {
82 die(_("clean.requireForce set to true and neither -n nor -f given; "
83 "refusing to clean"));
85 die(_("clean.requireForce defaults to true and neither -n nor -f given; "
86 "refusing to clean"));
92 dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
95 die(_("index file corrupt"));
98 setup_standard_excludes(&dir);
100 for (i = 0; i < exclude_list.nr; i++)
101 add_exclude(exclude_list.items[i].string, "", 0,
102 &dir.exclude_list[EXC_CMDL]);
104 pathspec = get_pathspec(prefix, argv);
106 fill_directory(&dir, pathspec);
109 seen = xmalloc(argc > 0 ? argc : 1);
111 for (i = 0; i < dir.nr; i++) {
112 struct dir_entry *ent = dir.entries[i];
115 struct cache_entry *ce;
119 * Remove the '/' at the end that directory
120 * walking adds for directory entries.
123 if (len && ent->name[len-1] == '/')
125 pos = cache_name_pos(ent->name, len);
127 continue; /* exact match */
129 if (pos < active_nr) {
130 ce = active_cache[pos];
131 if (ce_namelen(ce) == len &&
132 !memcmp(ce->name, ent->name, len))
133 continue; /* Yup, this one exists unmerged */
137 * we might have removed this as part of earlier
138 * recursive directory removal, so lstat() here could
141 if (lstat(ent->name, &st))
145 memset(seen, 0, argc > 0 ? argc : 1);
146 matches = match_pathspec(pathspec, ent->name, len,
150 if (S_ISDIR(st.st_mode)) {
151 strbuf_addstr(&directory, ent->name);
152 qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
153 if (show_only && (remove_directories ||
154 (matches == MATCHED_EXACTLY))) {
155 printf(_("Would remove %s\n"), qname);
156 } else if (remove_directories ||
157 (matches == MATCHED_EXACTLY)) {
159 printf(_("Removing %s\n"), qname);
160 if (remove_dir_recursively(&directory,
162 warning(_("failed to remove %s"), qname);
165 } else if (show_only) {
166 printf(_("Would not remove %s\n"), qname);
168 printf(_("Not removing %s\n"), qname);
170 strbuf_reset(&directory);
172 if (pathspec && !matches)
174 qname = quote_path_relative(ent->name, -1, &buf, prefix);
176 printf(_("Would remove %s\n"), qname);
179 printf(_("Removing %s\n"), qname);
181 if (unlink(ent->name) != 0) {
182 warning(_("failed to remove %s"), qname);
189 strbuf_release(&directory);
190 string_list_clear(&exclude_list, 0);
191 return (errors != 0);