Skip to content

Commit 1b8fd46

Browse files
jiangxingitster
authored andcommitted
git-clean: show items of del_list in columns
When there are lots of items to be cleaned, it is hard to see them all in one screen. Show them in columns will solve this problem. Signed-off-by: Jiang Xin <[email protected]> Comments-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1769600 commit 1b8fd46

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

Documentation/config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,10 @@ column.branch::
955955
Specify whether to output branch listing in `git branch` in columns.
956956
See `column.ui` for details.
957957

958+
column.clean::
959+
Specify the layout when list items in `git clean -i`, which always
960+
shows files and directories in columns. See `column.ui` for details.
961+
958962
column.status::
959963
Specify whether to output untracked files in `git status` in columns.
960964
See `column.ui` for details.

builtin/clean.c

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
#include "refs.h"
1414
#include "string-list.h"
1515
#include "quote.h"
16+
#include "column.h"
1617

1718
static int force = -1; /* unset */
1819
static int interactive;
1920
static struct string_list del_list = STRING_LIST_INIT_DUP;
21+
static unsigned int colopts;
2022

2123
static const char *const builtin_clean_usage[] = {
2224
N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
@@ -31,8 +33,13 @@ static const char *msg_warn_remove_failed = N_("failed to remove %s");
3133

3234
static int git_clean_config(const char *var, const char *value, void *cb)
3335
{
34-
if (!strcmp(var, "clean.requireforce"))
36+
if (!prefixcmp(var, "column."))
37+
return git_column_config(var, value, "clean", &colopts);
38+
39+
if (!strcmp(var, "clean.requireforce")) {
3540
force = !git_config_bool(var, value);
41+
return 0;
42+
}
3643
return git_default_config(var, value, cb);
3744
}
3845

@@ -144,21 +151,46 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
144151
return ret;
145152
}
146153

147-
static void interactive_main_loop(void)
154+
static void pretty_print_dels(void)
148155
{
149-
struct strbuf confirm = STRBUF_INIT;
150-
struct strbuf buf = STRBUF_INIT;
156+
struct string_list list = STRING_LIST_INIT_DUP;
151157
struct string_list_item *item;
158+
struct strbuf buf = STRBUF_INIT;
152159
const char *qname;
160+
struct column_options copts;
161+
162+
for_each_string_list_item(item, &del_list) {
163+
qname = quote_path_relative(item->string, NULL, &buf);
164+
string_list_append(&list, qname);
165+
}
166+
167+
/*
168+
* always enable column display, we only consult column.*
169+
* about layout strategy and stuff
170+
*/
171+
colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
172+
memset(&copts, 0, sizeof(copts));
173+
copts.indent = " ";
174+
copts.padding = 2;
175+
print_columns(&list, colopts, &copts);
176+
putchar('\n');
177+
strbuf_release(&buf);
178+
string_list_clear(&list, 0);
179+
}
180+
181+
static void interactive_main_loop(void)
182+
{
183+
struct strbuf confirm = STRBUF_INIT;
153184

154185
while (del_list.nr) {
155186
putchar('\n');
156-
for_each_string_list_item(item, &del_list) {
157-
qname = quote_path_relative(item->string, NULL, &buf);
158-
printf(_(msg_would_remove), qname);
159-
}
187+
printf_ln(Q_("Would remove the following item:",
188+
"Would remove the following items:",
189+
del_list.nr));
160190
putchar('\n');
161191

192+
pretty_print_dels();
193+
162194
printf(_("Remove [y/n]? "));
163195
if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
164196
strbuf_trim(&confirm);
@@ -184,7 +216,6 @@ static void interactive_main_loop(void)
184216
}
185217
}
186218

187-
strbuf_release(&buf);
188219
strbuf_release(&confirm);
189220
}
190221

0 commit comments

Comments
 (0)