switch: reject "do nothing" case
[gitweb.git] / builtin / checkout.c
index 0e6037b2968f326ca2781fa141d74683e52c983c..e9dfe38d69d46b11b271995a7d68cf18db4795a1 100644 (file)
@@ -1,30 +1,30 @@
 #define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
-#include "config.h"
+#include "advice.h"
+#include "blob.h"
+#include "branch.h"
+#include "cache-tree.h"
 #include "checkout.h"
+#include "commit.h"
+#include "config.h"
+#include "diff.h"
+#include "dir.h"
+#include "ll-merge.h"
 #include "lockfile.h"
+#include "merge-recursive.h"
+#include "object-store.h"
 #include "parse-options.h"
 #include "refs.h"
-#include "object-store.h"
-#include "commit.h"
+#include "remote.h"
+#include "resolve-undo.h"
+#include "revision.h"
+#include "run-command.h"
+#include "submodule.h"
+#include "submodule-config.h"
 #include "tree.h"
 #include "tree-walk.h"
-#include "cache-tree.h"
 #include "unpack-trees.h"
-#include "dir.h"
-#include "run-command.h"
-#include "merge-recursive.h"
-#include "branch.h"
-#include "diff.h"
-#include "revision.h"
-#include "remote.h"
-#include "blob.h"
 #include "xdiff-interface.h"
-#include "ll-merge.h"
-#include "resolve-undo.h"
-#include "submodule-config.h"
-#include "submodule.h"
-#include "advice.h"
 
 static int checkout_optimize_new_branch;
 
@@ -34,6 +34,11 @@ static const char * const checkout_usage[] = {
        NULL,
 };
 
