checkout: factor out some code in parse_branchname_arg()
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Fri, 29 Mar 2019 10:39:01 +0000 (17:39 +0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 2 Apr 2019 04:56:59 +0000 (13:56 +0900)
This is in preparation for the new command restore, which also
needs to parse opts->source_tree but does not need all the
disambiguation logic.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
index 15ee5181ebe758b6925abe1a06d7c653e6c3a928..897eefe16decfeb728b65bfd56747ebed2a60c20 100644 (file)
@@ -1081,6 +1081,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 +1116,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;
@@ -1213,26 +1239,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 */
+       setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg);
 
-       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);
-       }
-
-       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