Merge branch 'jx/clean-interactive'

Add "interactive" mode to "git clean".

The early part to refactor relative path related helper functions
looked sensible.

* jx/clean-interactive:
  test: run testcases with POSIX absolute paths on Windows
  test: add t7301 for git-clean--interactive
  git-clean: add documentation for interactive git-clean
  git-clean: add ask each interactive action
  git-clean: add select by numbers interactive action
  git-clean: add filter by pattern interactive action
  git-clean: use a git-add-interactive compatible UI
  git-clean: add colors to interactive git-clean
  git-clean: show items of del_list in columns
  git-clean: add support for -i/--interactive
  git-clean: refactor git-clean into two phases
  write_name{_quoted_relative,}(): remove redundant parameters
  quote_path_relative(): remove redundant parameter
  quote.c: substitute path_relative with relative_path
  path.c: refactor relative_path(), not only strip prefix
  test: add test cases for relative_path
This commit is contained in:
Junio C Hamano 2013-07-22 11:24:11 -07:00
commit 988f98f61f
14 changed files with 1473 additions and 171 deletions

View File

@ -879,16 +879,17 @@ The values of these variables may be specified as in color.branch.<slot>.
color.interactive::
When set to `always`, always use colors for interactive prompts
and displays (such as those used by "git-add --interactive").
When false (or `never`), never. When set to `true` or `auto`, use
colors only when the output is to the terminal. Defaults to false.
and displays (such as those used by "git-add --interactive" and
"git-clean --interactive"). When false (or `never`), never.
When set to `true` or `auto`, use colors only when the output is
to the terminal. Defaults to false.
color.interactive.<slot>::
Use customized color for 'git add --interactive'
output. `<slot>` may be `prompt`, `header`, `help` or `error`, for
four distinct types of normal output from interactive
commands. The values of these variables may be specified as
in color.branch.<slot>.
Use customized color for 'git add --interactive' and 'git clean
--interactive' output. `<slot>` may be `prompt`, `header`, `help`
or `error`, for four distinct types of normal output from
interactive commands. The values of these variables may be
specified as in color.branch.<slot>.
color.pager::
A boolean to enable/disable colored output when the pager is in
@ -973,6 +974,10 @@ column.branch::
Specify whether to output branch listing in `git branch` in columns.
See `column.ui` for details.
column.clean::
Specify the layout when list items in `git clean -i`, which always
shows files and directories in columns. See `column.ui` for details.
column.status::
Specify whether to output untracked files in `git status` in columns.
See `column.ui` for details.

View File

