checkout: factor out some code in parse_branchname_arg()

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>

Nguyễn Thái Ngọc Duy committed Mar 29, 2019 at 17:39 UTC 7ab4ad00ccb572574a6dfc73abfb3d5b724ab64e
1 file changed +31 -20
builtin/checkout.c
+31 -20
@@ -1081,6 +1081,34 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
1081 return git_xmerge_config(var, value, NULL);
1082 }
1083
1084 +static void setup_new_branch_info_and_source_tree(
1085 + struct branch_info *new_branch_info,
1086 + struct checkout_opts *opts,
1087 + struct object_id *rev,
1088 + const char *arg)
1089 +{
1090 + struct tree **source_tree = &opts->source_tree;
1091 + struct object_id branch_rev;
1092 +
1093 + new_branch_info->name = arg;
1094 + setup_branch_path(new_branch_info);
1095 +
1096 + if (!check_refname_format(new_branch_info->path, 0) &&
1097 + !read_ref(new_branch_info->path, &branch_rev))
1098 + oidcpy(rev, &branch_rev);
1099 + else
1100 + new_branch_info->path = NULL; /* not an existing branch */
1101 +
1102 + new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1);
1103 + if (!new_branch_info->commit) {
1104 + /* not a commit */
1105 + *source_tree = parse_tree_indirect(rev);
1106 + } else {
1107 + parse_commit_or_die(new_branch_info->commit);
1108 + *source_tree = get_commit_tree(new_branch_info->commit);
1109 + }
1110 +}
1111 +
1112 static int parse_branchname_arg(int argc, const char **argv,
1113 int dwim_new_local_branch_ok,
1114 struct branch_info *new_branch_info,
@@ -1088,10 +1116,8 @@ static int parse_branchname_arg(int argc, const char **argv,
1116 struct object_id *rev,
1117 int *dwim_remotes_matched)
1118 {
1091 - struct tree **source_tree = &opts->source_tree;
1119 const char **new_branch = &opts->new_branch;
1120 int argcount = 0;
1094 - struct object_id branch_rev;
1121 const char *arg;
1122 int dash_dash_pos;
1123 int has_dash_dash = 0;
@@ -1213,26 +1239,11 @@ static int parse_branchname_arg(int argc, const char **argv,
1239 argv++;
1240 argc--;
1241
1216 - new_branch_info->name = arg;
1217 - setup_branch_path(new_branch_info);
1218 -
1219 - if (!check_refname_format(new_branch_info->path, 0) &&
1220 - !read_ref(new_branch_info->path, &branch_rev))
1221 - oidcpy(rev, &branch_rev);
1222 - else
1223 - new_branch_info->path = NULL; /* not an existing branch */
1242 + setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg);
1243
1225 - new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1);
1226 - if (!new_branch_info->commit) {
1227 - /* not a commit */
1228 - *source_tree = parse_tree_indirect(rev);
1229 - } else {
1230 - parse_commit_or_die(new_branch_info->commit);
1231 - *source_tree = get_commit_tree(new_branch_info->commit);
1232 - }
1233 -
1234 - if (!*source_tree) /* case (1): want a tree */
1244 + if (!opts->source_tree) /* case (1): want a tree */
1245 die(_("reference is not a tree: %s"), arg);
1246 +
1247 if (!has_dash_dash) { /* case (3).(d) -> (1) */
1248 /*
1249 * Do not complain the most common case