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"
14 #include "string-list.h"
17 static int force
= -1; /* unset */
19 static const char *const builtin_clean_usage
[] = {
20 N_("git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
24 static const char *msg_remove
= N_("Removing %s\n");
25 static const char *msg_would_remove
= N_("Would remove %s\n");
26 static const char *msg_skip_git_dir
= N_("Skipping repository %s\n");
27 static const char *msg_would_skip_git_dir
= N_("Would skip repository %s\n");
28 static const char *msg_warn_remove_failed
= N_("failed to remove %s");
30 static int git_clean_config(const char *var
, const char *value
, void *cb
)
32 if (!strcmp(var
, "clean.requireforce"))
33 force
= !git_config_bool(var
, value
);
34 return git_default_config(var
, value
, cb
);
37 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
39 struct string_list
*exclude_list
= opt
->value
;
40 string_list_append(exclude_list
, arg
);
44 static int remove_dirs(struct strbuf
*path
, const char *prefix
, int force_flag
,
45 int dry_run
, int quiet
, int *dir_gone
)
48 struct strbuf quoted
= STRBUF_INIT
;
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
;
56 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
57 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
59 quote_path_relative(path
->buf
, strlen(path
->buf
), "ed
, prefix
);
60 printf(dry_run
? _(msg_would_skip_git_dir
) : _(msg_skip_git_dir
),
68 dir
= opendir(path
->buf
);
70 /* an empty dir could be removed even if it is unreadble */
71 res
= dry_run
? 0 : rmdir(path
->buf
);
73 quote_path_relative(path
->buf
, strlen(path
->buf
), "ed
, prefix
);
74 warning(_(msg_warn_remove_failed
), quoted
.buf
);
80 if (path
->buf
[original_len
- 1] != '/')
81 strbuf_addch(path
, '/');
84 while ((e
= readdir(dir
)) != NULL
) {
86 if (is_dot_or_dotdot(e
->d_name
))
89 strbuf_setlen(path
, len
);
90 strbuf_addstr(path
, e
->d_name
);
91 if (lstat(path
->buf
, &st
))
93 else if (S_ISDIR(st
.st_mode
)) {
94 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
97 quote_path_relative(path
->buf
, strlen(path
->buf
), "ed
, prefix
);
98 string_list_append(&dels
, quoted
.buf
);
103 res
= dry_run
? 0 : unlink(path
->buf
);
105 quote_path_relative(path
->buf
, strlen(path
->buf
), "ed
, prefix
);
106 string_list_append(&dels
, quoted
.buf
);
108 quote_path_relative(path
->buf
, strlen(path
->buf
), "ed
, prefix
);
109 warning(_(msg_warn_remove_failed
), quoted
.buf
);
116 /* path too long, stat fails, or non-directory still exists */
123 strbuf_setlen(path
, original_len
);
126 res
= dry_run
? 0 : rmdir(path
->buf
);
130 quote_path_relative(path
->buf
, strlen(path
->buf
), "ed
, prefix
);
131 warning(_(msg_warn_remove_failed
), quoted
.buf
);
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
);
141 string_list_clear(&dels
, 0);
145 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
148 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
149 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
150 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
151 struct strbuf directory
= STRBUF_INIT
;
152 struct dir_struct dir
;
153 static const char **pathspec
;
154 struct strbuf buf
= STRBUF_INIT
;
155 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
156 struct exclude_list
*el
;
159 struct option options
[] = {
160 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
161 OPT__DRY_RUN(&dry_run
, N_("dry run")),
162 OPT__FORCE(&force
, N_("force")),
163 OPT_BOOLEAN('d', NULL
, &remove_directories
,
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")),
168 OPT_BOOLEAN('X', NULL
, &ignored_only
,
169 N_("remove only ignored files")),
173 git_config(git_clean_config
, NULL
);
179 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
182 memset(&dir
, 0, sizeof(dir
));
184 dir
.flags
|= DIR_SHOW_IGNORED
;
186 if (ignored
&& ignored_only
)
187 die(_("-x and -X cannot be used together"));
189 if (!dry_run
&& !force
) {
191 die(_("clean.requireForce set to true and neither -n nor -f given; "
192 "refusing to clean"));
194 die(_("clean.requireForce defaults to true and neither -n nor -f given; "
195 "refusing to clean"));
201 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
203 if (read_cache() < 0)
204 die(_("index file corrupt"));
207 setup_standard_excludes(&dir
);
209 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
210 for (i
= 0; i
< exclude_list
.nr
; i
++)
211 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
213 pathspec
= get_pathspec(prefix
, argv
);
215 fill_directory(&dir
, pathspec
);
218 seen
= xmalloc(argc
> 0 ? argc
: 1);
220 for (i
= 0; i
< dir
.nr
; i
++) {
221 struct dir_entry
*ent
= dir
.entries
[i
];
224 struct cache_entry
*ce
;
228 * Remove the '/' at the end that directory
229 * walking adds for directory entries.
232 if (len
&& ent
->name
[len
-1] == '/')
234 pos
= cache_name_pos(ent
->name
, len
);
236 continue; /* exact match */
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 */
246 * we might have removed this as part of earlier
247 * recursive directory removal, so lstat() here could
250 if (lstat(ent
->name
, &st
))
254 memset(seen
, 0, argc
> 0 ? argc
: 1);
255 matches
= match_pathspec(pathspec
, ent
->name
, len
,
259 if (S_ISDIR(st
.st_mode
)) {
260 strbuf_addstr(&directory
, ent
->name
);
261 if (remove_directories
|| (matches
== MATCHED_EXACTLY
)) {
262 if (remove_dirs(&directory
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
264 if (gone
&& !quiet
) {
265 qname
= quote_path_relative(directory
.buf
, directory
.len
, &buf
, prefix
);
266 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
269 strbuf_reset(&directory
);
271 if (pathspec
&& !matches
)
273 res
= dry_run
? 0 : unlink(ent
->name
);
275 qname
= quote_path_relative(ent
->name
, -1, &buf
, prefix
);
276 warning(_(msg_warn_remove_failed
), qname
);
279 qname
= quote_path_relative(ent
->name
, -1, &buf
, prefix
);
280 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
286 strbuf_release(&directory
);
287 string_list_clear(&exclude_list
, 0);
288 return (errors
!= 0);