]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-clean.c
Increase the size of the die/warning buffer to avoid truncation
[thirdparty/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"
1fb32894 13#include "quote.h"
113f10f2 14
625db1b7 15static int force = -1; /* unset */
113f10f2
SB
16
17static const char *const builtin_clean_usage[] = {
1b1dd23f 18 "git clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
113f10f2
SB
19 NULL
20};
21
ef90d6d4 22static int git_clean_config(const char *var, const char *value, void *cb)
113f10f2
SB
23{
24 if (!strcmp(var, "clean.requireforce"))
25 force = !git_config_bool(var, value);
ef90d6d4 26 return git_default_config(var, value, cb);
113f10f2
SB
27}
28
29int cmd_clean(int argc, const char **argv, const char *prefix)
30{
d871c869 31 int i;
113f10f2 32 int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
aa9c83c2 33 int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
f285a2d7 34 struct strbuf directory = STRBUF_INIT;
113f10f2
SB
35 struct dir_struct dir;
36 const char *path, *base;
37 static const char **pathspec;
f285a2d7 38 struct strbuf buf = STRBUF_INIT;
1fb32894 39 const char *qname;
d871c869 40 char *seen = NULL;
113f10f2
SB
41 struct option options[] = {
42 OPT__QUIET(&quiet),
43 OPT__DRY_RUN(&show_only),
44 OPT_BOOLEAN('f', NULL, &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"),
50 OPT_END()
51 };
52
ef90d6d4 53 git_config(git_clean_config, NULL);
625db1b7
JH
54 if (force < 0)
55 force = 0;
56 else
57 config_set = 1;
58
113f10f2
SB
59 argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
60
61 memset(&dir, 0, sizeof(dir));
1617adc7
SB
62 if (ignored_only)
63 dir.show_ignored = 1;
113f10f2
SB
64
65 if (ignored && ignored_only)
66 die("-x and -X cannot be used together");
67
68 if (!show_only && !force)
625db1b7
JH
69 die("clean.requireForce%s set and -n or -f not given; "
70 "refusing to clean", config_set ? "" : " not");
113f10f2
SB
71
72 dir.show_other_directories = 1;
73
1617adc7
SB
74 if (!ignored)
75 setup_standard_excludes(&dir);
113f10f2
SB
76
77 pathspec = get_pathspec(prefix, argv);
78 read_cache();
79
80 /*
81 * Calculate common prefix for the pathspec, and
82 * use that to optimize the directory walk
83 */
84 baselen = common_prefix(pathspec);
85 path = ".";
86 base = "";
87 if (baselen)
88 path = base = xmemdupz(*pathspec, baselen);
89 read_directory(&dir, path, base, baselen, pathspec);
113f10f2 90
d871c869 91 if (pathspec)
a8db80c2 92 seen = xmalloc(argc > 0 ? argc : 1);
d871c869
JH
93
94 for (i = 0; i < dir.nr; i++) {
95 struct dir_entry *ent = dir.entries[i];
f2d0df71
SB
96 int len, pos;
97 int matches = 0;
113f10f2
SB
98 struct cache_entry *ce;
99 struct stat st;
113f10f2
SB
100
101 /*
102 * Remove the '/' at the end that directory
103 * walking adds for directory entries.
104 */
105 len = ent->len;
106 if (len && ent->name[len-1] == '/')
107 len--;
108 pos = cache_name_pos(ent->name, len);
109 if (0 <= pos)
110 continue; /* exact match */
111 pos = -pos - 1;
112 if (pos < active_nr) {
113 ce = active_cache[pos];
114 if (ce_namelen(ce) == len &&
115 !memcmp(ce->name, ent->name, len))
116 continue; /* Yup, this one exists unmerged */
117 }
118
d871c869
JH
119 /*
120 * we might have removed this as part of earlier
121 * recursive directory removal, so lstat() here could
122 * fail with ENOENT.
123 */
124 if (lstat(ent->name, &st))
125 continue;
126
127 if (pathspec) {
a8db80c2 128 memset(seen, 0, argc > 0 ? argc : 1);
f2d0df71 129 matches = match_pathspec(pathspec, ent->name, len,
d871c869 130 baselen, seen);
d871c869
JH
131 }
132
133 if (S_ISDIR(st.st_mode)) {
113f10f2 134 strbuf_addstr(&directory, ent->name);
1fb32894 135 qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
f2d0df71
SB
136 if (show_only && (remove_directories ||
137 (matches == MATCHED_EXACTLY))) {
1fb32894 138 printf("Would remove %s\n", qname);
f2d0df71
SB
139 } else if (remove_directories ||
140 (matches == MATCHED_EXACTLY)) {
aa9c83c2 141 if (!quiet)
1fb32894 142 printf("Removing %s\n", qname);
aa9c83c2 143 if (remove_dir_recursively(&directory, 0) != 0) {
1fb32894 144 warning("failed to remove '%s'", qname);
aa9c83c2
MV
145 errors++;
146 }
113f10f2 147 } else if (show_only) {
1fb32894 148 printf("Would not remove %s\n", qname);
113f10f2 149 } else {
1fb32894 150 printf("Not removing %s\n", qname);
113f10f2
SB
151 }
152 strbuf_reset(&directory);
153 } else {
d871c869
JH
154 if (pathspec && !matches)
155 continue;
1fb32894 156 qname = quote_path_relative(ent->name, -1, &buf, prefix);
113f10f2 157 if (show_only) {
1fb32894 158 printf("Would remove %s\n", qname);
113f10f2
SB
159 continue;
160 } else if (!quiet) {
1fb32894 161 printf("Removing %s\n", qname);
113f10f2 162 }
aa9c83c2 163 if (unlink(ent->name) != 0) {
1fb32894 164 warning("failed to remove '%s'", qname);
aa9c83c2
MV
165 errors++;
166 }
113f10f2
SB
167 }
168 }
d871c869 169 free(seen);
113f10f2
SB
170
171 strbuf_release(&directory);
aa9c83c2 172 return (errors != 0);
113f10f2 173}