+static const char * const switch_branch_usage[] = {
+       N_("git switch [<options>] [<branch>]"),
+       NULL,
+};
+
 struct checkout_opts {
        int patch_mode;
        int quiet;
@@ -47,6 +52,11 @@ struct checkout_opts {
        int show_progress;
        int count_checkout_paths;
        int overlay_mode;
+       int no_dwim_new_local_branch;
+       int discard_changes;
+       int accept_pathspec;
+       int switch_branch_doing_nothing_is_ok;
+
        /*
         * If new checkout options are added, skip_merge_working_tree
         * should be updated accordingly.
@@ -58,6 +68,7 @@ struct checkout_opts {
        int new_branch_log;
        enum branch_track track;
        struct diff_options diff_options;
+       char *conflict_style;
 
        int branch_exists;
        const char *prefix;
@@ -672,7 +683,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
                return error(_("index file corrupt"));
 
        resolve_undo_clear();
-       if (opts->force) {
+       if (opts->discard_changes) {
                ret = reset_tree(get_commit_tree(new_branch_info->commit),
                                 opts, 1, writeout_error);
                if (ret)
@@ -794,7 +805,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
        if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
                die(_("unable to write new index file"));
 
-       if (!opts->force && !opts->quiet)
+       if (!opts->discard_changes && !opts->quiet)
                show_local_changes(&new_branch_info->commit->object, &opts->diff_options);
 
        return 0;
@@ -899,7 +910,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
                                delete_reflog(old_branch_info->path);
                }
        }
-       remove_branch_state(the_repository);
+       remove_branch_state(the_repository, !opts->quiet);
        strbuf_release(&msg);
        if (!opts->quiet &&
            (new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name, "HEAD"))))
@@ -1081,6 +1092,34 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
        return git_xmerge_config(var, value, NULL);
 }
 
+static void setup_new_branch_info_and_source_tree(
+       struct branch_info *new_branch_info,
+       struct checkout_opts *opts,
+       struct object_id *rev,
+       const char *arg)
+{
+       struct tree **source_tree = &opts->source_tree;
+       struct object_id branch_rev;
+
+       new_branch_info->name = arg;
+       setup_branch_path(new_branch_info);
+
+       if (!check_refname_format(new_branch_info->path, 0) &&
+           !read_ref(new_branch_info->path, &branch_rev))
+               oidcpy(rev, &branch_rev);
+       else
+               new_branch_info->path = NULL; /* not an existing branch */
+
+       new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1);
+       if (!new_branch_info->commit) {
+               /* not a commit */
+               *source_tree = parse_tree_indirect(rev);
+       } else {
+               parse_commit_or_die(new_branch_info->commit);
+               *source_tree = get_commit_tree(new_branch_info->commit);
+       }
+}
+
 static int parse_branchname_arg(int argc, const char **argv,
                                int dwim_new_local_branch_ok,
                                struct branch_info *new_branch_info,
@@ -1088,10 +1127,8 @@ static int parse_branchname_arg(int argc, const char **argv,
                                struct object_id *rev,
                                int *dwim_remotes_matched)
 {
-       struct tree **source_tree = &opts->source_tree;
        const char **new_branch = &opts->new_branch;
        int argcount = 0;
-       struct object_id branch_rev;
        const char *arg;
        int dash_dash_pos;
        int has_dash_dash = 0;
@@ -1141,10 +1178,16 @@ static int parse_branchname_arg(int argc, const char **argv,
        if (!argc)
                return 0;
 
+       if (!opts->accept_pathspec) {
+               if (argc > 1)
+                       die(_("only one reference expected"));
+               has_dash_dash = 1; /* helps disambiguate */
+       }
+
        arg = argv[0];
        dash_dash_pos = -1;
        for (i = 0; i < argc; i++) {
-               if (!strcmp(argv[i], "--")) {
+               if (opts->accept_pathspec && !strcmp(argv[i], "--")) {
                        dash_dash_pos = i;
                        break;
                }
@@ -1178,11 +1221,12 @@ static int parse_branchname_arg(int argc, const char **argv,
                        recover_with_dwim = 0;
 
                /*
-                * Accept "git checkout foo" and "git checkout foo --"
-                * as candidates for dwim.
+                * Accept "git checkout foo", "git checkout foo --"
+                * and "git switch foo" as candidates for dwim.
                 */
                if (!(argc == 1 && !has_dash_dash) &&
-                   !(argc == 2 && has_dash_dash))
+                   !(argc == 2 && has_dash_dash) &&
+                   opts->accept_pathspec)
                        recover_with_dwim = 0;
 
                if (recover_with_dwim) {
@@ -1213,26 +1257,11 @@ static int parse_branchname_arg(int argc, const char **argv,
        argv++;
        argc--;
 
-       new_branch_info->name = arg;
-       setup_branch_path(new_branch_info);
-
-       if (!check_refname_format(new_branch_info->path, 0) &&
-           !read_ref(new_branch_info->path, &branch_rev))
-               oidcpy(rev, &branch_rev);
-       else
-               new_branch_info->path = NULL; /* not an existing branch */
-
-       new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1);
-       if (!new_branch_info->commit) {
-               /* not a commit */
-               *source_tree = parse_tree_indirect(rev);
-       } else {
-               parse_commit_or_die(new_branch_info->commit);
-               *source_tree = get_commit_tree(new_branch_info->commit);
-       }
+       setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg);
 
-       if (!*source_tree)                   /* case (1): want a tree */
+       if (!opts->source_tree)                   /* case (1): want a tree */
                die(_("reference is not a tree: %s"), arg);
+
        if (!has_dash_dash) {   /* case (3).(d) -> (1) */
                /*
                 * Do not complain the most common case
@@ -1242,7 +1271,7 @@ static int parse_branchname_arg(int argc, const char **argv,
                 */
                if (argc)
                        verify_non_filename(opts->prefix, arg);
-       } else {
+       } else if (opts->accept_pathspec) {
                argcount++;
                argv++;
                argc--;
@@ -1290,6 +1319,9 @@ static int checkout_branch(struct checkout_opts *opts,
        if (opts->force && opts->merge)
                die(_("'%s' cannot be used with '%s'"), "-f", "-m");
 
+       if (opts->discard_changes && opts->merge)
+               die(_("'%s' cannot be used with '%s'"), "--discard-changes", "--merge");
+
        if (opts->force_detach && opts->new_branch)
                die(_("'%s' cannot be used with '%s'"),
                    "--detach", "-b/-B/--orphan");
@@ -1307,6 +1339,12 @@ static int checkout_branch(struct checkout_opts *opts,
                die(_("Cannot switch branch to a non-commit '%s'"),
                    new_branch_info->name);
 
+       if (!opts->switch_branch_doing_nothing_is_ok &&
+           !new_branch_info->name &&
+           !opts->new_branch &&
+           !opts->force_detach)
+               die(_("missing branch or commit argument"));
+
        if (new_branch_info->path && !opts->force_detach && !opts->new_branch &&
            !opts->ignore_other_worktrees) {
                int flag;
@@ -1328,84 +1366,110 @@ static int checkout_branch(struct checkout_opts *opts,
        return switch_branches(opts, new_branch_info);
 }
 
-int cmd_checkout(int argc, const char **argv, const char *prefix)
+static struct option *add_common_options(struct checkout_opts *opts,
+                                        struct option *prevopts)
 {
-       struct checkout_opts opts;
-       struct branch_info new_branch_info;
-       char *conflict_style = NULL;
-       int dwim_new_local_branch, no_dwim_new_local_branch = 0;
-       int dwim_remotes_matched = 0;
        struct option options[] = {
-               OPT__QUIET(&opts.quiet, N_("suppress progress reporting")),
-               OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
-                          N_("create and checkout a new branch")),
-               OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"),
-                          N_("create/reset and checkout a branch")),
-               OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
-               OPT_BOOL(0, "detach", &opts.force_detach, N_("detach HEAD at named commit")),
-               OPT_SET_INT('t', "track",  &opts.track, N_("set upstream info for new branch"),
+               OPT__QUIET(&opts->quiet, N_("suppress progress reporting")),
+               { OPTION_CALLBACK, 0, "recurse-submodules", NULL,
+                           "checkout", "control recursive updating of submodules",
+                           PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
+               OPT_BOOL(0, "progress", &opts->show_progress, N_("force progress reporting")),
+               OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
+                          PARSE_OPT_NOCOMPLETE),
+               OPT_BOOL('m', "merge", &opts->merge, N_("perform a 3-way merge with the new branch")),
+               OPT_STRING(0, "conflict", &opts->conflict_style, N_("style"),
+                          N_("conflict style (merge or diff3)")),
+               OPT_END()
+       };
+       struct option *newopts = parse_options_concat(prevopts, options);
+       free(prevopts);
+       return newopts;
+}
+
+static struct option *add_common_switch_branch_options(
+       struct checkout_opts *opts, struct option *prevopts)
+{
+       struct option options[] = {
+               OPT_BOOL(0, "detach", &opts->force_detach, N_("detach HEAD at named commit")),
+               OPT_SET_INT('t', "track",  &opts->track, N_("set upstream info for new branch"),
                        BRANCH_TRACK_EXPLICIT),
-               OPT_STRING(0, "orphan", &opts.new_orphan_branch, N_("new-branch"), N_("new unparented branch")),
-               OPT_SET_INT_F('2', "ours", &opts.writeout_stage,
+               OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unparented branch")),
+               OPT_BOOL_F(0, "overwrite-ignore", &opts->overwrite_ignore,
+                          N_("update ignored files (default)"),
+                          PARSE_OPT_NOCOMPLETE),
+               OPT_BOOL(0, "no-guess", &opts->no_dwim_new_local_branch,
+                        N_("second guess 'git checkout <no-such-branch>'")),
+               OPT_BOOL(0, "ignore-other-worktrees", &opts->ignore_other_worktrees,
+                        N_("do not check if another worktree is holding the given ref")),
+               OPT_END()
+       };
+       struct option *newopts = parse_options_concat(prevopts, options);
+       free(prevopts);
+       return newopts;
+}
+
+static struct option *add_checkout_path_options(struct checkout_opts *opts,
+                                               struct option *prevopts)
+{
+       struct option options[] = {
+               OPT_SET_INT_F('2', "ours", &opts->writeout_stage,
                              N_("checkout our version for unmerged files"),
                              2, PARSE_OPT_NONEG),
-               OPT_SET_INT_F('3', "theirs", &opts.writeout_stage,
+               OPT_SET_INT_F('3', "theirs", &opts->writeout_stage,
                              N_("checkout their version for unmerged files"),
                              3, PARSE_OPT_NONEG),
-               OPT__FORCE(&opts.force, N_("force checkout (throw away local modifications)"),
-                          PARSE_OPT_NOCOMPLETE),
-               OPT_BOOL('m', "merge", &opts.merge, N_("perform a 3-way merge with the new branch")),
-               OPT_BOOL_F(0, "overwrite-ignore", &opts.overwrite_ignore,
-                          N_("update ignored files (default)"),
-                          PARSE_OPT_NOCOMPLETE),
-               OPT_STRING(0, "conflict", &conflict_style, N_("style"),
-                          N_("conflict style (merge or diff3)")),
-               OPT_BOOL('p', "patch", &opts.patch_mode, N_("select hunks interactively")),
-               OPT_BOOL(0, "ignore-skip-worktree-bits", &opts.ignore_skipworktree,
+               OPT_BOOL('p', "patch", &opts->patch_mode, N_("select hunks interactively")),
+               OPT_BOOL(0, "ignore-skip-worktree-bits", &opts->ignore_skipworktree,
                         N_("do not limit pathspecs to sparse entries only")),
-               OPT_BOOL(0, "no-guess", &no_dwim_new_local_branch,
-                        N_("do not second guess 'git checkout <no-such-branch>'")),
-               OPT_BOOL(0, "ignore-other-worktrees", &opts.ignore_other_worktrees,
-                        N_("do not check if another worktree is holding the given ref")),
-               { OPTION_CALLBACK, 0, "recurse-submodules", NULL,
-                           "checkout", "control recursive updating of submodules",
-                           PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
-               OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")),
-               OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
-               OPT_END(),
+               OPT_BOOL(0, "overlay", &opts->overlay_mode, N_("use overlay mode (default)")),
+               OPT_END()
        };
+       struct option *newopts = parse_options_concat(prevopts, options);
+       free(prevopts);
+       return newopts;
+}
+
+static int checkout_main(int argc, const char **argv, const char *prefix,
+                        struct checkout_opts *opts, struct option *options,
+                        const char * const usagestr[])
+{
+       struct branch_info new_branch_info;
+       int dwim_remotes_matched = 0;
+       int dwim_new_local_branch;
 
-       memset(&opts, 0, sizeof(opts));
        memset(&new_branch_info, 0, sizeof(new_branch_info));
-       opts.overwrite_ignore = 1;
-       opts.prefix = prefix;
-       opts.show_progress = -1;
-       opts.overlay_mode = -1;
+       opts->overwrite_ignore = 1;
+       opts->prefix = prefix;
+       opts->show_progress = -1;
+       opts->overlay_mode = -1;
 
-       git_config(git_checkout_config, &opts);
+       git_config(git_checkout_config, opts);
 
-       opts.track = BRANCH_TRACK_UNSPECIFIED;
+       opts->track = BRANCH_TRACK_UNSPECIFIED;
 
-       argc = parse_options(argc, argv, prefix, options, checkout_usage,
+       argc = parse_options(argc, argv, prefix, options, usagestr,
                             PARSE_OPT_KEEP_DASHDASH);
 
-       dwim_new_local_branch = !no_dwim_new_local_branch;
-       if (opts.show_progress < 0) {
-               if (opts.quiet)
-                       opts.show_progress = 0;
+       dwim_new_local_branch = !opts->no_dwim_new_local_branch;
+       if (opts->show_progress < 0) {
+               if (opts->quiet)
+                       opts->show_progress = 0;
                else
-                       opts.show_progress = isatty(2);
+                       opts->show_progress = isatty(2);
        }
 
-       if (conflict_style) {
-               opts.merge = 1; /* implied */
-               git_xmerge_config("merge.conflictstyle", conflict_style, NULL);
+       if (opts->conflict_style) {
+               opts->merge = 1; /* implied */
+               git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
        }
+       if (opts->force)
+               opts->discard_changes = 1;
 
-       if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) > 1)
+       if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)
                die(_("-b, -B and --orphan are mutually exclusive"));
 
-       if (opts.overlay_mode == 1 && opts.patch_mode)
+       if (opts->overlay_mode == 1 && opts->patch_mode)
                die(_("-p and --overlay are mutually exclusive"));
 
        /*
@@ -1413,14 +1477,14 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
         * and new_branch_force and new_orphan_branch will tell us which one of
         * -b/-B/--orphan is being used.
         */
-       if (opts.new_branch_force)
-               opts.new_branch = opts.new_branch_force;
+       if (opts->new_branch_force)
+               opts->new_branch = opts->new_branch_force;
 
-       if (opts.new_orphan_branch)
-               opts.new_branch = opts.new_orphan_branch;
+       if (opts->new_orphan_branch)
+               opts->new_branch = opts->new_orphan_branch;
 
        /* --track without -b/-B/--orphan should DWIM */
-       if (opts.track != BRANCH_TRACK_UNSPECIFIED && !opts.new_branch) {
+       if (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {
                const char *argv0 = argv[0];
                if (!argc || !strcmp(argv0, "--"))
                        die(_("--track needs a branch name"));
@@ -1429,7 +1493,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                argv0 = strchr(argv0, '/');
                if (!argv0 || !argv0[1])
                        die(_("missing branch name; try -b"));
-               opts.new_branch = argv0 + 1;
+               opts->new_branch = argv0 + 1;
        }
 
        /*
@@ -1448,56 +1512,56 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
        if (argc) {
                struct object_id rev;
                int dwim_ok =
-                       !opts.patch_mode &&
+                       !opts->patch_mode &&
                        dwim_new_local_branch &&
-                       opts.track == BRANCH_TRACK_UNSPECIFIED &&
-                       !opts.new_branch;
+                       opts->track == BRANCH_TRACK_UNSPECIFIED &&
+                       !opts->new_branch;
                int n = parse_branchname_arg(argc, argv, dwim_ok,
-                                            &new_branch_info, &opts, &rev,
+                                            &new_branch_info, opts, &rev,
                                             &dwim_remotes_matched);
                argv += n;
                argc -= n;
        }
 
        if (argc) {
-               parse_pathspec(&opts.pathspec, 0,
-                              opts.patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,
+               parse_pathspec(&opts->pathspec, 0,
+                              opts->patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,
                               prefix, argv);
 
-               if (!opts.pathspec.nr)
+               if (!opts->pathspec.nr)
                        die(_("invalid path specification"));
 
                /*
                 * Try to give more helpful suggestion.
                 * new_branch && argc > 1 will be caught later.
                 */
-               if (opts.new_branch && argc == 1)
+               if (opts->new_branch && argc == 1)
                        die(_("'%s' is not a commit and a branch '%s' cannot be created from it"),
-                               argv[0], opts.new_branch);
+                               argv[0], opts->new_branch);
 
-               if (opts.force_detach)
+               if (opts->force_detach)
                        die(_("git checkout: --detach does not take a path argument '%s'"),
                            argv[0]);
 
-               if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge)
+               if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge)
                        die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"
                              "checking out of the index."));
        }
 
-       if (opts.new_branch) {
+       if (opts->new_branch) {
                struct strbuf buf = STRBUF_INIT;
 
-               if (opts.new_branch_force)
-                       opts.branch_exists = validate_branchname(opts.new_branch, &buf);
+               if (opts->new_branch_force)
+                       opts->branch_exists = validate_branchname(opts->new_branch, &buf);
                else
-                       opts.branch_exists =
-                               validate_new_branchname(opts.new_branch, &buf, 0);
+                       opts->branch_exists =
+                               validate_new_branchname(opts->new_branch, &buf, 0);
                strbuf_release(&buf);
        }
 
        UNLEAK(opts);
-       if (opts.patch_mode || opts.pathspec.nr) {
-               int ret = checkout_paths(&opts, new_branch_info.name);
+       if (opts->patch_mode || opts->pathspec.nr) {
+               int ret = checkout_paths(opts, new_branch_info.name);
                if (ret && dwim_remotes_matched > 1 &&
                    advice_checkout_ambiguous_remote_branch_name)
                        advise(_("'%s' matched more than one remote tracking branch.\n"
@@ -1516,6 +1580,66 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                               dwim_remotes_matched);
                return ret;
        } else {
-               return checkout_branch(&opts, &new_branch_info);
+               return checkout_branch(opts, &new_branch_info);
        }
 }
+
+int cmd_checkout(int argc, const char **argv, const char *prefix)
+{
+       struct checkout_opts opts;
+       struct option *options;
+       struct option checkout_options[] = {
+               OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
+                          N_("create and checkout a new branch")),
+               OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"),
+                          N_("create/reset and checkout a branch")),
+               OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
+               OPT_END()
+       };
+       int ret;
+
+       memset(&opts, 0, sizeof(opts));
+       opts.no_dwim_new_local_branch = 0;
+       opts.switch_branch_doing_nothing_is_ok = 1;
+       opts.accept_pathspec = 1;
+
+       options = parse_options_dup(checkout_options);
+       options = add_common_options(&opts, options);
+       options = add_common_switch_branch_options(&opts, options);
+       options = add_checkout_path_options(&opts, options);
+
+       ret = checkout_main(argc, argv, prefix, &opts,
+                           options, checkout_usage);
+       FREE_AND_NULL(options);
+       return ret;
+}
+
+int cmd_switch(int argc, const char **argv, const char *prefix)
+{
+       struct checkout_opts opts;
+       struct option *options = NULL;
+       struct option switch_options[] = {
+               OPT_STRING('c', "create", &opts.new_branch, N_("branch"),
+                          N_("create and switch to a new branch")),
+               OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"),
+                          N_("create/reset and switch to a branch")),
+               OPT_BOOL(0, "discard-changes", &opts.discard_changes,
+                        N_("throw away local modifications")),
+               OPT_END()
+       };
+       int ret;
+
+       memset(&opts, 0, sizeof(opts));
+       opts.no_dwim_new_local_branch = 0;
+       opts.accept_pathspec = 0;
+       opts.switch_branch_doing_nothing_is_ok = 0;
+
+       options = parse_options_dup(switch_options);
+       options = add_common_options(&opts, options);
+       options = add_common_switch_branch_options(&opts, options);
+
+       ret = checkout_main(argc, argv, prefix, &opts,
+                           options, switch_branch_usage);
+       FREE_AND_NULL(options);
+       return ret;
+}