Skip to content

Commit 08353eb

Browse files
moygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 64fdc08 commit 08353eb

File tree

6 files changed

+46
-52
lines changed

6 files changed

+46
-52
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ static int merge_working_tree(struct checkout_opts *opts,
373373
topts.src_index = &the_index;
374374
topts.dst_index = &the_index;
375375

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.";
377377

378378
refresh_cache(REFRESH_QUIET);
379379

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +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.msgs = get_porcelain_error_msgs();
707+
set_porcelain_error_msgs(opts.msgs);
708708

709709
trees[nr_trees] = parse_tree_indirect(head);
710710
if (!trees[nr_trees++])

merge-recursive.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static int git_merge_trees(int index_only,
185185
opts.fn = threeway_merge;
186186
opts.src_index = &the_index;
187187
opts.dst_index = &the_index;
188-
opts.msgs = get_porcelain_error_msgs();
188+
set_porcelain_error_msgs(opts.msgs);
189189

190190
init_tree_desc_from_tree(t+0, common);
191191
init_tree_desc_from_tree(t+1, head);
@@ -1178,26 +1178,19 @@ static int process_entry(struct merge_options *o,
11781178
return clean_merge;
11791179
}
11801180

1181-
struct unpack_trees_error_msgs get_porcelain_error_msgs(void)
1181+
void set_porcelain_error_msgs(const char **msgs)
11821182
{
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] =
11971185
"Your local changes to '%s' would be overwritten by merge. Aborting.\n"
11981186
"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";
12011194
}
12021195

12031196
int merge_trees(struct merge_options *o,

merge-recursive.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ struct merge_options {
2323
struct string_list current_directory_set;
2424
};
2525

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);
2828

2929
/* merge_trees() but with recursive ancestor consolidation */
3030
int merge_recursive(struct merge_options *o,

unpack-trees.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,37 @@
1313
* Error messages expected by scripts out of plumbing commands such as
1414
* read-tree. Non-scripted Porcelain is not required to use these messages
1515
* 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
1717
* explain why it does not allow switching between branches when you have
1818
* local changes, for example.
1919
*/
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 */
2222
"Entry '%s' would be overwritten by merge. Cannot merge.",
2323

24-
/* not_uptodate_file */
24+
/* ERROR_NOT_UPTODATE_FILE */
2525
"Entry '%s' not uptodate. Cannot merge.",
2626

27-
/* not_uptodate_dir */
27+
/* ERROR_NOT_UPTODATE_DIR */
2828
"Updating '%s' would lose untracked files in it",
2929

30-
/* would_lose_untracked */
30+
/* ERROR_WOULD_LOSE_UNTRACKED */
3131
"Untracked working tree file '%s' would be %s by merge.",
3232

33-
/* bind_overlap */
33+
/* ERROR_BIND_OVERLAP */
3434
"Entry '%s' overlaps with '%s'. Cannot bind.",
3535

36-
/* sparse_not_uptodate_file */
36+
/* ERROR_SPARSE_NOT_UPTODATE_FILE */
3737
"Entry '%s' not uptodate. Cannot update sparse checkout.",
3838

39-
/* would_lose_orphaned */
39+
/* ERROR_WOULD_LOSE_ORPHANED */
4040
"Working tree file '%s' would be %s by sparse checkout update.",
4141
};
4242

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)]) )
4747

4848
static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
4949
unsigned int set, unsigned int clear)
@@ -838,7 +838,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
838838

839839
static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
840840
{
841-
return error(ERRORMSG(o, would_overwrite), ce->name);
841+
return error(ERRORMSG(o, ERROR_WOULD_OVERWRITE), ce->name);
842842
}
843843

844844
static int same(struct cache_entry *a, struct cache_entry *b)
@@ -893,13 +893,13 @@ static int verify_uptodate(struct cache_entry *ce,
893893
{
894894
if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
895895
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));
897897
}
898898

899899
static int verify_uptodate_sparse(struct cache_entry *ce,
900900
struct unpack_trees_options *o)
901901
{
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));
903903
}
904904

905905
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,
986986
i = read_directory(&d, pathbuf, namelen+1, NULL);
987987
if (i)
988988
return o->gently ? -1 :
989-
error(ERRORMSG(o, not_uptodate_dir), ce->name);
989+
error(ERRORMSG(o, ERROR_NOT_UPTODATE_DIR), ce->name);
990990
free(pathbuf);
991991
return cnt;
992992
}
@@ -1068,7 +1068,7 @@ static int verify_absent_1(struct cache_entry *ce, const char *action,
10681068
}
10691069

10701070
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);
10721072
}
10731073
return 0;
10741074
}
@@ -1077,13 +1077,13 @@ static int verify_absent(struct cache_entry *ce, const char *action,
10771077
{
10781078
if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
10791079
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));
10811081
}
10821082

10831083
static int verify_absent_sparse(struct cache_entry *ce, const char *action,
10841084
struct unpack_trees_options *o)
10851085
{
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));
10871087
}
10881088

10891089
static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
@@ -1412,7 +1412,7 @@ int bind_merge(struct cache_entry **src,
14121412
o->merge_size);
14131413
if (a && old)
14141414
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);
14161416
if (!a)
14171417
return keep_entry(old, o);
14181418
else

unpack-trees.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ struct exclude_list;
99
typedef int (*merge_fn_t)(struct cache_entry **src,
1010
struct unpack_trees_options *options);
1111

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
2021
};
2122

2223
struct unpack_trees_options {
@@ -38,7 +39,7 @@ struct unpack_trees_options {
3839
int cache_bottom;
3940
struct dir_struct *dir;
4041
merge_fn_t fn;
41-
struct unpack_trees_error_msgs msgs;
42+
const char *msgs[NB_UNPACK_TREES_ERROR_TYPES];
4243

4344
int head_idx;
4445
int merge_size;

0 commit comments

Comments
 (0)