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"
15 static int force = -1; /* unset */
17 static const char *const builtin_clean_usage[] = {
18 "git clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
22 static int git_clean_config(const char *var, const char *value, void *cb)
24 if (!strcmp(var, "clean.requireforce"))
25 force = !git_config_bool(var, value);
26 return git_default_config(var, value, cb);
29 int cmd_clean(int argc, const char **argv, const char *prefix)
32 int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
33 int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
34 struct strbuf directory = STRBUF_INIT;
35 struct dir_struct dir;
36 static const char **pathspec;
37 struct strbuf buf = STRBUF_INIT;
40 struct option options[] = {
42 OPT__DRY_RUN(&show_only),
43 OPT_BOOLEAN('f', NULL, &force, "force"),
44 OPT_BOOLEAN('d', NULL, &remove_directories,
45 "remove whole directories"),
46 OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
47 OPT_BOOLEAN('X', NULL, &ignored_only,
48 "remove only ignored files"),
52 git_config(git_clean_config, NULL);
58 argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
61 memset(&dir, 0, sizeof(dir));
63 dir.flags |= DIR_SHOW_IGNORED;
65 if (ignored && ignored_only)
66 die("-x and -X cannot be used together");
68 if (!show_only && !force)
69 die("clean.requireForce%s set and -n or -f not given; "
70 "refusing to clean", config_set ? "" : " not");
72 dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
75 setup_standard_excludes(&dir);
77 pathspec = get_pathspec(prefix, argv);
80 fill_directory(&dir, pathspec);
83 seen = xmalloc(argc > 0 ? argc : 1);
85 for (i = 0; i < dir.nr; i++) {
86 struct dir_entry *ent = dir.entries[i];
89 struct cache_entry *ce;
93 * Remove the '/' at the end that directory
94 * walking adds for directory entries.
97 if (len && ent->name[len-1] == '/')
99 pos = cache_name_pos(ent->name, len);
101 continue; /* exact match */
103 if (pos < active_nr) {
104 ce = active_cache[pos];
105 if (ce_namelen(ce) == len &&
106 !memcmp(ce->name, ent->name, len))
107 continue; /* Yup, this one exists unmerged */
111 * we might have removed this as part of earlier
112 * recursive directory removal, so lstat() here could
115 if (lstat(ent->name, &st))
119 memset(seen, 0, argc > 0 ? argc : 1);
120 matches = match_pathspec(pathspec, ent->name, len,
124 if (S_ISDIR(st.st_mode)) {
125 strbuf_addstr(&directory, ent->name);
126 qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
127 if (show_only && (remove_directories ||
128 (matches == MATCHED_EXACTLY))) {
129 printf("Would remove %s\n", qname);
130 } else if (remove_directories ||
131 (matches == MATCHED_EXACTLY)) {
133 printf("Removing %s\n", qname);
134 if (remove_dir_recursively(&directory, 0) != 0) {
135 warning("failed to remove '%s'", qname);
138 } else if (show_only) {
139 printf("Would not remove %s\n", qname);
141 printf("Not removing %s\n", qname);
143 strbuf_reset(&directory);
145 if (pathspec && !matches)
147 qname = quote_path_relative(ent->name, -1, &buf, prefix);
149 printf("Would remove %s\n", qname);
152 printf("Removing %s\n", qname);
154 if (unlink(ent->name) != 0) {
155 warning("failed to remove '%s'", qname);
162 strbuf_release(&directory);
163 return (errors != 0);