1/*
2 * "git clean" builtin command
3 *
4 * Copyright (C) 2007 Shawn Bohrer
5 *
6 * Based on git-clean.sh by Pavel Roskin
7 */
8
9#include "builtin.h"
10#include "cache.h"
11#include "dir.h"
12#include "parse-options.h"
13
14static int force;
15
16static const char *const builtin_clean_usage[] = {
17 "git-clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
18 NULL
19};
20
21static int git_clean_config(const char *var, const char *value)
22{
23 if (!strcmp(var, "clean.requireforce"))
24 force = !git_config_bool(var, value);
25 return 0;
26}
27
28int cmd_clean(int argc, const char **argv, const char *prefix)
29{
30 int j;
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[] = {
38 OPT__QUIET(&quiet),
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"),
46 OPT_END()
47 };
48
49 git_config(git_clean_config);
50 argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
51
52 memset(&dir, 0, sizeof(dir));
53 if (ignored_only) {
54 dir.show_ignored =1;
55 dir.exclude_per_dir = ".gitignore";
56 }
57
58 if (ignored && ignored_only)
59 die("-x and -X cannot be used together");
60
61 if (!show_only && !force)
62 die("clean.requireForce set and -n or -f not given; refusing to clean");
63
64 dir.show_other_directories = 1;
65
66 if (!ignored) {
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);
71 }
72 }
73
74 pathspec = get_pathspec(prefix, argv);
75 read_cache();
76
77 /*
78 * Calculate common prefix for the pathspec, and
79 * use that to optimize the directory walk
80 */
81 baselen = common_prefix(pathspec);
82 path = ".";
83 base = "";
84 if (baselen)
85 path = base = xmemdupz(*pathspec, baselen);
86 read_directory(&dir, path, base, baselen, pathspec);
87 strbuf_init(&directory, 0);
88
89 for (j = 0; j < dir.nr; ++j) {
90 struct dir_entry *ent = dir.entries[j];
91 int len, pos, specs;
92 struct cache_entry *ce;
93 struct stat st;
94 char *seen;
95
96 /*
97 * Remove the '/' at the end that directory
98 * walking adds for directory entries.
99 */
100 len = ent->len;
101 if (len && ent->name[len-1] == '/')
102 len--;
103 pos = cache_name_pos(ent->name, len);
104 if (0 <= pos)
105 continue; /* exact match */
106 pos = -pos - 1;
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 */
112 }
113
114 if (!lstat(ent->name, &st) && (S_ISDIR(st.st_mode))) {
115 int matched_path = 0;
116 strbuf_addstr(&directory, ent->name);
117 if (pathspec) {
118 for (specs =0; pathspec[specs]; ++specs)
119 /* nothing */;
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,
124 baselen, seen))
125 matched_path = 1;
126 free(seen);
127 }
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);
137 } else {
138 printf("Not removing %s\n", directory.buf);
139 }
140 strbuf_reset(&directory);
141 } else {
142 if (show_only) {
143 printf("Would remove %s\n", ent->name);
144 continue;
145 } else if (!quiet) {
146 printf("Removing %s\n", ent->name);
147 }
148 unlink(ent->name);
149 }
150 }
151
152 strbuf_release(&directory);
153 return 0;
154}