@matheusbsbs / matheus-bagliano / commits / 08353ebbab
Fork 0

Turn unpack_trees_options.msgs into an array + enum

The list of error messages was introduced as a structure, but an array indexed over an enum is more flexible, since it allows one to store a type of error message (index in the array) in a variable. This change needs to rename would_lose_untracked -> would_lose_untracked_file to avoid a clash with the function would_lose_untracked in merge-recursive.c. 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 08353ebbab2dfdee50a6daa616ec8b6483cb07c8
6 files changed +46 -52
builtin/checkout.c
+1 -1
@@ -373,7 +373,7 @@ static int merge_working_tree(struct checkout_opts *opts,
373 topts.src_index = &the_index;
374 topts.dst_index = &the_index;
375
376 - topts.msgs.not_uptodate_file = "You have local changes to '%s'; cannot switch branches.";
376 + topts.msgs[ERROR_NOT_UPTODATE_FILE] = "You have local changes to '%s'; cannot switch branches.";
377
378 refresh_cache(REFRESH_QUIET);
379
builtin/merge.c
+1 -1
@@ -704,7 +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.msgs = get_porcelain_error_msgs();
707 + set_porcelain_error_msgs(opts.msgs);
708
709 trees[nr_trees] = parse_tree_indirect(head);
710 if (!trees[nr_trees++])
merge-recursive.c
+11 -18
@@ -185,7 +185,7 @@ static int git_merge_trees(int index_only,
185 opts.fn = threeway_merge;
186 opts.src_index = &the_index;
187 opts.dst_index = &the_index;
188 - opts.msgs = get_porcelain_error_msgs();
188 + set_porcelain_error_msgs(opts.msgs);
189
190 init_tree_desc_from_tree(t+0, common);
191 init_tree_desc_from_tree(t+1, head);
@@ -1178,26 +1178,19 @@ static int process_entry(struct merge_options *o,
1178 return clean_merge;
1179 }
1180
1181 -struct unpack_trees_error_msgs get_porcelain_error_msgs(void)
1181 +void set_porcelain_error_msgs(const char **msgs)
1182 {
1183 - struct unpack_trees_error_msgs msgs = {
1184 - /* would_overwrite */
1185 - "Your local changes to '%s' would be overwritten by merge. Aborting.",
1186 - /* not_uptodate_file */
1187 - "Your local changes to '%s' would be overwritten by merge. Aborting.",
1188 - /* not_uptodate_dir */
1189 - "Updating '%s' would lose untracked files in it. Aborting.",
1190 - /* would_lose_untracked */
1191 - "Untracked working tree file '%s' would be %s by merge. Aborting",
1192 - /* bind_overlap -- will not happen here */
1193 - NULL,
1194 - };
1195 - if (advice_commit_before_merge) {
1196 - msgs.would_overwrite = msgs.not_uptodate_file =
1183 + if (advice_commit_before_merge)
1184 + msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
1185 "Your local changes to '%s' would be overwritten by merge. Aborting.\n"
1186 "Please, commit your changes or stash them before you can merge.";
1199 - }
1200 - return msgs;
1187 + else
1188 + msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
1189 + "Your local changes to '%s' would be overwritten by merge. Aborting.";
1190 + msgs[ERROR_NOT_UPTODATE_DIR] =
1191 + "Updating '%s' would lose untracked files in it. Aborting.";
1192 + msgs[ERROR_WOULD_LOSE_UNTRACKED] =
1193 + "Untracked working tree file '%s' would be %s by merge. Aborting";
1194 }
1195
1196 int merge_trees(struct merge_options *o,
merge-recursive.h
+2 -2
@@ -23,8 +23,8 @@ struct merge_options {
23 struct string_list current_directory_set;
24 };
25
26 -/* Return a list of user-friendly error messages to be used by merge */
27 -struct unpack_trees_error_msgs get_porcelain_error_msgs(void);
26 +/* Sets the list of user-friendly error messages to be used by merge */
27 +void set_porcelain_error_msgs(const char **msgs);
28
29 /* merge_trees() but with recursive ancestor consolidation */
30 int merge_recursive(struct merge_options *o,
unpack-trees.c
+21 -21
@@ -13,37 +13,37 @@
13 * Error messages expected by scripts out of plumbing commands such as
14 * read-tree. Non-scripted Porcelain is not required to use these messages
15 * and in fact are encouraged to reword them to better suit their particular
16 - * situation better. See how "git checkout" replaces not_uptodate_file to
16 + * situation better. See how "git checkout" replaces ERROR_NOT_UPTODATE_FILE to
17 * explain why it does not allow switching between branches when you have
18 * local changes, for example.
19 */
20 -static struct unpack_trees_error_msgs unpack_plumbing_errors = {
21 - /* would_overwrite */
20 +const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
21 + /* ERROR_WOULD_OVERWRITE */
22 "Entry '%s' would be overwritten by merge. Cannot merge.",
23
24 - /* not_uptodate_file */
24 + /* ERROR_NOT_UPTODATE_FILE */
25 "Entry '%s' not uptodate. Cannot merge.",
26
27 - /* not_uptodate_dir */
27 + /* ERROR_NOT_UPTODATE_DIR */
28 "Updating '%s' would lose untracked files in it",
29
30 - /* would_lose_untracked */
30 + /* ERROR_WOULD_LOSE_UNTRACKED */
31 "Untracked working tree file '%s' would be %s by merge.",
32
33 - /* bind_overlap */
33 + /* ERROR_BIND_OVERLAP */
34 "Entry '%s' overlaps with '%s'. Cannot bind.",
35
36 - /* sparse_not_uptodate_file */
36 + /* ERROR_SPARSE_NOT_UPTODATE_FILE */
37 "Entry '%s' not uptodate. Cannot update sparse checkout.",
38
39 - /* would_lose_orphaned */
39 + /* ERROR_WOULD_LOSE_ORPHANED */
40 "Working tree file '%s' would be %s by sparse checkout update.",
41 };
42
43 -#define ERRORMSG(o,fld) \
44 - ( ((o) && (o)->msgs.fld) \
45 - ? ((o)->msgs.fld) \
46 - : (unpack_plumbing_errors.fld) )
43 +#define ERRORMSG(o,type) \
44 + ( ((o) && (o)->msgs[(type)]) \
45 + ? ((o)->msgs[(type)]) \
46 + : (unpack_plumbing_errors[(type)]) )
47
48 static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
49 unsigned int set, unsigned int clear)
@@ -838,7 +838,7 @@ return_failed:
838
839 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
840 {
841 - return error(ERRORMSG(o, would_overwrite), ce->name);
841 + return error(ERRORMSG(o, ERROR_WOULD_OVERWRITE), ce->name);
842 }
843
844 static int same(struct cache_entry *a, struct cache_entry *b)
@@ -893,13 +893,13 @@ static int verify_uptodate(struct cache_entry *ce,
893 {
894 if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
895 return 0;
896 - return verify_uptodate_1(ce, o, ERRORMSG(o, not_uptodate_file));
896 + return verify_uptodate_1(ce, o, ERRORMSG(o, ERROR_NOT_UPTODATE_FILE));
897 }
898
899 static int verify_uptodate_sparse(struct cache_entry *ce,
900 struct unpack_trees_options *o)
901 {
902 - return verify_uptodate_1(ce, o, ERRORMSG(o, sparse_not_uptodate_file));
902 + return verify_uptodate_1(ce, o, ERRORMSG(o, ERROR_SPARSE_NOT_UPTODATE_FILE));
903 }
904
905 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
@@ -986,7 +986,7 @@ static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
986 i = read_directory(&d, pathbuf, namelen+1, NULL);
987 if (i)
988 return o->gently ? -1 :
989 - error(ERRORMSG(o, not_uptodate_dir), ce->name);
989 + error(ERRORMSG(o, ERROR_NOT_UPTODATE_DIR), ce->name);
990 free(pathbuf);
991 return cnt;
992 }
@@ -1068,7 +1068,7 @@ static int verify_absent_1(struct cache_entry *ce, const char *action,
1068 }
1069
1070 return o->gently ? -1 :
1071 - error(ERRORMSG(o, would_lose_untracked), ce->name, action);
1071 + error(ERRORMSG(o, ERROR_WOULD_LOSE_UNTRACKED), ce->name, action);
1072 }
1073 return 0;
1074 }
@@ -1077,13 +1077,13 @@ static int verify_absent(struct cache_entry *ce, const char *action,
1077 {
1078 if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
1079 return 0;
1080 - return verify_absent_1(ce, action, o, ERRORMSG(o, would_lose_untracked));
1080 + return verify_absent_1(ce, action, o, ERRORMSG(o, ERROR_WOULD_LOSE_UNTRACKED));
1081 }
1082
1083 static int verify_absent_sparse(struct cache_entry *ce, const char *action,
1084 struct unpack_trees_options *o)
1085 {
1086 - return verify_absent_1(ce, action, o, ERRORMSG(o, would_lose_orphaned));
1086 + return verify_absent_1(ce, action, o, ERRORMSG(o, ERROR_WOULD_LOSE_ORPHANED));
1087 }
1088
1089 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
@@ -1412,7 +1412,7 @@ int bind_merge(struct cache_entry **src,
1412 o->merge_size);
1413 if (a && old)
1414 return o->gently ? -1 :
1415 - error(ERRORMSG(o, bind_overlap), a->name, old->name);
1415 + error(ERRORMSG(o, ERROR_BIND_OVERLAP), a->name, old->name);
1416 if (!a)
1417 return keep_entry(old, o);
1418 else
unpack-trees.h
+10 -9
@@ -9,14 +9,15 @@ struct exclude_list;
9 typedef int (*merge_fn_t)(struct cache_entry **src,
10 struct unpack_trees_options *options);
11
12 -struct unpack_trees_error_msgs {
13 - const char *would_overwrite;
14 - const char *not_uptodate_file;
15 - const char *not_uptodate_dir;
16 - const char *would_lose_untracked;
17 - const char *bind_overlap;
18 - const char *sparse_not_uptodate_file;
19 - const char *would_lose_orphaned;
12 +enum unpack_trees_error_types {
13 + ERROR_WOULD_OVERWRITE = 0,
14 + ERROR_NOT_UPTODATE_FILE,
15 + ERROR_NOT_UPTODATE_DIR,
16 + ERROR_WOULD_LOSE_UNTRACKED,
17 + ERROR_BIND_OVERLAP,
18 + ERROR_SPARSE_NOT_UPTODATE_FILE,
19 + ERROR_WOULD_LOSE_ORPHANED,
20 + NB_UNPACK_TREES_ERROR_TYPES
21 };
22
23 struct unpack_trees_options {
@@ -38,7 +39,7 @@ struct unpack_trees_options {
39 int cache_bottom;
40 struct dir_struct *dir;
41 merge_fn_t fn;
41 - struct unpack_trees_error_msgs msgs;
42 + const char *msgs[NB_UNPACK_TREES_ERROR_TYPES];
43
44 int head_idx;
45 int merge_size;