Skip to content

Commit e6c111b

Browse files
moygitster
authored andcommitted
unpack_trees: group error messages by type
When an error is encountered, it calls add_rejected_file() which either - directly displays the error message and stops if in plumbing mode (i.e. if show_all_errors is not initialized at 1) - or stores it so that it will be displayed at the end with display_error_msgs(), Storing the files by error type permits to have a list of files for which there is the same error instead of having a serie of almost identical errors. As each bind_overlap error combines a file and an old file, a list cannot be done, therefore, theses errors are not stored but directly displayed. Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 08402b0 commit e6c111b

File tree

11 files changed

+123
-17
lines changed

11 files changed

+123
-17
lines changed

Documentation/technical/api-tree-walking.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ information.
4242

4343
* `data` can be anything the `fn` callback would want to use.
4444

45+
* `show_all_errors` tells whether to stop at the first error or not.
46+
4547
Initializing
4648
------------
4749

builtin/checkout.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ static int merge_working_tree(struct checkout_opts *opts,
392392
topts.dir = xcalloc(1, sizeof(*topts.dir));
393393
topts.dir->flags |= DIR_SHOW_IGNORED;
394394
topts.dir->exclude_per_dir = ".gitignore";
395+
topts.show_all_errors = 1;
395396
tree = parse_tree_indirect(old->commit ?
396397
old->commit->object.sha1 :
397398
(unsigned char *)EMPTY_TREE_SHA1_BIN);

builtin/merge.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ int checkout_fast_forward(const unsigned char *head, const unsigned char *remote
704704
opts.verbose_update = 1;
705705
opts.merge = 1;
706706
opts.fn = twoway_merge;
707+
opts.show_all_errors = 1;
707708
set_porcelain_error_msgs(opts.msgs, "merge");
708709

709710
trees[nr_trees] = parse_tree_indirect(head);

merge-recursive.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,29 +1184,42 @@ void set_porcelain_error_msgs(const char **msgs, const char *cmd)
11841184
char *tmp;
11851185
const char *cmd2 = strcmp(cmd, "checkout") ? cmd : "switch branches";
11861186
if (advice_commit_before_merge)
1187-
msg = "Your local changes to '%%s' would be overwritten by %s. Aborting.\n"
1187+
msg = "Your local changes to the following files would be overwritten by %s:\n%%s"
11881188
"Please, commit your changes or stash them before you can %s.";
11891189
else
1190-
msg = "Your local changes to '%%s' would be overwritten by %s. Aborting.";
1191-
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen(cmd2) - 3);
1190+
msg = "Your local changes to the following files would be overwritten by %s:\n%%s";
1191+
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen(cmd2) - 2);
11921192
sprintf(tmp, msg, cmd, cmd2);
11931193
msgs[ERROR_WOULD_OVERWRITE] = tmp;
11941194
msgs[ERROR_NOT_UPTODATE_FILE] = tmp;
11951195

11961196
msgs[ERROR_NOT_UPTODATE_DIR] =
1197-
"Updating '%s' would lose untracked files in it. Aborting.";
1197+
"Updating the following directories would lose untracked files in it:\n%s";
11981198

11991199
if (advice_commit_before_merge)
1200-
msg = "Untracked working tree file '%%s' would be %s by %s. Aborting"
1200+
msg = "The following untracked working tree files would be %s by %s:\n%%s"
12011201
"Please move or remove them before you can %s.";
12021202
else
1203-
msg = "Untracked working tree file '%%s' would be %s by %s. Aborting";
1203+
msg = "The following untracked working tree files would be %s by %s:\n%%s";
12041204
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("removed") + strlen(cmd2) - 4);
12051205
sprintf(tmp, msg, "removed", cmd, cmd2);
12061206
msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = tmp;
12071207
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("overwritten") + strlen(cmd2) - 4);
12081208
sprintf(tmp, msg, "overwritten", cmd, cmd2);
12091209
msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = tmp;
1210+
1211+
/*
1212+
* Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
1213+
* cannot easily display it as a list.
1214+
*/
1215+
msgs[ERROR_BIND_OVERLAP] = "Entry '%s' overlaps with '%s'. Cannot bind.";
1216+
1217+
msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] =
1218+
"Cannot update sparse checkout: the following entries are not up-to-date:\n%s";
1219+
msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] =
1220+
"The following Working tree files would be overwritten by sparse checkout update:\n%s";
1221+
msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] =
1222+
"The following Working tree files would be removed by sparse checkout update:\n%s";
12101223
}
12111224

