Skip to content

Commit 7ab4ad0

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent 0dabeff commit 7ab4ad0

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

builtin/checkout.c

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,17 +1081,43 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
10811081
return git_xmerge_config(var, value, NULL);
10821082
}
10831083

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+
10841112
static int parse_branchname_arg(int argc, const char **argv,
10851113
int dwim_new_local_branch_ok,
10861114
struct branch_info *new_branch_info,
10871115
struct checkout_opts *opts,
10881116
struct object_id *rev,
10891117
int *dwim_remotes_matched)
10901118
{
1091-
struct tree **source_tree = &opts->source_tree;
10921119
const char **new_branch = &opts->new_branch;
10931120
int argcount = 0;
1094-
struct object_id branch_rev;
10951121
const char *arg;
10961122
int dash_dash_pos;
10971123
int has_dash_dash = 0;
@@ -1213,26 +1239,11 @@ static int parse_branchname_arg(int argc, const char **argv,
12131239
argv++;
12141240
argc--;
12151241

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);
12241243

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 */
12351245
die(_("reference is not a tree: %s"), arg);
1246+
12361247
if (!has_dash_dash) { /* case (3).(d) -> (1) */
12371248
/*
12381249
* Do not complain the most common case

0 commit comments

Comments
 (0)