|
24 | 24 | #include "tree.h"
|
25 | 25 | #include "tree-walk.h"
|
26 | 26 | #include "unpack-trees.h"
|
| 27 | +#include "wt-status.h" |
27 | 28 | #include "xdiff-interface.h"
|
28 | 29 |
|
29 | 30 | static const char * const checkout_usage[] = {
|
@@ -56,6 +57,7 @@ struct checkout_opts {
|
56 | 57 | int accept_pathspec;
|
57 | 58 | int switch_branch_doing_nothing_is_ok;
|
58 | 59 | int only_merge_on_switching_branches;
|
| 60 | + int can_switch_when_in_progress; |
59 | 61 |
|
60 | 62 | const char *new_branch;
|
61 | 63 | const char *new_branch_force;
|
@@ -1202,6 +1204,39 @@ static void die_expecting_a_branch(const struct branch_info *branch_info)
|
1202 | 1204 | die(_("a branch is expected, got '%s'"), branch_info->name);
|
1203 | 1205 | }
|
1204 | 1206 |
|
| 1207 | +static void die_if_some_operation_in_progress(void) |
| 1208 | +{ |
| 1209 | + struct wt_status_state state; |
| 1210 | + |
| 1211 | + memset(&state, 0, sizeof(state)); |
| 1212 | + wt_status_get_state(the_repository, &state, 0); |
| 1213 | + |
| 1214 | + if (state.merge_in_progress) |
| 1215 | + die(_("cannot switch branch while merging\n" |
| 1216 | + "Consider \"git merge --quit\" " |
| 1217 | + "or \"git worktree add\".")); |
| 1218 | + if (state.am_in_progress) |
| 1219 | + die(_("cannot switch branch in the middle of an am session\n" |
| 1220 | + "Consider \"git am --quit\" " |
| 1221 | + "or \"git worktree add\".")); |
| 1222 | + if (state.rebase_interactive_in_progress || state.rebase_in_progress) |
| 1223 | + die(_("cannot switch branch while rebasing\n" |
| 1224 | + "Consider \"git rebase --quit\" " |
| 1225 | + "or \"git worktree add\".")); |
| 1226 | + if (state.cherry_pick_in_progress) |
| 1227 | + die(_("cannot switch branch while cherry-picking\n" |
| 1228 | + "Consider \"git cherry-pick --quit\" " |
| 1229 | + "or \"git worktree add\".")); |
| 1230 | + if (state.revert_in_progress) |
| 1231 | + die(_("cannot switch branch while reverting\n" |
| 1232 | + "Consider \"git revert --quit\" " |
| 1233 | + "or \"git worktree add\".")); |
| 1234 | + if (state.bisect_in_progress) |
| 1235 | + die(_("cannot switch branch while bisecting\n" |
| 1236 | + "Consider \"git bisect reset HEAD\" " |
| 1237 | + "or \"git worktree add\".")); |
| 1238 | +} |
| 1239 | + |
1205 | 1240 | static int checkout_branch(struct checkout_opts *opts,
|
1206 | 1241 | struct branch_info *new_branch_info)
|
1207 | 1242 | {
|
@@ -1257,6 +1292,9 @@ static int checkout_branch(struct checkout_opts *opts,
|
1257 | 1292 | !new_branch_info->path)
|
1258 | 1293 | die_expecting_a_branch(new_branch_info);
|
1259 | 1294 |
|
| 1295 | + if (!opts->can_switch_when_in_progress) |
| 1296 | + die_if_some_operation_in_progress(); |
| 1297 | + |
1260 | 1298 | if (new_branch_info->path && !opts->force_detach && !opts->new_branch &&
|
1261 | 1299 | !opts->ignore_other_worktrees) {
|
1262 | 1300 | int flag;
|
@@ -1514,6 +1552,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
|
1514 | 1552 | opts.only_merge_on_switching_branches = 0;
|
1515 | 1553 | opts.accept_pathspec = 1;
|
1516 | 1554 | opts.implicit_detach = 1;
|
| 1555 | + opts.can_switch_when_in_progress = 1; |
1517 | 1556 |
|
1518 | 1557 | options = parse_options_dup(checkout_options);
|
1519 | 1558 | options = add_common_options(&opts, options);
|
@@ -1549,6 +1588,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
|
1549 | 1588 | opts.switch_branch_doing_nothing_is_ok = 0;
|
1550 | 1589 | opts.only_merge_on_switching_branches = 1;
|
1551 | 1590 | opts.implicit_detach = 0;
|
| 1591 | + opts.can_switch_when_in_progress = 0; |
1552 | 1592 |
|
1553 | 1593 | options = parse_options_dup(switch_options);
|
1554 | 1594 | options = add_common_options(&opts, options);
|
|
0 commit comments