@ -8,7 +8,7 @@ git-clean - Remove untracked files from the working tree
SYNOPSIS
--------
[verse]
'git clean' [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>...
'git clean' [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>...
DESCRIPTION
-----------
@ -34,7 +34,13 @@ OPTIONS
-f::
--force::
If the Git configuration variable clean.requireForce is not set
to false, 'git clean' will refuse to run unless given -f or -n.
to false, 'git clean' will refuse to run unless given -f, -n or
-i.
-i::
--interactive::
Show what would be done and clean files interactively. See
``Interactive mode'' for details.
-n::
--dry-run::
@ -63,6 +69,67 @@ OPTIONS
Remove only files ignored by Git. This may be useful to rebuild
everything from scratch, but keep manually created files.
Interactive mode
----------------
When the command enters the interactive mode, it shows the
files and directories to be cleaned, and goes into its
interactive command loop.
The command loop shows the list of subcommands available, and
gives a prompt "What now> ". In general, when the prompt ends
with a single '>', you can pick only one of the choices given
and type return, like this:
------------
*** Commands ***
1: clean 2: filter by pattern 3: select by numbers
4: ask each 5: quit 6: help
What now> 1
------------
You also could say `c` or `clean` above as long as the choice is unique.
The main command loop has 6 subcommands.
clean::
Start cleaning files and directories, and then quit.
filter by pattern::
This shows the files and directories to be deleted and issues an
"Input ignore patterns>>" prompt. You can input space-seperated
patterns to exclude files and directories from deletion.
E.g. "*.c *.h" will excludes files end with ".c" and ".h" from
deletion. When you are satisfied with the filtered result, press
ENTER (empty) back to the main menu.
select by numbers::
This shows the files and directories to be deleted and issues an
"Select items to delete>>" prompt. When the prompt ends with double
'>>' like this, you can make more than one selection, concatenated
with whitespace or comma. Also you can say ranges. E.g. "2-5 7,9"
to choose 2,3,4,5,7,9 from the list. If the second number in a
range is omitted, all remaining patches are taken. E.g. "7-" to
choose 7,8,9 from the list. You can say '*' to choose everything.
Also when you are satisfied with the filtered result, press ENTER
(empty) back to the main menu.
ask each::
This will start to clean, and you must confirm one by one in order
to delete items. Please note that this action is not as efficient
as the above two actions.
quit::
This lets you quit without do cleaning.
help::
Show brief usage of interactive git-clean.
SEE ALSO
--------
linkgit:gitignore[5]

View File

@ -13,11 +13,16 @@
#include "refs.h"
#include "string-list.h"
#include "quote.h"
#include "column.h"
#include "color.h"
static int force = -1; /* unset */
static int interactive;
static struct string_list del_list = STRING_LIST_INIT_DUP;
static unsigned int colopts;
static const char *const builtin_clean_usage[] = {
N_("git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
NULL
};
@ -27,11 +32,112 @@ static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
static const char *msg_warn_remove_failed = N_("failed to remove %s");
static int clean_use_color = -1;
static char clean_colors[][COLOR_MAXLEN] = {
GIT_COLOR_RESET,
GIT_COLOR_NORMAL, /* PLAIN */
GIT_COLOR_BOLD_BLUE, /* PROMPT */
GIT_COLOR_BOLD, /* HEADER */
GIT_COLOR_BOLD_RED, /* HELP */
GIT_COLOR_BOLD_RED, /* ERROR */
};
enum color_clean {
CLEAN_COLOR_RESET = 0,
CLEAN_COLOR_PLAIN = 1,
CLEAN_COLOR_PROMPT = 2,
CLEAN_COLOR_HEADER = 3,
CLEAN_COLOR_HELP = 4,
CLEAN_COLOR_ERROR = 5,
};
#define MENU_OPTS_SINGLETON 01
#define MENU_OPTS_IMMEDIATE 02
#define MENU_OPTS_LIST_ONLY 04
struct menu_opts {
const char *header;
const char *prompt;
int flags;
};
#define MENU_RETURN_NO_LOOP 10
struct menu_item {
char hotkey;
const char *title;
int selected;
int (*fn)();
};
enum menu_stuff_type {
MENU_STUFF_TYPE_STRING_LIST = 1,
MENU_STUFF_TYPE_MENU_ITEM
};
struct menu_stuff {
enum menu_stuff_type type;
int nr;
void *stuff;
};
static int parse_clean_color_slot(const char *var)
{
if (!strcasecmp(var, "reset"))
return CLEAN_COLOR_RESET;
if (!strcasecmp(var, "plain"))
return CLEAN_COLOR_PLAIN;
if (!strcasecmp(var, "prompt"))
return CLEAN_COLOR_PROMPT;
if (!strcasecmp(var, "header"))
return CLEAN_COLOR_HEADER;
if (!strcasecmp(var, "help"))
return CLEAN_COLOR_HELP;
if (!strcasecmp(var, "error"))
return CLEAN_COLOR_ERROR;
return -1;
}
static int git_clean_config(const char *var, const char *value, void *cb)
{
if (!strcmp(var, "clean.requireforce"))
if (!prefixcmp(var, "column."))
return git_column_config(var, value, "clean", &colopts);
/* honors the color.interactive* config variables which also
applied in git-add--interactive and git-stash */
if (!strcmp(var, "color.interactive")) {
clean_use_color = git_config_colorbool(var, value);
return 0;
}
if (!prefixcmp(var, "color.interactive.")) {
int slot = parse_clean_color_slot(var +
strlen("color.interactive."));
if (slot < 0)
return 0;
if (!value)
return config_error_nonbool(var);
color_parse(value, var, clean_colors[slot]);
return 0;
}
if (!strcmp(var, "clean.requireforce")) {
force = !git_config_bool(var, value);
return git_default_config(var, value, cb);
return 0;
}
/* inspect the color.ui config variable and others */
return git_color_default_config(var, value, cb);
}
static const char *clean_get_color(enum color_clean ix)
{
if (want_color(clean_use_color))
return clean_colors[ix];
return "";
}
static void clean_print_color(enum color_clean ix)
{
printf("%s", clean_get_color(ix));
}
static int exclude_cb(const struct option *opt, const char *arg, int unset)
@ -56,7 +162,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
!resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
if (!quiet) {
quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
quote_path_relative(path->buf, prefix, &quoted);
printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
quoted.buf);
}
@ -70,7 +176,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
/* an empty dir could be removed even if it is unreadble */
res = dry_run ? 0 : rmdir(path->buf);
if (res) {
quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
quote_path_relative(path->buf, prefix, &quoted);
warning(_(msg_warn_remove_failed), quoted.buf);
*dir_gone = 0;
}
@ -94,7 +200,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone))
ret = 1;
if (gone) {
quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
quote_path_relative(path->buf, prefix, &quoted);
string_list_append(&dels, quoted.buf);
} else
*dir_gone = 0;
@ -102,10 +208,10 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
} else {
res = dry_run ? 0 : unlink(path->buf);
if (!res) {
quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
quote_path_relative(path->buf, prefix, &quoted);
string_list_append(&dels, quoted.buf);
} else {
quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
quote_path_relative(path->buf, prefix, &quoted);
warning(_(msg_warn_remove_failed), quoted.buf);
*dir_gone = 0;
ret = 1;
@ -127,7 +233,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
if (!res)
*dir_gone = 1;
else {
quote_path_relative(path->buf, strlen(path->buf), &quoted, prefix);
quote_path_relative(path->buf, prefix, &quoted);
warning(_(msg_warn_remove_failed), quoted.buf);
*dir_gone = 0;
ret = 1;
@ -142,24 +248,609 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
return ret;
}
static void pretty_print_dels(void)
{
struct string_list list = STRING_LIST_INIT_DUP;
struct string_list_item *item;
struct strbuf buf = STRBUF_INIT;
const char *qname;
struct column_options copts;
for_each_string_list_item(item, &del_list) {
qname = quote_path_relative(item->string, NULL, &buf);
string_list_append(&list, qname);
}
/*
* always enable column display, we only consult column.*
* about layout strategy and stuff
*/
colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
memset(&copts, 0, sizeof(copts));
copts.indent = " ";
copts.padding = 2;
print_columns(&list, colopts, &copts);
strbuf_release(&buf);
string_list_clear(&list, 0);
}
static void pretty_print_menus(struct string_list *menu_list)
{
unsigned int local_colopts = 0;
struct column_options copts;
local_colopts = COL_ENABLED | COL_ROW;
memset(&copts, 0, sizeof(copts));
copts.indent = " ";
copts.padding = 2;
print_columns(menu_list, local_colopts, &copts);
}
static void prompt_help_cmd(int singleton)
{
clean_print_color(CLEAN_COLOR_HELP);
printf_ln(singleton ?
_("Prompt help:\n"
"1 - select a numbered item\n"
"foo - select item based on unique prefix\n"
" - (empty) select nothing") :
_("Prompt help:\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 unique prefix\n"
"-... - unselect specified items\n"
"* - choose all items\n"
" - (empty) finish selecting"));
clean_print_color(CLEAN_COLOR_RESET);
}
/*
* display menu stuff with number prefix and hotkey highlight
*/
static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen)
{
struct string_list menu_list = STRING_LIST_INIT_DUP;
struct strbuf menu = STRBUF_INIT;
struct strbuf buf = STRBUF_INIT;
struct menu_item *menu_item;
struct string_list_item *string_list_item;
int i;
switch (stuff->type) {
default:
die("Bad type of menu_staff when print menu");
case MENU_STUFF_TYPE_MENU_ITEM:
menu_item = (struct menu_item *)stuff->stuff;
for (i = 0; i < stuff->nr; i++, menu_item++) {
const char *p;
int highlighted = 0;
p = menu_item->title;
if ((*chosen)[i] < 0)
(*chosen)[i] = menu_item->selected ? 1 : 0;
strbuf_addf(&menu, "%s%2d: ", (*chosen)[i] ? "*" : " ", i+1);
for (; *p; p++) {
if (!highlighted && *p == menu_item->hotkey) {
strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_PROMPT));
strbuf_addch(&menu, *p);
strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_RESET));
highlighted = 1;
} else {
strbuf_addch(&menu, *p);
}
}
string_list_append(&menu_list, menu.buf);
strbuf_reset(&menu);
}
break;
case MENU_STUFF_TYPE_STRING_LIST:
i = 0;
for_each_string_list_item(string_list_item, (struct string_list *)stuff->stuff) {
if ((*chosen)[i] < 0)
(*chosen)[i] = 0;
strbuf_addf(&menu, "%s%2d: %s",
(*chosen)[i] ? "*" : " ", i+1, string_list_item->string);
string_list_append(&menu_list, menu.buf);
strbuf_reset(&menu);
i++;
}
break;
}
pretty_print_menus(&menu_list);
strbuf_release(&menu);
strbuf_release(&buf);
string_list_clear(&menu_list, 0);
}
/*
* Parse user input, and return choice(s) for menu (menu_stuff).
*
* Input
* (for single choice)
* 1 - select a numbered item
* foo - select item based on menu title
* - (empty) select nothing
*
* (for multiple choice)
* 1 - select a single item
* 3-5 - select a range of items
* 2-3,6-9 - select multiple ranges
* foo - select item based on menu title
* -... - unselect specified items
* * - choose all items
* - (empty) finish selecting
*
* The parse result will be saved in array **chosen, and
* return number of total selections.
*/
static int parse_choice(struct menu_stuff *menu_stuff,
int is_single,
struct strbuf input,
int **chosen)
{
struct strbuf **choice_list, **ptr;
struct menu_item *menu_item;
struct string_list_item *string_list_item;
int nr = 0;
int i;
if (is_single) {
choice_list = strbuf_split_max(&input, '\n', 0);
} else {
char *p = input.buf;
do {
if (*p == ',')
*p = ' ';
} while (*p++);
choice_list = strbuf_split_max(&input, ' ', 0);
}
for (ptr = choice_list; *ptr; ptr++) {
char *p;
int choose = 1;
int bottom = 0, top = 0;
int is_range, is_number;
strbuf_trim(*ptr);
if (!(*ptr)->len)
continue;
/* Input that begins with '-'; unchoose */
if (*(*ptr)->buf == '-') {
choose = 0;
strbuf_remove((*ptr), 0, 1);
}
is_range = 0;
is_number = 1;
for (p = (*ptr)->buf; *p; p++) {
if ('-' == *p) {
if (!is_range) {
is_range = 1;
is_number = 0;
} else {
is_number = 0;
is_range = 0;
break;
}
} else if (!isdigit(*p)) {
is_number = 0;
is_range = 0;
break;
}
}
if (is_number) {
bottom = atoi((*ptr)->buf);
top = bottom;
} else if (is_range) {
bottom = atoi((*ptr)->buf);
/* a range can be specified like 5-7 or 5- */
if (!*(strchr((*ptr)->buf, '-') + 1))
top = menu_stuff->nr;
else
top = atoi(strchr((*ptr)->buf, '-') + 1);
} else if (!strcmp((*ptr)->buf, "*")) {
bottom = 1;
top = menu_stuff->nr;
} else {
switch (menu_stuff->type) {
default:
die("Bad type of menu_stuff when parse choice");
case MENU_STUFF_TYPE_MENU_ITEM:
menu_item = (struct menu_item *)menu_stuff->stuff;
for (i = 0; i < menu_stuff->nr; i++, menu_item++) {
if (((*ptr)->len == 1 &&
*(*ptr)->buf == menu_item->hotkey) ||
!strcasecmp((*ptr)->buf, menu_item->title)) {
bottom = i + 1;
top = bottom;
break;
}
}
break;
case MENU_STUFF_TYPE_STRING_LIST:
string_list_item = ((struct string_list *)menu_stuff->stuff)->items;
for (i = 0; i < menu_stuff->nr; i++, string_list_item++) {
if (!strcasecmp((*ptr)->buf, string_list_item->string)) {
bottom = i + 1;
top = bottom;
break;
}
}
break;
}
}
if (top <= 0 || bottom <= 0 || top > menu_stuff->nr || bottom > top ||
(is_single && bottom != top)) {
clean_print_color(CLEAN_COLOR_ERROR);
printf_ln(_("Huh (%s)?"), (*ptr)->buf);
clean_print_color(CLEAN_COLOR_RESET);
continue;
}
for (i = bottom; i <= top; i++)
(*chosen)[i-1] = choose;
}
strbuf_list_free(choice_list);
for (i = 0; i < menu_stuff->nr; i++)
nr += (*chosen)[i];
return nr;
}
/*
* Implement a git-add-interactive compatible UI, which is borrowed
* from git-add--interactive.perl.
*
* Return value:
*
* - Return an array of integers
* - , and it is up to you to free the allocated memory.
* - The array ends with EOF.
* - If user pressed CTRL-D (i.e. EOF), no selection returned.
*/
static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
{
struct strbuf choice = STRBUF_INIT;
int *chosen, *result;
int nr = 0;
int eof = 0;
int i;
chosen = xmalloc(sizeof(int) * stuff->nr);
/* set chosen as uninitialized */
for (i = 0; i < stuff->nr; i++)
chosen[i] = -1;
for (;;) {
if (opts->header) {
printf_ln("%s%s%s",
clean_get_color(CLEAN_COLOR_HEADER),
_(opts->header),
clean_get_color(CLEAN_COLOR_RESET));
}
/* chosen will be initialized by print_highlight_menu_stuff */
print_highlight_menu_stuff(stuff, &chosen);
if (opts->flags & MENU_OPTS_LIST_ONLY)
break;
if (opts->prompt) {
printf("%s%s%s%s",
clean_get_color(CLEAN_COLOR_PROMPT),
_(opts->prompt),
opts->flags & MENU_OPTS_SINGLETON ? "> " : ">> ",
clean_get_color(CLEAN_COLOR_RESET));
}
if (strbuf_getline(&choice, stdin, '\n') != EOF) {
strbuf_trim(&choice);
} else {
eof = 1;
break;
}
/* help for prompt */
if (!strcmp(choice.buf, "?")) {
prompt_help_cmd(opts->flags & MENU_OPTS_SINGLETON);
continue;
}
/* for a multiple-choice menu, press ENTER (empty) will return back */
if (!(opts->flags & MENU_OPTS_SINGLETON) && !choice.len)
break;
nr = parse_choice(stuff,
opts->flags & MENU_OPTS_SINGLETON,
choice,
&chosen);
if (opts->flags & MENU_OPTS_SINGLETON) {
if (nr)
break;
} else if (opts->flags & MENU_OPTS_IMMEDIATE) {
break;
}
}
if (eof) {
result = xmalloc(sizeof(int));
*result = EOF;
} else {
int j = 0;
/*
* recalculate nr, if return back from menu directly with
* default selections.
*/
if (!nr) {
for (i = 0; i < stuff->nr; i++)
nr += chosen[i];
}
result = xmalloc(sizeof(int) * (nr + 1));
memset(result, 0, sizeof(int) * (nr + 1));
for (i = 0; i < stuff->nr && j < nr; i++) {
if (chosen[i])
result[j++] = i;
}
result[j] = EOF;
}
free(chosen);
strbuf_release(&choice);
return result;
}
static int clean_cmd(void)
{
return MENU_RETURN_NO_LOOP;
}
static int filter_by_patterns_cmd(void)
{
struct dir_struct dir;
struct strbuf confirm = STRBUF_INIT;
struct strbuf **ignore_list;
struct string_list_item *item;
struct exclude_list *el;
int changed = -1, i;
for (;;) {
if (!del_list.nr)
break;
if (changed)
pretty_print_dels();
clean_print_color(CLEAN_COLOR_PROMPT);
printf(_("Input ignore patterns>> "));
clean_print_color(CLEAN_COLOR_RESET);
if (strbuf_getline(&confirm, stdin, '\n') != EOF)
strbuf_trim(&confirm);
else
putchar('\n');
/* quit filter_by_pattern mode if press ENTER or Ctrl-D */
if (!confirm.len)
break;
memset(&dir, 0, sizeof(dir));
el = add_exclude_list(&dir, EXC_CMDL, "manual exclude");
ignore_list = strbuf_split_max(&confirm, ' ', 0);
for (i = 0; ignore_list[i]; i++) {
strbuf_trim(ignore_list[i]);
if (!ignore_list[i]->len)
continue;
add_exclude(ignore_list[i]->buf, "", 0, el, -(i+1));
}
changed = 0;
for_each_string_list_item(item, &del_list) {
int dtype = DT_UNKNOWN;
if (is_excluded(&dir, item->string, &dtype)) {
*item->string = '\0';
changed++;
}
}
if (changed) {
string_list_remove_empty_items(&del_list, 0);
} else {
clean_print_color(CLEAN_COLOR_ERROR);
printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm.buf);
clean_print_color(CLEAN_COLOR_RESET);
}
strbuf_list_free(ignore_list);
clear_directory(&dir);
}
strbuf_release(&confirm);
return 0;
}
static int select_by_numbers_cmd(void)
{
struct menu_opts menu_opts;
struct menu_stuff menu_stuff;
struct string_list_item *items;
int *chosen;
int i, j;
menu_opts.header = NULL;
menu_opts.prompt = N_("Select items to delete");
menu_opts.flags = 0;
menu_stuff.type = MENU_STUFF_TYPE_STRING_LIST;
menu_stuff.stuff = &del_list;
menu_stuff.nr = del_list.nr;
chosen = list_and_choose(&menu_opts, &menu_stuff);
items = del_list.items;
for (i = 0, j = 0; i < del_list.nr; i++) {
if (i < chosen[j]) {
*(items[i].string) = '\0';
} else if (i == chosen[j]) {
/* delete selected item */
j++;
continue;
} else {
/* end of chosen (chosen[j] == EOF), won't delete */
*(items[i].string) = '\0';
}
}
string_list_remove_empty_items(&del_list, 0);
free(chosen);
return 0;
}
static int ask_each_cmd(void)
{
struct strbuf confirm = STRBUF_INIT;
struct strbuf buf = STRBUF_INIT;
struct string_list_item *item;
const char *qname;
int changed = 0, eof = 0;
for_each_string_list_item(item, &del_list) {
/* Ctrl-D should stop removing files */
if (!eof) {
qname = quote_path_relative(item->string, NULL, &buf);
printf(_("remove %s? "), qname);
if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
strbuf_trim(&confirm);
} else {
putchar('\n');
eof = 1;
}
}
if (!confirm.len || strncasecmp(confirm.buf, "yes", confirm.len)) {
*item->string = '\0';
changed++;
}
}
if (changed)
string_list_remove_empty_items(&del_list, 0);
strbuf_release(&buf);
strbuf_release(&confirm);
return MENU_RETURN_NO_LOOP;
}
static int quit_cmd(void)
{
string_list_clear(&del_list, 0);
printf_ln(_("Bye."));
return MENU_RETURN_NO_LOOP;
}
static int help_cmd(void)
{
clean_print_color(CLEAN_COLOR_HELP);
printf_ln(_(
"clean - start cleaning\n"
"filter by pattern - exclude items from deletion\n"
"select by numbers - select items to be deleted by numbers\n"
"ask each - confirm each deletion (like \"rm -i\")\n"
"quit - stop cleaning\n"
"help - this screen\n"
"? - help for prompt selection"
));
clean_print_color(CLEAN_COLOR_RESET);
return 0;
}
static void interactive_main_loop(void)
{
while (del_list.nr) {
struct menu_opts menu_opts;
struct menu_stuff menu_stuff;
struct menu_item menus[] = {
{'c', "clean", 0, clean_cmd},
{'f', "filter by pattern", 0, filter_by_patterns_cmd},
{'s', "select by numbers", 0, select_by_numbers_cmd},
{'a', "ask each", 0, ask_each_cmd},
{'q', "quit", 0, quit_cmd},
{'h', "help", 0, help_cmd},
};
int *chosen;
menu_opts.header = N_("*** Commands ***");
menu_opts.prompt = N_("What now");
menu_opts.flags = MENU_OPTS_SINGLETON;
menu_stuff.type = MENU_STUFF_TYPE_MENU_ITEM;
menu_stuff.stuff = menus;
menu_stuff.nr = sizeof(menus) / sizeof(struct menu_item);
clean_print_color(CLEAN_COLOR_HEADER);
printf_ln(Q_("Would remove the following item:",
"Would remove the following items:",
del_list.nr));
clean_print_color(CLEAN_COLOR_RESET);
pretty_print_dels();
chosen = list_and_choose(&menu_opts, &menu_stuff);
if (*chosen != EOF) {
int ret;
ret = menus[*chosen].fn();
if (ret != MENU_RETURN_NO_LOOP) {
free(chosen);
chosen = NULL;
if (!del_list.nr) {
clean_print_color(CLEAN_COLOR_ERROR);
printf_ln(_("No more files to clean, exiting."));
clean_print_color(CLEAN_COLOR_RESET);
break;
}
continue;
}
} else {
quit_cmd();
}
free(chosen);
chosen = NULL;
break;
}
}
int cmd_clean(int argc, const char **argv, const char *prefix)
{
int i, res;
int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0;
int ignored_only = 0, config_set = 0, errors = 0, gone = 1;
int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
struct strbuf directory = STRBUF_INIT;
struct strbuf abs_path = STRBUF_INIT;
struct dir_struct dir;
static const char **pathspec;
struct strbuf buf = STRBUF_INIT;
struct string_list exclude_list = STRING_LIST_INIT_NODUP;
struct exclude_list *el;
struct string_list_item *item;
const char *qname;
char *seen = NULL;
struct option options[] = {
OPT__QUIET(&quiet, N_("do not print names of files removed")),
OPT__DRY_RUN(&dry_run, N_("dry run")),
OPT__FORCE(&force, N_("force")),
OPT_BOOL('i', "interactive", &interactive, N_("interactive cleaning")),
OPT_BOOLEAN('d', NULL, &remove_directories,
N_("remove whole directories")),
{ OPTION_CALLBACK, 'e', "exclude", &exclude_list, N_("pattern"),
@ -186,12 +877,12 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
if (ignored && ignored_only)
die(_("-x and -X cannot be used together"));
if (!dry_run && !force) {
if (!interactive && !dry_run && !force) {
if (config_set)
die(_("clean.requireForce set to true and neither -n nor -f given; "
die(_("clean.requireForce set to true and neither -i, -n nor -f given; "
"refusing to clean"));
else
die(_("clean.requireForce defaults to true and neither -n nor -f given; "
die(_("clean.requireForce defaults to true and neither -i, -n nor -f given; "
"refusing to clean"));
}
@ -223,6 +914,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
int matches = 0;
const struct cache_entry *ce;
struct stat st;
const char *rel;
/*
* Remove the '/' at the end that directory
@ -242,13 +934,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
continue; /* Yup, this one exists unmerged */
}
/*
* we might have removed this as part of earlier
* recursive directory removal, so lstat() here could
* fail with ENOENT.
*/
if (lstat(ent->name, &st))
continue;
die_errno("Cannot lstat '%s'", ent->name);
if (pathspec) {
memset(seen, 0, argc > 0 ? argc : 1);
@ -257,33 +944,62 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
}
if (S_ISDIR(st.st_mode)) {
strbuf_addstr(&directory, ent->name);
if (remove_directories || (matches == MATCHED_EXACTLY)) {
if (remove_dirs(&directory, prefix, rm_flags, dry_run, quiet, &gone))
errors++;
if (gone && !quiet) {
qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
}
rel = relative_path(ent->name, prefix, &buf);
string_list_append(&del_list, rel);
}
strbuf_reset(&directory);
} else {
if (pathspec && !matches)
continue;
res = dry_run ? 0 : unlink(ent->name);
rel = relative_path(ent->name, prefix, &buf);
string_list_append(&del_list, rel);
}
}
if (interactive && del_list.nr > 0)
interactive_main_loop();
for_each_string_list_item(item, &del_list) {
struct stat st;
if (prefix)
strbuf_addstr(&abs_path, prefix);
strbuf_addstr(&abs_path, item->string);
/*
* we might have removed this as part of earlier
* recursive directory removal, so lstat() here could
* fail with ENOENT.
*/
if (lstat(abs_path.buf, &st))
continue;
if (S_ISDIR(st.st_mode)) {
if (remove_dirs(&abs_path, prefix, rm_flags, dry_run, quiet, &gone))
errors++;
if (gone && !quiet) {
qname = quote_path_relative(item->string, NULL, &buf);
printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
}
} else {
res = dry_run ? 0 : unlink(abs_path.buf);
if (res) {
qname = quote_path_relative(ent->name, -1, &buf, prefix);
qname = quote_path_relative(item->string, NULL, &buf);
warning(_(msg_warn_remove_failed), qname);
errors++;
} else if (!quiet) {
qname = quote_path_relative(ent->name, -1, &buf, prefix);
qname = quote_path_relative(item->string, NULL, &buf);
printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
}
}
strbuf_reset(&abs_path);
}
free(seen);
strbuf_release(&directory);
strbuf_release(&abs_path);
strbuf_release(&buf);
string_list_clear(&del_list, 0);
string_list_clear(&exclude_list, 0);
return (errors != 0);
}

View File

@ -286,8 +286,7 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
struct strbuf pathbuf = STRBUF_INIT;
if (opt->relative && opt->prefix_length) {
quote_path_relative(filename + tree_name_len, -1, &pathbuf,
opt->prefix);
quote_path_relative(filename + tree_name_len, opt->prefix, &pathbuf);
strbuf_insert(&pathbuf, 0, filename, tree_name_len);
} else {
strbuf_addstr(&pathbuf, filename);
@ -318,7 +317,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
struct strbuf buf = STRBUF_INIT;
if (opt->relative && opt->prefix_length)
quote_path_relative(filename, -1, &buf, opt->prefix);
quote_path_relative(filename, opt->prefix, &buf);
else
strbuf_addstr(&buf, filename);

View File

@ -46,10 +46,14 @@ static const char *tag_modified = "";
static const char *tag_skip_worktree = "";
static const char *tag_resolve_undo = "";
static void write_name(const char* name, size_t len)
static void write_name(const char *name)
{
write_name_quoted_relative(name, len, prefix, prefix_len, stdout,
line_terminator);
/*
* With "--full-name", prefix_len=0; this caller needs to pass
* an empty string in that case (a NULL is good for "").
*/
write_name_quoted_relative(name, prefix_len ? prefix : NULL,
stdout, line_terminator);
}
static void show_dir_entry(const char *tag, struct dir_entry *ent)
@ -63,7 +67,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
return;
fputs(tag, stdout);
write_name(ent->name, ent->len);
write_name(ent->name);
}
static void show_other_files(struct dir_struct *dir)
@ -163,7 +167,7 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
find_unique_abbrev(ce->sha1,abbrev),
ce_stage(ce));
}
write_name(ce->name, ce_namelen(ce));
write_name(ce->name);
if (debug_mode) {
const struct stat_data *sd = &ce->ce_stat_data;
@ -198,7 +202,7 @@ static void show_ru_info(void)
printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
find_unique_abbrev(ui->sha1[i], abbrev),
i + 1);
write_name(path, len);
write_name(path);
}
}
}
@ -391,7 +395,7 @@ int report_path_error(const char *ps_matched, const char **pathspec, const char
if (found_dup)
continue;
name = quote_path_relative(pathspec[num], -1, &sb, prefix);
name = quote_path_relative(pathspec[num], prefix, &sb);
error("pathspec '%s' did not match any file(s) known to git.",
name);
errors++;

View File

@ -760,7 +760,7 @@ int is_directory(const char *);
const char *real_path(const char *path);
const char *real_path_if_valid(const char *path);
const char *absolute_path(const char *path);
const char *relative_path(const char *abs, const char *base);
const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
int normalize_path_copy(char *dst, const char *src);
int longest_ancestor_length(const char *path, struct string_list *prefixes);
char *strip_path_suffix(const char *path, const char *suffix);

112
path.c
View File

@ -441,42 +441,100 @@ int adjust_shared_perm(const char *path)
return 0;
}
const char *relative_path(const char *abs, const char *base)
/*
* Give path as relative to prefix.
*
* The strbuf may or may not be used, so do not assume it contains the
* returned path.
*/
const char *relative_path(const char *in, const char *prefix,
struct strbuf *sb)
{
static char buf[PATH_MAX + 1];
int in_len = in ? strlen(in) : 0;
int prefix_len = prefix ? strlen(prefix) : 0;
int in_off = 0;
int prefix_off = 0;
int i = 0, j = 0;
if (!base || !base[0])
return abs;
while (base[i]) {
if (is_dir_sep(base[i])) {
if (!is_dir_sep(abs[j]))
return abs;
while (is_dir_sep(base[i]))
if (!in_len)
return "./";
else if (!prefix_len)
return in;
while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
if (is_dir_sep(prefix[i])) {
while (is_dir_sep(prefix[i]))
i++;
while (is_dir_sep(abs[j]))
while (is_dir_sep(in[j]))
j++;
prefix_off = i;
in_off = j;
} else {
i++;
j++;
}
}
if (
/* "prefix" seems like prefix of "in" */
i >= prefix_len &&
/*
* but "/foo" is not a prefix of "/foobar"
* (i.e. prefix not end with '/')
*/
prefix_off < prefix_len) {
if (j >= in_len) {
/* in="/a/b", prefix="/a/b" */
in_off = in_len;
} else if (is_dir_sep(in[j])) {
/* in="/a/b/c", prefix="/a/b" */
while (is_dir_sep(in[j]))
j++;
in_off = j;
} else {
/* in="/a/bbb/c", prefix="/a/b" */
i = prefix_off;
}
} else if (
/* "in" is short than "prefix" */
j >= in_len &&
/* "in" not end with '/' */
in_off < in_len) {
if (is_dir_sep(prefix[i])) {
/* in="/a/b", prefix="/a/b/c/" */
while (is_dir_sep(prefix[i]))
i++;
in_off = in_len;
}
}
in += in_off;
in_len -= in_off;
if (i >= prefix_len) {
if (!in_len)
return "./";
else
return in;
}
strbuf_reset(sb);
strbuf_grow(sb, in_len);
while (i < prefix_len) {
if (is_dir_sep(prefix[i])) {
strbuf_addstr(sb, "../");
while (is_dir_sep(prefix[i]))
i++;
continue;
} else if (abs[j] != base[i]) {
return abs;
}
i++;
j++;
}
if (
/* "/foo" is a prefix of "/foo" */
abs[j] &&
/* "/foo" is not a prefix of "/foobar" */
!is_dir_sep(base[i-1]) && !is_dir_sep(abs[j])
)
return abs;
while (is_dir_sep(abs[j]))
j++;
if (!abs[j])
strcpy(buf, ".");
else
strcpy(buf, abs + j);
return buf;
if (!is_dir_sep(prefix[prefix_len - 1]))
strbuf_addstr(sb, "../");
strbuf_addstr(sb, in);
return sb->buf;
}
/*

65
quote.c
View File

@ -312,82 +312,27 @@ void write_name_quotedpfx(const char *pfx, size_t pfxlen,
fputc(terminator, fp);
}
static const char *path_relative(const char *in, int len,
struct strbuf *sb, const char *prefix,
int prefix_len);
void write_name_quoted_relative(const char *name, size_t len,
const char *prefix, size_t prefix_len,
void write_name_quoted_relative(const char *name, const char *prefix,
FILE *fp, int terminator)
{
struct strbuf sb = STRBUF_INIT;
name = path_relative(name, len, &sb, prefix, prefix_len);
name = relative_path(name, prefix, &sb);
write_name_quoted(name, fp, terminator);
strbuf_release(&sb);
}
/*
* Give path as relative to prefix.
*
* The strbuf may or may not be used, so do not assume it contains the
* returned path.
*/
static const char *path_relative(const char *in, int len,
struct strbuf *sb, const char *prefix,
int prefix_len)
{
int off, i;
if (len < 0)
len = strlen(in);
if (prefix_len < 0) {
if (prefix)
prefix_len = strlen(prefix);
else
prefix_len = 0;
}
off = 0;
i = 0;
while (i < prefix_len && i < len && prefix[i] == in[i]) {
if (prefix[i] == '/')
off = i + 1;
i++;
}
in += off;
len -= off;
if (i >= prefix_len)
return in;
strbuf_reset(sb);
strbuf_grow(sb, len);
while (i < prefix_len) {
if (prefix[i] == '/')
strbuf_addstr(sb, "../");
i++;
}
strbuf_add(sb, in, len);
return sb->buf;
}
/* quote path as relative to the given prefix */
char *quote_path_relative(const char *in, int len,
struct strbuf *out, const char *prefix)
char *quote_path_relative(const char *in, const char *prefix,
struct strbuf *out)
{
struct strbuf sb = STRBUF_INIT;
const char *rel = path_relative(in, len, &sb, prefix, -1);
const char *rel = relative_path(in, prefix, &sb);
strbuf_reset(out);
quote_c_style_counted(rel, strlen(rel), out, NULL, 0);
strbuf_release(&sb);
if (!out->len)
strbuf_addstr(out, "./");
return out->buf;
}

View File

@ -60,13 +60,12 @@ extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);
extern void write_name_quoted(const char *name, FILE *, int terminator);
extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
const char *name, FILE *, int terminator);
extern void write_name_quoted_relative(const char *name, size_t len,
const char *prefix, size_t prefix_len,
extern void write_name_quoted_relative(const char *name, const char *prefix,
FILE *fp, int terminator);
/* quote path as relative to the given prefix */
extern char *quote_path_relative(const char *in, int len,
struct strbuf *out, const char *prefix);
extern char *quote_path_relative(const char *in, const char *prefix,
struct strbuf *out);
/* quoting as a string literal for other languages */
extern void perl_quote_print(FILE *stream, const char *src);

View File

@ -360,6 +360,7 @@ int is_inside_work_tree(void)
void setup_work_tree(void)
{
struct strbuf sb = STRBUF_INIT;
const char *work_tree, *git_dir;
static int initialized = 0;
@ -379,8 +380,10 @@ void setup_work_tree(void)
if (getenv(GIT_WORK_TREE_ENVIRONMENT))
setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
set_git_dir(relative_path(git_dir, work_tree));
set_git_dir(relative_path(git_dir, work_tree, &sb));
initialized = 1;
strbuf_release(&sb);
}
static int check_repository_format_gently(const char *gitdir, int *nongit_ok)

View File

@ -8,8 +8,15 @@ test_description='Test various path utilities'
. ./test-lib.sh
norm_path() {
expected=$(test-path-utils mingw_path "$2")
test_expect_success $3 "normalize path: $1 => $2" \
"test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
"test \"\$(test-path-utils normalize_path_copy '$1')\" = '$expected'"
}
relative_path() {
expected=$(test-path-utils mingw_path "$3")
test_expect_success $4 "relative path: $1 $2 => $3" \
"test \"\$(test-path-utils relative_path '$1' '$2')\" = '$expected'"
}
# On Windows, we are using MSYS's bash, which mangles the paths.
@ -34,8 +41,8 @@ ancestor() {
test \"\$actual\" = '$expected'"
}
# Absolute path tests must be skipped on Windows because due to path mangling
# the test program never sees a POSIX-style absolute path
# Some absolute path tests should be skipped on Windows due to path mangling
# on POSIX-style absolute paths
case $(uname -s) in
*MINGW*)
;;
@ -68,30 +75,30 @@ norm_path d1/s1//../s2/../../d2 d2
norm_path d1/.../d2 d1/.../d2
norm_path d1/..././../d2 d1/d2
norm_path / / POSIX
norm_path / /
norm_path // / POSIX
norm_path /// / POSIX
norm_path /. / POSIX
norm_path /. /
norm_path /./ / POSIX
norm_path /./.. ++failed++ POSIX
norm_path /../. ++failed++ POSIX
norm_path /../. ++failed++
norm_path /./../.// ++failed++ POSIX
norm_path /dir/.. / POSIX
norm_path /dir/sub/../.. / POSIX
norm_path /dir/sub/../../.. ++failed++ POSIX
norm_path /dir /dir POSIX
norm_path /dir// /dir/ POSIX
norm_path /./dir /dir POSIX
norm_path /dir/. /dir/ POSIX
norm_path /dir///./ /dir/ POSIX
norm_path /dir//sub/.. /dir/ POSIX
norm_path /dir/sub/../ /dir/ POSIX
norm_path /dir /dir
norm_path /dir// /dir/
norm_path /./dir /dir
norm_path /dir/. /dir/
norm_path /dir///./ /dir/
norm_path /dir//sub/.. /dir/
norm_path /dir/sub/../ /dir/
norm_path //dir/sub/../. /dir/ POSIX
norm_path /dir/s1/../s2/ /dir/s2/ POSIX
norm_path /d1/s1///s2/..//../s3/ /d1/s3/ POSIX
norm_path /d1/s1//../s2/../../d2 /d2 POSIX
norm_path /d1/.../d2 /d1/.../d2 POSIX
norm_path /d1/..././../d2 /d1/d2 POSIX
norm_path /dir/s1/../s2/ /dir/s2/
norm_path /d1/s1///s2/..//../s3/ /d1/s3/
norm_path /d1/s1//../s2/../../d2 /d2
norm_path /d1/.../d2 /d1/.../d2
norm_path /d1/..././../d2 /d1/d2
ancestor / / -1
ancestor /foo / 0
@ -183,4 +190,33 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
'
relative_path /a/b/c/ /a/b/ c/
relative_path /a/b/c/ /a/b c/
relative_path /a//b//c/ //a/b// c/ POSIX
relative_path /a/b /a/b ./
relative_path /a/b/ /a/b ./
relative_path /a /a/b ../
relative_path / /a/b/ ../../
relative_path /a/c /a/b/ ../c
relative_path /a/c /a/b ../c
relative_path /x/y /a/b/ ../../x/y
relative_path /a/b "<empty>" /a/b
relative_path /a/b "<null>" /a/b
relative_path a/b/c/ a/b/ c/
relative_path a/b/c/ a/b c/
relative_path a/b//c a//b c
relative_path a/b/ a/b/ ./
relative_path a/b/ a/b ./
relative_path a a/b ../
relative_path x/y a/b ../../x/y
relative_path a/c a/b ../c
relative_path a/b "<empty>" a/b
relative_path a/b "<null>" a/b
relative_path "<empty>" /a/b ./
relative_path "<empty>" "<empty>" ./
relative_path "<empty>" "<null>" ./
relative_path "<null>" "<empty>" ./
relative_path "<null>" "<null>" ./
relative_path "<null>" /a/b ./
test_done

