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>
This commit is contained in:
Nguyễn Thái Ngọc Duy 2019-03-29 17:39:01 +07:00 committed by Junio C Hamano
parent 0dabeffc16
commit 7ab4ad00cc

View 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);
setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg);
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);
}
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