write_name{_quoted_relative,}(): remove redundant parameters
[git/git.git] / builtin / clean.c
CommitLineData
113f10f2
SB
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"
f538a91e 13#include "refs.h"
07de4eba 14#include "string-list.h"
1fb32894 15#include "quote.h"
113f10f2 16
625db1b7 17static int force = -1; /* unset */
113f10f2
SB
18
19static const char *const builtin_clean_usage[] = {
145f9c81 20 N_("git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
113f10f2
SB
21 NULL
22};
23
f538a91e
ZK
24static const char *msg_remove = N_("Removing %s\n");
25static const char *msg_would_remove = N_("Would remove %s\n");
26static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
27static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
28static const char *msg_warn_remove_failed = N_("failed to remove %s");
29
ef90d6d4 30static int git_clean_config(const char *var, const char *value, void *cb)
113f10f2
SB
31{
32 if (!strcmp(var, "clean.requireforce"))
33 force = !git_config_bool(var, value);
ef90d6d4 34 return git_default_config(var, value, cb);
113f10f2
SB
35}
36
07de4eba
JH
37static int exclude_cb(const struct option *opt, const char *arg, int unset)
38{
39 struct string_list *exclude_list = opt->value;
40 string_list_append(exclude_list, arg);
41 return 0;
42}
43
f538a91e
ZK
44static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
45 int dry_run, int quiet, int *dir_gone)
46{
47 DIR *dir;
48 struct strbuf quoted = STRBUF_INIT;
49 struct dirent *e;
50 int res = 0, ret = 0, gone = 1, original_len = path->len, len, i;
51 unsigned char submodule_head[20];
52 struct string_list dels = STRING_LIST_INIT_DUP;
53
54 *dir_gone = 1;
55
56 if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
57 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
58 if (!quiet) {
39598f99 59 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
60 printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
61 quoted.buf);
62 }
63
64 *dir_gone = 0;
65 return 0;
66 }
67
68 dir = opendir(path->buf);
69 if (!dir) {
70 /* an empty dir could be removed even if it is unreadble */
71 res = dry_run ? 0 : rmdir(path->buf);
72 if (res) {
39598f99 73 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
74 warning(_(msg_warn_remove_failed), quoted.buf);
75 *dir_gone = 0;
76 }
77 return res;
78 }
79
80 if (path->buf[original_len - 1] != '/')
81 strbuf_addch(path, '/');
82
83 len = path->len;
84 while ((e = readdir(dir)) != NULL) {
85 struct stat st;
86 if (is_dot_or_dotdot(e->d_name))
87 continue;
88
89 strbuf_setlen(path, len);
90 strbuf_addstr(path, e->d_name);
91 if (lstat(path->buf, &st))
92 ; /* fall thru */
93 else if (S_ISDIR(st.st_mode)) {
94 if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone))
95 ret = 1;
96 if (gone) {
39598f99 97 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
98 string_list_append(&dels, quoted.buf);
99 } else
100 *dir_gone = 0;
101 continue;
102 } else {
103 res = dry_run ? 0 : unlink(path->buf);
104 if (!res) {
39598f99 105 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
106 string_list_append(&dels, quoted.buf);
107 } else {
39598f99 108 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
109 warning(_(msg_warn_remove_failed), quoted.buf);
110 *dir_gone = 0;
111 ret = 1;
112 }
113 continue;
114 }
115
116 /* path too long, stat fails, or non-directory still exists */
117 *dir_gone = 0;
118 ret = 1;
119 break;
120 }
121 closedir(dir);
122
123 strbuf_setlen(path, original_len);
124
125 if (*dir_gone) {
126 res = dry_run ? 0 : rmdir(path->buf);
127 if (!res)
128 *dir_gone = 1;
129 else {
39598f99 130 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
131 warning(_(msg_warn_remove_failed), quoted.buf);
132 *dir_gone = 0;
133 ret = 1;
134 }
135 }
136
137 if (!*dir_gone && !quiet) {
138 for (i = 0; i < dels.nr; i++)
139 printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string);
140 }
141 string_list_clear(&dels, 0);
142 return ret;
143}
144
113f10f2
SB
145int cmd_clean(int argc, const char **argv, const char *prefix)
146{
f538a91e
ZK
147 int i, res;
148 int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0;
149 int ignored_only = 0, config_set = 0, errors = 0, gone = 1;
a0f4afbe 150 int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
f285a2d7 151 struct strbuf directory = STRBUF_INIT;
113f10f2 152 struct dir_struct dir;
113f10f2 153 static const char **pathspec;
f285a2d7 154 struct strbuf buf = STRBUF_INIT;
bdab6a59 155 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
72aeb187 156 struct exclude_list *el;
1fb32894 157 const char *qname;
d871c869 158 char *seen = NULL;
113f10f2 159 struct option options[] = {
145f9c81 160 OPT__QUIET(&quiet, N_("do not print names of files removed")),
f538a91e 161 OPT__DRY_RUN(&dry_run, N_("dry run")),
145f9c81 162 OPT__FORCE(&force, N_("force")),
113f10f2 163 OPT_BOOLEAN('d', NULL, &remove_directories,
145f9c81
NTND
164 N_("remove whole directories")),
165 { OPTION_CALLBACK, 'e', "exclude", &exclude_list, N_("pattern"),
166 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb },
167 OPT_BOOLEAN('x', NULL, &ignored, N_("remove ignored files, too")),
113f10f2 168 OPT_BOOLEAN('X', NULL, &ignored_only,
145f9c81 169 N_("remove only ignored files")),
113f10f2
SB
170 OPT_END()
171 };
172
ef90d6d4 173 git_config(git_clean_config, NULL);
625db1b7
JH
174 if (force < 0)
175 force = 0;
176 else
177 config_set = 1;
178
37782920
SB
179 argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
180 0);
113f10f2
SB
181
182 memset(&dir, 0, sizeof(dir));
1617adc7 183 if (ignored_only)
7c4c97c0 184 dir.flags |= DIR_SHOW_IGNORED;
113f10f2
SB
185
186 if (ignored && ignored_only)
2da57add 187 die(_("-x and -X cannot be used together"));
113f10f2 188
f538a91e 189 if (!dry_run && !force) {
a66f9b2a
ÆAB
190 if (config_set)
191 die(_("clean.requireForce set to true and neither -n nor -f given; "
192 "refusing to clean"));
193 else
194 die(_("clean.requireForce defaults to true and neither -n nor -f given; "
195 "refusing to clean"));
196 }
113f10f2 197
a0f4afbe
JH
198 if (force > 1)
199 rm_flags = 0;
200
7c4c97c0 201 dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
113f10f2 202
c28b3d6e 203 if (read_cache() < 0)
2da57add 204 die(_("index file corrupt"));
c28b3d6e 205
1617adc7
SB
206 if (!ignored)
207 setup_standard_excludes(&dir);
113f10f2 208
72aeb187 209 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
07de4eba 210 for (i = 0; i < exclude_list.nr; i++)
72aeb187 211 add_exclude(exclude_list.items[i].string, "", 0, el, -(i+1));
07de4eba 212
113f10f2 213 pathspec = get_pathspec(prefix, argv);
113f10f2 214
1d8842d9 215 fill_directory(&dir, pathspec);
113f10f2 216
d871c869 217 if (pathspec)
a8db80c2 218 seen = xmalloc(argc > 0 ? argc : 1);
d871c869
JH
219
220 for (i = 0; i < dir.nr; i++) {
221 struct dir_entry *ent = dir.entries[i];
f2d0df71
SB
222 int len, pos;
223 int matches = 0;
113f10f2
SB
224 struct cache_entry *ce;
225 struct stat st;
113f10f2
SB
226
227 /*
228 * Remove the '/' at the end that directory
229 * walking adds for directory entries.
230 */
231 len = ent->len;
232 if (len && ent->name[len-1] == '/')
233 len--;
234 pos = cache_name_pos(ent->name, len);
235 if (0 <= pos)
236 continue; /* exact match */
237 pos = -pos - 1;
238 if (pos < active_nr) {
239 ce = active_cache[pos];
240 if (ce_namelen(ce) == len &&
241 !memcmp(ce->name, ent->name, len))
242 continue; /* Yup, this one exists unmerged */
243 }
244
d871c869
JH
245 /*
246 * we might have removed this as part of earlier
247 * recursive directory removal, so lstat() here could
248 * fail with ENOENT.
249 */
250 if (lstat(ent->name, &st))
251 continue;
252
253 if (pathspec) {
a8db80c2 254 memset(seen, 0, argc > 0 ? argc : 1);
f2d0df71 255 matches = match_pathspec(pathspec, ent->name, len,
1c7d402b 256 0, seen);
d871c869
JH
257 }
258
259 if (S_ISDIR(st.st_mode)) {
113f10f2 260 strbuf_addstr(&directory, ent->name);
f538a91e
ZK
261 if (remove_directories || (matches == MATCHED_EXACTLY)) {
262 if (remove_dirs(&directory, prefix, rm_flags, dry_run, quiet, &gone))
aa9c83c2 263 errors++;
f538a91e 264 if (gone && !quiet) {
39598f99 265 qname = quote_path_relative(directory.buf, prefix, &buf);
f538a91e 266 printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
aa9c83c2 267 }
113f10f2
SB
268 }
269 strbuf_reset(&directory);
270 } else {
d871c869
JH
271 if (pathspec && !matches)
272 continue;
f538a91e
ZK
273 res = dry_run ? 0 : unlink(ent->name);
274 if (res) {
39598f99 275 qname = quote_path_relative(ent->name, prefix, &buf);
f538a91e 276 warning(_(msg_warn_remove_failed), qname);
aa9c83c2 277 errors++;
f538a91e 278 } else if (!quiet) {
39598f99 279 qname = quote_path_relative(ent->name, prefix, &buf);
f538a91e 280 printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
aa9c83c2 281 }
113f10f2
SB
282 }
283 }
d871c869 284 free(seen);
113f10f2
SB
285
286 strbuf_release(&directory);
07de4eba 287 string_list_clear(&exclude_list, 0);
aa9c83c2 288 return (errors != 0);
113f10f2 289}