439
t/t7301-clean-interactive.sh Executable file
View File

@ -0,0 +1,439 @@
#!/bin/sh
test_description='git clean -i basic tests'
. ./test-lib.sh
test_expect_success 'setup' '
mkdir -p src &&
touch src/part1.c Makefile &&
echo build >.gitignore &&
echo \*.o >>.gitignore &&
git add . &&
git commit -m setup &&
touch src/part2.c README &&
git add .
'
test_expect_success 'git clean -i (clean)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
echo c | git clean -i &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test -f docs/manual.txt &&
test ! -f src/part3.c &&
test ! -f src/part3.h &&
test ! -f src/part4.c &&
test ! -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -i (quit)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
echo q | git clean -i &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test -f a.out &&
test -f docs/manual.txt &&
test -f src/part3.c &&
test -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -i (Ctrl+D)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
echo "\04" | git clean -i &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test -f a.out &&
test -f docs/manual.txt &&
test -f src/part3.c &&
test -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (filter all)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo f; echo "*"; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test -f a.out &&
test -f docs/manual.txt &&
test -f src/part3.c &&
test -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (filter patterns)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo f; echo "part3.* *.out"; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test -f a.out &&
test ! -f docs/manual.txt &&
test -f src/part3.c &&
test -f src/part3.h &&
test ! -f src/part4.c &&
test ! -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (filter patterns 2)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo f; echo "* !*.out"; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test -f docs/manual.txt &&
test -f src/part3.c &&
test -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (select - all)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo s; echo "*"; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test ! -f docs/manual.txt &&
test ! -f src/part3.c &&
test ! -f src/part3.h &&
test ! -f src/part4.c &&
test ! -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (select - none)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo s; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test -f a.out &&
test -f docs/manual.txt &&
test -f src/part3.c &&
test -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (select - number)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo s; echo 3; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test -f a.out &&
test -f docs/manual.txt &&
test ! -f src/part3.c &&
test -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (select - number 2)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo s; echo 2 3; echo 5; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test -f a.out &&
test ! -f docs/manual.txt &&
test ! -f src/part3.c &&
test -f src/part3.h &&
test ! -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (select - number 3)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo s; echo 3,4 5; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test -f a.out &&
test -f docs/manual.txt &&
test ! -f src/part3.c &&
test ! -f src/part3.h &&
test ! -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (select - range)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo s; echo 1,3-4; echo 2; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test ! -f src/part3.c &&
test ! -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test ! -f docs/manual.txt &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (select - range 2)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo s; echo 4- 1; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test -f docs/manual.txt &&
test -f src/part3.c &&
test ! -f src/part3.h &&
test ! -f src/part4.c &&
test ! -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (inverse select)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo s; echo "*"; echo -5- 1 -2; echo; echo c) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test -f docs/manual.txt &&
test ! -f src/part3.c &&
test ! -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (ask)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo a; echo Y; echo y; echo no; echo yes; echo bad; echo) | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test ! -f docs/manual.txt &&
test -f src/part3.c &&
test ! -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id (ask - Ctrl+D)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(echo a; echo Y; echo no; echo yes; echo "\04") | \
git clean -id &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test -f docs/manual.txt &&
test ! -f src/part3.c &&
test -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id with prefix and path (filter)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(cd build/ && \
(echo f; echo "docs"; echo "*.h"; echo ; echo c) | \
git clean -id ..) &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test -f docs/manual.txt &&
test ! -f src/part3.c &&
test -f src/part3.h &&
test ! -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id with prefix and path (select by name)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(cd build/ && \
(echo s; echo "../docs/"; echo "../src/part3.c"; \
echo "../src/part4.c"; echo; echo c) | \
git clean -id ..) &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test -f a.out &&
test ! -f docs/manual.txt &&
test ! -f src/part3.c &&
test -f src/part3.h &&
test ! -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_expect_success 'git clean -id with prefix and path (ask)' '
mkdir -p build docs &&
touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
docs/manual.txt obj.o build/lib.so &&
(cd build/ && \
(echo a; echo Y; echo y; echo no; echo yes; echo bad; echo) | \
git clean -id ..) &&
test -f Makefile &&
test -f README &&
test -f src/part1.c &&
test -f src/part2.c &&
test ! -f a.out &&
test ! -f docs/manual.txt &&
test -f src/part3.c &&
test ! -f src/part3.h &&
test -f src/part4.c &&
test -f src/part4.h &&
test -f obj.o &&
test -f build/lib.so
'
test_done

