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