{"thread":{"id":"33816","subject":"[PATCH v9 0/9] interactive git-clean","startedAt":"2013-05-14T08:45:14Z","lastAt":"2013-05-15T18:24:15Z","messageCount":19,"participants":["Jiang Xin","Junio C Hamano"],"isPatch":true,"patchVersion":9,"patchTotal":9},"messages":[{"id":"217313","messageId":"cover.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":null,"subject":"[PATCH v9 0/9] interactive git-clean","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:14Z","receivedAt":"2013-05-14T08:45:14Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Updates since v8:\n\n * Run interactive git-clean even if in dry_run mode. \n\n       -\tif (interactive && !dry_run && del_list.nr > 0)\n       +\tif (interactive && del_list.nr > 0)\n        \t\tinteractive_main_loop();\n \n * Variable menu_list is not static.\n   (copy from global del_list, and forget to remove static)\n\n        static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen)\n        {\n       -\tstatic struct string_list menu_list = STRING_LIST_INIT_DUP;\n       +\tstruct string_list menu_list = STRING_LIST_INIT_DUP;\n        \tstruct strbuf menu = STRBUF_INIT;\n        \tint i;\n\n * i18n:\n\n       @@ -567,21 +567,21 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)\n        \t\tif (opts->header) {\n        \t\t\tprintf_ln(\"%s%s%s\",\n        \t\t\t\t  clean_get_color(CLEAN_COLOR_HEADER),\n       -\t\t\t\t  opts->header,\n       +\t\t\t\t  _(opts->header),\n        \t\t\t\t  clean_get_color(CLEAN_COLOR_RESET));\n        \t\t}\n        \n       ... \n       \n        \t\tif (opts->prompt) {\n        \t\t\tprintf(\"%s%s%s%s\",\n        \t\t\t       clean_get_color(CLEAN_COLOR_PROMPT),\n       -\t\t\t       opts->prompt,\n       -\t\t\t       opts->flag & MENU_OPTS_SINGLETON ? \"> \" : \">> \",\n       +\t\t\t       _(opts->prompt),\n       +\t\t\t       opts->flags & MENU_OPTS_SINGLETON ? \"> \" : \">> \",\n        \t\t\t       clean_get_color(CLEAN_COLOR_RESET));\n        \t\t}\n\n\n       @@ -721,8 +724,8 @@ static int select_by_numbers_cmd(void)\n        \tint i, j;\n        \n       -\tmenu_opts.prompt = \"Select items to delete\";\n       +\tmenu_opts.prompt = N_(\"Select items to delete\");\n\n       -\t\tmenu_opts.header = _(\"*** Commands ***\");\n       -\t\tmenu_opts.prompt = \"What now\";\n       +\t\tmenu_opts.header = N_(\"*** Commands ***\");\n       +\t\tmenu_opts.prompt = N_(\"What now\");\n  \n\n * Update documentation for \"color.interactive\" and \"color.interactive.<slot>\",\n   because `git clean --interactive` reuses these variables from\n   `git-add--interactive`.\n\n        color.interactive::\n        \tWhen set to `always`, always use colors for interactive prompts\n       -\tand displays (such as those used by \"git-add --interactive\").\n       -\tWhen false (or `never`), never.  When set to `true` or `auto`, use\n       -\tcolors only when the output is to the terminal. Defaults to false.\n       +\tand displays (such as those used by \"git-add --interactive\" and\n       +\t\"git-clean --interactive\"). When false (or `never`), never.\n       +\tWhen set to `true` or `auto`, use colors only when the output is\n       +\tto the terminal. Defaults to false.\n        \n        color.interactive.<slot>::\n       -\tUse customized color for 'git add --interactive'\n       -\toutput. `<slot>` may be `prompt`, `header`, `help` or `error`, for\n       -\tfour distinct types of normal output from interactive\n       -\tcommands.  The values of these variables may be specified as\n       -\tin color.branch.<slot>.\n       +\tUse customized color for 'git add --interactive' and 'git clean\n       +\t--interactive' output. `<slot>` may be `prompt`, `header`, `help`\n       +\tor `error`, for four distinct types of normal output from\n       +\tinteractive commands.  The values of these variables may be\n       +\tspecified as in color.branch.<slot>.\n \n * Update documentation for \"column.clean\", because we only use layout settings.\n\n        column.clean::\n       -\tSpecify whether to output cleaning files in `git clean -i` in columns.\n       -\tSee `column.ui` for details.\n       +\tSpecify the layout when list items in `git clean -i`, which always\n       +\tshows files and directories in columns. See `column.ui` for details.\n\n * Refactor: change variables' names, such as:\n\n       @@ -57,7 +57,7 @@ enum color_clean {\n        struct menu_opts {\n        \tconst char *header;\n        \tconst char *prompt;\n       -\tint flag;\n       +\tint flags;\n        };\n \n\n       @@ -428,7 +428,7 @@ static int parse_choice(struct menu_stuff *menu_stuff,\n        \t\t\tstruct strbuf input,\n        \t\t\tint **chosen)\n        {\n       -\tstruct strbuf **choice_list, **choice_p;\n       +\tstruct strbuf **choice_list, **ptr;\n        \tint nr = 0;\n        \tint i;\n\n * Refactor parse_clean_color_slot:\n\n       @@ -80,19 +80,19 @@ struct menu_stuff {\n        \tvoid *stuff;\n        };\n        \n       -static int parse_clean_color_slot(const char *var, int ofs)\n       +static int parse_clean_color_slot(const char *var)\n        {\n       -\tif (!strcasecmp(var+ofs, \"reset\"))\n       +\tif (!strcasecmp(var, \"reset\"))\n        \t\treturn CLEAN_COLOR_RESET;\n       -\tif (!strcasecmp(var+ofs, \"plain\"))\n       +\tif (!strcasecmp(var, \"plain\"))\n        \t\treturn CLEAN_COLOR_PLAIN;\n       -\tif (!strcasecmp(var+ofs, \"prompt\"))\n       +\tif (!strcasecmp(var, \"prompt\"))\n        \t\treturn CLEAN_COLOR_PROMPT;\n       -\tif (!strcasecmp(var+ofs, \"header\"))\n       +\tif (!strcasecmp(var, \"header\"))\n        \t\treturn CLEAN_COLOR_HEADER;\n       -\tif (!strcasecmp(var+ofs, \"help\"))\n       +\tif (!strcasecmp(var, \"help\"))\n        \t\treturn CLEAN_COLOR_HELP;\n       -\tif (!strcasecmp(var+ofs, \"error\"))\n       +\tif (!strcasecmp(var, \"error\"))\n        \t\treturn CLEAN_COLOR_ERROR;\n        \treturn -1;\n        }\n       @@ -109,7 +109,8 @@ static int git_clean_config(const char *var, const char *value, void *cb)\n        \t\treturn 0;\n        \t}\n        \tif (!prefixcmp(var, \"color.interactive.\")) {\n       -\t\tint slot = parse_clean_color_slot(var, 18);\n       +\t\tint slot = parse_clean_color_slot(var +\n       +\t\t\t\t\t\t  strlen(\"color.interactive.\"));\n        \t\tif (slot < 0)\n        \t\t\treturn 0;\n        \t\tif (!value)\n\n * Withdraw PATCH v8 10/12, 11/12 and 12/12.\n\n   - git-clean refactor: save some options in clean_flags\n   - git-clean refactor: add wrapper scan_clean_candidates\n   - git-clean: add toggle flags interactive action\n\nUsage:\n\nSee: [PATCH v9 9/9] git-clean: add documentation for interactive git-clean\n\n\nJiang Xin (9):\n  git-clean: refactor git-clean into two phases\n  git-clean: add support for -i/--interactive\n  git-clean: show items of del_list in columns\n  git-clean: add colors to interactive git-clean\n  git-clean: use a git-add-interactive compatible UI\n  git-clean: add filter by pattern interactive action\n  git-clean: add select by numbers interactive action\n  git-clean: add ask each interactive action\n  git-clean: add documentation for interactive git-clean\n\n Documentation/config.txt    |  21 +-\n Documentation/git-clean.txt |  71 +++-\n builtin/clean.c             | 802 ++++++++++++++++++++++++++++++++++++++++++--\n 3 files changed, 860 insertions(+), 34 deletions(-)\n\n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217316","messageId":"7c551bf22bc45cfcdd62d1baf6300f3f86244312.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"[PATCH v9 1/9] git-clean: refactor git-clean into two phases","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:15Z","receivedAt":"2013-05-14T08:45:15Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Before introducing interactive git-clean, refactor git-clean operations\ninto two phases:\n\n * hold cleaning items in del_list,\n * and remove them in a separate loop at the end.\n\nWe will introduce interactive git-clean between the two phases. The\ninteractive git-clean will show what would be done and must confirm\nbefore do real cleaning.\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\n---\n builtin/clean.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++----------\n 1 file changed, 85 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 04e39..ccd4 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -15,6 +15,7 @@\n #include \"quote.h\"\n \n static int force = -1; /* unset */\n+static struct string_list del_list = STRING_LIST_INIT_DUP;\n \n static const char *const builtin_clean_usage[] = {\n \tN_(\"git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...\"),\n@@ -142,18 +143,61 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,\n \treturn ret;\n }\n \n+/*\n+ * Give path as relative to prefix.\n+ *\n+ * This function is a combination of path_relative (in quote.c) and\n+ * relative_path (in path.c)\n+ */\n+static const char *path_relative(const char *in, const char *prefix)\n+{\n+\tstatic char buf[PATH_MAX + 1];\n+\tint off, i;\n+\tint len, prefix_len;\n+\n+\tlen = strlen(in);\n+\tif (prefix)\n+\t\tprefix_len = strlen(prefix);\n+\telse\n+\t\tprefix_len = 0;\n+\n+\toff = 0;\n+\ti = 0;\n+\twhile (i < prefix_len && i < len && prefix[i] == in[i]) {\n+\t\tif (prefix[i] == '/')\n+\t\t\toff = i + 1;\n+\t\ti++;\n+\t}\n+\tin += off;\n+\tlen -= off;\n+\n+\tif (i >= prefix_len)\n+\t\treturn in;\n+\n+\tbuf[0] = '\\0';\n+\twhile (i < prefix_len) {\n+\t\tif (prefix[i] == '/')\n+\t\t\tstrcat(buf, \"../\");\n+\t\ti++;\n+\t}\n+\tstrcat(buf, in);\n+\n+\treturn buf;\n+}\n+\n int cmd_clean(int argc, const char **argv, const char *prefix)\n {\n \tint i, res;\n \tint dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0;\n \tint ignored_only = 0, config_set = 0, errors = 0, gone = 1;\n \tint rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;\n-\tstruct strbuf directory = STRBUF_INIT;\n+\tstruct strbuf abs_path = STRBUF_INIT;\n \tstruct dir_struct dir;\n \tstatic const char **pathspec;\n \tstruct strbuf buf = STRBUF_INIT;\n \tstruct string_list exclude_list = STRING_LIST_INIT_NODUP;\n \tstruct exclude_list *el;\n+\tstruct string_list_item *item;\n \tconst char *qname;\n \tchar *seen = NULL;\n \tstruct option options[] = {\n@@ -223,6 +267,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\tint matches = 0;\n \t\tstruct cache_entry *ce;\n \t\tstruct stat st;\n+\t\tconst char *rel;\n \n \t\t/*\n \t\t * Remove the '/' at the end that directory\n@@ -242,11 +287,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t\t\tcontinue; /* Yup, this one exists unmerged */\n \t\t}\n \n-\t\t/*\n-\t\t * we might have removed this as part of earlier\n-\t\t * recursive directory removal, so lstat() here could\n-\t\t * fail with ENOENT.\n-\t\t */\n \t\tif (lstat(ent->name, &st))\n \t\t\tcontinue;\n \n@@ -257,33 +297,60 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t}\n \n \t\tif (S_ISDIR(st.st_mode)) {\n-\t\t\tstrbuf_addstr(&directory, ent->name);\n \t\t\tif (remove_directories || (matches == MATCHED_EXACTLY)) {\n-\t\t\t\tif (remove_dirs(&directory, prefix, rm_flags, dry_run, quiet, &gone))\n-\t\t\t\t\terrors++;\n-\t\t\t\tif (gone && !quiet) {\n-\t\t\t\t\tqname = quote_path_relative(directory.buf, directory.len, &buf, prefix);\n-\t\t\t\t\tprintf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);\n-\t\t\t\t}\n+\t\t\t\trel = path_relative(ent->name, prefix);\n+\t\t\t\tstring_list_append(&del_list, rel);\n \t\t\t}\n-\t\t\tstrbuf_reset(&directory);\n \t\t} else {\n \t\t\tif (pathspec && !matches)\n \t\t\t\tcontinue;\n-\t\t\tres = dry_run ? 0 : unlink(ent->name);\n+\t\t\trel = path_relative(ent->name, prefix);\n+\t\t\tstring_list_append(&del_list, rel);\n+\t\t}\n+\t}\n+\n+\t/* TODO: do interactive git-clean here, which will modify del_list */\n+\n+\tfor_each_string_list_item(item, &del_list) {\n+\t\tstruct stat st;\n+\n+\t\tif (prefix) {\n+\t\t\tstrbuf_addstr(&abs_path, prefix);\n+\t\t}\n+\t\tstrbuf_addstr(&abs_path, item->string);\n+\n+\t\t/*\n+\t\t * we might have removed this as part of earlier\n+\t\t * recursive directory removal, so lstat() here could\n+\t\t * fail with ENOENT.\n+\t\t */\n+\t\tif (lstat(abs_path.buf, &st))\n+\t\t\tcontinue;\n+\n+\t\tif (S_ISDIR(st.st_mode)) {\n+\t\t\tif (remove_dirs(&abs_path, prefix, rm_flags, dry_run, quiet, &gone))\n+\t\t\t\terrors++;\n+\t\t\tif (gone && !quiet) {\n+\t\t\t\tqname = quote_path_relative(item->string, -1, &buf, NULL);\n+\t\t\t\tprintf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);\n+\t\t\t}\n+\t\t} else {\n+\t\t\tres = dry_run ? 0 : unlink(abs_path.buf);\n \t\t\tif (res) {\n-\t\t\t\tqname = quote_path_relative(ent->name, -1, &buf, prefix);\n+\t\t\t\tqname = quote_path_relative(item->string, -1, &buf, NULL);\n \t\t\t\twarning(_(msg_warn_remove_failed), qname);\n \t\t\t\terrors++;\n \t\t\t} else if (!quiet) {\n-\t\t\t\tqname = quote_path_relative(ent->name, -1, &buf, prefix);\n+\t\t\t\tqname = quote_path_relative(item->string, -1, &buf, NULL);\n \t\t\t\tprintf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);\n \t\t\t}\n \t\t}\n+\t\tstrbuf_reset(&abs_path);\n \t}\n \tfree(seen);\n \n-\tstrbuf_release(&directory);\n+\tstrbuf_release(&abs_path);\n+\tstring_list_clear(&del_list, 0);\n \tstring_list_clear(&exclude_list, 0);\n \treturn (errors != 0);\n }\n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217314","messageId":"9c5ea46607a9e50e0ddb1e74eec9fb36bb04facc.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"[PATCH v9 2/9] git-clean: add support for -i/--interactive","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:16Z","receivedAt":"2013-05-14T08:45:16Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Show what would be done and the user must confirm before actually\ncleaning.\n\n    Would remove ...\n    Would remove ...\n    Would remove ...\n\n    Remove [y/n]?\n\nPress \"y\" to start cleaning, and press \"n\" if you want to abort.\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\n---\n Documentation/git-clean.txt | 10 ++++++--\n builtin/clean.c             | 61 +++++++++++++++++++++++++++++++++++++++++----\n 2 files changed, 64 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-clean.txt b/Documentation/git-clean.txt\nindex bdc3a..186e34 100644\n--- a/Documentation/git-clean.txt\n+++ b/Documentation/git-clean.txt\n@@ -8,7 +8,7 @@ git-clean - Remove untracked files from the working tree\n SYNOPSIS\n --------\n [verse]\n-'git clean' [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>...\n+'git clean' [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>...\n \n DESCRIPTION\n -----------\n@@ -34,7 +34,13 @@ OPTIONS\n -f::\n --force::\n \tIf the Git configuration variable clean.requireForce is not set\n-\tto false, 'git clean' will refuse to run unless given -f or -n.\n+\tto false, 'git clean' will refuse to run unless given -f, -n or\n+\t-i.\n+\n+-i::\n+--interactive::\n+\tShow what would be done and the user must confirm before actually\n+\tcleaning.\n \n -n::\n --dry-run::\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex ccd4..127463 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -15,10 +15,11 @@\n #include \"quote.h\"\n \n static int force = -1; /* unset */\n+static int interactive;\n static struct string_list del_list = STRING_LIST_INIT_DUP;\n \n static const char *const builtin_clean_usage[] = {\n-\tN_(\"git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...\"),\n+\tN_(\"git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...\"),\n \tNULL\n };\n \n@@ -185,6 +186,50 @@ static const char *path_relative(const char *in, const char *prefix)\n \treturn buf;\n }\n \n+static void interactive_main_loop(void)\n+{\n+\tstruct strbuf confirm = STRBUF_INIT;\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tstruct string_list_item *item;\n+\tconst char *qname;\n+\n+\twhile (del_list.nr) {\n+\t\tputchar('\\n');\n+\t\tfor_each_string_list_item(item, &del_list) {\n+\t\t\tqname = quote_path_relative(item->string, -1, &buf, NULL);\n+\t\t\tprintf(_(msg_would_remove), qname);\n+\t\t}\n+\t\tputchar('\\n');\n+\n+\t\tprintf(_(\"Remove [y/n]? \"));\n+\t\tif (strbuf_getline(&confirm, stdin, '\\n') != EOF) {\n+\t\t\tstrbuf_trim(&confirm);\n+\t\t} else {\n+\t\t\t/* Ctrl-D is the same as \"quit\" */\n+\t\t\tstring_list_clear(&del_list, 0);\n+\t\t\tputchar('\\n');\n+\t\t\tprintf_ln(\"Bye.\");\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tif (confirm.len) {\n+\t\t\tif (!strncasecmp(confirm.buf, \"yes\", confirm.len)) {\n+\t\t\t\tbreak;\n+\t\t\t} else if (!strncasecmp(confirm.buf, \"no\", confirm.len) ||\n+\t\t\t\t   !strncasecmp(confirm.buf, \"quit\", confirm.len)) {\n+\t\t\t\tstring_list_clear(&del_list, 0);\n+\t\t\t\tprintf_ln(\"Bye.\");\n+\t\t\t\tbreak;\n+\t\t\t} else {\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tstrbuf_release(&buf);\n+\tstrbuf_release(&confirm);\n+}\n+\n int cmd_clean(int argc, const char **argv, const char *prefix)\n {\n \tint i, res;\n@@ -204,6 +249,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\tOPT__QUIET(&quiet, N_(\"do not print names of files removed\")),\n \t\tOPT__DRY_RUN(&dry_run, N_(\"dry run\")),\n \t\tOPT__FORCE(&force, N_(\"force\")),\n+\t\tOPT_BOOL('i', \"interactive\", &interactive, N_(\"interactive cleaning\")),\n \t\tOPT_BOOLEAN('d', NULL, &remove_directories,\n \t\t\t\tN_(\"remove whole directories\")),\n \t\t{ OPTION_CALLBACK, 'e', \"exclude\", &exclude_list, N_(\"pattern\"),\n@@ -230,12 +276,16 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \tif (ignored && ignored_only)\n \t\tdie(_(\"-x and -X cannot be used together\"));\n \n-\tif (!dry_run && !force) {\n+\tif (interactive) {\n+\t\tif (!isatty(0) || !isatty(1))\n+\t\t\tdie(_(\"interactive clean can not run without a valid tty; \"\n+\t\t\t\t  \"refusing to clean\"));\n+\t} else if (!dry_run && !force) {\n \t\tif (config_set)\n-\t\t\tdie(_(\"clean.requireForce set to true and neither -n nor -f given; \"\n+\t\t\tdie(_(\"clean.requireForce set to true and neither -i, -n nor -f given; \"\n \t\t\t\t  \"refusing to clean\"));\n \t\telse\n-\t\t\tdie(_(\"clean.requireForce defaults to true and neither -n nor -f given; \"\n+\t\t\tdie(_(\"clean.requireForce defaults to true and neither -i, -n nor -f given; \"\n \t\t\t\t  \"refusing to clean\"));\n \t}\n \n@@ -309,7 +359,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t}\n \t}\n \n-\t/* TODO: do interactive git-clean here, which will modify del_list */\n+\tif (interactive && del_list.nr > 0)\n+\t\tinteractive_main_loop();\n \n \tfor_each_string_list_item(item, &del_list) {\n \t\tstruct stat st;\n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217315","messageId":"0b7d32f9f4e3b457124ca663479220bd78791d7c.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"[PATCH v9 3/9] git-clean: show items of del_list in columns","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:17Z","receivedAt":"2013-05-14T08:45:17Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"When there are lots of items to be cleaned, it is hard to see them all\nin one screen. Show them in columns will solve this problem.\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\nComments-by: Matthieu Moy <Matthieu.Moy@imag.fr>\n---\n Documentation/config.txt |  4 ++++\n builtin/clean.c          | 49 +++++++++++++++++++++++++++++++++++++++---------\n 2 files changed, 44 insertions(+), 9 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 6e53f..e031b 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -955,6 +955,10 @@ column.branch::\n \tSpecify whether to output branch listing in `git branch` in columns.\n \tSee `column.ui` for details.\n \n+column.clean::\n+\tSpecify the layout when list items in `git clean -i`, which always\n+\tshows files and directories in columns. See `column.ui` for details.\n+\n column.status::\n \tSpecify whether to output untracked files in `git status` in columns.\n \tSee `column.ui` for details.\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 127463..d7c68 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -13,10 +13,12 @@\n #include \"refs.h\"\n #include \"string-list.h\"\n #include \"quote.h\"\n+#include \"column.h\"\n \n static int force = -1; /* unset */\n static int interactive;\n static struct string_list del_list = STRING_LIST_INIT_DUP;\n+static unsigned int colopts;\n \n static const char *const builtin_clean_usage[] = {\n \tN_(\"git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...\"),\n@@ -31,8 +33,13 @@ static const char *msg_warn_remove_failed = N_(\"failed to remove %s\");\n \n static int git_clean_config(const char *var, const char *value, void *cb)\n {\n-\tif (!strcmp(var, \"clean.requireforce\"))\n+\tif (!prefixcmp(var, \"column.\"))\n+\t\treturn git_column_config(var, value, \"clean\", &colopts);\n+\n+\tif (!strcmp(var, \"clean.requireforce\")) {\n \t\tforce = !git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n \treturn git_default_config(var, value, cb);\n }\n \n@@ -186,21 +193,46 @@ static const char *path_relative(const char *in, const char *prefix)\n \treturn buf;\n }\n \n-static void interactive_main_loop(void)\n+static void pretty_print_dels(void)\n {\n-\tstruct strbuf confirm = STRBUF_INIT;\n-\tstruct strbuf buf = STRBUF_INIT;\n+\tstruct string_list list = STRING_LIST_INIT_DUP;\n \tstruct string_list_item *item;\n+\tstruct strbuf buf = STRBUF_INIT;\n \tconst char *qname;\n+\tstruct column_options copts;\n+\n+\tfor_each_string_list_item(item, &del_list) {\n+\t\tqname = quote_path_relative(item->string, -1, &buf, NULL);\n+\t\tstring_list_append(&list, qname);\n+\t}\n+\n+\t/*\n+\t * always enable column display, we only consult column.*\n+\t * about layout strategy and stuff\n+\t */\n+\tcolopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;\n+\tmemset(&copts, 0, sizeof(copts));\n+\tcopts.indent = \"  \";\n+\tcopts.padding = 2;\n+\tprint_columns(&list, colopts, &copts);\n+\tputchar('\\n');\n+\tstrbuf_release(&buf);\n+\tstring_list_clear(&list, 0);\n+}\n+\n+static void interactive_main_loop(void)\n+{\n+\tstruct strbuf confirm = STRBUF_INIT;\n \n \twhile (del_list.nr) {\n \t\tputchar('\\n');\n-\t\tfor_each_string_list_item(item, &del_list) {\n-\t\t\tqname = quote_path_relative(item->string, -1, &buf, NULL);\n-\t\t\tprintf(_(msg_would_remove), qname);\n-\t\t}\n+\t\tprintf_ln(Q_(\"Would remove the following item:\",\n+\t\t\t     \"Would remove the following items:\",\n+\t\t\t     del_list.nr));\n \t\tputchar('\\n');\n \n+\t\tpretty_print_dels();\n+\n \t\tprintf(_(\"Remove [y/n]? \"));\n \t\tif (strbuf_getline(&confirm, stdin, '\\n') != EOF) {\n \t\t\tstrbuf_trim(&confirm);\n@@ -226,7 +258,6 @@ static void interactive_main_loop(void)\n \t\t}\n \t}\n \n-\tstrbuf_release(&buf);\n \tstrbuf_release(&confirm);\n }\n \n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217320","messageId":"7d05d46ee9db7b37b38982a4ef299aa525f52e1b.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"[PATCH v9 4/9] git-clean: add colors to interactive git-clean","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:18Z","receivedAt":"2013-05-14T08:45:18Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Show header, help, error messages, and prompt in colors for interactive\ngit-clean. Re-use config variables, such as \"color.interactive\" and\n\"color.interactive.<slot>\" for command `git-add--interactive`.\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\nComments-by: Matthieu Moy <Matthieu.Moy@imag.fr>\n---\n Documentation/config.txt | 17 +++++------\n builtin/clean.c          | 73 +++++++++++++++++++++++++++++++++++++++++++++++-\n 2 files changed, 81 insertions(+), 9 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex e031b..83613 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -876,16 +876,17 @@ The values of these variables may be specified as in color.branch.<slot>.\n \n color.interactive::\n \tWhen set to `always`, always use colors for interactive prompts\n-\tand displays (such as those used by \"git-add --interactive\").\n-\tWhen false (or `never`), never.  When set to `true` or `auto`, use\n-\tcolors only when the output is to the terminal. Defaults to false.\n+\tand displays (such as those used by \"git-add --interactive\" and\n+\t\"git-clean --interactive\"). When false (or `never`), never.\n+\tWhen set to `true` or `auto`, use colors only when the output is\n+\tto the terminal. Defaults to false.\n \n color.interactive.<slot>::\n-\tUse customized color for 'git add --interactive'\n-\toutput. `<slot>` may be `prompt`, `header`, `help` or `error`, for\n-\tfour distinct types of normal output from interactive\n-\tcommands.  The values of these variables may be specified as\n-\tin color.branch.<slot>.\n+\tUse customized color for 'git add --interactive' and 'git clean\n+\t--interactive' output. `<slot>` may be `prompt`, `header`, `help`\n+\tor `error`, for four distinct types of normal output from\n+\tinteractive commands.  The values of these variables may be\n+\tspecified as in color.branch.<slot>.\n \n color.pager::\n \tA boolean to enable/disable colored output when the pager is in\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex d7c68..5c781 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -14,6 +14,7 @@\n #include \"string-list.h\"\n #include \"quote.h\"\n #include \"column.h\"\n+#include \"color.h\"\n \n static int force = -1; /* unset */\n static int interactive;\n@@ -31,16 +32,82 @@ static const char *msg_skip_git_dir = N_(\"Skipping repository %s\\n\");\n static const char *msg_would_skip_git_dir = N_(\"Would skip repository %s\\n\");\n static const char *msg_warn_remove_failed = N_(\"failed to remove %s\");\n \n+static int clean_use_color = -1;\n+static char clean_colors[][COLOR_MAXLEN] = {\n+\tGIT_COLOR_RESET,\n+\tGIT_COLOR_NORMAL,\t/* PLAIN */\n+\tGIT_COLOR_BOLD_BLUE,\t/* PROMPT */\n+\tGIT_COLOR_BOLD,\t\t/* HEADER */\n+\tGIT_COLOR_BOLD_RED,\t/* HELP */\n+\tGIT_COLOR_BOLD_RED,\t/* ERROR */\n+};\n+enum color_clean {\n+\tCLEAN_COLOR_RESET = 0,\n+\tCLEAN_COLOR_PLAIN = 1,\n+\tCLEAN_COLOR_PROMPT = 2,\n+\tCLEAN_COLOR_HEADER = 3,\n+\tCLEAN_COLOR_HELP = 4,\n+\tCLEAN_COLOR_ERROR = 5,\n+};\n+\n+static int parse_clean_color_slot(const char *var)\n+{\n+\tif (!strcasecmp(var, \"reset\"))\n+\t\treturn CLEAN_COLOR_RESET;\n+\tif (!strcasecmp(var, \"plain\"))\n+\t\treturn CLEAN_COLOR_PLAIN;\n+\tif (!strcasecmp(var, \"prompt\"))\n+\t\treturn CLEAN_COLOR_PROMPT;\n+\tif (!strcasecmp(var, \"header\"))\n+\t\treturn CLEAN_COLOR_HEADER;\n+\tif (!strcasecmp(var, \"help\"))\n+\t\treturn CLEAN_COLOR_HELP;\n+\tif (!strcasecmp(var, \"error\"))\n+\t\treturn CLEAN_COLOR_ERROR;\n+\treturn -1;\n+}\n+\n static int git_clean_config(const char *var, const char *value, void *cb)\n {\n \tif (!prefixcmp(var, \"column.\"))\n \t\treturn git_column_config(var, value, \"clean\", &colopts);\n \n+\t/* honors the color.interactive* config variables which also\n+\t   applied in git-add--interactive and git-stash */\n+\tif (!strcmp(var, \"color.interactive\")) {\n+\t\tclean_use_color = git_config_colorbool(var, value);\n+\t\treturn 0;\n+\t}\n+\tif (!prefixcmp(var, \"color.interactive.\")) {\n+\t\tint slot = parse_clean_color_slot(var +\n+\t\t\t\t\t\t  strlen(\"color.interactive.\"));\n+\t\tif (slot < 0)\n+\t\t\treturn 0;\n+\t\tif (!value)\n+\t\t\treturn config_error_nonbool(var);\n+\t\tcolor_parse(value, var, clean_colors[slot]);\n+\t\treturn 0;\n+\t}\n+\n \tif (!strcmp(var, \"clean.requireforce\")) {\n \t\tforce = !git_config_bool(var, value);\n \t\treturn 0;\n \t}\n-\treturn git_default_config(var, value, cb);\n+\n+\t/* inspect the color.ui config variable and others */\n+\treturn git_color_default_config(var, value, cb);\n+}\n+\n+static const char *clean_get_color(enum color_clean ix)\n+{\n+\tif (want_color(clean_use_color))\n+\t\treturn clean_colors[ix];\n+\treturn \"\";\n+}\n+\n+static void clean_print_color(enum color_clean ix)\n+{\n+\tprintf(\"%s\", clean_get_color(ix));\n }\n \n static int exclude_cb(const struct option *opt, const char *arg, int unset)\n@@ -226,14 +293,18 @@ static void interactive_main_loop(void)\n \n \twhile (del_list.nr) {\n \t\tputchar('\\n');\n+\t\tclean_print_color(CLEAN_COLOR_HEADER);\n \t\tprintf_ln(Q_(\"Would remove the following item:\",\n \t\t\t     \"Would remove the following items:\",\n \t\t\t     del_list.nr));\n+\t\tclean_print_color(CLEAN_COLOR_RESET);\n \t\tputchar('\\n');\n \n \t\tpretty_print_dels();\n \n+\t\tclean_print_color(CLEAN_COLOR_PROMPT);\n \t\tprintf(_(\"Remove [y/n]? \"));\n+\t\tclean_print_color(CLEAN_COLOR_RESET);\n \t\tif (strbuf_getline(&confirm, stdin, '\\n') != EOF) {\n \t\t\tstrbuf_trim(&confirm);\n \t\t} else {\n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217321","messageId":"06429848f012b2fd1e9dc6aff5ca3004b5a63a47.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"[PATCH v9 5/9] git-clean: use a git-add-interactive compatible UI","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:19Z","receivedAt":"2013-05-14T08:45:19Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Rewrite menu using a new method `list_and_choose`, which is borrowed\nfrom `git-add--interactive.perl`. We will use this framework to add\nnew actions for interactive git-clean later.\n\nPlease NOTE:\n\n * Method `list_and_choose` return an array of integers, and\n * it is up to you to free the allocated memory of the array.\n * The array ends with EOF.\n * If user pressed CTRL-D (i.e. EOF), no selection returned.\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\n---\n builtin/clean.c | 449 ++++++++++++++++++++++++++++++++++++++++++++++++++++----\n 1 file changed, 420 insertions(+), 29 deletions(-)\n\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 5c781..7930f 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -50,6 +50,36 @@ enum color_clean {\n \tCLEAN_COLOR_ERROR = 5,\n };\n \n+#define MENU_OPTS_SINGLETON\t\t01\n+#define MENU_OPTS_IMMEDIATE\t\t02\n+#define MENU_OPTS_LIST_ONLY\t\t04\n+\n+struct menu_opts {\n+\tconst char *header;\n+\tconst char *prompt;\n+\tint flags;\n+};\n+\n+#define MENU_RETURN_NO_LOOP\t\t10\n+\n+struct menu_item {\n+\tchar hotkey;\n+\tchar *title;\n+\tint selected;\n+\tint (*fn)();\n+};\n+\n+enum menu_stuff_type {\n+\tMENU_STUFF_TYPE_STRING_LIST = 1,\n+\tMENU_STUFF_TYPE_MENU_ITEM\n+};\n+\n+struct menu_stuff {\n+\tenum menu_stuff_type type;\n+\tint nr;\n+\tvoid *stuff;\n+};\n+\n static int parse_clean_color_slot(const char *var)\n {\n \tif (!strcasecmp(var, \"reset\"))\n@@ -282,54 +312,415 @@ static void pretty_print_dels(void)\n \tcopts.indent = \"  \";\n \tcopts.padding = 2;\n \tprint_columns(&list, colopts, &copts);\n-\tputchar('\\n');\n \tstrbuf_release(&buf);\n \tstring_list_clear(&list, 0);\n }\n \n-static void interactive_main_loop(void)\n+static void pretty_print_menus(struct string_list *menu_list)\n+{\n+\tunsigned int local_colopts = 0;\n+\tstruct column_options copts;\n+\n+\tlocal_colopts = COL_ENABLED | COL_ROW;\n+\tmemset(&copts, 0, sizeof(copts));\n+\tcopts.indent = \"  \";\n+\tcopts.padding = 2;\n+\tprint_columns(menu_list, local_colopts, &copts);\n+}\n+\n+static void prompt_help_cmd(int singleton)\n+{\n+\tclean_print_color(CLEAN_COLOR_HELP);\n+\tprintf_ln(singleton ?\n+\t\t  _(\"Prompt help:\\n\"\n+\t\t    \"1          - select a numbered item\\n\"\n+\t\t    \"foo        - select item based on unique prefix\\n\"\n+\t\t    \"           - (empty) select nothing\") :\n+\t\t  _(\"Prompt help:\\n\"\n+\t\t    \"1          - select a single item\\n\"\n+\t\t    \"3-5        - select a range of items\\n\"\n+\t\t    \"2-3,6-9    - select multiple ranges\\n\"\n+\t\t    \"foo        - select item based on unique prefix\\n\"\n+\t\t    \"-...       - unselect specified items\\n\"\n+\t\t    \"*          - choose all items\\n\"\n+\t\t    \"           - (empty) finish selecting\"));\n+\tclean_print_color(CLEAN_COLOR_RESET);\n+}\n+\n+/*\n+ * display menu stuff with number prefix and hotkey highlight\n+ */\n+static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen)\n+{\n+\tstruct string_list menu_list = STRING_LIST_INIT_DUP;\n+\tstruct strbuf menu = STRBUF_INIT;\n+\tint i;\n+\n+\tif (MENU_STUFF_TYPE_MENU_ITEM == stuff->type) {\n+\t\tstruct menu_item *item;\n+\n+\t\titem = (struct menu_item *)stuff->stuff;\n+\t\tfor (i = 0; i < stuff->nr; i++, item++) {\n+\t\t\tchar *p;\n+\t\t\tint highlighted = 0;\n+\n+\t\t\tp = item->title;\n+\t\t\tif ((*chosen)[i] < 0)\n+\t\t\t\t(*chosen)[i] = item->selected ? 1 : 0;\n+\t\t\tstrbuf_addf(&menu, \"%s%2d: \", (*chosen)[i] ? \"*\" : \" \", i+1);\n+\t\t\tfor (; *p; p++) {\n+\t\t\t\tif (!highlighted && *p == item->hotkey) {\n+\t\t\t\t\tstrbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_PROMPT));\n+\t\t\t\t\tstrbuf_addch(&menu, *p);\n+\t\t\t\t\tstrbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_RESET));\n+\t\t\t\t\thighlighted = 1;\n+\t\t\t\t} else {\n+\t\t\t\t\tstrbuf_addch(&menu, *p);\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tstring_list_append(&menu_list, menu.buf);\n+\t\t\tstrbuf_reset(&menu);\n+\t\t}\n+\t} else if (MENU_STUFF_TYPE_STRING_LIST == stuff->type) {\n+\t\tstruct string_list_item *item;\n+\t\tstruct strbuf buf = STRBUF_INIT;\n+\t\ti = 0;\n+\n+\t\tfor_each_string_list_item(item, (struct string_list *)stuff->stuff) {\n+\t\t\tif ((*chosen)[i] < 0)\n+\t\t\t\t(*chosen)[i] = 0;\n+\t\t\tstrbuf_addf(&menu, \"%s%2d: %s\", (*chosen)[i] ? \"*\" : \" \", ++i, item->string);\n+\t\t\tstring_list_append(&menu_list, menu.buf);\n+\t\t\tstrbuf_reset(&menu);\n+\t\t}\n+\t\tstrbuf_release(&buf);\n+\t}\n+\n+\tpretty_print_menus(&menu_list);\n+\n+\tstrbuf_release(&menu);\n+\tstring_list_clear(&menu_list, 0);\n+}\n+\n+/*\n+ * Parse user input, and return choice(s) for menu (menu_stuff).\n+ *\n+ * Input\n+ *     (for single choice)\n+ *         1          - select a numbered item\n+ *         foo        - select item based on menu title\n+ *                    - (empty) select nothing\n+ *\n+ *     (for multiple choice)\n+ *         1          - select a single item\n+ *         3-5        - select a range of items\n+ *         2-3,6-9    - select multiple ranges\n+ *         foo        - select item based on menu title\n+ *         -...       - unselect specified items\n+ *         *          - choose all items\n+ *                    - (empty) finish selecting\n+ *\n+ * The parse result will be saved in array **chosen, and\n+ * return number of total selections.\n+ */\n+static int parse_choice(struct menu_stuff *menu_stuff,\n+\t\t\tint is_single,\n+\t\t\tstruct strbuf input,\n+\t\t\tint **chosen)\n+{\n+\tstruct strbuf **choice_list, **ptr;\n+\tint nr = 0;\n+\tint i;\n+\n+\tif (is_single) {\n+\t\tchoice_list = strbuf_split_max(&input, '\\n', 0);\n+\t} else {\n+\t\tchar *p = input.buf;\n+\t\tdo {\n+\t\t\tif (*p == ',')\n+\t\t\t\t*p = ' ';\n+\t\t} while (*p++);\n+\t\tchoice_list = strbuf_split_max(&input, ' ', 0);\n+\t}\n+\n+\tfor (ptr = choice_list; *ptr; ptr++) {\n+\t\tchar *p;\n+\t\tint choose = 1;\n+\t\tint bottom = 0, top = 0;\n+\t\tint is_range, is_number;\n+\n+\t\tstrbuf_trim(*ptr);\n+\t\tif (!(*ptr)->len)\n+\t\t\tcontinue;\n+\n+\t\t/* Input that begins with '-'; unchoose */\n+\t\tif (*(*ptr)->buf == '-') {\n+\t\t\tchoose = 0;\n+\t\t\tstrbuf_remove((*ptr), 0, 1);\n+\t\t}\n+\n+\t\tis_range = 0;\n+\t\tis_number = 1;\n+\t\tfor(p = (*ptr)->buf; *p; p++) {\n+\t\t\tif ('-' == *p) {\n+\t\t\t\tif (!is_range) {\n+\t\t\t\t\tis_range = 1;\n+\t\t\t\t\tis_number = 0;\n+\t\t\t\t} else {\n+\t\t\t\t\tis_number = 0;\n+\t\t\t\t\tis_range = 0;\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\t\t\t} else if (!isdigit(*p)) {\n+\t\t\t\tis_number = 0;\n+\t\t\t\tis_range = 0;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (is_number) {\n+\t\t\tbottom = atoi((*ptr)->buf);\n+\t\t\ttop = bottom;\n+\t\t} else if (is_range) {\n+\t\t\tbottom = atoi((*ptr)->buf);\n+\t\t\t/* a range can be specified like 5-7 or 5- */\n+\t\t\tif (!*(strchr((*ptr)->buf, '-') + 1)) {\n+\t\t\t\ttop = menu_stuff->nr - 1;\n+\t\t\t} else {\n+\t\t\t\ttop = atoi(strchr((*ptr)->buf, '-') + 1);\n+\t\t\t}\n+\t\t} else if (!strcmp((*ptr)->buf, \"*\")) {\n+\t\t\tbottom = 1;\n+\t\t\ttop = menu_stuff->nr;\n+\t\t} else {\n+\t\t\tif (MENU_STUFF_TYPE_MENU_ITEM == menu_stuff->type) {\n+\t\t\t\tstruct menu_item *item;\n+\n+\t\t\t\titem = (struct menu_item *)menu_stuff->stuff;\n+\t\t\t\tfor (i = 0; i < menu_stuff->nr; i++, item++) {\n+\t\t\t\t\tif (((*ptr)->len == 1 &&\n+\t\t\t\t\t     *(*ptr)->buf == item->hotkey) ||\n+\t\t\t\t\t    !strcasecmp((*ptr)->buf, item->title)) {\n+\t\t\t\t\t\tbottom = i + 1;\n+\t\t\t\t\t\ttop = bottom;\n+\t\t\t\t\t\tbreak;\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\t\t\t} else if (MENU_STUFF_TYPE_STRING_LIST == menu_stuff->type) {\n+\t\t\t\tstruct string_list_item *item;\n+\n+\t\t\t\titem = ((struct string_list *)menu_stuff->stuff)->items;\n+\t\t\t\tfor (i = 0; i < menu_stuff->nr; i++, item++) {\n+\t\t\t\t\tif (!strcasecmp((*ptr)->buf, item->string)) {\n+\t\t\t\t\t\tbottom = i + 1;\n+\t\t\t\t\t\ttop = bottom;\n+\t\t\t\t\t\tbreak;\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (top <= 0 || bottom <= 0 || top > menu_stuff->nr || bottom > top ||\n+\t\t    (is_single && bottom != top)) {\n+\t\t\tclean_print_color(CLEAN_COLOR_ERROR);\n+\t\t\tprintf_ln(_(\"Huh (%s)?\"), (*ptr)->buf);\n+\t\t\tclean_print_color(CLEAN_COLOR_RESET);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tfor (i = bottom; i <= top; i++)\n+\t\t\t(*chosen)[i-1] = choose;\n+\t}\n+\n+\tstrbuf_list_free(choice_list);\n+\n+\tfor (i = 0; i < menu_stuff->nr; i++)\n+\t\tnr += (*chosen)[i];\n+\treturn nr;\n+}\n+\n+/*\n+ * Implement a git-add-interactive compatible UI, which is borrowed\n+ * from git-add--interactive.perl.\n+ *\n+ * Return value:\n+ *\n+ *   - Return an array of integers\n+ *   - , and it is up to you to free the allocated memory.\n+ *   - The array ends with EOF.\n+ *   - If user pressed CTRL-D (i.e. EOF), no selection returned.\n+ */\n+static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)\n+{\n+\tstruct strbuf choice = STRBUF_INIT;\n+\tint *chosen, *result;\n+\tint nr = 0;\n+\tint eof = 0;\n+\tint i;\n+\n+\tchosen = xmalloc(sizeof(int) * stuff->nr);\n+\t/* set chosen as uninitialized */\n+\tfor (i = 0; i < stuff->nr; i++)\n+\t\tchosen[i] = -1;\n+\n+\tfor (;;) {\n+\t\tif (opts->header) {\n+\t\t\tprintf_ln(\"%s%s%s\",\n+\t\t\t\t  clean_get_color(CLEAN_COLOR_HEADER),\n+\t\t\t\t  _(opts->header),\n+\t\t\t\t  clean_get_color(CLEAN_COLOR_RESET));\n+\t\t}\n+\n+\t\t/* chosen will be initialized by print_highlight_menu_stuff */\n+\t\tprint_highlight_menu_stuff(stuff, &chosen);\n+\n+\t\tif (opts->flags & MENU_OPTS_LIST_ONLY)\n+\t\t\tbreak;\n+\n+\t\tif (opts->prompt) {\n+\t\t\tprintf(\"%s%s%s%s\",\n+\t\t\t       clean_get_color(CLEAN_COLOR_PROMPT),\n+\t\t\t       _(opts->prompt),\n+\t\t\t       opts->flags & MENU_OPTS_SINGLETON ? \"> \" : \">> \",\n+\t\t\t       clean_get_color(CLEAN_COLOR_RESET));\n+\t\t}\n+\n+\t\tif (strbuf_getline(&choice, stdin, '\\n') != EOF) {\n+\t\t\tstrbuf_trim(&choice);\n+\t\t} else {\n+\t\t\teof = 1;\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\t/* help for prompt */\n+\t\tif (!strcmp(choice.buf, \"?\")) {\n+\t\t\tprompt_help_cmd(opts->flags & MENU_OPTS_SINGLETON);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\t/* for a multiple-choice menu, press ENTER (empty) will return back */\n+\t\tif (!(opts->flags & MENU_OPTS_SINGLETON) && !choice.len)\n+\t\t\tbreak;\n+\n+\t\tnr = parse_choice(stuff,\n+\t\t\t\t  opts->flags & MENU_OPTS_SINGLETON,\n+\t\t\t\t  choice,\n+\t\t\t\t  &chosen);\n+\n+\t\tif (opts->flags & MENU_OPTS_SINGLETON) {\n+\t\t\tif (nr)\n+\t\t\t\tbreak;\n+\t\t} else if (opts->flags & MENU_OPTS_IMMEDIATE) {\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (eof) {\n+\t\tresult = xmalloc(sizeof(int));\n+\t\t*result = EOF;\n+\t} else {\n+\t\tint j = 0;\n+\n+\t\t/*\n+\t\t * recalculate nr, if return back from menu directly with\n+\t\t * default selections.\n+\t\t */\n+\t\tif (!nr) {\n+\t\t\tfor (i = 0; i < stuff->nr; i++)\n+\t\t\t\tnr += chosen[i];\n+\t\t}\n+\n+\t\tresult = xmalloc(sizeof(int) * (nr + 1));\n+\t\tmemset(result, 0, sizeof(int) * (nr + 1));\n+\t\tfor (i = 0; i < stuff->nr && j < nr; i++) {\n+\t\t\tif (chosen[i])\n+\t\t\t\tresult[j++] = i;\n+\t\t}\n+\t\tresult[j] = EOF;\n+\t}\n+\n+\tfree(chosen);\n+\tstrbuf_release(&choice);\n+\treturn result;\n+}\n+\n+static int clean_cmd(void)\n+{\n+\treturn MENU_RETURN_NO_LOOP;\n+}\n+\n+static int quit_cmd(void)\n+{\n+\tstring_list_clear(&del_list, 0);\n+\tprintf_ln(_(\"Bye.\"));\n+\treturn MENU_RETURN_NO_LOOP;\n+}\n+\n+static int help_cmd(void)\n {\n-\tstruct strbuf confirm = STRBUF_INIT;\n+\tclean_print_color(CLEAN_COLOR_HELP);\n+\tprintf_ln(_(\n+\t\t    \"clean               - start cleaning\\n\"\n+\t\t    \"quit                - stop cleaning\\n\"\n+\t\t    \"help                - this screen\\n\"\n+\t\t    \"?                   - help for prompt selection\"\n+\t\t   ));\n+\tclean_print_color(CLEAN_COLOR_RESET);\n+\treturn 0;\n+}\n \n+static void interactive_main_loop(void)\n+{\n \twhile (del_list.nr) {\n-\t\tputchar('\\n');\n+\t\tstruct menu_opts menu_opts;\n+\t\tstruct menu_stuff menu_stuff;\n+\t\tstruct menu_item menus[] = {\n+\t\t\t{'c', \"clean\",\t\t\t0, clean_cmd},\n+\t\t\t{'q', \"quit\",\t\t\t0, quit_cmd},\n+\t\t\t{'h', \"help\",\t\t\t0, help_cmd},\n+\t\t};\n+\t\tint *chosen;\n+\n+\t\tmenu_opts.header = N_(\"*** Commands ***\");\n+\t\tmenu_opts.prompt = N_(\"What now\");\n+\t\tmenu_opts.flags = MENU_OPTS_SINGLETON;\n+\n+\t\tmenu_stuff.type = MENU_STUFF_TYPE_MENU_ITEM;\n+\t\tmenu_stuff.stuff = menus;\n+\t\tmenu_stuff.nr = sizeof(menus) / sizeof(struct menu_item);\n+\n \t\tclean_print_color(CLEAN_COLOR_HEADER);\n \t\tprintf_ln(Q_(\"Would remove the following item:\",\n \t\t\t     \"Would remove the following items:\",\n \t\t\t     del_list.nr));\n \t\tclean_print_color(CLEAN_COLOR_RESET);\n-\t\tputchar('\\n');\n \n \t\tpretty_print_dels();\n \n-\t\tclean_print_color(CLEAN_COLOR_PROMPT);\n-\t\tprintf(_(\"Remove [y/n]? \"));\n-\t\tclean_print_color(CLEAN_COLOR_RESET);\n-\t\tif (strbuf_getline(&confirm, stdin, '\\n') != EOF) {\n-\t\t\tstrbuf_trim(&confirm);\n-\t\t} else {\n-\t\t\t/* Ctrl-D is the same as \"quit\" */\n-\t\t\tstring_list_clear(&del_list, 0);\n-\t\t\tputchar('\\n');\n-\t\t\tprintf_ln(\"Bye.\");\n-\t\t\tbreak;\n-\t\t}\n-\n-\t\tif (confirm.len) {\n-\t\t\tif (!strncasecmp(confirm.buf, \"yes\", confirm.len)) {\n-\t\t\t\tbreak;\n-\t\t\t} else if (!strncasecmp(confirm.buf, \"no\", confirm.len) ||\n-\t\t\t\t   !strncasecmp(confirm.buf, \"quit\", confirm.len)) {\n-\t\t\t\tstring_list_clear(&del_list, 0);\n-\t\t\t\tprintf_ln(\"Bye.\");\n-\t\t\t\tbreak;\n-\t\t\t} else {\n+\t\tchosen = list_and_choose(&menu_opts, &menu_stuff);\n+\n+\t\tif (*chosen != EOF) {\n+\t\t\tint ret;\n+\t\t\tret = menus[*chosen].fn();\n+\t\t\tif (ret != MENU_RETURN_NO_LOOP) {\n+\t\t\t\tfree(chosen);\n+\t\t\t\tchosen = NULL;\n+\t\t\t\tif (!del_list.nr) {\n+\t\t\t\t\tclean_print_color(CLEAN_COLOR_ERROR);\n+\t\t\t\t\tprintf_ln(_(\"No more files to clean, exiting.\"));\n+\t\t\t\t\tclean_print_color(CLEAN_COLOR_RESET);\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t} else {\n+\t\t\tquit_cmd();\n \t\t}\n-\t}\n \n-\tstrbuf_release(&confirm);\n+\t\tfree(chosen);\n+\t\tchosen = NULL;\n+\t\tbreak;\n+\t}\n }\n \n int cmd_clean(int argc, const char **argv, const char *prefix)\n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217322","messageId":"b1c1164ef7bba3299b3f4333492ea07bc3afd0cd.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"[PATCH v9 6/9] git-clean: add filter by pattern interactive action","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:20Z","receivedAt":"2013-05-14T08:45:20Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Add a new action for interactive git-clean: filter by pattern. When the\nuser chooses this action, user can input space-separated patterns (the\nsame syntax as gitignore), and each clean candidate that matches with\none of the patterns will be excluded from cleaning. When the user feels\nit's OK, presses ENTER and back to the confirmation dialog.\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\nSuggested-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/clean.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 68 insertions(+)\n\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 7930f..80cf1 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -649,6 +649,72 @@ static int clean_cmd(void)\n \treturn MENU_RETURN_NO_LOOP;\n }\n \n+static int filter_by_patterns_cmd(void)\n+{\n+\tstruct dir_struct dir;\n+\tstruct strbuf confirm = STRBUF_INIT;\n+\tstruct strbuf **ignore_list;\n+\tstruct string_list_item *item;\n+\tstruct exclude_list *el;\n+\tint changed = -1, i;\n+\n+\tfor (;;) {\n+\t\tif (!del_list.nr)\n+\t\t\tbreak;\n+\n+\t\tif (changed)\n+\t\t\tpretty_print_dels();\n+\n+\t\tclean_print_color(CLEAN_COLOR_PROMPT);\n+\t\tprintf(_(\"Input ignore patterns>> \"));\n+\t\tclean_print_color(CLEAN_COLOR_RESET);\n+\t\tif (strbuf_getline(&confirm, stdin, '\\n') != EOF)\n+\t\t\tstrbuf_trim(&confirm);\n+\t\telse\n+\t\t\tputchar('\\n');\n+\n+\t\t/* quit filter_by_pattern mode if press ENTER or Ctrl-D */\n+\t\tif (!confirm.len)\n+\t\t\tbreak;\n+\n+\t\tmemset(&dir, 0, sizeof(dir));\n+\t\tel = add_exclude_list(&dir, EXC_CMDL, \"manual exclude\");\n+\t\tignore_list = strbuf_split_max(&confirm, ' ', 0);\n+\n+\t\tfor (i = 0; ignore_list[i]; i++) {\n+\t\t\tstrbuf_trim(ignore_list[i]);\n+\t\t\tif (!ignore_list[i]->len)\n+\t\t\t\tcontinue;\n+\n+\t\t\tadd_exclude(ignore_list[i]->buf, \"\", 0, el, -(i+1));\n+\t\t}\n+\n+\t\tchanged = 0;\n+\t\tfor_each_string_list_item(item, &del_list) {\n+\t\t\tint dtype = DT_UNKNOWN;\n+\n+\t\t\tif (is_excluded(&dir, item->string, &dtype)) {\n+\t\t\t\t*item->string = '\\0';\n+\t\t\t\tchanged++;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (changed) {\n+\t\t\tstring_list_remove_empty_items(&del_list, 0);\n+\t\t} else {\n+\t\t\tclean_print_color(CLEAN_COLOR_ERROR);\n+\t\t\tprintf_ln(_(\"WARNING: Cannot find items matched by: %s\"), confirm.buf);\n+\t\t\tclean_print_color(CLEAN_COLOR_RESET);\n+\t\t}\n+\n+\t\tstrbuf_list_free(ignore_list);\n+\t\tclear_directory(&dir);\n+\t}\n+\n+\tstrbuf_release(&confirm);\n+\treturn 0;\n+}\n+\n static int quit_cmd(void)\n {\n \tstring_list_clear(&del_list, 0);\n@@ -661,6 +727,7 @@ static int help_cmd(void)\n \tclean_print_color(CLEAN_COLOR_HELP);\n \tprintf_ln(_(\n \t\t    \"clean               - start cleaning\\n\"\n+\t\t    \"filter by pattern   - exclude items from deletion\\n\"\n \t\t    \"quit                - stop cleaning\\n\"\n \t\t    \"help                - this screen\\n\"\n \t\t    \"?                   - help for prompt selection\"\n@@ -676,6 +743,7 @@ static void interactive_main_loop(void)\n \t\tstruct menu_stuff menu_stuff;\n \t\tstruct menu_item menus[] = {\n \t\t\t{'c', \"clean\",\t\t\t0, clean_cmd},\n+\t\t\t{'f', \"filter by pattern\",\t0, filter_by_patterns_cmd},\n \t\t\t{'q', \"quit\",\t\t\t0, quit_cmd},\n \t\t\t{'h', \"help\",\t\t\t0, help_cmd},\n \t\t};\n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217319","messageId":"2bc55597c82db022fc235f2c77561900e3ee6cca.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"[PATCH v9 7/9] git-clean: add select by numbers interactive action","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:21Z","receivedAt":"2013-05-14T08:45:21Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Draw a multiple choice menu using `list_and_choose` to select items\nto be deleted by numbers.\n\nUser can input:\n\n *  1,5-7 : select 1,5,6,7 items to be deleted\n *  *     : select all items to be deleted\n *  -*    : unselect all, nothing will be deleted\n *        : (empty) finish selecting, and return back to main menu\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\n---\n builtin/clean.c | 39 +++++++++++++++++++++++++++++++++++++++\n 1 file changed, 39 insertions(+)\n\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 80cf1..74fc9 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -715,6 +715,43 @@ static int filter_by_patterns_cmd(void)\n \treturn 0;\n }\n \n+static int select_by_numbers_cmd(void)\n+{\n+\tstruct menu_opts menu_opts;\n+\tstruct menu_stuff menu_stuff;\n+\tstruct string_list_item *items;\n+\tint *chosen;\n+\tint i, j;\n+\n+\tmenu_opts.header = NULL;\n+\tmenu_opts.prompt = N_(\"Select items to delete\");\n+\tmenu_opts.flags = 0;\n+\n+\tmenu_stuff.type = MENU_STUFF_TYPE_STRING_LIST;\n+\tmenu_stuff.stuff = &del_list;\n+\tmenu_stuff.nr = del_list.nr;\n+\n+\tchosen = list_and_choose(&menu_opts, &menu_stuff);\n+\titems = del_list.items;\n+\tfor(i = 0, j = 0; i < del_list.nr; i++) {\n+\t\tif (i < chosen[j]) {\n+\t\t\t*(items[i].string) = '\\0';\n+\t\t} else if (i == chosen[j]) {\n+\t\t\t/* delete selected item */\n+\t\t\tj++;\n+\t\t\tcontinue;\n+\t\t} else {\n+\t\t\t/* end of chosen (chosen[j] == EOF), won't delete */\n+\t\t\t*(items[i].string) = '\\0';\n+\t\t}\n+\t}\n+\n+\tstring_list_remove_empty_items(&del_list, 0);\n+\n+\tfree(chosen);\n+\treturn 0;\n+}\n+\n static int quit_cmd(void)\n {\n \tstring_list_clear(&del_list, 0);\n@@ -728,6 +765,7 @@ static int help_cmd(void)\n \tprintf_ln(_(\n \t\t    \"clean               - start cleaning\\n\"\n \t\t    \"filter by pattern   - exclude items from deletion\\n\"\n+\t\t    \"select by numbers   - select items to be deleted by numbers\\n\"\n \t\t    \"quit                - stop cleaning\\n\"\n \t\t    \"help                - this screen\\n\"\n \t\t    \"?                   - help for prompt selection\"\n@@ -744,6 +782,7 @@ static void interactive_main_loop(void)\n \t\tstruct menu_item menus[] = {\n \t\t\t{'c', \"clean\",\t\t\t0, clean_cmd},\n \t\t\t{'f', \"filter by pattern\",\t0, filter_by_patterns_cmd},\n+\t\t\t{'s', \"select by numbers\",\t0, select_by_numbers_cmd},\n \t\t\t{'q', \"quit\",\t\t\t0, quit_cmd},\n \t\t\t{'h', \"help\",\t\t\t0, help_cmd},\n \t\t};\n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217317","messageId":"dd85bb5724f571585a26d41b01b987616ff7cfb2.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"[PATCH v9 8/9] git-clean: add ask each interactive action","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:22Z","receivedAt":"2013-05-14T08:45:22Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Add a new action for interactive git-clean: ask each. It's just like\nthe \"rm -i\" command, that the user must confirm one by one for each\nfile or directory to be cleaned.\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\n---\n builtin/clean.c | 36 ++++++++++++++++++++++++++++++++++++\n 1 file changed, 36 insertions(+)\n\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 74fc9..fc68b 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -752,6 +752,40 @@ static int select_by_numbers_cmd(void)\n \treturn 0;\n }\n \n+static int ask_each_cmd(void)\n+{\n+\tstruct strbuf confirm = STRBUF_INIT;\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tstruct string_list_item *item;\n+\tconst char *qname;\n+\tint changed = 0, eof = 0;\n+\n+\tfor_each_string_list_item(item, &del_list) {\n+\t\t/* Ctrl-D should stop removing files */\n+\t\tif (!eof) {\n+\t\t\tqname = quote_path_relative(item->string, -1, &buf, NULL);\n+\t\t\tprintf(_(\"remove %s? \"), qname);\n+\t\t\tif (strbuf_getline(&confirm, stdin, '\\n') != EOF) {\n+\t\t\t\tstrbuf_trim(&confirm);\n+\t\t\t} else {\n+\t\t\t\tputchar('\\n');\n+\t\t\t\teof = 1;\n+\t\t\t}\n+\t\t}\n+\t\tif (!confirm.len || !strncasecmp(confirm.buf, \"no\", confirm.len)) {\n+\t\t\t*item->string = '\\0';\n+\t\t\tchanged++;\n+\t\t}\n+\t}\n+\n+\tif (changed)\n+\t\tstring_list_remove_empty_items(&del_list, 0);\n+\n+\tstrbuf_release(&buf);\n+\tstrbuf_release(&confirm);\n+\treturn MENU_RETURN_NO_LOOP;\n+}\n+\n static int quit_cmd(void)\n {\n \tstring_list_clear(&del_list, 0);\n@@ -766,6 +800,7 @@ static int help_cmd(void)\n \t\t    \"clean               - start cleaning\\n\"\n \t\t    \"filter by pattern   - exclude items from deletion\\n\"\n \t\t    \"select by numbers   - select items to be deleted by numbers\\n\"\n+\t\t    \"ask each            - confirm each deletion (like \\\"rm -i\\\")\\n\"\n \t\t    \"quit                - stop cleaning\\n\"\n \t\t    \"help                - this screen\\n\"\n \t\t    \"?                   - help for prompt selection\"\n@@ -783,6 +818,7 @@ static void interactive_main_loop(void)\n \t\t\t{'c', \"clean\",\t\t\t0, clean_cmd},\n \t\t\t{'f', \"filter by pattern\",\t0, filter_by_patterns_cmd},\n \t\t\t{'s', \"select by numbers\",\t0, select_by_numbers_cmd},\n+\t\t\t{'a', \"ask each\",\t\t0, ask_each_cmd},\n \t\t\t{'q', \"quit\",\t\t\t0, quit_cmd},\n \t\t\t{'h', \"help\",\t\t\t0, help_cmd},\n \t\t};\n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217318","messageId":"d85f9305d99bcdccbd54421de88c95d625c38f57.1368518327.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"[PATCH v9 9/9] git-clean: add documentation for interactive git-clean","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-14T08:45:23Z","receivedAt":"2013-05-14T08:45:23Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Add new section \"Interactive mode\" for documentation of interactive\ngit-clean.\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\n---\n Documentation/git-clean.txt | 65 +++++++++++++++++++++++++++++++++++++++++++--\n 1 file changed, 63 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-clean.txt b/Documentation/git-clean.txt\nindex 186e34..5bf76 100644\n--- a/Documentation/git-clean.txt\n+++ b/Documentation/git-clean.txt\n@@ -39,8 +39,8 @@ OPTIONS\n \n -i::\n --interactive::\n-\tShow what would be done and the user must confirm before actually\n-\tcleaning.\n+\tShow what would be done and clean files interactively. See\n+\t``Interactive mode'' for details.\n \n -n::\n --dry-run::\n@@ -69,6 +69,67 @@ OPTIONS\n \tRemove only files ignored by Git.  This may be useful to rebuild\n \teverything from scratch, but keep manually created files.\n \n+Interactive mode\n+----------------\n+When the command enters the interactive mode, it shows the\n+files and directories to be cleaned, and goes into its\n+interactive command loop.\n+\n+The command loop shows the list of subcommands available, and\n+gives a prompt \"What now> \".  In general, when the prompt ends\n+with a single '>', you can pick only one of the choices given\n+and type return, like this:\n+\n+------------\n+    *** Commands ***\n+        1: clean                2: filter by pattern    3: select by numbers\n+        4: ask each             5: quit                 6: help\n+    What now> 1\n+------------\n+\n+You also could say `c` or `clean` above as long as the choice is unique.\n+\n+The main command loop has 6 subcommands.\n+\n+clean::\n+\n+   Start cleaning files and directories, and then quit.\n+\n+filter by pattern::\n+\n+   This shows the files and directories to be deleted and issues an\n+   \"Input ignore patterns>>\" prompt. You can input space-seperated\n+   patterns to exclude files and directories from deletion.\n+   E.g. \"*.c *.h\" will excludes files end with \".c\" and \".h\" from\n+   deletion. When you are satisfied with the filtered result, press\n+   ENTER (empty) back to the main menu.\n+\n+select by numbers::\n+\n+   This shows the files and directories to be deleted and issues an\n+   \"Select items to delete>>\" prompt. When the prompt ends with double\n+   '>>' like this, you can make more than one selection, concatenated\n+   with whitespace or comma.  Also you can say ranges.  E.g. \"2-5 7,9\"\n+   to choose 2,3,4,5,7,9 from the list.  If the second number in a\n+   range is omitted, all remaining patches are taken.  E.g. \"7-\" to\n+   choose 7,8,9 from the list.  You can say '*' to choose everything.\n+   Also when you are satisfied with the filtered result, press ENTER\n+   (empty) back to the main menu.\n+\n+ask each::\n+\n+  This will start to clean, and you must confirm one by one in order\n+  to delete items. Please note that this action is not as efficient\n+  as the above two actions.\n+\n+quit::\n+\n+  This lets you quit without do cleaning.\n+\n+help::\n+\n+  Show brief usage of interactive git-clean.\n+\n SEE ALSO\n --------\n linkgit:gitignore[5]\n-- \n1.8.3.rc1.404.gb9fcf3e\n"},{"id":"217397","messageId":"7vvc6ldtx7.fsf@alter.siamese.dyndns.org","threadId":"33816","inReplyTo":"7c551bf22bc45cfcdd62d1baf6300f3f86244312.1368518327.git.worldhello.net@gmail.com","subject":"Re: [PATCH v9 1/9] git-clean: refactor git-clean into two phases","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-05-14T23:27:00Z","receivedAt":"2013-05-14T23:27:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jiang Xin <worldhello.net@gmail.com> writes:\n\n> +/*\n> + * Give path as relative to prefix.\n> + *\n> + * This function is a combination of path_relative (in quote.c) and\n> + * relative_path (in path.c)\n> + */\n> +static const char *path_relative(const char *in, const char *prefix)\n> +{\n> +...\n\nHmph.  Is it possible to reuse the public one (in path.c) here and\nin quote.c, perhaps after enhancing it a bit to serve needs of the\ncallers of two existing ones and the new callers of this one?\n\n> @@ -242,11 +287,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n>  \t\t\t\tcontinue; /* Yup, this one exists unmerged */\n>  \t\t}\n>  \n> -\t\t/*\n> -\t\t * we might have removed this as part of earlier\n> -\t\t * recursive directory removal, so lstat() here could\n> -\t\t * fail with ENOENT.\n> -\t\t */\n>  \t\tif (lstat(ent->name, &st))\n>  \t\t\tcontinue;\n\nI am guessing that the reason why you removed the comment is because\nduring this phase there is no way we \"might have removed\".  But if\nthat is the case, does it still make sense to run lstat() and ignore\nerrors from the call?\n"},{"id":"217398","messageId":"7vr4h9dtwa.fsf@alter.siamese.dyndns.org","threadId":"33816","inReplyTo":"cover.1368518327.git.worldhello.net@gmail.com","subject":"Re: [PATCH v9 0/9] interactive git-clean","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-05-14T23:27:33Z","receivedAt":"2013-05-14T23:27:33Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Except for one nit in 1/9, the series seems to be nicely done.\n"},{"id":"217403","messageId":"CANYiYbF3AC=iHaOkj96yhBSj8caC2SdZDygrxV43+mzAd-wGJA@mail.gmail.com","threadId":"33816","inReplyTo":"7vvc6ldtx7.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v9 1/9] git-clean: refactor git-clean into two phases","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-15T00:40:22Z","receivedAt":"2013-05-15T00:40:22Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"2013/5/15 Junio C Hamano <gitster@pobox.com>:\n>> @@ -242,11 +287,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n>>                               continue; /* Yup, this one exists unmerged */\n>>               }\n>>\n>> -             /*\n>> -              * we might have removed this as part of earlier\n>> -              * recursive directory removal, so lstat() here could\n>> -              * fail with ENOENT.\n>> -              */\n>>               if (lstat(ent->name, &st))\n>>                       continue;\n>\n> I am guessing that the reason why you removed the comment is because\n> during this phase there is no way we \"might have removed\".  But if\n> that is the case, does it still make sense to run lstat() and ignore\n> errors from the call?\n>\n\nRun lstat() here is necessary, because we need to check whether\nent->name points to a file or a directory. If ent points to a directory,\nonly add to del_list when user provides '-x' option to git-clean.\n\n                if (S_ISDIR(st.st_mode)) {\n                        if (remove_directories || (matches ==\nMATCHED_EXACTLY)) {\n                                rel = path_relative(ent->name, prefix);\n                                string_list_append(&del_list, rel);\n                        }\n                } ...\n\n\nWhen start to do cleaning, there is a duplicate lstat() call, and the removed\ncomments above are moved here.\n\n                /*\n                 * we might have removed this as part of earlier\n                 * recursive directory removal, so lstat() here could\n                 * fail with ENOENT.\n                 */\n                if (lstat(abs_path.buf, &st))\n                        continue;\n\n                if (S_ISDIR(st.st_mode)) {\n                        if (remove_dirs(&abs_path, prefix, rm_flags,\ndry_run, quiet, &gone))\n                                errors++;\n                        if (gone && !quiet) {\n                                qname =\nquote_path_relative(item->string, -1, &buf, NULL);\n                                printf(dry_run ? _(msg_would_remove) :\n_(msg_remove), qname);\n                        }\n                }\n\nBut I am not clear how \"earlier recursive directory removal\" could\nmake lstat() failure here. If it is not the case, maybe we can test a\ndirectory by ent->name (ends with '/' or '\\\\' ?).\n\n-- \nJiang Xin\n"},{"id":"217439","messageId":"7vbo8ce14u.fsf@alter.siamese.dyndns.org","threadId":"33816","inReplyTo":"CANYiYbF3AC=iHaOkj96yhBSj8caC2SdZDygrxV43+mzAd-wGJA@mail.gmail.com","subject":"Re: [PATCH v9 1/9] git-clean: refactor git-clean into two phases","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-05-15T15:03:29Z","receivedAt":"2013-05-15T15:03:29Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jiang Xin <worldhello.net@gmail.com> writes:\n\n> 2013/5/15 Junio C Hamano <gitster@pobox.com>:\n>>> @@ -242,11 +287,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n>>>                               continue; /* Yup, this one exists unmerged */\n>>>               }\n>>>\n>>> -             /*\n>>> -              * we might have removed this as part of earlier\n>>> -              * recursive directory removal, so lstat() here could\n>>> -              * fail with ENOENT.\n>>> -              */\n>>>               if (lstat(ent->name, &st))\n>>>                       continue;\n>>\n>> I am guessing that the reason why you removed the comment is because\n>> during this phase there is no way we \"might have removed\".  But if\n>> that is the case, does it still make sense to run lstat() and ignore\n>> errors from the call?\n>>\n>\n> Run lstat() here is necessary, because we need to check whether\n> ent->name points to a file or a directory. If ent points to a directory,\n> only add to del_list when user provides '-x' option to git-clean.\n\nSorry, but that was not the question; we can see st is used\nimmediately below so somebody needs to fill it.\n\nI was pointing out that the \"lstat() is expected to fail with ENOENT\nbut it is not an error worth reporting\" justification the original\ncode had to silently ignore an error, because you no longer remove\nanything immediately in this part of the code.  Is \"if () continue\"\nstill valid thing to do here, not \"if () die()\"?\n"},{"id":"217440","messageId":"CANYiYbF+JLqyB19gHxcC+-ysM=hbMZMve=4JNoiu15B=jaUS9g@mail.gmail.com","threadId":"33816","inReplyTo":"7vbo8ce14u.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v9 1/9] git-clean: refactor git-clean into two phases","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-15T15:07:27Z","receivedAt":"2013-05-15T15:07:27Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"2013/5/15 Junio C Hamano <gitster@pobox.com>:\n> Jiang Xin <worldhello.net@gmail.com> writes:\n>\n>> 2013/5/15 Junio C Hamano <gitster@pobox.com>:\n>>>> @@ -242,11 +287,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n>>>>                               continue; /* Yup, this one exists unmerged */\n>>>>               }\n>>>>\n>>>> -             /*\n>>>> -              * we might have removed this as part of earlier\n>>>> -              * recursive directory removal, so lstat() here could\n>>>> -              * fail with ENOENT.\n>>>> -              */\n>>>>               if (lstat(ent->name, &st))\n>>>>                       continue;\n>>>\n>>> I am guessing that the reason why you removed the comment is because\n>>> during this phase there is no way we \"might have removed\".  But if\n>>> that is the case, does it still make sense to run lstat() and ignore\n>>> errors from the call?\n>>>\n>>\n>> Run lstat() here is necessary, because we need to check whether\n>> ent->name points to a file or a directory. If ent points to a directory,\n>> only add to del_list when user provides '-x' option to git-clean.\n>\n> Sorry, but that was not the question; we can see st is used\n> immediately below so somebody needs to fill it.\n>\n> I was pointing out that the \"lstat() is expected to fail with ENOENT\n> but it is not an error worth reporting\" justification the original\n> code had to silently ignore an error, because you no longer remove\n> anything immediately in this part of the code.  Is \"if () continue\"\n> still valid thing to do here, not \"if () die()\"?\n\nI'm clear, it could be:\n\n                if (lstat(ent->name, &st))\n                        die_errno(\"Cannot lstat '%s'\", ent->name);\n\n\n-- \nJiang Xin\n"},{"id":"217442","messageId":"cover.1368630636.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"7vvc6ldtx7.fsf@alter.siamese.dyndns.org","subject":"[RFC 0/2] refactor relative_path in path.c","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-15T15:18:37Z","receivedAt":"2013-05-15T15:18:37Z","isPatch":false,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"2013/5/15 Junio C Hamano <gitster@pobox.com>:\n> Jiang Xin <worldhello.net@gmail.com> writes:\n>\n>> +/*\n>> + * Give path as relative to prefix.\n>> + *\n>> + * This function is a combination of path_relative (in quote.c) and\n>> + * relative_path (in path.c)\n>> + */\n>> +static const char *path_relative(const char *in, const char *prefix)\n>> +{\n>> +...\n>\n> Hmph.  Is it possible to reuse the public one (in path.c) here and\n> in quote.c, perhaps after enhancing it a bit to serve needs of the\n> callers of two existing ones and the new callers of this one?\n>\n\nThese two patches enhance relative_path() in path.c, so that function\nrelative_path() will return real relative path, not a path strip off\nthe prefix.\n\nThe 2nd patch is a bit aggressive, it refactor all related functions,\nremove unnecessary arguments: len and/or prefix_len.\n\nPlease review them. They will be prerequisites for the interactive\ngit-clean patch series.\n\nJiang Xin (2):\n  path.c: refactor relative_path(), not only strip prefix\n  quote.c: remove path_relative, use relative_path instead\n\n builtin/clean.c    | 18 +++++------\n builtin/grep.c     |  4 +--\n builtin/ls-files.c | 13 ++++----\n path.c             | 94 ++++++++++++++++++++++++++++++++++++++++++------------\n quote.c            | 71 +++--------------------------------------\n quote.h            |  7 ++--\n wt-status.c        | 17 +++++-----\n 7 files changed, 107 insertions(+), 117 deletions(-)\n\n-- \n1.8.3.rc1.404.ga32c147\n"},{"id":"217443","messageId":"7f77be827e21940dfe415d34f7239799d999fef4.1368630636.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368630636.git.worldhello.net@gmail.com","subject":"[RFC 1/2] path.c: refactor relative_path(), not only strip prefix","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-15T15:18:38Z","receivedAt":"2013-05-15T15:18:38Z","isPatch":false,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Original design of relative_path() is simple, just strip the prefix\n(*base) from the abosolute path (*abs). In most cases, we need a real\nrelative path, such as: ../foo, ../../bar. That's why there is another\nreimplementation (path_relative()) in quote.c.\n\nRefactor relative_path() in path.c to return real relative path, so\nthat user can reuse this function without reimplement his/her own.\nI will use this method for interactive git-clean later. Some of the\nimplementations are borrowed from path_relative() in quote.c.\n\nDifferent results for relative_path() before and after this refactor:\n\n    base path  abs path  relative (orignal)  relative (refactor)\n    =========  ========  ==================  ===================\n    /a/b       /a/b/c/   c/                  c/\n    //a///b/   /a/b//c/  c/                  c/\n    /a/b       /a/b/     .                   ./\n    /a/b/      /a        /a                  ../\n    /a/b/      /         /                   ../../\n    /a/b/      /a/c      /a/c                ../c\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\n---\n path.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------------\n 1 file changed, 74 insertions(+), 20 deletions(-)\n\ndiff --git a/path.c b/path.c\nindex 04ff..4dafa8 100644\n--- a/path.c\n+++ b/path.c\n@@ -444,38 +444,92 @@ int adjust_shared_perm(const char *path)\n const char *relative_path(const char *abs, const char *base)\n {\n \tstatic char buf[PATH_MAX + 1];\n-\tint i = 0, j = 0;\n+\tint abs_off, base_off, i, j;\n+\tint abs_len, base_len;\n \n \tif (!base || !base[0])\n \t\treturn abs;\n-\twhile (base[i]) {\n+\n+\tabs_len = strlen(abs);\n+\tbase_len = strlen(base);\n+\n+\tabs_off = 0;\n+\tbase_off = 0;\n+\ti = 0;\n+\tj = 0;\n+\twhile (i < base_len && j < abs_len && base[i] == abs[j]) {\n \t\tif (is_dir_sep(base[i])) {\n-\t\t\tif (!is_dir_sep(abs[j]))\n-\t\t\t\treturn abs;\n \t\t\twhile (is_dir_sep(base[i]))\n \t\t\t\ti++;\n \t\t\twhile (is_dir_sep(abs[j]))\n \t\t\t\tj++;\n-\t\t\tcontinue;\n-\t\t} else if (abs[j] != base[i]) {\n+\t\t\tbase_off = i;\n+\t\t\tabs_off = j;\n+\t\t} else {\n+\t\t\ti++;\n+\t\t\tj++;\n+\t\t}\n+\t}\n+\tif (\n+\t    /* base seems like prefix of abs */\n+\t    i >= base_len &&\n+\t    /*\n+\t     * but \"/foo\" is not a prefix of \"/foobar\"\n+\t     * (i.e. base not end with '/')\n+\t     */\n+\t    base_off < base_len) {\n+\t\tif (j >= abs_len) {\n+\t\t\t/* abs=\"/a/b\", base=\"/a/b\" */\n+\t\t\tabs_off = abs_len;\n+\t\t} else if (is_dir_sep(abs[j])) {\n+\t\t\t/* abs=\"/a/b/c\", base=\"/a/b\" */\n+\t\t\twhile (is_dir_sep(abs[j]))\n+\t\t\t\tj++;\n+\t\t\tabs_off = j;\n+\t\t} else {\n+\t\t\t/* abs=\"/a/bbb/c\", base=\"/a/b\" */\n+\t\t\ti = base_off;\n+\t\t}\n+\t} else if (\n+\t\t   /* abs is short than base (prefix of base) */\n+\t\t   j >= abs_len &&\n+\t\t   /* abs not end with '/' */\n+\t\t   abs_off < abs_len) {\n+\t\tif (is_dir_sep(base[i])) {\n+\t\t\t/* abs=\"/a/b\", base=\"/a/b/c/\" */\n+\t\t\twhile (is_dir_sep(base[i]))\n+\t\t\t\ti++;\n+\t\t\tabs_off = abs_len;\n+\t\t}\n+\t}\n+\tabs += abs_off;\n+\tabs_len -= abs_off;\n+\n+\t/* base is prefix of abs */\n+\tif (i >= base_len) {\n+\t\tif (*abs == '\\0') {\n+\t\t\tstrcpy(buf, \"./\");\n+\t\t\treturn buf;\n+\t\t} else {\n \t\t\treturn abs;\n \t\t}\n+\t}\n+\n+\tbuf[0] = '\\0';\n+\twhile (i < base_len) {\n+\t\tif (is_dir_sep(base[i])) {\n+\t\t\tstrcat(buf, \"../\");\n+\t\t\twhile (is_dir_sep(base[i]))\n+\t\t\t\ti++;\n+\t\t\tcontinue;\n+\t\t}\n \t\ti++;\n-\t\tj++;\n \t}\n-\tif (\n-\t    /* \"/foo\" is a prefix of \"/foo\" */\n-\t    abs[j] &&\n-\t    /* \"/foo\" is not a prefix of \"/foobar\" */\n-\t    !is_dir_sep(base[i-1]) && !is_dir_sep(abs[j])\n-\t   )\n-\t\treturn abs;\n-\twhile (is_dir_sep(abs[j]))\n-\t\tj++;\n-\tif (!abs[j])\n-\t\tstrcpy(buf, \".\");\n-\telse\n-\t\tstrcpy(buf, abs + j);\n+\tif (!is_dir_sep(base[base_len - 1]))\n+\t\tstrcat(buf, \"../\");\n+\n+\tstrcat(buf, abs);\n+\n \treturn buf;\n }\n \n-- \n1.8.3.rc1.404.ga32c147\n"},{"id":"217444","messageId":"8c7a22b9ef2515e22975f819b24841cac4fba8f5.1368630636.git.worldhello.net@gmail.com","threadId":"33816","inReplyTo":"cover.1368630636.git.worldhello.net@gmail.com","subject":"[RFC 2/2] quote.c: remove path_relative, use relative_path instead","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2013-05-15T15:18:39Z","receivedAt":"2013-05-15T15:18:39Z","isPatch":false,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"Since there is a enhanced version of relative_path() in path.c,\nremove duplicate counterpart path_relative() in quote.c.\n\nAlso refactor related functions, remove unnecessary arguments:\nlen and/or prefix_len.\n\nSigned-off-by: Jiang Xin <worldhello.net@gmail.com>\n---\n builtin/clean.c    | 18 +++++++-------\n builtin/grep.c     |  4 +--\n builtin/ls-files.c | 13 +++++-----\n quote.c            | 71 ++++--------------------------------------------------\n quote.h            |  7 +++---\n wt-status.c        | 17 ++++++-------\n 6 files changed, 33 insertions(+), 97 deletions(-)\n\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 04e39..a93c3 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -56,7 +56,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,\n \tif ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) &&\n \t\t\t!resolve_gitlink_ref(path->buf, \"HEAD\", submodule_head)) {\n \t\tif (!quiet) {\n-\t\t\tquote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);\n+\t\t\tquote_path_relative(path->buf, &quoted, prefix);\n \t\t\tprintf(dry_run ?  _(msg_would_skip_git_dir) : _(msg_skip_git_dir),\n \t\t\t\t\tquoted.buf);\n \t\t}\n@@ -70,7 +70,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,\n \t\t/* an empty dir could be removed even if it is unreadble */\n \t\tres = dry_run ? 0 : rmdir(path->buf);\n \t\tif (res) {\n-\t\t\tquote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);\n+\t\t\tquote_path_relative(path->buf, &quoted, prefix);\n \t\t\twarning(_(msg_warn_remove_failed), quoted.buf);\n \t\t\t*dir_gone = 0;\n \t\t}\n@@ -94,7 +94,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,\n \t\t\tif (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone))\n \t\t\t\tret = 1;\n \t\t\tif (gone) {\n-\t\t\t\tquote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);\n+\t\t\t\tquote_path_relative(path->buf, &quoted, prefix);\n \t\t\t\tstring_list_append(&dels, quoted.buf);\n \t\t\t} else\n \t\t\t\t*dir_gone = 0;\n@@ -102,10 +102,10 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,\n \t\t} else {\n \t\t\tres = dry_run ? 0 : unlink(path->buf);\n \t\t\tif (!res) {\n-\t\t\t\tquote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);\n+\t\t\t\tquote_path_relative(path->buf, &quoted, prefix);\n \t\t\t\tstring_list_append(&dels, quoted.buf);\n \t\t\t} else {\n-\t\t\t\tquote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);\n+\t\t\t\tquote_path_relative(path->buf, &quoted, prefix);\n \t\t\t\twarning(_(msg_warn_remove_failed), quoted.buf);\n \t\t\t\t*dir_gone = 0;\n \t\t\t\tret = 1;\n@@ -127,7 +127,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,\n \t\tif (!res)\n \t\t\t*dir_gone = 1;\n \t\telse {\n-\t\t\tquote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);\n+\t\t\tquote_path_relative(path->buf, &quoted, prefix);\n \t\t\twarning(_(msg_warn_remove_failed), quoted.buf);\n \t\t\t*dir_gone = 0;\n \t\t\tret = 1;\n@@ -262,7 +262,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t\t\tif (remove_dirs(&directory, prefix, rm_flags, dry_run, quiet, &gone))\n \t\t\t\t\terrors++;\n \t\t\t\tif (gone && !quiet) {\n-\t\t\t\t\tqname = quote_path_relative(directory.buf, directory.len, &buf, prefix);\n+\t\t\t\t\tqname = quote_path_relative(directory.buf, &buf, prefix);\n \t\t\t\t\tprintf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);\n \t\t\t\t}\n \t\t\t}\n@@ -272,11 +272,11 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t\t\tcontinue;\n \t\t\tres = dry_run ? 0 : unlink(ent->name);\n \t\t\tif (res) {\n-\t\t\t\tqname = quote_path_relative(ent->name, -1, &buf, prefix);\n+\t\t\t\tqname = quote_path_relative(ent->name, &buf, prefix);\n \t\t\t\twarning(_(msg_warn_remove_failed), qname);\n \t\t\t\terrors++;\n \t\t\t} else if (!quiet) {\n-\t\t\t\tqname = quote_path_relative(ent->name, -1, &buf, prefix);\n+\t\t\t\tqname = quote_path_relative(ent->name, &buf, prefix);\n \t\t\t\tprintf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);\n \t\t\t}\n \t\t}\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 159e65..b5222 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -286,7 +286,7 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,\n \tstruct strbuf pathbuf = STRBUF_INIT;\n \n \tif (opt->relative && opt->prefix_length) {\n-\t\tquote_path_relative(filename + tree_name_len, -1, &pathbuf,\n+\t\tquote_path_relative(filename + tree_name_len, &pathbuf,\n \t\t\t\t    opt->prefix);\n \t\tstrbuf_insert(&pathbuf, 0, filename, tree_name_len);\n \t} else {\n@@ -318,7 +318,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)\n \tstruct strbuf buf = STRBUF_INIT;\n \n \tif (opt->relative && opt->prefix_length)\n-\t\tquote_path_relative(filename, -1, &buf, opt->prefix);\n+\t\tquote_path_relative(filename, &buf, opt->prefix);\n \telse\n \t\tstrbuf_addstr(&buf, filename);\n \ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 220207..bb563 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -46,10 +46,9 @@ static const char *tag_modified = \"\";\n static const char *tag_skip_worktree = \"\";\n static const char *tag_resolve_undo = \"\";\n \n-static void write_name(const char* name, size_t len)\n+static void write_name(const char* name)\n {\n-\twrite_name_quoted_relative(name, len, prefix, prefix_len, stdout,\n-\t\t\tline_terminator);\n+\twrite_name_quoted_relative(name, prefix, stdout, line_terminator);\n }\n \n static void show_dir_entry(const char *tag, struct dir_entry *ent)\n@@ -63,7 +62,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)\n \t\treturn;\n \n \tfputs(tag, stdout);\n-\twrite_name(ent->name, ent->len);\n+\twrite_name(ent->name);\n }\n \n static void show_other_files(struct dir_struct *dir)\n@@ -163,7 +162,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)\n \t\t       find_unique_abbrev(ce->sha1,abbrev),\n \t\t       ce_stage(ce));\n \t}\n-\twrite_name(ce->name, ce_namelen(ce));\n+\twrite_name(ce->name);\n \tif (debug_mode) {\n \t\tprintf(\"  ctime: %d:%d\\n\", ce->ce_ctime.sec, ce->ce_ctime.nsec);\n \t\tprintf(\"  mtime: %d:%d\\n\", ce->ce_mtime.sec, ce->ce_mtime.nsec);\n@@ -196,7 +195,7 @@ static void show_ru_info(void)\n \t\t\tprintf(\"%s%06o %s %d\\t\", tag_resolve_undo, ui->mode[i],\n \t\t\t       find_unique_abbrev(ui->sha1[i], abbrev),\n \t\t\t       i + 1);\n-\t\t\twrite_name(path, len);\n+\t\t\twrite_name(path);\n \t\t}\n \t}\n }\n@@ -389,7 +388,7 @@ int report_path_error(const char *ps_matched, const char **pathspec, const char\n \t\tif (found_dup)\n \t\t\tcontinue;\n \n-\t\tname = quote_path_relative(pathspec[num], -1, &sb, prefix);\n+\t\tname = quote_path_relative(pathspec[num], &sb, prefix);\n \t\terror(\"pathspec '%s' did not match any file(s) known to git.\",\n \t\t      name);\n \t\terrors++;\ndiff --git a/quote.c b/quote.c\nindex 91122..97c57 100644\n--- a/quote.c\n+++ b/quote.c\n@@ -312,81 +312,20 @@ void write_name_quotedpfx(const char *pfx, size_t pfxlen,\n \tfputc(terminator, fp);\n }\n \n-static const char *path_relative(const char *in, int len,\n-\t\t\t\t struct strbuf *sb, const char *prefix,\n-\t\t\t\t int prefix_len);\n-\n-void write_name_quoted_relative(const char *name, size_t len,\n-\t\t\t\tconst char *prefix, size_t prefix_len,\n+void write_name_quoted_relative(const char *name, const char *prefix,\n \t\t\t\tFILE *fp, int terminator)\n {\n-\tstruct strbuf sb = STRBUF_INIT;\n-\n-\tname = path_relative(name, len, &sb, prefix, prefix_len);\n+\tname = relative_path(name, prefix);\n \twrite_name_quoted(name, fp, terminator);\n-\n-\tstrbuf_release(&sb);\n-}\n-\n-/*\n- * Give path as relative to prefix.\n- *\n- * The strbuf may or may not be used, so do not assume it contains the\n- * returned path.\n- */\n-static const char *path_relative(const char *in, int len,\n-\t\t\t\t struct strbuf *sb, const char *prefix,\n-\t\t\t\t int prefix_len)\n-{\n-\tint off, i;\n-\n-\tif (len < 0)\n-\t\tlen = strlen(in);\n-\tif (prefix_len < 0) {\n-\t\tif (prefix)\n-\t\t\tprefix_len = strlen(prefix);\n-\t\telse\n-\t\t\tprefix_len = 0;\n-\t}\n-\n-\toff = 0;\n-\ti = 0;\n-\twhile (i < prefix_len && i < len && prefix[i] == in[i]) {\n-\t\tif (prefix[i] == '/')\n-\t\t\toff = i + 1;\n-\t\ti++;\n-\t}\n-\tin += off;\n-\tlen -= off;\n-\n-\tif (i >= prefix_len)\n-\t\treturn in;\n-\n-\tstrbuf_reset(sb);\n-\tstrbuf_grow(sb, len);\n-\n-\twhile (i < prefix_len) {\n-\t\tif (prefix[i] == '/')\n-\t\t\tstrbuf_addstr(sb, \"../\");\n-\t\ti++;\n-\t}\n-\tstrbuf_add(sb, in, len);\n-\n-\treturn sb->buf;\n }\n \n /* quote path as relative to the given prefix */\n-char *quote_path_relative(const char *in, int len,\n-\t\t\t  struct strbuf *out, const char *prefix)\n+char *quote_path_relative(const char *in, struct strbuf *out,\n+\t\t\t  const char *prefix)\n {\n-\tstruct strbuf sb = STRBUF_INIT;\n-\tconst char *rel = path_relative(in, len, &sb, prefix, -1);\n+\tconst char *rel = relative_path(in, prefix);\n \tstrbuf_reset(out);\n \tquote_c_style_counted(rel, strlen(rel), out, NULL, 0);\n-\tstrbuf_release(&sb);\n-\n-\tif (!out->len)\n-\t\tstrbuf_addstr(out, \"./\");\n \n \treturn out->buf;\n }\ndiff --git a/quote.h b/quote.h\nindex 13315..27022 100644\n--- a/quote.h\n+++ b/quote.h\n@@ -60,12 +60,11 @@ extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);\n extern void write_name_quoted(const char *name, FILE *, int terminator);\n extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,\n                                  const char *name, FILE *, int terminator);\n-extern void write_name_quoted_relative(const char *name, size_t len,\n-\t\tconst char *prefix, size_t prefix_len,\n-\t\tFILE *fp, int terminator);\n+extern void write_name_quoted_relative(const char *name, const char *prefix,\n+\t\t\t\t       FILE *fp, int terminator);\n \n /* quote path as relative to the given prefix */\n-extern char *quote_path_relative(const char *in, int len,\n+extern char *quote_path_relative(const char *in,\n \t\t\t  struct strbuf *out, const char *prefix);\n \n /* quoting as a string literal for other languages */\ndiff --git a/wt-status.c b/wt-status.c\nindex bf84a..002ac 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -243,7 +243,7 @@ static void wt_status_print_unmerged_data(struct wt_status *s,\n \tstruct strbuf onebuf = STRBUF_INIT;\n \tconst char *one, *how = _(\"bug\");\n \n-\tone = quote_path(it->string, -1, &onebuf, s->prefix);\n+\tone = quote_path(it->string, &onebuf, s->prefix);\n \tstatus_printf(s, color(WT_STATUS_HEADER, s), \"\\t\");\n \tswitch (d->stagemask) {\n \tcase 1: how = _(\"both deleted:\"); break;\n@@ -297,8 +297,8 @@ static void wt_status_print_change_data(struct wt_status *s,\n \t\t    change_type);\n \t}\n \n-\tone = quote_path(one_name, -1, &onebuf, s->prefix);\n-\ttwo = quote_path(two_name, -1, &twobuf, s->prefix);\n+\tone = quote_path(one_name, &onebuf, s->prefix);\n+\ttwo = quote_path(two_name, &twobuf, s->prefix);\n \n \tstatus_printf(s, color(WT_STATUS_HEADER, s), \"\\t\");\n \tswitch (status) {\n@@ -706,8 +706,7 @@ static void wt_status_print_other(struct wt_status *s,\n \t\tstruct string_list_item *it;\n \t\tconst char *path;\n \t\tit = &(l->items[i]);\n-\t\tpath = quote_path(it->string, strlen(it->string),\n-\t\t\t\t  &buf, s->prefix);\n+\t\tpath = quote_path(it->string, &buf, s->prefix);\n \t\tif (column_active(s->colopts)) {\n \t\t\tstring_list_append(&output, path);\n \t\t\tcontinue;\n@@ -1289,7 +1288,7 @@ static void wt_shortstatus_unmerged(struct string_list_item *it,\n \t} else {\n \t\tstruct strbuf onebuf = STRBUF_INIT;\n \t\tconst char *one;\n-\t\tone = quote_path(it->string, -1, &onebuf, s->prefix);\n+\t\tone = quote_path(it->string, &onebuf, s->prefix);\n \t\tprintf(\" %s\\n\", one);\n \t\tstrbuf_release(&onebuf);\n \t}\n@@ -1317,7 +1316,7 @@ static void wt_shortstatus_status(struct string_list_item *it,\n \t\tstruct strbuf onebuf = STRBUF_INIT;\n \t\tconst char *one;\n \t\tif (d->head_path) {\n-\t\t\tone = quote_path(d->head_path, -1, &onebuf, s->prefix);\n+\t\t\tone = quote_path(d->head_path, &onebuf, s->prefix);\n \t\t\tif (*one != '\"' && strchr(one, ' ') != NULL) {\n \t\t\t\tputchar('\"');\n \t\t\t\tstrbuf_addch(&onebuf, '\"');\n@@ -1326,7 +1325,7 @@ static void wt_shortstatus_status(struct string_list_item *it,\n \t\t\tprintf(\"%s -> \", one);\n \t\t\tstrbuf_release(&onebuf);\n \t\t}\n-\t\tone = quote_path(it->string, -1, &onebuf, s->prefix);\n+\t\tone = quote_path(it->string, &onebuf, s->prefix);\n \t\tif (*one != '\"' && strchr(one, ' ') != NULL) {\n \t\t\tputchar('\"');\n \t\t\tstrbuf_addch(&onebuf, '\"');\n@@ -1345,7 +1344,7 @@ static void wt_shortstatus_other(struct string_list_item *it,\n \t} else {\n \t\tstruct strbuf onebuf = STRBUF_INIT;\n \t\tconst char *one;\n-\t\tone = quote_path(it->string, -1, &onebuf, s->prefix);\n+\t\tone = quote_path(it->string, &onebuf, s->prefix);\n \t\tcolor_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), \"%s\", sign);\n \t\tprintf(\" %s\\n\", one);\n \t\tstrbuf_release(&onebuf);\n-- \n1.8.3.rc1.404.ga32c147\n"},{"id":"217467","messageId":"7vli7gaypc.fsf@alter.siamese.dyndns.org","threadId":"33816","inReplyTo":"cover.1368630636.git.worldhello.net@gmail.com","subject":"Re: [RFC 0/2] refactor relative_path in path.c","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-05-15T18:24:15Z","receivedAt":"2013-05-15T18:24:15Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jiang Xin <worldhello.net@gmail.com> writes:\n\n> These two patches enhance relative_path() in path.c, so that function\n> relative_path() will return real relative path, not a path strip off\n> the prefix.\n>\n> The 2nd patch is a bit aggressive, it refactor all related functions,\n> remove unnecessary arguments: len and/or prefix_len.\n\nI did not particularly find the second one \"aggressive\"; it would\nhave been much more pleasant to review if the \"drop unused 'len'\"\npart were made into a separate patch [3/2] as a follow-up, though.\n\nIt is a bit sad that relative_path() in [1/2] uses a single static\nand fixed sized buffer.  How is the new implementation making sure\nthe expanded result does not overflow the buf[]?\n"}]}