Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 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" |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 13 | #include "refs.h" |
Jared Hance | 07de4eb | 2010-07-20 15:35:56 -0400 | [diff] [blame] | 14 | #include "string-list.h" |
Dmitry Potapov | 1fb3289 | 2008-03-07 04:13:17 +0300 | [diff] [blame] | 15 | #include "quote.h" |
Jiang Xin | 1b8fd46 | 2013-06-25 23:53:49 +0800 | [diff] [blame] | 16 | #include "column.h" |
Jiang Xin | 7a9b0b8 | 2013-06-25 23:53:50 +0800 | [diff] [blame] | 17 | #include "color.h" |
Nguyễn Thái Ngọc Duy | 893d839 | 2013-07-14 15:35:37 +0700 | [diff] [blame] | 18 | #include "pathspec.h" |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 19 | |
Junio C Hamano | 625db1b | 2007-11-12 21:13:05 -0800 | [diff] [blame] | 20 | static int force = -1; /* unset */ |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 21 | static int interactive; |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 22 | static struct string_list del_list = STRING_LIST_INIT_DUP; |
Jiang Xin | 1b8fd46 | 2013-06-25 23:53:49 +0800 | [diff] [blame] | 23 | static unsigned int colopts; |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 24 | |
| 25 | static const char *const builtin_clean_usage[] = { |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 26 | N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."), |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 27 | NULL |
| 28 | }; |
| 29 | |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 30 | static const char *msg_remove = N_("Removing %s\n"); |
| 31 | static const char *msg_would_remove = N_("Would remove %s\n"); |
| 32 | static const char *msg_skip_git_dir = N_("Skipping repository %s\n"); |
| 33 | static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n"); |
| 34 | static const char *msg_warn_remove_failed = N_("failed to remove %s"); |
| 35 | |
Jiang Xin | 7a9b0b8 | 2013-06-25 23:53:50 +0800 | [diff] [blame] | 36 | static int clean_use_color = -1; |
| 37 | static char clean_colors[][COLOR_MAXLEN] = { |
| 38 | GIT_COLOR_RESET, |
| 39 | GIT_COLOR_NORMAL, /* PLAIN */ |
| 40 | GIT_COLOR_BOLD_BLUE, /* PROMPT */ |
| 41 | GIT_COLOR_BOLD, /* HEADER */ |
| 42 | GIT_COLOR_BOLD_RED, /* HELP */ |
| 43 | GIT_COLOR_BOLD_RED, /* ERROR */ |
| 44 | }; |
| 45 | enum color_clean { |
| 46 | CLEAN_COLOR_RESET = 0, |
| 47 | CLEAN_COLOR_PLAIN = 1, |
| 48 | CLEAN_COLOR_PROMPT = 2, |
| 49 | CLEAN_COLOR_HEADER = 3, |
| 50 | CLEAN_COLOR_HELP = 4, |
| 51 | CLEAN_COLOR_ERROR = 5, |
| 52 | }; |
| 53 | |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 54 | #define MENU_OPTS_SINGLETON 01 |
| 55 | #define MENU_OPTS_IMMEDIATE 02 |
| 56 | #define MENU_OPTS_LIST_ONLY 04 |
| 57 | |
| 58 | struct menu_opts { |
| 59 | const char *header; |
| 60 | const char *prompt; |
| 61 | int flags; |
| 62 | }; |
| 63 | |
| 64 | #define MENU_RETURN_NO_LOOP 10 |
| 65 | |
| 66 | struct menu_item { |
| 67 | char hotkey; |
| 68 | const char *title; |
| 69 | int selected; |
| 70 | int (*fn)(); |
| 71 | }; |
| 72 | |
| 73 | enum menu_stuff_type { |
| 74 | MENU_STUFF_TYPE_STRING_LIST = 1, |
| 75 | MENU_STUFF_TYPE_MENU_ITEM |
| 76 | }; |
| 77 | |
| 78 | struct menu_stuff { |
| 79 | enum menu_stuff_type type; |
| 80 | int nr; |
| 81 | void *stuff; |
| 82 | }; |
| 83 | |
Jiang Xin | 7a9b0b8 | 2013-06-25 23:53:50 +0800 | [diff] [blame] | 84 | static int parse_clean_color_slot(const char *var) |
| 85 | { |
| 86 | if (!strcasecmp(var, "reset")) |
| 87 | return CLEAN_COLOR_RESET; |
| 88 | if (!strcasecmp(var, "plain")) |
| 89 | return CLEAN_COLOR_PLAIN; |
| 90 | if (!strcasecmp(var, "prompt")) |
| 91 | return CLEAN_COLOR_PROMPT; |
| 92 | if (!strcasecmp(var, "header")) |
| 93 | return CLEAN_COLOR_HEADER; |
| 94 | if (!strcasecmp(var, "help")) |
| 95 | return CLEAN_COLOR_HELP; |
| 96 | if (!strcasecmp(var, "error")) |
| 97 | return CLEAN_COLOR_ERROR; |
| 98 | return -1; |
| 99 | } |
| 100 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 101 | static int git_clean_config(const char *var, const char *value, void *cb) |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 102 | { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 103 | if (starts_with(var, "column.")) |
Jiang Xin | 1b8fd46 | 2013-06-25 23:53:49 +0800 | [diff] [blame] | 104 | return git_column_config(var, value, "clean", &colopts); |
| 105 | |
Jiang Xin | 7a9b0b8 | 2013-06-25 23:53:50 +0800 | [diff] [blame] | 106 | /* honors the color.interactive* config variables which also |
| 107 | applied in git-add--interactive and git-stash */ |
| 108 | if (!strcmp(var, "color.interactive")) { |
| 109 | clean_use_color = git_config_colorbool(var, value); |
| 110 | return 0; |
| 111 | } |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 112 | if (starts_with(var, "color.interactive.")) { |
Jiang Xin | 7a9b0b8 | 2013-06-25 23:53:50 +0800 | [diff] [blame] | 113 | int slot = parse_clean_color_slot(var + |
| 114 | strlen("color.interactive.")); |
| 115 | if (slot < 0) |
| 116 | return 0; |
| 117 | if (!value) |
| 118 | return config_error_nonbool(var); |
| 119 | color_parse(value, var, clean_colors[slot]); |
| 120 | return 0; |
| 121 | } |
| 122 | |
Jiang Xin | 1b8fd46 | 2013-06-25 23:53:49 +0800 | [diff] [blame] | 123 | if (!strcmp(var, "clean.requireforce")) { |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 124 | force = !git_config_bool(var, value); |
Jiang Xin | 1b8fd46 | 2013-06-25 23:53:49 +0800 | [diff] [blame] | 125 | return 0; |
| 126 | } |
Jiang Xin | 7a9b0b8 | 2013-06-25 23:53:50 +0800 | [diff] [blame] | 127 | |
| 128 | /* inspect the color.ui config variable and others */ |
| 129 | return git_color_default_config(var, value, cb); |
| 130 | } |
| 131 | |
| 132 | static const char *clean_get_color(enum color_clean ix) |
| 133 | { |
| 134 | if (want_color(clean_use_color)) |
| 135 | return clean_colors[ix]; |
| 136 | return ""; |
| 137 | } |
| 138 | |
| 139 | static void clean_print_color(enum color_clean ix) |
| 140 | { |
| 141 | printf("%s", clean_get_color(ix)); |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 142 | } |
| 143 | |
Jared Hance | 07de4eb | 2010-07-20 15:35:56 -0400 | [diff] [blame] | 144 | static int exclude_cb(const struct option *opt, const char *arg, int unset) |
| 145 | { |
| 146 | struct string_list *exclude_list = opt->value; |
| 147 | string_list_append(exclude_list, arg); |
| 148 | return 0; |
| 149 | } |
| 150 | |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 151 | static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, |
| 152 | int dry_run, int quiet, int *dir_gone) |
| 153 | { |
| 154 | DIR *dir; |
| 155 | struct strbuf quoted = STRBUF_INIT; |
| 156 | struct dirent *e; |
| 157 | int res = 0, ret = 0, gone = 1, original_len = path->len, len, i; |
| 158 | unsigned char submodule_head[20]; |
| 159 | struct string_list dels = STRING_LIST_INIT_DUP; |
| 160 | |
| 161 | *dir_gone = 1; |
| 162 | |
| 163 | if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && |
| 164 | !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) { |
| 165 | if (!quiet) { |
Jiang Xin | 39598f9 | 2013-06-25 23:53:45 +0800 | [diff] [blame] | 166 | quote_path_relative(path->buf, prefix, "ed); |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 167 | printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir), |
| 168 | quoted.buf); |
| 169 | } |
| 170 | |
| 171 | *dir_gone = 0; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | dir = opendir(path->buf); |
| 176 | if (!dir) { |
| 177 | /* an empty dir could be removed even if it is unreadble */ |
| 178 | res = dry_run ? 0 : rmdir(path->buf); |
| 179 | if (res) { |
Jiang Xin | 39598f9 | 2013-06-25 23:53:45 +0800 | [diff] [blame] | 180 | quote_path_relative(path->buf, prefix, "ed); |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 181 | warning(_(msg_warn_remove_failed), quoted.buf); |
| 182 | *dir_gone = 0; |
| 183 | } |
| 184 | return res; |
| 185 | } |
| 186 | |
| 187 | if (path->buf[original_len - 1] != '/') |
| 188 | strbuf_addch(path, '/'); |
| 189 | |
| 190 | len = path->len; |
| 191 | while ((e = readdir(dir)) != NULL) { |
| 192 | struct stat st; |
| 193 | if (is_dot_or_dotdot(e->d_name)) |
| 194 | continue; |
| 195 | |
| 196 | strbuf_setlen(path, len); |
| 197 | strbuf_addstr(path, e->d_name); |
| 198 | if (lstat(path->buf, &st)) |
| 199 | ; /* fall thru */ |
| 200 | else if (S_ISDIR(st.st_mode)) { |
| 201 | if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone)) |
| 202 | ret = 1; |
| 203 | if (gone) { |
Jiang Xin | 39598f9 | 2013-06-25 23:53:45 +0800 | [diff] [blame] | 204 | quote_path_relative(path->buf, prefix, "ed); |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 205 | string_list_append(&dels, quoted.buf); |
| 206 | } else |
| 207 | *dir_gone = 0; |
| 208 | continue; |
| 209 | } else { |
| 210 | res = dry_run ? 0 : unlink(path->buf); |
| 211 | if (!res) { |
Jiang Xin | 39598f9 | 2013-06-25 23:53:45 +0800 | [diff] [blame] | 212 | quote_path_relative(path->buf, prefix, "ed); |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 213 | string_list_append(&dels, quoted.buf); |
| 214 | } else { |
Jiang Xin | 39598f9 | 2013-06-25 23:53:45 +0800 | [diff] [blame] | 215 | quote_path_relative(path->buf, prefix, "ed); |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 216 | warning(_(msg_warn_remove_failed), quoted.buf); |
| 217 | *dir_gone = 0; |
| 218 | ret = 1; |
| 219 | } |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | /* path too long, stat fails, or non-directory still exists */ |
| 224 | *dir_gone = 0; |
| 225 | ret = 1; |
| 226 | break; |
| 227 | } |
| 228 | closedir(dir); |
| 229 | |
| 230 | strbuf_setlen(path, original_len); |
| 231 | |
| 232 | if (*dir_gone) { |
| 233 | res = dry_run ? 0 : rmdir(path->buf); |
| 234 | if (!res) |
| 235 | *dir_gone = 1; |
| 236 | else { |
Jiang Xin | 39598f9 | 2013-06-25 23:53:45 +0800 | [diff] [blame] | 237 | quote_path_relative(path->buf, prefix, "ed); |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 238 | warning(_(msg_warn_remove_failed), quoted.buf); |
| 239 | *dir_gone = 0; |
| 240 | ret = 1; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (!*dir_gone && !quiet) { |
| 245 | for (i = 0; i < dels.nr; i++) |
| 246 | printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string); |
| 247 | } |
| 248 | string_list_clear(&dels, 0); |
| 249 | return ret; |
| 250 | } |
| 251 | |
Jiang Xin | 1b8fd46 | 2013-06-25 23:53:49 +0800 | [diff] [blame] | 252 | static void pretty_print_dels(void) |
| 253 | { |
| 254 | struct string_list list = STRING_LIST_INIT_DUP; |
| 255 | struct string_list_item *item; |
| 256 | struct strbuf buf = STRBUF_INIT; |
| 257 | const char *qname; |
| 258 | struct column_options copts; |
| 259 | |
| 260 | for_each_string_list_item(item, &del_list) { |
| 261 | qname = quote_path_relative(item->string, NULL, &buf); |
| 262 | string_list_append(&list, qname); |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * always enable column display, we only consult column.* |
| 267 | * about layout strategy and stuff |
| 268 | */ |
| 269 | colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED; |
| 270 | memset(&copts, 0, sizeof(copts)); |
| 271 | copts.indent = " "; |
| 272 | copts.padding = 2; |
| 273 | print_columns(&list, colopts, &copts); |
Jiang Xin | 1b8fd46 | 2013-06-25 23:53:49 +0800 | [diff] [blame] | 274 | strbuf_release(&buf); |
| 275 | string_list_clear(&list, 0); |
| 276 | } |
| 277 | |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 278 | static void pretty_print_menus(struct string_list *menu_list) |
| 279 | { |
| 280 | unsigned int local_colopts = 0; |
| 281 | struct column_options copts; |
| 282 | |
| 283 | local_colopts = COL_ENABLED | COL_ROW; |
| 284 | memset(&copts, 0, sizeof(copts)); |
| 285 | copts.indent = " "; |
| 286 | copts.padding = 2; |
| 287 | print_columns(menu_list, local_colopts, &copts); |
| 288 | } |
| 289 | |
| 290 | static void prompt_help_cmd(int singleton) |
| 291 | { |
| 292 | clean_print_color(CLEAN_COLOR_HELP); |
| 293 | printf_ln(singleton ? |
| 294 | _("Prompt help:\n" |
| 295 | "1 - select a numbered item\n" |
| 296 | "foo - select item based on unique prefix\n" |
| 297 | " - (empty) select nothing") : |
| 298 | _("Prompt help:\n" |
| 299 | "1 - select a single item\n" |
| 300 | "3-5 - select a range of items\n" |
| 301 | "2-3,6-9 - select multiple ranges\n" |
| 302 | "foo - select item based on unique prefix\n" |
| 303 | "-... - unselect specified items\n" |
| 304 | "* - choose all items\n" |
| 305 | " - (empty) finish selecting")); |
| 306 | clean_print_color(CLEAN_COLOR_RESET); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * display menu stuff with number prefix and hotkey highlight |
| 311 | */ |
| 312 | static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen) |
| 313 | { |
| 314 | struct string_list menu_list = STRING_LIST_INIT_DUP; |
| 315 | struct strbuf menu = STRBUF_INIT; |
| 316 | struct strbuf buf = STRBUF_INIT; |
| 317 | struct menu_item *menu_item; |
| 318 | struct string_list_item *string_list_item; |
| 319 | int i; |
| 320 | |
| 321 | switch (stuff->type) { |
| 322 | default: |
| 323 | die("Bad type of menu_staff when print menu"); |
| 324 | case MENU_STUFF_TYPE_MENU_ITEM: |
| 325 | menu_item = (struct menu_item *)stuff->stuff; |
| 326 | for (i = 0; i < stuff->nr; i++, menu_item++) { |
| 327 | const char *p; |
| 328 | int highlighted = 0; |
| 329 | |
| 330 | p = menu_item->title; |
| 331 | if ((*chosen)[i] < 0) |
| 332 | (*chosen)[i] = menu_item->selected ? 1 : 0; |
| 333 | strbuf_addf(&menu, "%s%2d: ", (*chosen)[i] ? "*" : " ", i+1); |
| 334 | for (; *p; p++) { |
| 335 | if (!highlighted && *p == menu_item->hotkey) { |
| 336 | strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_PROMPT)); |
| 337 | strbuf_addch(&menu, *p); |
| 338 | strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_RESET)); |
| 339 | highlighted = 1; |
| 340 | } else { |
| 341 | strbuf_addch(&menu, *p); |
| 342 | } |
| 343 | } |
| 344 | string_list_append(&menu_list, menu.buf); |
| 345 | strbuf_reset(&menu); |
| 346 | } |
| 347 | break; |
| 348 | case MENU_STUFF_TYPE_STRING_LIST: |
| 349 | i = 0; |
| 350 | for_each_string_list_item(string_list_item, (struct string_list *)stuff->stuff) { |
| 351 | if ((*chosen)[i] < 0) |
| 352 | (*chosen)[i] = 0; |
| 353 | strbuf_addf(&menu, "%s%2d: %s", |
| 354 | (*chosen)[i] ? "*" : " ", i+1, string_list_item->string); |
| 355 | string_list_append(&menu_list, menu.buf); |
| 356 | strbuf_reset(&menu); |
| 357 | i++; |
| 358 | } |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | pretty_print_menus(&menu_list); |
| 363 | |
| 364 | strbuf_release(&menu); |
| 365 | strbuf_release(&buf); |
| 366 | string_list_clear(&menu_list, 0); |
| 367 | } |
| 368 | |
Jiang Xin | 6083861 | 2013-07-24 10:22:04 +0800 | [diff] [blame] | 369 | static int find_unique(const char *choice, struct menu_stuff *menu_stuff) |
| 370 | { |
| 371 | struct menu_item *menu_item; |
| 372 | struct string_list_item *string_list_item; |
| 373 | int i, len, found = 0; |
| 374 | |
| 375 | len = strlen(choice); |
| 376 | switch (menu_stuff->type) { |
| 377 | default: |
| 378 | die("Bad type of menu_stuff when parse choice"); |
| 379 | case MENU_STUFF_TYPE_MENU_ITEM: |
| 380 | |
| 381 | menu_item = (struct menu_item *)menu_stuff->stuff; |
| 382 | for (i = 0; i < menu_stuff->nr; i++, menu_item++) { |
| 383 | if (len == 1 && *choice == menu_item->hotkey) { |
| 384 | found = i + 1; |
| 385 | break; |
| 386 | } |
| 387 | if (!strncasecmp(choice, menu_item->title, len)) { |
| 388 | if (found) { |
| 389 | if (len == 1) { |
| 390 | /* continue for hotkey matching */ |
| 391 | found = -1; |
| 392 | } else { |
| 393 | found = 0; |
| 394 | break; |
| 395 | } |
| 396 | } else { |
| 397 | found = i + 1; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | break; |
| 402 | case MENU_STUFF_TYPE_STRING_LIST: |
| 403 | string_list_item = ((struct string_list *)menu_stuff->stuff)->items; |
| 404 | for (i = 0; i < menu_stuff->nr; i++, string_list_item++) { |
| 405 | if (!strncasecmp(choice, string_list_item->string, len)) { |
| 406 | if (found) { |
| 407 | found = 0; |
| 408 | break; |
| 409 | } |
| 410 | found = i + 1; |
| 411 | } |
| 412 | } |
| 413 | break; |
| 414 | } |
| 415 | return found; |
| 416 | } |
| 417 | |
| 418 | |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 419 | /* |
| 420 | * Parse user input, and return choice(s) for menu (menu_stuff). |
| 421 | * |
| 422 | * Input |
| 423 | * (for single choice) |
| 424 | * 1 - select a numbered item |
| 425 | * foo - select item based on menu title |
| 426 | * - (empty) select nothing |
| 427 | * |
| 428 | * (for multiple choice) |
| 429 | * 1 - select a single item |
| 430 | * 3-5 - select a range of items |
| 431 | * 2-3,6-9 - select multiple ranges |
| 432 | * foo - select item based on menu title |
| 433 | * -... - unselect specified items |
| 434 | * * - choose all items |
| 435 | * - (empty) finish selecting |
| 436 | * |
| 437 | * The parse result will be saved in array **chosen, and |
| 438 | * return number of total selections. |
| 439 | */ |
| 440 | static int parse_choice(struct menu_stuff *menu_stuff, |
| 441 | int is_single, |
| 442 | struct strbuf input, |
| 443 | int **chosen) |
| 444 | { |
| 445 | struct strbuf **choice_list, **ptr; |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 446 | int nr = 0; |
| 447 | int i; |
| 448 | |
| 449 | if (is_single) { |
| 450 | choice_list = strbuf_split_max(&input, '\n', 0); |
| 451 | } else { |
| 452 | char *p = input.buf; |
| 453 | do { |
| 454 | if (*p == ',') |
| 455 | *p = ' '; |
| 456 | } while (*p++); |
| 457 | choice_list = strbuf_split_max(&input, ' ', 0); |
| 458 | } |
| 459 | |
| 460 | for (ptr = choice_list; *ptr; ptr++) { |
| 461 | char *p; |
| 462 | int choose = 1; |
| 463 | int bottom = 0, top = 0; |
| 464 | int is_range, is_number; |
| 465 | |
| 466 | strbuf_trim(*ptr); |
| 467 | if (!(*ptr)->len) |
| 468 | continue; |
| 469 | |
| 470 | /* Input that begins with '-'; unchoose */ |
| 471 | if (*(*ptr)->buf == '-') { |
| 472 | choose = 0; |
| 473 | strbuf_remove((*ptr), 0, 1); |
| 474 | } |
| 475 | |
| 476 | is_range = 0; |
| 477 | is_number = 1; |
| 478 | for (p = (*ptr)->buf; *p; p++) { |
| 479 | if ('-' == *p) { |
| 480 | if (!is_range) { |
| 481 | is_range = 1; |
| 482 | is_number = 0; |
| 483 | } else { |
| 484 | is_number = 0; |
| 485 | is_range = 0; |
| 486 | break; |
| 487 | } |
| 488 | } else if (!isdigit(*p)) { |
| 489 | is_number = 0; |
| 490 | is_range = 0; |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (is_number) { |
| 496 | bottom = atoi((*ptr)->buf); |
| 497 | top = bottom; |
| 498 | } else if (is_range) { |
| 499 | bottom = atoi((*ptr)->buf); |
| 500 | /* a range can be specified like 5-7 or 5- */ |
| 501 | if (!*(strchr((*ptr)->buf, '-') + 1)) |
| 502 | top = menu_stuff->nr; |
| 503 | else |
| 504 | top = atoi(strchr((*ptr)->buf, '-') + 1); |
| 505 | } else if (!strcmp((*ptr)->buf, "*")) { |
| 506 | bottom = 1; |
| 507 | top = menu_stuff->nr; |
| 508 | } else { |
Jiang Xin | 6083861 | 2013-07-24 10:22:04 +0800 | [diff] [blame] | 509 | bottom = find_unique((*ptr)->buf, menu_stuff); |
| 510 | top = bottom; |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (top <= 0 || bottom <= 0 || top > menu_stuff->nr || bottom > top || |
| 514 | (is_single && bottom != top)) { |
| 515 | clean_print_color(CLEAN_COLOR_ERROR); |
| 516 | printf_ln(_("Huh (%s)?"), (*ptr)->buf); |
| 517 | clean_print_color(CLEAN_COLOR_RESET); |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | for (i = bottom; i <= top; i++) |
| 522 | (*chosen)[i-1] = choose; |
| 523 | } |
| 524 | |
| 525 | strbuf_list_free(choice_list); |
| 526 | |
| 527 | for (i = 0; i < menu_stuff->nr; i++) |
| 528 | nr += (*chosen)[i]; |
| 529 | return nr; |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Implement a git-add-interactive compatible UI, which is borrowed |
| 534 | * from git-add--interactive.perl. |
| 535 | * |
| 536 | * Return value: |
| 537 | * |
| 538 | * - Return an array of integers |
| 539 | * - , and it is up to you to free the allocated memory. |
| 540 | * - The array ends with EOF. |
| 541 | * - If user pressed CTRL-D (i.e. EOF), no selection returned. |
| 542 | */ |
| 543 | static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff) |
| 544 | { |
| 545 | struct strbuf choice = STRBUF_INIT; |
| 546 | int *chosen, *result; |
| 547 | int nr = 0; |
| 548 | int eof = 0; |
| 549 | int i; |
| 550 | |
| 551 | chosen = xmalloc(sizeof(int) * stuff->nr); |
| 552 | /* set chosen as uninitialized */ |
| 553 | for (i = 0; i < stuff->nr; i++) |
| 554 | chosen[i] = -1; |
| 555 | |
| 556 | for (;;) { |
| 557 | if (opts->header) { |
| 558 | printf_ln("%s%s%s", |
| 559 | clean_get_color(CLEAN_COLOR_HEADER), |
| 560 | _(opts->header), |
| 561 | clean_get_color(CLEAN_COLOR_RESET)); |
| 562 | } |
| 563 | |
| 564 | /* chosen will be initialized by print_highlight_menu_stuff */ |
| 565 | print_highlight_menu_stuff(stuff, &chosen); |
| 566 | |
| 567 | if (opts->flags & MENU_OPTS_LIST_ONLY) |
| 568 | break; |
| 569 | |
| 570 | if (opts->prompt) { |
| 571 | printf("%s%s%s%s", |
| 572 | clean_get_color(CLEAN_COLOR_PROMPT), |
| 573 | _(opts->prompt), |
| 574 | opts->flags & MENU_OPTS_SINGLETON ? "> " : ">> ", |
| 575 | clean_get_color(CLEAN_COLOR_RESET)); |
| 576 | } |
| 577 | |
| 578 | if (strbuf_getline(&choice, stdin, '\n') != EOF) { |
| 579 | strbuf_trim(&choice); |
| 580 | } else { |
| 581 | eof = 1; |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | /* help for prompt */ |
| 586 | if (!strcmp(choice.buf, "?")) { |
| 587 | prompt_help_cmd(opts->flags & MENU_OPTS_SINGLETON); |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | /* for a multiple-choice menu, press ENTER (empty) will return back */ |
| 592 | if (!(opts->flags & MENU_OPTS_SINGLETON) && !choice.len) |
| 593 | break; |
| 594 | |
| 595 | nr = parse_choice(stuff, |
| 596 | opts->flags & MENU_OPTS_SINGLETON, |
| 597 | choice, |
| 598 | &chosen); |
| 599 | |
| 600 | if (opts->flags & MENU_OPTS_SINGLETON) { |
| 601 | if (nr) |
| 602 | break; |
| 603 | } else if (opts->flags & MENU_OPTS_IMMEDIATE) { |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (eof) { |
| 609 | result = xmalloc(sizeof(int)); |
| 610 | *result = EOF; |
| 611 | } else { |
| 612 | int j = 0; |
| 613 | |
| 614 | /* |
| 615 | * recalculate nr, if return back from menu directly with |
| 616 | * default selections. |
| 617 | */ |
| 618 | if (!nr) { |
| 619 | for (i = 0; i < stuff->nr; i++) |
| 620 | nr += chosen[i]; |
| 621 | } |
| 622 | |
| 623 | result = xmalloc(sizeof(int) * (nr + 1)); |
| 624 | memset(result, 0, sizeof(int) * (nr + 1)); |
| 625 | for (i = 0; i < stuff->nr && j < nr; i++) { |
| 626 | if (chosen[i]) |
| 627 | result[j++] = i; |
| 628 | } |
| 629 | result[j] = EOF; |
| 630 | } |
| 631 | |
| 632 | free(chosen); |
| 633 | strbuf_release(&choice); |
| 634 | return result; |
| 635 | } |
| 636 | |
| 637 | static int clean_cmd(void) |
| 638 | { |
| 639 | return MENU_RETURN_NO_LOOP; |
| 640 | } |
| 641 | |
Jiang Xin | d123926 | 2013-06-25 23:53:52 +0800 | [diff] [blame] | 642 | static int filter_by_patterns_cmd(void) |
| 643 | { |
| 644 | struct dir_struct dir; |
| 645 | struct strbuf confirm = STRBUF_INIT; |
| 646 | struct strbuf **ignore_list; |
| 647 | struct string_list_item *item; |
| 648 | struct exclude_list *el; |
| 649 | int changed = -1, i; |
| 650 | |
| 651 | for (;;) { |
| 652 | if (!del_list.nr) |
| 653 | break; |
| 654 | |
| 655 | if (changed) |
| 656 | pretty_print_dels(); |
| 657 | |
| 658 | clean_print_color(CLEAN_COLOR_PROMPT); |
| 659 | printf(_("Input ignore patterns>> ")); |
| 660 | clean_print_color(CLEAN_COLOR_RESET); |
| 661 | if (strbuf_getline(&confirm, stdin, '\n') != EOF) |
| 662 | strbuf_trim(&confirm); |
| 663 | else |
| 664 | putchar('\n'); |
| 665 | |
| 666 | /* quit filter_by_pattern mode if press ENTER or Ctrl-D */ |
| 667 | if (!confirm.len) |
| 668 | break; |
| 669 | |
| 670 | memset(&dir, 0, sizeof(dir)); |
| 671 | el = add_exclude_list(&dir, EXC_CMDL, "manual exclude"); |
| 672 | ignore_list = strbuf_split_max(&confirm, ' ', 0); |
| 673 | |
| 674 | for (i = 0; ignore_list[i]; i++) { |
| 675 | strbuf_trim(ignore_list[i]); |
| 676 | if (!ignore_list[i]->len) |
| 677 | continue; |
| 678 | |
| 679 | add_exclude(ignore_list[i]->buf, "", 0, el, -(i+1)); |
| 680 | } |
| 681 | |
| 682 | changed = 0; |
| 683 | for_each_string_list_item(item, &del_list) { |
| 684 | int dtype = DT_UNKNOWN; |
| 685 | |
| 686 | if (is_excluded(&dir, item->string, &dtype)) { |
| 687 | *item->string = '\0'; |
| 688 | changed++; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | if (changed) { |
| 693 | string_list_remove_empty_items(&del_list, 0); |
| 694 | } else { |
| 695 | clean_print_color(CLEAN_COLOR_ERROR); |
| 696 | printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm.buf); |
| 697 | clean_print_color(CLEAN_COLOR_RESET); |
| 698 | } |
| 699 | |
| 700 | strbuf_list_free(ignore_list); |
| 701 | clear_directory(&dir); |
| 702 | } |
| 703 | |
| 704 | strbuf_release(&confirm); |
| 705 | return 0; |
| 706 | } |
| 707 | |
Jiang Xin | c1f1d24 | 2013-06-25 23:53:53 +0800 | [diff] [blame] | 708 | static int select_by_numbers_cmd(void) |
| 709 | { |
| 710 | struct menu_opts menu_opts; |
| 711 | struct menu_stuff menu_stuff; |
| 712 | struct string_list_item *items; |
| 713 | int *chosen; |
| 714 | int i, j; |
| 715 | |
| 716 | menu_opts.header = NULL; |
| 717 | menu_opts.prompt = N_("Select items to delete"); |
| 718 | menu_opts.flags = 0; |
| 719 | |
| 720 | menu_stuff.type = MENU_STUFF_TYPE_STRING_LIST; |
| 721 | menu_stuff.stuff = &del_list; |
| 722 | menu_stuff.nr = del_list.nr; |
| 723 | |
| 724 | chosen = list_and_choose(&menu_opts, &menu_stuff); |
| 725 | items = del_list.items; |
| 726 | for (i = 0, j = 0; i < del_list.nr; i++) { |
| 727 | if (i < chosen[j]) { |
| 728 | *(items[i].string) = '\0'; |
| 729 | } else if (i == chosen[j]) { |
| 730 | /* delete selected item */ |
| 731 | j++; |
| 732 | continue; |
| 733 | } else { |
| 734 | /* end of chosen (chosen[j] == EOF), won't delete */ |
| 735 | *(items[i].string) = '\0'; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | string_list_remove_empty_items(&del_list, 0); |
| 740 | |
| 741 | free(chosen); |
| 742 | return 0; |
| 743 | } |
| 744 | |
Jiang Xin | 96a799b | 2013-06-25 23:53:54 +0800 | [diff] [blame] | 745 | static int ask_each_cmd(void) |
| 746 | { |
| 747 | struct strbuf confirm = STRBUF_INIT; |
| 748 | struct strbuf buf = STRBUF_INIT; |
| 749 | struct string_list_item *item; |
| 750 | const char *qname; |
| 751 | int changed = 0, eof = 0; |
| 752 | |
| 753 | for_each_string_list_item(item, &del_list) { |
| 754 | /* Ctrl-D should stop removing files */ |
| 755 | if (!eof) { |
| 756 | qname = quote_path_relative(item->string, NULL, &buf); |
| 757 | printf(_("remove %s? "), qname); |
| 758 | if (strbuf_getline(&confirm, stdin, '\n') != EOF) { |
| 759 | strbuf_trim(&confirm); |
| 760 | } else { |
| 761 | putchar('\n'); |
| 762 | eof = 1; |
| 763 | } |
| 764 | } |
| 765 | if (!confirm.len || strncasecmp(confirm.buf, "yes", confirm.len)) { |
| 766 | *item->string = '\0'; |
| 767 | changed++; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (changed) |
| 772 | string_list_remove_empty_items(&del_list, 0); |
| 773 | |
| 774 | strbuf_release(&buf); |
| 775 | strbuf_release(&confirm); |
| 776 | return MENU_RETURN_NO_LOOP; |
| 777 | } |
| 778 | |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 779 | static int quit_cmd(void) |
| 780 | { |
| 781 | string_list_clear(&del_list, 0); |
| 782 | printf_ln(_("Bye.")); |
| 783 | return MENU_RETURN_NO_LOOP; |
| 784 | } |
| 785 | |
| 786 | static int help_cmd(void) |
| 787 | { |
| 788 | clean_print_color(CLEAN_COLOR_HELP); |
| 789 | printf_ln(_( |
| 790 | "clean - start cleaning\n" |
Jiang Xin | d123926 | 2013-06-25 23:53:52 +0800 | [diff] [blame] | 791 | "filter by pattern - exclude items from deletion\n" |
Jiang Xin | c1f1d24 | 2013-06-25 23:53:53 +0800 | [diff] [blame] | 792 | "select by numbers - select items to be deleted by numbers\n" |
Jiang Xin | 96a799b | 2013-06-25 23:53:54 +0800 | [diff] [blame] | 793 | "ask each - confirm each deletion (like \"rm -i\")\n" |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 794 | "quit - stop cleaning\n" |
| 795 | "help - this screen\n" |
| 796 | "? - help for prompt selection" |
| 797 | )); |
| 798 | clean_print_color(CLEAN_COLOR_RESET); |
| 799 | return 0; |
| 800 | } |
| 801 | |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 802 | static void interactive_main_loop(void) |
| 803 | { |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 804 | while (del_list.nr) { |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 805 | struct menu_opts menu_opts; |
| 806 | struct menu_stuff menu_stuff; |
| 807 | struct menu_item menus[] = { |
| 808 | {'c', "clean", 0, clean_cmd}, |
Jiang Xin | d123926 | 2013-06-25 23:53:52 +0800 | [diff] [blame] | 809 | {'f', "filter by pattern", 0, filter_by_patterns_cmd}, |
Jiang Xin | c1f1d24 | 2013-06-25 23:53:53 +0800 | [diff] [blame] | 810 | {'s', "select by numbers", 0, select_by_numbers_cmd}, |
Jiang Xin | 96a799b | 2013-06-25 23:53:54 +0800 | [diff] [blame] | 811 | {'a', "ask each", 0, ask_each_cmd}, |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 812 | {'q', "quit", 0, quit_cmd}, |
| 813 | {'h', "help", 0, help_cmd}, |
| 814 | }; |
| 815 | int *chosen; |
| 816 | |
| 817 | menu_opts.header = N_("*** Commands ***"); |
| 818 | menu_opts.prompt = N_("What now"); |
| 819 | menu_opts.flags = MENU_OPTS_SINGLETON; |
| 820 | |
| 821 | menu_stuff.type = MENU_STUFF_TYPE_MENU_ITEM; |
| 822 | menu_stuff.stuff = menus; |
| 823 | menu_stuff.nr = sizeof(menus) / sizeof(struct menu_item); |
| 824 | |
Jiang Xin | 7a9b0b8 | 2013-06-25 23:53:50 +0800 | [diff] [blame] | 825 | clean_print_color(CLEAN_COLOR_HEADER); |
Jiang Xin | 1b8fd46 | 2013-06-25 23:53:49 +0800 | [diff] [blame] | 826 | printf_ln(Q_("Would remove the following item:", |
| 827 | "Would remove the following items:", |
| 828 | del_list.nr)); |
Jiang Xin | 7a9b0b8 | 2013-06-25 23:53:50 +0800 | [diff] [blame] | 829 | clean_print_color(CLEAN_COLOR_RESET); |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 830 | |
Jiang Xin | 1b8fd46 | 2013-06-25 23:53:49 +0800 | [diff] [blame] | 831 | pretty_print_dels(); |
| 832 | |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 833 | chosen = list_and_choose(&menu_opts, &menu_stuff); |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 834 | |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 835 | if (*chosen != EOF) { |
| 836 | int ret; |
| 837 | ret = menus[*chosen].fn(); |
| 838 | if (ret != MENU_RETURN_NO_LOOP) { |
| 839 | free(chosen); |
| 840 | chosen = NULL; |
| 841 | if (!del_list.nr) { |
| 842 | clean_print_color(CLEAN_COLOR_ERROR); |
| 843 | printf_ln(_("No more files to clean, exiting.")); |
| 844 | clean_print_color(CLEAN_COLOR_RESET); |
| 845 | break; |
| 846 | } |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 847 | continue; |
| 848 | } |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 849 | } else { |
| 850 | quit_cmd(); |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 851 | } |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 852 | |
Jiang Xin | 9f93e46 | 2013-06-25 23:53:51 +0800 | [diff] [blame] | 853 | free(chosen); |
| 854 | chosen = NULL; |
| 855 | break; |
| 856 | } |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 857 | } |
| 858 | |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 859 | int cmd_clean(int argc, const char **argv, const char *prefix) |
| 860 | { |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 861 | int i, res; |
| 862 | int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0; |
| 863 | int ignored_only = 0, config_set = 0, errors = 0, gone = 1; |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 864 | int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT; |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 865 | struct strbuf abs_path = STRBUF_INIT; |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 866 | struct dir_struct dir; |
Nguyễn Thái Ngọc Duy | 893d839 | 2013-07-14 15:35:37 +0700 | [diff] [blame] | 867 | struct pathspec pathspec; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 868 | struct strbuf buf = STRBUF_INIT; |
Thiago Farina | bdab6a5 | 2010-09-06 20:32:55 -0300 | [diff] [blame] | 869 | struct string_list exclude_list = STRING_LIST_INIT_NODUP; |
Adam Spiers | 72aeb18 | 2013-01-16 13:25:58 +0000 | [diff] [blame] | 870 | struct exclude_list *el; |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 871 | struct string_list_item *item; |
Dmitry Potapov | 1fb3289 | 2008-03-07 04:13:17 +0300 | [diff] [blame] | 872 | const char *qname; |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 873 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | 145f9c8 | 2012-08-20 19:32:01 +0700 | [diff] [blame] | 874 | OPT__QUIET(&quiet, N_("do not print names of files removed")), |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 875 | OPT__DRY_RUN(&dry_run, N_("dry run")), |
Nguyễn Thái Ngọc Duy | 145f9c8 | 2012-08-20 19:32:01 +0700 | [diff] [blame] | 876 | OPT__FORCE(&force, N_("force")), |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 877 | OPT_BOOL('i', "interactive", &interactive, N_("interactive cleaning")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 878 | OPT_BOOL('d', NULL, &remove_directories, |
Nguyễn Thái Ngọc Duy | 145f9c8 | 2012-08-20 19:32:01 +0700 | [diff] [blame] | 879 | N_("remove whole directories")), |
| 880 | { OPTION_CALLBACK, 'e', "exclude", &exclude_list, N_("pattern"), |
| 881 | N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb }, |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 882 | OPT_BOOL('x', NULL, &ignored, N_("remove ignored files, too")), |
| 883 | OPT_BOOL('X', NULL, &ignored_only, |
Nguyễn Thái Ngọc Duy | 145f9c8 | 2012-08-20 19:32:01 +0700 | [diff] [blame] | 884 | N_("remove only ignored files")), |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 885 | OPT_END() |
| 886 | }; |
| 887 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 888 | git_config(git_clean_config, NULL); |
Junio C Hamano | 625db1b | 2007-11-12 21:13:05 -0800 | [diff] [blame] | 889 | if (force < 0) |
| 890 | force = 0; |
| 891 | else |
| 892 | config_set = 1; |
| 893 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 894 | argc = parse_options(argc, argv, prefix, options, builtin_clean_usage, |
| 895 | 0); |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 896 | |
| 897 | memset(&dir, 0, sizeof(dir)); |
Shawn Bohrer | 1617adc | 2007-11-14 23:00:54 -0600 | [diff] [blame] | 898 | if (ignored_only) |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 899 | dir.flags |= DIR_SHOW_IGNORED; |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 900 | |
| 901 | if (ignored && ignored_only) |
Ævar Arnfjörð Bjarmason | 2da57ad | 2011-02-22 23:42:21 +0000 | [diff] [blame] | 902 | die(_("-x and -X cannot be used together")); |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 903 | |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 904 | if (!interactive && !dry_run && !force) { |
Ævar Arnfjörð Bjarmason | a66f9b2 | 2011-02-22 23:42:22 +0000 | [diff] [blame] | 905 | if (config_set) |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 906 | die(_("clean.requireForce set to true and neither -i, -n nor -f given; " |
Ævar Arnfjörð Bjarmason | a66f9b2 | 2011-02-22 23:42:22 +0000 | [diff] [blame] | 907 | "refusing to clean")); |
| 908 | else |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 909 | die(_("clean.requireForce defaults to true and neither -i, -n nor -f given; " |
Ævar Arnfjörð Bjarmason | a66f9b2 | 2011-02-22 23:42:22 +0000 | [diff] [blame] | 910 | "refusing to clean")); |
| 911 | } |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 912 | |
Junio C Hamano | a0f4afb | 2009-06-30 15:33:45 -0700 | [diff] [blame] | 913 | if (force > 1) |
| 914 | rm_flags = 0; |
| 915 | |
Johannes Schindelin | 7c4c97c | 2009-02-16 13:20:25 +0100 | [diff] [blame] | 916 | dir.flags |= DIR_SHOW_OTHER_DIRECTORIES; |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 917 | |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 918 | if (read_cache() < 0) |
Ævar Arnfjörð Bjarmason | 2da57ad | 2011-02-22 23:42:21 +0000 | [diff] [blame] | 919 | die(_("index file corrupt")); |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 920 | |
Shawn Bohrer | 1617adc | 2007-11-14 23:00:54 -0600 | [diff] [blame] | 921 | if (!ignored) |
| 922 | setup_standard_excludes(&dir); |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 923 | |
Adam Spiers | 72aeb18 | 2013-01-16 13:25:58 +0000 | [diff] [blame] | 924 | el = add_exclude_list(&dir, EXC_CMDL, "--exclude option"); |
Jared Hance | 07de4eb | 2010-07-20 15:35:56 -0400 | [diff] [blame] | 925 | for (i = 0; i < exclude_list.nr; i++) |
Adam Spiers | 72aeb18 | 2013-01-16 13:25:58 +0000 | [diff] [blame] | 926 | add_exclude(exclude_list.items[i].string, "", 0, el, -(i+1)); |
Jared Hance | 07de4eb | 2010-07-20 15:35:56 -0400 | [diff] [blame] | 927 | |
Nguyễn Thái Ngọc Duy | 893d839 | 2013-07-14 15:35:37 +0700 | [diff] [blame] | 928 | parse_pathspec(&pathspec, 0, |
| 929 | PATHSPEC_PREFER_CWD, |
| 930 | prefix, argv); |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 931 | |
Nguyễn Thái Ngọc Duy | 7327d3d | 2013-07-14 15:35:55 +0700 | [diff] [blame] | 932 | fill_directory(&dir, &pathspec); |
Junio C Hamano | d871c86 | 2007-12-04 23:55:41 -0800 | [diff] [blame] | 933 | |
| 934 | for (i = 0; i < dir.nr; i++) { |
| 935 | struct dir_entry *ent = dir.entries[i]; |
Shawn Bohrer | f2d0df7 | 2008-04-14 22:14:09 -0500 | [diff] [blame] | 936 | int len, pos; |
| 937 | int matches = 0; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 938 | const struct cache_entry *ce; |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 939 | struct stat st; |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 940 | const char *rel; |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 941 | |
| 942 | /* |
| 943 | * Remove the '/' at the end that directory |
| 944 | * walking adds for directory entries. |
| 945 | */ |
| 946 | len = ent->len; |
| 947 | if (len && ent->name[len-1] == '/') |
| 948 | len--; |
| 949 | pos = cache_name_pos(ent->name, len); |
| 950 | if (0 <= pos) |
| 951 | continue; /* exact match */ |
| 952 | pos = -pos - 1; |
| 953 | if (pos < active_nr) { |
| 954 | ce = active_cache[pos]; |
| 955 | if (ce_namelen(ce) == len && |
| 956 | !memcmp(ce->name, ent->name, len)) |
| 957 | continue; /* Yup, this one exists unmerged */ |
| 958 | } |
| 959 | |
Junio C Hamano | d871c86 | 2007-12-04 23:55:41 -0800 | [diff] [blame] | 960 | if (lstat(ent->name, &st)) |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 961 | die_errno("Cannot lstat '%s'", ent->name); |
Junio C Hamano | d871c86 | 2007-12-04 23:55:41 -0800 | [diff] [blame] | 962 | |
Nguyễn Thái Ngọc Duy | 893d839 | 2013-07-14 15:35:37 +0700 | [diff] [blame] | 963 | if (pathspec.nr) |
Nguyễn Thái Ngọc Duy | 05b8502 | 2014-01-24 20:40:34 +0700 | [diff] [blame^] | 964 | matches = dir_path_match(ent, &pathspec, 0, NULL); |
Junio C Hamano | d871c86 | 2007-12-04 23:55:41 -0800 | [diff] [blame] | 965 | |
| 966 | if (S_ISDIR(st.st_mode)) { |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 967 | if (remove_directories || (matches == MATCHED_EXACTLY)) { |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 968 | rel = relative_path(ent->name, prefix, &buf); |
| 969 | string_list_append(&del_list, rel); |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 970 | } |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 971 | } else { |
Nguyễn Thái Ngọc Duy | 893d839 | 2013-07-14 15:35:37 +0700 | [diff] [blame] | 972 | if (pathspec.nr && !matches) |
Junio C Hamano | d871c86 | 2007-12-04 23:55:41 -0800 | [diff] [blame] | 973 | continue; |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 974 | rel = relative_path(ent->name, prefix, &buf); |
| 975 | string_list_append(&del_list, rel); |
| 976 | } |
| 977 | } |
| 978 | |
Jiang Xin | 1769600 | 2013-06-25 23:53:48 +0800 | [diff] [blame] | 979 | if (interactive && del_list.nr > 0) |
| 980 | interactive_main_loop(); |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 981 | |
| 982 | for_each_string_list_item(item, &del_list) { |
| 983 | struct stat st; |
| 984 | |
| 985 | if (prefix) |
| 986 | strbuf_addstr(&abs_path, prefix); |
| 987 | |
| 988 | strbuf_addstr(&abs_path, item->string); |
| 989 | |
| 990 | /* |
| 991 | * we might have removed this as part of earlier |
| 992 | * recursive directory removal, so lstat() here could |
| 993 | * fail with ENOENT. |
| 994 | */ |
| 995 | if (lstat(abs_path.buf, &st)) |
| 996 | continue; |
| 997 | |
| 998 | if (S_ISDIR(st.st_mode)) { |
| 999 | if (remove_dirs(&abs_path, prefix, rm_flags, dry_run, quiet, &gone)) |
| 1000 | errors++; |
| 1001 | if (gone && !quiet) { |
| 1002 | qname = quote_path_relative(item->string, NULL, &buf); |
| 1003 | printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname); |
| 1004 | } |
| 1005 | } else { |
| 1006 | res = dry_run ? 0 : unlink(abs_path.buf); |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 1007 | if (res) { |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 1008 | qname = quote_path_relative(item->string, NULL, &buf); |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 1009 | warning(_(msg_warn_remove_failed), qname); |
Miklos Vajna | aa9c83c | 2008-02-21 02:44:46 +0100 | [diff] [blame] | 1010 | errors++; |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 1011 | } else if (!quiet) { |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 1012 | qname = quote_path_relative(item->string, NULL, &buf); |
Zoltan Klinger | f538a91 | 2013-01-11 09:53:46 +1100 | [diff] [blame] | 1013 | printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname); |
Miklos Vajna | aa9c83c | 2008-02-21 02:44:46 +0100 | [diff] [blame] | 1014 | } |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 1015 | } |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 1016 | strbuf_reset(&abs_path); |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 1017 | } |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 1018 | |
Jiang Xin | 396049e | 2013-06-25 23:53:47 +0800 | [diff] [blame] | 1019 | strbuf_release(&abs_path); |
| 1020 | strbuf_release(&buf); |
| 1021 | string_list_clear(&del_list, 0); |
Jared Hance | 07de4eb | 2010-07-20 15:35:56 -0400 | [diff] [blame] | 1022 | string_list_clear(&exclude_list, 0); |
Miklos Vajna | aa9c83c | 2008-02-21 02:44:46 +0100 | [diff] [blame] | 1023 | return (errors != 0); |
Shawn Bohrer | 113f10f | 2007-11-11 19:48:47 -0600 | [diff] [blame] | 1024 | } |