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 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
35 struct strbuf directory
= STRBUF_INIT
;
36 struct dir_struct dir
;
37 static const char **pathspec
;
38 struct strbuf buf
= STRBUF_INIT
;
41 struct option options
[] = {
43 OPT__DRY_RUN(&show_only
),
44 OPT_BOOLEAN('f', "force", &force
, "force"),
45 OPT_BOOLEAN('d', NULL
, &remove_directories
,
46 "remove whole directories"),
47 OPT_BOOLEAN('x', NULL
, &ignored
, "remove ignored files, too"),
48 OPT_BOOLEAN('X', NULL
, &ignored_only
,
49 "remove only ignored files"),
53 git_config(git_clean_config
, NULL
);
59 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
62 memset(&dir
, 0, sizeof(dir
));
64 dir
.flags
|= DIR_SHOW_IGNORED
;
66 if (ignored
&& ignored_only
)
67 die("-x and -X cannot be used together");
69 if (!show_only
&& !force
)
70 die("clean.requireForce%s set and -n or -f not given; "
71 "refusing to clean", config_set
? "" : " not");
76 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
79 die("index file corrupt");
82 setup_standard_excludes(&dir
);
84 pathspec
= get_pathspec(prefix
, argv
);
86 fill_directory(&dir
, pathspec
);
89 seen
= xmalloc(argc
> 0 ? argc
: 1);
91 for (i
= 0; i
< dir
.nr
; i
++) {
92 struct dir_entry
*ent
= dir
.entries
[i
];
95 struct cache_entry
*ce
;
99 * Remove the '/' at the end that directory
100 * walking adds for directory entries.
103 if (len
&& ent
->name
[len
-1] == '/')
105 pos
= cache_name_pos(ent
->name
, len
);
107 continue; /* exact match */
109 if (pos
< active_nr
) {
110 ce
= active_cache
[pos
];
111 if (ce_namelen(ce
) == len
&&
112 !memcmp(ce
->name
, ent
->name
, len
))
113 continue; /* Yup, this one exists unmerged */
117 * we might have removed this as part of earlier
118 * recursive directory removal, so lstat() here could
121 if (lstat(ent
->name
, &st
))
125 memset(seen
, 0, argc
> 0 ? argc
: 1);
126 matches
= match_pathspec(pathspec
, ent
->name
, len
,
130 if (S_ISDIR(st
.st_mode
)) {
131 strbuf_addstr(&directory
, ent
->name
);
132 qname
= quote_path_relative(directory
.buf
, directory
.len
, &buf
, prefix
);
133 if (show_only
&& (remove_directories
||
134 (matches
== MATCHED_EXACTLY
))) {
135 printf("Would remove %s\n", qname
);
136 } else if (remove_directories
||
137 (matches
== MATCHED_EXACTLY
)) {
139 printf("Removing %s\n", qname
);
140 if (remove_dir_recursively(&directory
,
142 warning("failed to remove '%s'", qname
);
145 } else if (show_only
) {
146 printf("Would not remove %s\n", qname
);
148 printf("Not removing %s\n", qname
);
150 strbuf_reset(&directory
);
152 if (pathspec
&& !matches
)
154 qname
= quote_path_relative(ent
->name
, -1, &buf
, prefix
);
156 printf("Would remove %s\n", qname
);
159 printf("Removing %s\n", qname
);
161 if (unlink(ent
->name
) != 0) {
162 warning("failed to remove '%s'", qname
);
169 strbuf_release(&directory
);
170 return (errors
!= 0);