View File

@ -28,6 +28,19 @@ static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
return 1;
}
static void normalize_argv_string(const char **var, const char *input)
{
if (!strcmp(input, "<null>"))
*var = NULL;
else if (!strcmp(input, "<empty>"))
*var = "";
else
*var = input;
if (*var && (**var == '<' || **var == '('))
die("Bad value: %s\n", input);
}
int main(int argc, char **argv)
{
if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
@ -103,6 +116,25 @@ int main(int argc, char **argv)
return 0;
}
if (argc == 3 && !strcmp(argv[1], "mingw_path")) {
puts(argv[2]);
return 0;
}
if (argc == 4 && !strcmp(argv[1], "relative_path")) {
struct strbuf sb = STRBUF_INIT;
const char *in, *prefix, *rel;
normalize_argv_string(&in, argv[2]);
normalize_argv_string(&prefix, argv[3]);
rel = relative_path(in, prefix, &sb);
if (!rel)
puts("(null)");
else
puts(strlen(rel) > 0 ? rel : "(empty)");
strbuf_release(&sb);
return 0;
}
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
argv[1] ? argv[1] : "(there was none)");
return 1;

View File

@ -244,7 +244,7 @@ static void wt_status_print_unmerged_data(struct wt_status *s,
struct strbuf onebuf = STRBUF_INIT;
const char *one, *how = _("bug");
one = quote_path(it->string, -1, &onebuf, s->prefix);
one = quote_path(it->string, s->prefix, &onebuf);
status_printf(s, color(WT_STATUS_HEADER, s), "\t");
switch (d->stagemask) {
case 1: how = _("both deleted:"); break;
@ -298,8 +298,8 @@ static void wt_status_print_change_data(struct wt_status *s,
change_type);
}
one = quote_path(one_name, -1, &onebuf, s->prefix);
two = quote_path(two_name, -1, &twobuf, s->prefix);
one = quote_path(one_name, s->prefix, &onebuf);
two = quote_path(two_name, s->prefix, &twobuf);
status_printf(s, color(WT_STATUS_HEADER, s), "\t");
switch (status) {
@ -707,8 +707,7 @@ static void wt_status_print_other(struct wt_status *s,
struct string_list_item *it;
const char *path;
it = &(l->items[i]);
path = quote_path(it->string, strlen(it->string),
&buf, s->prefix);
path = quote_path(it->string, s->prefix, &buf);
if (column_active(s->colopts)) {
string_list_append(&output, path);
continue;
@ -1293,7 +1292,7 @@ static void wt_shortstatus_unmerged(struct string_list_item *it,
} else {
struct strbuf onebuf = STRBUF_INIT;
const char *one;
one = quote_path(it->string, -1, &onebuf, s->prefix);
one = quote_path(it->string, s->prefix, &onebuf);
printf(" %s\n", one);
strbuf_release(&onebuf);
}
@ -1321,7 +1320,7 @@ static void wt_shortstatus_status(struct string_list_item *it,
struct strbuf onebuf = STRBUF_INIT;
const char *one;
if (d->head_path) {
one = quote_path(d->head_path, -1, &onebuf, s->prefix);
one = quote_path(d->head_path, s->prefix, &onebuf);
if (*one != '"' && strchr(one, ' ') != NULL) {
putchar('"');
strbuf_addch(&onebuf, '"');
@ -1330,7 +1329,7 @@ static void wt_shortstatus_status(struct string_list_item *it,
printf("%s -> ", one);
strbuf_release(&onebuf);
}
one = quote_path(it->string, -1, &onebuf, s->prefix);
one = quote_path(it->string, s->prefix, &onebuf);
if (*one != '"' && strchr(one, ' ') != NULL) {
putchar('"');
strbuf_addch(&onebuf, '"');
@ -1349,7 +1348,7 @@ static void wt_shortstatus_other(struct string_list_item *it,
} else {
struct strbuf onebuf = STRBUF_INIT;
const char *one;
one = quote_path(it->string, -1, &onebuf, s->prefix);
one = quote_path(it->string, s->prefix, &onebuf);
color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
printf(" %s\n", one);
strbuf_release(&onebuf);