Skip to content

Commit 23cbf11

Browse files
Diane Gasselingitster
authored andcommitted
merge-recursive: porcelain messages for checkout
A porcelain message was first added in checkout.c in the commit 8ccba00 (Junio C Hamano, Sat May 17 21:03:49 2008, unpack-trees: allow Porcelain to give different error messages) to give better feedback in the case of merge errors. This patch adapts the porcelain messages for the case of checkout instead. This way, when having a checkout error, "merge" no longer appears in the error message. While we're there, we add an advice in the case of would_lose_untracked_file. Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 08353eb commit 23cbf11

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
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[ERROR_NOT_UPTODATE_FILE] = "You have local changes to '%s'; cannot switch branches.";
376+
set_porcelain_error_msgs(topts.msgs, "checkout");
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-
set_porcelain_error_msgs(opts.msgs);
707+
set_porcelain_error_msgs(opts.msgs, "merge");
708708

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

merge-recursive.c

Lines changed: 22 additions & 9 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-
set_porcelain_error_msgs(opts.msgs);
188+
set_porcelain_error_msgs(opts.msgs, "merge");
189189

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

1181-
void set_porcelain_error_msgs(const char **msgs)
1181+
void set_porcelain_error_msgs(const char **msgs, const char *cmd)
11821182
{
1183+
const char *msg;
1184+
char *tmp;
1185+
const char *cmd2 = strcmp(cmd, "checkout") ? cmd : "switch branches";
11831186
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.";
1187+
msg = "Your local changes to '%%s' would be overwritten by %s. Aborting.\n"
1188+
"Please, commit your changes or stash them before you can %s.";
11871189
else
1188-
msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
1189-
"Your local changes to '%s' would be overwritten by merge. Aborting.";
1190+
msg = "Your local changes to '%%s' would be overwritten by %s. Aborting.";
1191+
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen(cmd2) - 3);
1192+
sprintf(tmp, msg, cmd, cmd2);
1193+
msgs[ERROR_WOULD_OVERWRITE] = tmp;
1194+
msgs[ERROR_NOT_UPTODATE_FILE] = tmp;
1195+
11901196
msgs[ERROR_NOT_UPTODATE_DIR] =
11911197
"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";
1198+
1199+
if (advice_commit_before_merge)
1200+
msg = "Untracked working tree file '%%s' would be %%s by %s. Aborting"
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";
1204+
tmp = xmalloc(strlen(msg) + strlen(cmd) + strlen(cmd2) - 3);
1205+
sprintf(tmp, msg, cmd, cmd2);
1206+
msgs[ERROR_WOULD_LOSE_UNTRACKED] = tmp;
11941207
}
11951208

11961209
int merge_trees(struct merge_options *o,

merge-recursive.h

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

26-
/* Sets the list of user-friendly error messages to be used by merge */
27-
void set_porcelain_error_msgs(const char **msgs);
26+
/*
27+
* Sets the list of user-friendly error messages to be used by the
28+
* command "cmd" (either merge or checkout)
29+
*/
30+
void set_porcelain_error_msgs(const char **msgs, const char *cmd);
2831

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

unpack-trees.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
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 ERROR_NOT_UPTODATE_FILE to
17-
* explain why it does not allow switching between branches when you have
18-
* local changes, for example.
16+
* situation better. See how "git checkout" and "git merge" replaces
17+
* them using set_porcelain_error_msgs(), for example.
1918
*/
2019
const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
2120
/* ERROR_WOULD_OVERWRITE */

0 commit comments

Comments
 (0)