12121225
int merge_trees(struct merge_options *o,

t/t3030-merge-recursive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ test_expect_success 'fail if the index has unresolved entries' '
294294
grep "You have not concluded your merge" out &&
295295
rm -f .git/MERGE_HEAD &&
296296
test_must_fail git merge "$c5" 2> out &&
297-
grep "Your local changes to .* would be overwritten by merge." out
297+
grep "Your local changes to the following files would be overwritten by merge:" out
298298
'
299299

300300
test_expect_success 'merge-recursive remove conflict' '

t/t3400-rebase.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ test_expect_success 'rebase a single mode change' '
129129
test_expect_success 'Show verbose error when HEAD could not be detached' '
130130
: > B &&
131131
test_must_fail git rebase topic 2> output.err > output.out &&
132-
grep "Untracked working tree file .B. would be overwritten" output.err
132+
grep "The following untracked working tree files would be overwritten by checkout:" output.err &&
133+
grep B output.err
133134
'
134135
rm -f B
135136

t/t3404-rebase-interactive.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ test_expect_success 'abort with error when new base cannot be checked out' '
150150
git rm --cached file1 &&
151151
git commit -m "remove file in base" &&
152152
test_must_fail git rebase -i master > output 2>&1 &&
153-
grep "Untracked working tree file .file1. would be overwritten" \
153+
grep "The following untracked working tree files would be overwritten by checkout:" \
154154
output &&
155+
grep "file1" output &&
155156
! test -d .git/rebase-merge &&
156157
git reset --hard HEAD^
157158
'

tree-walk.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "tree-walk.h"
3+
#include "unpack-trees.h"
34
#include "tree.h"
45

56
static const char *get_mode(const char *str, unsigned int *modep)
@@ -310,6 +311,7 @@ static void free_extended_entry(struct tree_desc_x *t)
310311
int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
311312
{
312313
int ret = 0;
314+
int error = 0;
313315
struct name_entry *entry = xmalloc(n*sizeof(*entry));
314316
int i;
315317
struct tree_desc_x *tx = xcalloc(n, sizeof(*tx));
@@ -377,8 +379,11 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
377379
if (!mask)
378380
break;
379381
ret = info->fn(n, mask, dirmask, entry, info);
380-
if (ret < 0)
381-
break;
382+
if (ret < 0) {
383+
error = ret;
384+
if (!info->show_all_errors)
385+
break;
386+
}
382387
mask &= ret;
383388
ret = 0;
384389
for (i = 0; i < n; i++)
@@ -389,7 +394,7 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
389394
for (i = 0; i < n; i++)
390395
free_extended_entry(tx + i);
391396
free(tx);
392-
return ret;
397+
return error;
393398
}
394399

395400
static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode)

tree-walk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct traverse_info {
4545
unsigned long conflicts;
4646
traverse_callback_t fn;
4747
void *data;
48+
int show_all_errors;
4849
};
4950

5051
int get_tree_entry(const unsigned char *, const char *, unsigned char *, unsigned *);

unpack-trees.c

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,73 @@ static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
6464
add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
6565
}
6666

