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 <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matthieu Moy committed
Aug 11, 2010 at 10:38 UTC
e6c111b4c092c0dd24c541b9721f5bc04641dcb0
11 files changed
+123
-17
Documentation/technical/api-tree-walking.txt
+2
@@ -42,6 +42,8 @@ information.
42
43
* `data` can be anything the `fn` callback would want to use.
44
45
+* `show_all_errors` tells whether to stop at the first error or not.
46
+
47
Initializing
48
------------
49
builtin/checkout.c
+1
@@ -392,6 +392,7 @@ static int merge_working_tree(struct checkout_opts *opts,
392
topts.dir = xcalloc(1, sizeof(*topts.dir));
393
topts.dir->flags |= DIR_SHOW_IGNORED;
394
topts.dir->exclude_per_dir = ".gitignore";
395
+ topts.show_all_errors = 1;
396
tree = parse_tree_indirect(old->commit ?
397
old->commit->object.sha1 :
398
(unsigned char *)EMPTY_TREE_SHA1_BIN);
builtin/merge.c
+1
@@ -704,6 +704,7 @@ int checkout_fast_forward(const unsigned char *head, const unsigned char *remote
704
opts.verbose_update = 1;
705
opts.merge = 1;
706
opts.fn = twoway_merge;
707
+ opts.show_all_errors = 1;
708
set_porcelain_error_msgs(opts.msgs, "merge");
709
710
trees[nr_trees] = parse_tree_indirect(head);
merge-recursive.c
+19
-6
@@ -1184,29 +1184,42 @@ void set_porcelain_error_msgs(const char **msgs, const char *cmd)
1184
char *tmp;
1185
const char *cmd2 = strcmp(cmd, "checkout") ? cmd : "switch branches";
1186
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"
1188
"Please, commit your changes or stash them before you can %s.";
1189
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);
1192
sprintf(tmp, msg, cmd, cmd2);
1193
msgs[ERROR_WOULD_OVERWRITE] = tmp;
1194
msgs[ERROR_NOT_UPTODATE_FILE] = tmp;
1195
1196
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";
1198
1199
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"
1201
"Please move or remove them before you can %s.";
1202
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";
1204
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("removed") + strlen(cmd2) - 4);
1205
sprintf(tmp, msg, "removed", cmd, cmd2);
1206
msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = tmp;
1207
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen("overwritten") + strlen(cmd2) - 4);
1208
sprintf(tmp, msg, "overwritten", cmd, cmd2);
1209
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";
1223
}
1224
1225
int merge_trees(struct merge_options *o,
t/t3030-merge-recursive.sh
+1
-1
@@ -294,7 +294,7 @@ test_expect_success 'fail if the index has unresolved entries' '
294
grep "You have not concluded your merge" out &&
295
rm -f .git/MERGE_HEAD &&
296
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
298
'
299
300
test_expect_success 'merge-recursive remove conflict' '
t/t3400-rebase.sh
+2
-1
@@ -129,7 +129,8 @@ test_expect_success 'rebase a single mode change' '
129
test_expect_success 'Show verbose error when HEAD could not be detached' '
130
: > B &&
131
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
134
'
135
rm -f B
136
t/t3404-rebase-interactive.sh
+2
-1
@@ -150,8 +150,9 @@ test_expect_success 'abort with error when new base cannot be checked out' '
150
git rm --cached file1 &&
151
git commit -m "remove file in base" &&
152
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:" \
154
output &&
155
+ grep "file1" output &&
156
! test -d .git/rebase-merge &&
157
git reset --hard HEAD^
158
'
tree-walk.c
+8
-3
@@ -1,5 +1,6 @@
1
#include "cache.h"
2
#include "tree-walk.h"
3
+#include "unpack-trees.h"
4
#include "tree.h"
5
6
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)
311
int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
312
{
313
int ret = 0;
314
+ int error = 0;
315
struct name_entry *entry = xmalloc(n*sizeof(*entry));
316
int i;
317
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)
379
if (!mask)
380
break;
381
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
+ }
387
mask &= ret;
388
ret = 0;
389
for (i = 0; i < n; i++)
@@ -389,7 +394,7 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
394
for (i = 0; i < n; i++)
395
free_extended_entry(tx + i);
396
free(tx);
392
- return ret;
397
+ return error;
398
}
399
400
static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode)
tree-walk.h
+1
@@ -45,6 +45,7 @@ struct traverse_info {
45
unsigned long conflicts;
46
traverse_callback_t fn;
47
void *data;
48
+ int show_all_errors;
49
};
50
51
int get_tree_entry(const unsigned char *, const char *, unsigned char *, unsigned *);
unpack-trees.c
+74
-4
@@ -64,6 +64,73 @@ static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
64
add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
65
}
66
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
+
134
/*
135
* Unlink the last component and schedule the leading directories for
136
* removal, such that empty directories get removed.
@@ -755,6 +822,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
822
setup_traverse_info(&info, prefix);
823
info.fn = unpack_callback;
824
info.data = o;
825
+ info.show_all_errors = o->show_all_errors;
826
827
if (o->prefix) {
828
/*
@@ -834,6 +902,8 @@ done:
902
return ret;
903
904
return_failed:
905
+ if (o->show_all_errors)
906
+ display_error_msgs(o);
907
mark_all_ce_unused(o->src_index);
908
ret = unpack_failed(o, NULL);
909
goto done;
@@ -843,7 +913,7 @@ return_failed:
913
914
static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
915
{
846
- return error(ERRORMSG(o, ERROR_WOULD_OVERWRITE), ce->name);
916
+ return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
917
}
918
919
static int same(struct cache_entry *a, struct cache_entry *b)
@@ -890,7 +960,7 @@ static int verify_uptodate_1(struct cache_entry *ce,
960
if (errno == ENOENT)
961
return 0;
962
return o->gently ? -1 :
893
- error(ERRORMSG(o, error_type), ce->name);
963
+ add_rejected_path(o, error_type, ce->name);
964
}
965
966
static int verify_uptodate(struct cache_entry *ce,
@@ -993,7 +1063,7 @@ static int verify_clean_subdirectory(struct cache_entry *ce,
1063
i = read_directory(&d, pathbuf, namelen+1, NULL);
1064
if (i)
1065
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);
1067
free(pathbuf);
1068
return cnt;
1069
}
@@ -1075,7 +1145,7 @@ static int verify_absent_1(struct cache_entry *ce,
1145
}
1146
1147
return o->gently ? -1 :
1078
- error(ERRORMSG(o, error_type), ce->name);
1148
+ add_rejected_path(o, error_type, ce->name);
1149
}
1150
return 0;
1151
}
unpack-trees.h
+12
-1
@@ -22,6 +22,11 @@ enum unpack_trees_error_types {
22
NB_UNPACK_TREES_ERROR_TYPES
23
};
24
25
+struct rejected_paths_list {
26
+ char *path;
27
+ struct rejected_paths_list *next;
28
+};
29
+
30
struct unpack_trees_options {
31
unsigned int reset,
32
merge,
@@ -36,12 +41,18 @@ struct unpack_trees_options {
41
diff_index_cached,
42
debug_unpack,
43
skip_sparse_checkout,
39
- gently;
44
+ gently,
45
+ show_all_errors;
46
const char *prefix;
47
int cache_bottom;
48
struct dir_struct *dir;
49
merge_fn_t fn;
50
const char *msgs[NB_UNPACK_TREES_ERROR_TYPES];
51
+ /*
52
+ * Store error messages in an array, each case
53
+ * corresponding to a error message type
54
+ */
55
+ struct rejected_paths_list *unpack_rejects[NB_UNPACK_TREES_ERROR_TYPES];
56
57
int head_idx;
58
int merge_size;