Add CRLF after Content-Length header.
[git/gitweb-warthdog9.git] / builtin / clean.c
blobc8798f549ed12192a098ab90b35c0437b8059351
1 /*
2 * "git clean" builtin command
4 * Copyright (C) 2007 Shawn Bohrer
6 * Based on git-clean.sh by Pavel Roskin
7 */
9 #include "builtin.h"
10 #include "cache.h"
11 #include "dir.h"
12 #include "parse-options.h"
13 #include "string-list.h"
14 #include "quote.h"
16 static int force = -1; /* unset */
18 static const char *const builtin_clean_usage[] = {
19 "git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...",
20 NULL
23 static int git_clean_config(const char *var, const char *value, void *cb)
25 if (!strcmp(var, "clean.requireforce"))
26 force = !git_config_bool(var, value);
27 return git_default_config(var, value, cb);
30 static int exclude_cb(const struct option *opt, const char *arg, int unset)
32 struct string_list *exclude_list = opt->value;
33 string_list_append(exclude_list, arg);
34 return 0;
37 int cmd_clean(int argc, const char **argv, const char *prefix)
39 int i;
40 int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
41 int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
42 int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
43 struct strbuf directory = STRBUF_INIT;
44 struct dir_struct dir;
45 static const char **pathspec;
46 struct strbuf buf = STRBUF_INIT;
47 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
48 const char *qname;
49 char *seen = NULL;
50 struct option options[] = {
51 OPT__QUIET(&quiet),
52 OPT__DRY_RUN(&show_only),
53 OPT_BOOLEAN('f', "force", &force, "force"),
54 OPT_BOOLEAN('d', NULL, &remove_directories,
55 "remove whole directories"),
56 { OPTION_CALLBACK, 'e', "exclude", &exclude_list, "pattern",
57 "exclude <pattern>", PARSE_OPT_NONEG, exclude_cb },
58 OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
59 OPT_BOOLEAN('X', NULL, &ignored_only,
60 "remove only ignored files"),
61 OPT_END()
64 git_config(git_clean_config, NULL);
65 if (force < 0)
66 force = 0;
67 else
68 config_set = 1;
70 argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
71 0);
73 memset(&dir, 0, sizeof(dir));
74 if (ignored_only)
75 dir.flags |= DIR_SHOW_IGNORED;
77 if (ignored && ignored_only)
78 die("-x and -X cannot be used together");
80 if (!show_only && !force)
81 die("clean.requireForce %s to true and neither -n nor -f given; "
82 "refusing to clean", config_set ? "set" : "defaults");
84 if (force > 1)
85 rm_flags = 0;
87 dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
89 if (read_cache() < 0)
90 die("index file corrupt");
92 if (!ignored)
93 setup_standard_excludes(&dir);
95 for (i = 0; i < exclude_list.nr; i++)
96 add_exclude(exclude_list.items[i].string, "", 0, dir.exclude_list);
98 pathspec = get_pathspec(prefix, argv);
100 fill_directory(&dir, pathspec);
102 if (pathspec)
103 seen = xmalloc(argc > 0 ? argc : 1);
105 for (i = 0; i < dir.nr; i++) {
106 struct dir_entry *ent = dir.entries[i];
107 int len, pos;
108 int matches = 0;
109 struct cache_entry *ce;
110 struct stat st;
113 * Remove the '/' at the end that directory
114 * walking adds for directory entries.
116 len = ent->len;
117 if (len && ent->name[len-1] == '/')
118 len--;
119 pos = cache_name_pos(ent->name, len);
120 if (0 <= pos)
121 continue; /* exact match */
122 pos = -pos - 1;
123 if (pos < active_nr) {
124 ce = active_cache[pos];
125 if (ce_namelen(ce) == len &&
126 !memcmp(ce->name, ent->name, len))
127 continue; /* Yup, this one exists unmerged */
131 * we might have removed this as part of earlier
132 * recursive directory removal, so lstat() here could
133 * fail with ENOENT.
135 if (lstat(ent->name, &st))
136 continue;
138 if (pathspec) {
139 memset(seen, 0, argc > 0 ? argc : 1);
140 matches = match_pathspec(pathspec, ent->name, len,
141 baselen, seen);
144 if (S_ISDIR(st.st_mode)) {
145 strbuf_addstr(&directory, ent->name);
146 qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
147 if (show_only && (remove_directories ||
148 (matches == MATCHED_EXACTLY))) {
149 printf("Would remove %s\n", qname);
150 } else if (remove_directories ||
151 (matches == MATCHED_EXACTLY)) {
152 if (!quiet)
153 printf("Removing %s\n", qname);
154 if (remove_dir_recursively(&directory,
155 rm_flags) != 0) {
156 warning("failed to remove '%s'", qname);
157 errors++;
159 } else if (show_only) {
160 printf("Would not remove %s\n", qname);
161 } else {
162 printf("Not removing %s\n", qname);
164 strbuf_reset(&directory);
165 } else {
166 if (pathspec && !matches)
167 continue;
168 qname = quote_path_relative(ent->name, -1, &buf, prefix);
169 if (show_only) {
170 printf("Would remove %s\n", qname);
171 continue;
172 } else if (!quiet) {
173 printf("Removing %s\n", qname);
175 if (unlink(ent->name) != 0) {
176 warning("failed to remove '%s'", qname);
177 errors++;
181 free(seen);
183 strbuf_release(&directory);
184 string_list_clear(&exclude_list, 0);
185 return (errors != 0);