Skip to content

Commit 8d8975a

Browse files
committed
Merge branch 'mm/rm-coalesce-errors'
Give a single message followed by list of paths from "git rm" to report multiple paths that cannot be removed. * mm/rm-coalesce-errors: rm: introduce advice.rmHints to shorten messages rm: better error message on failure for multiple files
2 parents 22d94a7 + 7e30944 commit 8d8975a

File tree

5 files changed

+184
-19
lines changed

5 files changed

+184
-19
lines changed

Documentation/config.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ advice.*::
199199
amWorkDir::
200200
Advice that shows the location of the patch file when
201201
linkgit:git-am[1] fails to apply it.
202+
rmHints::
203+
In case of failure in the output of linkgit:git-rm[1],
204+
show directions on how to proceed from the current state.
202205
--
203206

204207
core.fileMode::

advice.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ int advice_implicit_identity = 1;
1515
int advice_detached_head = 1;
1616
int advice_set_upstream_failure = 1;
1717
int advice_object_name_warning = 1;
18+
int advice_rm_hints = 1;
1819

1920
static struct {
2021
const char *name;
@@ -35,6 +36,7 @@ static struct {
3536
{ "detachedhead", &advice_detached_head },
3637
{ "setupstreamfailure", &advice_set_upstream_failure },
3738
{ "object_name_warning", &advice_object_name_warning },
39+
{ "rmhints", &advice_rm_hints },
3840

3941
/* make this an alias for backward compatibility */
4042
{ "pushnonfastforward", &advice_push_update_rejected }

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern int advice_implicit_identity;
1818
extern int advice_detached_head;
1919
extern int advice_set_upstream_failure;
2020
extern int advice_object_name_warning;
21+
extern int advice_rm_hints;
2122

2223
int git_default_advice_config(const char *var, const char *value);
2324
void advise(const char *advice, ...);

builtin/rm.c

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "cache-tree.h"
1010
#include "tree-walk.h"
1111
#include "parse-options.h"
12+
#include "string-list.h"
1213
#include "submodule.h"
1314

1415
static const char * const builtin_rm_usage[] = {
@@ -36,10 +37,32 @@ static int get_ours_cache_pos(const char *path, int pos)
3637
return -1;
3738
}
3839

40+
static void print_error_files(struct string_list *files_list,
41+
const char *main_msg,
42+
const char *hints_msg,
43+
int *errs)
44+
{
45+
if (files_list->nr) {
46+
int i;
47+
struct strbuf err_msg = STRBUF_INIT;
48+
49+
strbuf_addstr(&err_msg, main_msg);
50+
for (i = 0; i < files_list->nr; i++)
51+
strbuf_addf(&err_msg,
52+
"\n %s",
53+
files_list->items[i].string);
54+
if (advice_rm_hints)
55+
strbuf_addstr(&err_msg, hints_msg);
56+
*errs = error("%s", err_msg.buf);
57+
strbuf_release(&err_msg);
58+
}
59+
}
60+
3961
static int check_submodules_use_gitfiles(void)
4062
{
4163
int i;
4264
int errs = 0;
65+
struct string_list files = STRING_LIST_INIT_NODUP;
4366

4467
for (i = 0; i < list.nr; i++) {
4568
const char *name = list.entry[i].name;
@@ -61,11 +84,18 @@ static int check_submodules_use_gitfiles(void)
6184
continue;
6285

6386
if (!submodule_uses_gitfile(name))
64-
errs = error(_("submodule '%s' (or one of its nested "
65-
"submodules) uses a .git directory\n"
66-
"(use 'rm -rf' if you really want to remove "
67-
"it including all of its history)"), name);
87+
string_list_append(&files, name);
6888
}
89+
print_error_files(&files,
90+
Q_("the following submodule (or one of its nested "
91+
"submodules)\n uses a .git directory:",
92+
"the following submodules (or one of its nested "
93+
"submodules)\n use a .git directory:",
94+
files.nr),
95+
_("\n(use 'rm -rf' if you really want to remove "
96+
"it including all of its history)"),
97+
&errs);
98+
string_list_clear(&files, 0);
6999

70100
return errs;
71101
}
@@ -81,6 +111,10 @@ static int check_local_mod(unsigned char *head, int index_only)
81111
*/
82112
int i, no_head;
83113
int errs = 0;
114+
struct string_list files_staged = STRING_LIST_INIT_NODUP;
115+
struct string_list files_cached = STRING_LIST_INIT_NODUP;
116+
struct string_list files_submodule = STRING_LIST_INIT_NODUP;
117+
struct string_list files_local = STRING_LIST_INIT_NODUP;
84118

85119
no_head = is_null_sha1(head);
86120
for (i = 0; i < list.nr; i++) {
@@ -171,29 +205,58 @@ static int check_local_mod(unsigned char *head, int index_only)
171205
*/
172206
if (local_changes && staged_changes) {
173207
if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD))
174-
errs = error(_("'%s' has staged content different "
175-
"from both the file and the HEAD\n"
176-
"(use -f to force removal)"), name);
208+
string_list_append(&files_staged, name);
177209
}
178210
else if (!index_only) {
179211
if (staged_changes)
180-
errs = error(_("'%s' has changes staged in the index\n"
181-
"(use --cached to keep the file, "
182-
"or -f to force removal)"), name);
212+
string_list_append(&files_cached, name);
183213
if (local_changes) {
184214
if (S_ISGITLINK(ce->ce_mode) &&
185-
!submodule_uses_gitfile(name)) {
186-
errs = error(_("submodule '%s' (or one of its nested "
187-
"submodules) uses a .git directory\n"
188-
"(use 'rm -rf' if you really want to remove "
189-
"it including all of its history)"), name);
190-
} else
191-
errs = error(_("'%s' has local modifications\n"
192-
"(use --cached to keep the file, "
193-
"or -f to force removal)"), name);
215+
!submodule_uses_gitfile(name))
216+
string_list_append(&files_submodule, name);
217+
else
218+
string_list_append(&files_local, name);
194219
}
195220
}
196221
}
222+
print_error_files(&files_staged,
223+
Q_("the following file has staged content different "
224+
"from both the\nfile and the HEAD:",
225+
"the following files have staged content different"
226+
" from both the\nfile and the HEAD:",
227+
files_staged.nr),
228+
_("\n(use -f to force removal)"),
229+
&errs);
230+
string_list_clear(&files_staged, 0);
231+
print_error_files(&files_cached,
232+
Q_("the following file has changes "
233+
"staged in the index:",
234+
"the following files have changes "
235+
"staged in the index:", files_cached.nr),
236+
_("\n(use --cached to keep the file,"
237+
" or -f to force removal)"),
238+
&errs);
239+
string_list_clear(&files_cached, 0);
240+
print_error_files(&files_submodule,
241+
Q_("the following submodule (or one of its nested "
242+
"submodule)\nuses a .git directory:",
243+
"the following submodules (or one of its nested "
244+
"submodule)\nuse a .git directory:",
245+
files_submodule.nr),
246+
_("\n(use 'rm -rf' if you really "
247+
"want to remove it including all "
248+
"of its history)"),
249+
&errs);
250+
string_list_clear(&files_submodule, 0);
251+
print_error_files(&files_local,
252+
Q_("the following file has local modifications:",
253+
"the following files have local modifications:",
254+
files_local.nr),
255+
_("\n(use --cached to keep the file,"
256+
" or -f to force removal)"),
257+
&errs);
258+
string_list_clear(&files_local, 0);
259+
197260
return errs;
198261
}
199262

