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"
16 static const char *const builtin_clean_usage[] = {
17 "git-clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
21 static int git_clean_config(const char *var, const char *value)
23 if (!strcmp(var, "clean.requireforce"))
24 force = !git_config_bool(var, value);
28 int cmd_clean(int argc, const char **argv, const char *prefix)
31 int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
32 int ignored_only = 0, baselen = 0;
33 struct strbuf directory;
34 struct dir_struct dir;
35 const char *path, *base;
36 static const char **pathspec;
37 struct option options[] = {
39 OPT__DRY_RUN(&show_only),
40 OPT_BOOLEAN('f', NULL, &force, "force"),
41 OPT_BOOLEAN('d', NULL, &remove_directories,
42 "remove whole directories"),
43 OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
44 OPT_BOOLEAN('X', NULL, &ignored_only,
45 "remove only ignored files"),
49 git_config(git_clean_config);
50 argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
52 memset(&dir, 0, sizeof(dir));
55 dir.exclude_per_dir = ".gitignore";
58 if (ignored && ignored_only)
59 die("-x and -X cannot be used together");
61 if (!show_only && !force)
62 die("clean.requireForce set and -n or -f not given; refusing to clean");
64 dir.show_other_directories = 1;
67 dir.exclude_per_dir = ".gitignore";
68 if (!access(git_path("info/exclude"), F_OK)) {
69 char *exclude_path = git_path("info/exclude");
70 add_excludes_from_file(&dir, exclude_path);
74 pathspec = get_pathspec(prefix, argv);
78 * Calculate common prefix for the pathspec, and
79 * use that to optimize the directory walk
81 baselen = common_prefix(pathspec);
85 path = base = xmemdupz(*pathspec, baselen);
86 read_directory(&dir, path, base, baselen, pathspec);
87 strbuf_init(&directory, 0);
89 for (j = 0; j < dir.nr; ++j) {
90 struct dir_entry *ent = dir.entries[j];
92 struct cache_entry *ce;
97 * Remove the '/' at the end that directory
98 * walking adds for directory entries.
101 if (len && ent->name[len-1] == '/')
103 pos = cache_name_pos(ent->name, len);
105 continue; /* exact match */
107 if (pos < active_nr) {
108 ce = active_cache[pos];
109 if (ce_namelen(ce) == len &&
110 !memcmp(ce->name, ent->name, len))
111 continue; /* Yup, this one exists unmerged */
114 if (!lstat(ent->name, &st) && (S_ISDIR(st.st_mode))) {
115 int matched_path = 0;
116 strbuf_addstr(&directory, ent->name);
118 for (specs =0; pathspec[specs]; ++specs)
120 seen = xcalloc(specs, 1);
121 /* Check if directory was explictly passed as
122 * pathspec. If so we want to remove it */
123 if (match_pathspec(pathspec, ent->name, ent->len,
128 if (show_only && (remove_directories || matched_path)) {
129 printf("Would remove %s\n", directory.buf);
130 } else if (quiet && (remove_directories || matched_path)) {
131 remove_dir_recursively(&directory, 0);
132 } else if (remove_directories || matched_path) {
133 printf("Removing %s\n", directory.buf);
134 remove_dir_recursively(&directory, 0);
135 } else if (show_only) {
136 printf("Would not remove %s\n", directory.buf);
138 printf("Not removing %s\n", directory.buf);
140 strbuf_reset(&directory);
143 printf("Would remove %s\n", ent->name);
146 printf("Removing %s\n", ent->name);
152 strbuf_release(&directory);