switch: add --discard-changes
--discard-changes is a better name than --force for this option since it's what really happens. --force is turned to an alias for --discard-changes. But it's meant to be an alias for potentially more force options in the future. Side note. It's not obvious from the patch but --discard-changes also affects submodules if --recurse-submodules is used. The knob to force updating submodules is hidden behind unpack-trees.c Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Mar 29, 2019 at 17:39 UTC
3ec37ad13dea55f6a614753accc7fad84cb170be
1 file changed
+10
-2
builtin/checkout.c
+10
-2
@@ -53,6 +53,7 @@ struct checkout_opts {
53
int count_checkout_paths;
54
int overlay_mode;
55
int no_dwim_new_local_branch;
56
+ int discard_changes;
57
58
/*
59
* If new checkout options are added, skip_merge_working_tree
@@ -680,7 +681,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
681
return error(_("index file corrupt"));
682
683
resolve_undo_clear();
683
- if (opts->force) {
684
+ if (opts->discard_changes) {
685
ret = reset_tree(get_commit_tree(new_branch_info->commit),
686
opts, 1, writeout_error);
687
if (ret)
@@ -802,7 +803,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
803
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
804
die(_("unable to write new index file"));
805
805
- if (!opts->force && !opts->quiet)
806
+ if (!opts->discard_changes && !opts->quiet)
807
show_local_changes(&new_branch_info->commit->object, &opts->diff_options);
808
809
return 0;
@@ -1309,6 +1310,9 @@ static int checkout_branch(struct checkout_opts *opts,
1310
if (opts->force && opts->merge)
1311
die(_("'%s' cannot be used with '%s'"), "-f", "-m");
1312
1313
+ if (opts->discard_changes && opts->merge)
1314
+ die(_("'%s' cannot be used with '%s'"), "--discard-changes", "--merge");
1315
+
1316
if (opts->force_detach && opts->new_branch)
1317
die(_("'%s' cannot be used with '%s'"),
1318
"--detach", "-b/-B/--orphan");
@@ -1445,6 +1449,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1449
opts->merge = 1; /* implied */
1450
git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
1451
}
1452
+ if (opts->force)
1453
+ opts->discard_changes = 1;
1454
1455
if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)
1456
die(_("-b, -B and --orphan are mutually exclusive"));
@@ -1600,6 +1606,8 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
1606
N_("create and switch to a new branch")),
1607
OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"),
1608
N_("create/reset and switch to a branch")),
1609
+ OPT_BOOL(0, "discard-changes", &opts.discard_changes,
1610
+ N_("throw away local modifications")),
1611
OPT_END()
1612
};
1613
int ret;