t/t3600-rm.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,4 +687,100 @@ test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
687687
test_path_is_file e/f
688688
'
689689

690+
test_expect_success 'setup for testing rm messages' '
691+
>bar.txt &&
692+
>foo.txt &&
693+
git add bar.txt foo.txt
694+
'
695+
696+
test_expect_success 'rm files with different staged content' '
697+
cat >expect <<-\EOF &&
698+
error: the following files have staged content different from both the
699+
file and the HEAD:
700+
bar.txt
701+
foo.txt
702+
(use -f to force removal)
703+
EOF
704+
echo content1 >foo.txt &&
705+
echo content1 >bar.txt &&
706+
test_must_fail git rm foo.txt bar.txt 2>actual &&
707+
test_i18ncmp expect actual
708+
'
709+
710+
test_expect_success 'rm files with different staged content without hints' '
711+
cat >expect <<-\EOF &&
712+
error: the following files have staged content different from both the
713+
file and the HEAD:
714+
bar.txt
715+
foo.txt
716+
EOF
717+
echo content2 >foo.txt &&
718+
echo content2 >bar.txt &&
719+
test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
720+
test_i18ncmp expect actual
721+
'
722+
723+
test_expect_success 'rm file with local modification' '
724+
cat >expect <<-\EOF &&
725+
error: the following file has local modifications:
726+
foo.txt
727+
(use --cached to keep the file, or -f to force removal)
728+
EOF
729+
git commit -m "testing rm 3" &&
730+
echo content3 >foo.txt &&
731+
test_must_fail git rm foo.txt 2>actual &&
732+
test_i18ncmp expect actual
733+
'
734+
735+
test_expect_success 'rm file with local modification without hints' '
736+
cat >expect <<-\EOF &&
737+
error: the following file has local modifications:
738+
bar.txt
739+
EOF
740+
echo content4 >bar.txt &&
741+
test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
742+
test_i18ncmp expect actual
743+
'
744+
745+
test_expect_success 'rm file with changes in the index' '
746+
cat >expect <<-\EOF &&
747+
error: the following file has changes staged in the index:
748+
foo.txt
749+
(use --cached to keep the file, or -f to force removal)
750+
EOF
751+
git reset --hard &&
752+
echo content5 >foo.txt &&
753+
git add foo.txt &&
754+
test_must_fail git rm foo.txt 2>actual &&
755+
test_i18ncmp expect actual
756+
'
757+
758+
test_expect_success 'rm file with changes in the index without hints' '
759+
cat >expect <<-\EOF &&
760+
error: the following file has changes staged in the index:
761+
foo.txt
762+
EOF
763+
test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
764+
test_i18ncmp expect actual
765+
'
766+
767+
test_expect_success 'rm files with two different errors' '
768+
cat >expect <<-\EOF &&
769+
error: the following file has staged content different from both the
770+
file and the HEAD:
771+
foo1.txt
772+
(use -f to force removal)
773+
error: the following file has changes staged in the index:
774+
bar1.txt
775+
(use --cached to keep the file, or -f to force removal)
776+
EOF
777+
echo content >foo1.txt &&
778+
git add foo1.txt &&
779+
echo content6 >foo1.txt &&
780+
echo content6 >bar1.txt &&
781+
git add bar1.txt &&
782+
test_must_fail git rm bar1.txt foo1.txt 2>actual &&
783+
test_i18ncmp expect actual
784+
'
785+
690786
test_done

0 commit comments

Comments
 (0)