]>
Commit | Line | Data |
---|---|---|
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 | 15 | static int force = -1; /* unset */ |
113f10f2 SB |
16 | |
17 | static const char *const builtin_clean_usage[] = { | |
1b1dd23f | 18 | "git clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...", |
113f10f2 SB |
19 | NULL |
20 | }; | |
21 | ||
ef90d6d4 | 22 | static 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 | ||
29 | int 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 | 35 | struct dir_struct dir; |
113f10f2 | 36 | static const char **pathspec; |
f285a2d7 | 37 | struct strbuf buf = STRBUF_INIT; |
1fb32894 | 38 | const char *qname; |
d871c869 | 39 | char *seen = NULL; |
113f10f2 SB |
40 | struct option options[] = { |
41 | OPT__QUIET(&quiet), | |
42 | OPT__DRY_RUN(&show_only), | |
43 | OPT_BOOLEAN('f', NULL, &force, "force"), | |
44 | OPT_BOOLEAN('d', NULL, &remove_directories, | |
45 | "remove whole directories"), | |
46 | OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"), | |
47 | OPT_BOOLEAN('X', NULL, &ignored_only, | |
48 | "remove only ignored files"), | |
49 | OPT_END() | |
50 | }; | |
51 | ||
ef90d6d4 | 52 | git_config(git_clean_config, NULL); |
625db1b7 JH |
53 | if (force < 0) |
54 | force = 0; | |
55 | else | |
56 | config_set = 1; | |
57 | ||
37782920 SB |
58 | argc = parse_options(argc, argv, prefix, options, builtin_clean_usage, |
59 | 0); | |
113f10f2 SB |
60 | |
61 | memset(&dir, 0, sizeof(dir)); | |
1617adc7 | 62 | if (ignored_only) |
7c4c97c0 | 63 | dir.flags |= DIR_SHOW_IGNORED; |
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 | 71 | |
7c4c97c0 | 72 | dir.flags |= DIR_SHOW_OTHER_DIRECTORIES; |
113f10f2 | 73 | |
1617adc7 SB |
74 | if (!ignored) |
75 | setup_standard_excludes(&dir); | |
113f10f2 SB |
76 | |
77 | pathspec = get_pathspec(prefix, argv); | |
78 | read_cache(); | |
79 | ||
1d8842d9 | 80 | fill_directory(&dir, pathspec); |
113f10f2 | 81 | |
d871c869 | 82 | if (pathspec) |
a8db80c2 | 83 | seen = xmalloc(argc > 0 ? argc : 1); |
d871c869 JH |
84 | |
85 | for (i = 0; i < dir.nr; i++) { | |
86 | struct dir_entry *ent = dir.entries[i]; | |
f2d0df71 SB |
87 | int len, pos; |
88 | int matches = 0; | |
113f10f2 SB |
89 | struct cache_entry *ce; |
90 | struct stat st; | |
113f10f2 SB |
91 | |
92 | /* | |
93 | * Remove the '/' at the end that directory | |
94 | * walking adds for directory entries. | |
95 | */ | |
96 | len = ent->len; | |
97 | if (len && ent->name[len-1] == '/') | |
98 | len--; | |
99 | pos = cache_name_pos(ent->name, len); | |
100 | if (0 <= pos) | |
101 | continue; /* exact match */ | |
102 | pos = -pos - 1; | |
103 | if (pos < active_nr) { | |
104 | ce = active_cache[pos]; | |
105 | if (ce_namelen(ce) == len && | |
106 | !memcmp(ce->name, ent->name, len)) | |
107 | continue; /* Yup, this one exists unmerged */ | |
108 | } | |
109 | ||
d871c869 JH |
110 | /* |
111 | * we might have removed this as part of earlier | |
112 | * recursive directory removal, so lstat() here could | |
113 | * fail with ENOENT. | |
114 | */ | |
115 | if (lstat(ent->name, &st)) | |
116 | continue; | |
117 | ||
118 | if (pathspec) { | |
a8db80c2 | 119 | memset(seen, 0, argc > 0 ? argc : 1); |
f2d0df71 | 120 | matches = match_pathspec(pathspec, ent->name, len, |
d871c869 | 121 | baselen, seen); |
d871c869 JH |
122 | } |
123 | ||
124 | if (S_ISDIR(st.st_mode)) { | |
113f10f2 | 125 | strbuf_addstr(&directory, ent->name); |
1fb32894 | 126 | qname = quote_path_relative(directory.buf, directory.len, &buf, prefix); |
f2d0df71 SB |
127 | if (show_only && (remove_directories || |
128 | (matches == MATCHED_EXACTLY))) { | |
1fb32894 | 129 | printf("Would remove %s\n", qname); |
f2d0df71 SB |
130 | } else if (remove_directories || |
131 | (matches == MATCHED_EXACTLY)) { | |
aa9c83c2 | 132 | if (!quiet) |
1fb32894 | 133 | printf("Removing %s\n", qname); |
aa9c83c2 | 134 | if (remove_dir_recursively(&directory, 0) != 0) { |
1fb32894 | 135 | warning("failed to remove '%s'", qname); |
aa9c83c2 MV |
136 | errors++; |
137 | } | |
113f10f2 | 138 | } else if (show_only) { |
1fb32894 | 139 | printf("Would not remove %s\n", qname); |
113f10f2 | 140 | } else { |
1fb32894 | 141 | printf("Not removing %s\n", qname); |
113f10f2 SB |
142 | } |
143 | strbuf_reset(&directory); | |
144 | } else { | |
d871c869 JH |
145 | if (pathspec && !matches) |
146 | continue; | |
1fb32894 | 147 | qname = quote_path_relative(ent->name, -1, &buf, prefix); |
113f10f2 | 148 | if (show_only) { |
1fb32894 | 149 | printf("Would remove %s\n", qname); |
113f10f2 SB |
150 | continue; |
151 | } else if (!quiet) { | |
1fb32894 | 152 | printf("Removing %s\n", qname); |
113f10f2 | 153 | } |
aa9c83c2 | 154 | if (unlink(ent->name) != 0) { |
1fb32894 | 155 | warning("failed to remove '%s'", qname); |
aa9c83c2 MV |
156 | errors++; |
157 | } | |
113f10f2 SB |
158 | } |
159 | } | |
d871c869 | 160 | free(seen); |
113f10f2 SB |
161 | |
162 | strbuf_release(&directory); | |
aa9c83c2 | 163 | return (errors != 0); |
113f10f2 | 164 | } |