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