67+
/*
68+
* add error messages on path <path>
69+
* corresponding to the type <e> with the message <msg>
70+
* indicating if it should be display in porcelain or not
71+
*/
72+
static int add_rejected_path(struct unpack_trees_options *o,
73+
enum unpack_trees_error_types e,
74+
const char *path)
75+
{
76+
struct rejected_paths_list *newentry;
77+
int porcelain = o && (o)->msgs[e];
78+
/*
79+
* simply display the given error message if in plumbing mode
80+
*/
81+
if (!porcelain)
82+
o->show_all_errors = 0;
83+
if (!o->show_all_errors)
84+
return error(ERRORMSG(o, e), path);
85+
86+
/*
87+
* Otherwise, insert in a list for future display by
88+
* display_error_msgs()
89+
*/
90+
newentry = xmalloc(sizeof(struct rejected_paths_list));
91+
newentry->path = (char *)path;
92+
newentry->next = o->unpack_rejects[e];
93+
o->unpack_rejects[e] = newentry;
94+
return -1;
95+
}
96+
97+
/*
98+
* free all the structures allocated for the error <e>
99+
*/
100+
static void free_rejected_paths(struct unpack_trees_options *o,
101+
enum unpack_trees_error_types e)
102+
{
103+
while (o->unpack_rejects[e]) {
104+
struct rejected_paths_list *del = o->unpack_rejects[e];
105+
o->unpack_rejects[e] = o->unpack_rejects[e]->next;
106+
free(del);
107+
}
108+
free(o->unpack_rejects[e]);
109+
}
110+
111+
/*
112+
* display all the error messages stored in a nice way
113+
*/
114+
static void display_error_msgs(struct unpack_trees_options *o)
115+
{
116+
int e;
117+
int something_displayed = 0;
118+
for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
119+
if (o->unpack_rejects[e]) {
120+
struct rejected_paths_list *rp;
121+
struct strbuf path = STRBUF_INIT;
122+
something_displayed = 1;
123+
for (rp = o->unpack_rejects[e]; rp; rp = rp->next)
124+
strbuf_addf(&path, "\t%s\n", rp->path);
125+
error(ERRORMSG(o, e), path.buf);
126+
strbuf_release(&path);
127+
free_rejected_paths(o, e);
128+
}
129+
}
130+
if (something_displayed)
131+
printf("Aborting\n");
132+
}
133+
67134
/*
68135
* Unlink the last component and schedule the leading directories for
69136
* removal, such that empty directories get removed.
@@ -755,6 +822,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
755822
setup_traverse_info(&info, prefix);
756823
info.fn = unpack_callback;
757824
info.data = o;
825+
info.show_all_errors = o->show_all_errors;
758826

759827
if (o->prefix) {
760828
/*
@@ -834,6 +902,8 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
834902
return ret;
835903

836904
return_failed:
905+
if (o->show_all_errors)
906+
display_error_msgs(o);
837907
mark_all_ce_unused(o->src_index);
838908
ret = unpack_failed(o, NULL);
839909
goto done;
@@ -843,7 +913,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
843913

844914
static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
845915
{
846-
return error(ERRORMSG(o, ERROR_WOULD_OVERWRITE), ce->name);
916+
return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
847917
}
848918

849919
static int same(struct cache_entry *a, struct cache_entry *b)
@@ -890,7 +960,7 @@ static int verify_uptodate_1(struct cache_entry *ce,
890960
if (errno == ENOENT)
891961
return 0;
892962
return o->gently ? -1 :
893-
error(ERRORMSG(o, error_type), ce->name);
963+
add_rejected_path(o, error_type, ce->name);
894964
}
895965

896966
static int verify_uptodate(struct cache_entry *ce,
@@ -993,7 +1063,7 @@ static int verify_clean_subdirectory(struct cache_entry *ce,
9931063
i = read_directory(&d, pathbuf, namelen+1, NULL);
9941064
if (i)
9951065
return o->gently ? -1 :
996-
error(ERRORMSG(o, ERROR_NOT_UPTODATE_DIR), ce->name);
1066+
add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
9971067
free(pathbuf);
9981068
return cnt;
9991069
}
@@ -1075,7 +1145,7 @@ static int verify_absent_1(struct cache_entry *ce,
10751145
}
10761146

10771147
return o->gently ? -1 :
1078-
error(ERRORMSG(o, error_type), ce->name);
1148+
add_rejected_path(o, error_type, ce->name);
10791149
}
10801150
return 0;
10811151
}

0 commit comments

Comments
 (0)