Skip to content

Commit b3edccb

Browse files
pcloudsgitster
authored andcommitted
checkout: make "opts" in cmd_checkout() a pointer
"opts" will soon be moved out of cmd_checkout(). To keep changes in that patch smaller, convert "opts" to a pointer and keep the real thing behind "real_opts". Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7ab4ad0 commit b3edccb

File tree

1 file changed

+58
-57
lines changed

1 file changed

+58
-57
lines changed

builtin/checkout.c

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,97 +1341,98 @@ static int checkout_branch(struct checkout_opts *opts,
13411341

13421342
int cmd_checkout(int argc, const char **argv, const char *prefix)
13431343
{
1344-
struct checkout_opts opts;
1344+
struct checkout_opts real_opts;
1345+
struct checkout_opts *opts = &real_opts;
13451346
struct branch_info new_branch_info;
13461347
char *conflict_style = NULL;
13471348
int dwim_new_local_branch, no_dwim_new_local_branch = 0;
13481349
int dwim_remotes_matched = 0;
13491350
struct option options[] = {
1350-
OPT__QUIET(&opts.quiet, N_("suppress progress reporting")),
1351-
OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
1351+
OPT__QUIET(&opts->quiet, N_("suppress progress reporting")),
1352+
OPT_STRING('b', NULL, &opts->new_branch, N_("branch"),
13521353
N_("create and checkout a new branch")),
1353-
OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"),
1354+
OPT_STRING('B', NULL, &opts->new_branch_force, N_("branch"),
13541355
N_("create/reset and checkout a branch")),
1355-
OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
1356-
OPT_BOOL(0, "detach", &opts.force_detach, N_("detach HEAD at named commit")),
1357-
OPT_SET_INT('t', "track", &opts.track, N_("set upstream info for new branch"),
1356+
OPT_BOOL('l', NULL, &opts->new_branch_log, N_("create reflog for new branch")),
1357+
OPT_BOOL(0, "detach", &opts->force_detach, N_("detach HEAD at named commit")),
1358+
OPT_SET_INT('t', "track", &opts->track, N_("set upstream info for new branch"),
13581359
BRANCH_TRACK_EXPLICIT),
1359-
OPT_STRING(0, "orphan", &opts.new_orphan_branch, N_("new-branch"), N_("new unparented branch")),
1360-
OPT_SET_INT_F('2', "ours", &opts.writeout_stage,
1360+
OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unparented branch")),
1361+
OPT_SET_INT_F('2', "ours", &opts->writeout_stage,
13611362
N_("checkout our version for unmerged files"),
13621363
2, PARSE_OPT_NONEG),
1363-
OPT_SET_INT_F('3', "theirs", &opts.writeout_stage,
1364+
OPT_SET_INT_F('3', "theirs", &opts->writeout_stage,
13641365
N_("checkout their version for unmerged files"),
13651366
3, PARSE_OPT_NONEG),
1366-
OPT__FORCE(&opts.force, N_("force checkout (throw away local modifications)"),
1367+
OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
13671368
PARSE_OPT_NOCOMPLETE),
1368-
OPT_BOOL('m', "merge", &opts.merge, N_("perform a 3-way merge with the new branch")),
1369-
OPT_BOOL_F(0, "overwrite-ignore", &opts.overwrite_ignore,
1369+
OPT_BOOL('m', "merge", &opts->merge, N_("perform a 3-way merge with the new branch")),
1370+
OPT_BOOL_F(0, "overwrite-ignore", &opts->overwrite_ignore,
13701371
N_("update ignored files (default)"),
13711372
PARSE_OPT_NOCOMPLETE),
13721373
OPT_STRING(0, "conflict", &conflict_style, N_("style"),
13731374
N_("conflict style (merge or diff3)")),
1374-
OPT_BOOL('p', "patch", &opts.patch_mode, N_("select hunks interactively")),
1375-
OPT_BOOL(0, "ignore-skip-worktree-bits", &opts.ignore_skipworktree,
1375+
OPT_BOOL('p', "patch", &opts->patch_mode, N_("select hunks interactively")),
1376+
OPT_BOOL(0, "ignore-skip-worktree-bits", &opts->ignore_skipworktree,
13761377
N_("do not limit pathspecs to sparse entries only")),
13771378
OPT_BOOL(0, "no-guess", &no_dwim_new_local_branch,
13781379
N_("do not second guess 'git checkout <no-such-branch>'")),
1379-
OPT_BOOL(0, "ignore-other-worktrees", &opts.ignore_other_worktrees,
1380+
OPT_BOOL(0, "ignore-other-worktrees", &opts->ignore_other_worktrees,
13801381
N_("do not check if another worktree is holding the given ref")),
13811382
{ OPTION_CALLBACK, 0, "recurse-submodules", NULL,
13821383
"checkout", "control recursive updating of submodules",
13831384
PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
1384-
OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")),
1385-
OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
1385+
OPT_BOOL(0, "progress", &opts->show_progress, N_("force progress reporting")),
1386+
OPT_BOOL(0, "overlay", &opts->overlay_mode, N_("use overlay mode (default)")),
13861387
OPT_END(),
13871388
};
13881389

1389-
memset(&opts, 0, sizeof(opts));
1390+
memset(opts, 0, sizeof(*opts));
13901391
memset(&new_branch_info, 0, sizeof(new_branch_info));
1391-
opts.overwrite_ignore = 1;
1392-
opts.prefix = prefix;
1393-
opts.show_progress = -1;
1394-
opts.overlay_mode = -1;
1392+
opts->overwrite_ignore = 1;
1393+
opts->prefix = prefix;
1394+
opts->show_progress = -1;
1395+
opts->overlay_mode = -1;
13951396

1396-
git_config(git_checkout_config, &opts);
1397+
git_config(git_checkout_config, opts);
13971398

1398-
opts.track = BRANCH_TRACK_UNSPECIFIED;
1399+
opts->track = BRANCH_TRACK_UNSPECIFIED;
13991400

14001401
argc = parse_options(argc, argv, prefix, options, checkout_usage,
14011402
PARSE_OPT_KEEP_DASHDASH);
14021403

14031404
dwim_new_local_branch = !no_dwim_new_local_branch;
1404-
if (opts.show_progress < 0) {
1405-
if (opts.quiet)
1406-
opts.show_progress = 0;
1405+
if (opts->show_progress < 0) {
1406+
if (opts->quiet)
1407+
opts->show_progress = 0;
14071408
else
1408-
opts.show_progress = isatty(2);
1409+
opts->show_progress = isatty(2);
14091410
}
14101411

14111412
if (conflict_style) {
1412-
opts.merge = 1; /* implied */
1413+
opts->merge = 1; /* implied */
14131414
git_xmerge_config("merge.conflictstyle", conflict_style, NULL);
14141415
}
14151416

1416-
if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) > 1)
1417+
if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)
14171418
die(_("-b, -B and --orphan are mutually exclusive"));
14181419

1419-
if (opts.overlay_mode == 1 && opts.patch_mode)
1420+
if (opts->overlay_mode == 1 && opts->patch_mode)
14201421
die(_("-p and --overlay are mutually exclusive"));
14211422

14221423
/*
14231424
* From here on, new_branch will contain the branch to be checked out,
14241425
* and new_branch_force and new_orphan_branch will tell us which one of
14251426
* -b/-B/--orphan is being used.
14261427
*/
1427-
if (opts.new_branch_force)
1428-
opts.new_branch = opts.new_branch_force;
1428+
if (opts->new_branch_force)
1429+
opts->new_branch = opts->new_branch_force;
14291430

1430-
if (opts.new_orphan_branch)
1431-
opts.new_branch = opts.new_orphan_branch;
1431+
if (opts->new_orphan_branch)
1432+
opts->new_branch = opts->new_orphan_branch;
14321433

14331434
/* --track without -b/-B/--orphan should DWIM */
1434-
if (opts.track != BRANCH_TRACK_UNSPECIFIED && !opts.new_branch) {
1435+
if (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {
14351436
const char *argv0 = argv[0];
14361437
if (!argc || !strcmp(argv0, "--"))
14371438
die(_("--track needs a branch name"));
@@ -1440,7 +1441,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
14401441
argv0 = strchr(argv0, '/');
14411442
if (!argv0 || !argv0[1])
14421443
die(_("missing branch name; try -b"));
1443-
opts.new_branch = argv0 + 1;
1444+
opts->new_branch = argv0 + 1;
14441445
}
14451446

14461447
/*
@@ -1459,56 +1460,56 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
14591460
if (argc) {
14601461
struct object_id rev;
14611462
int dwim_ok =
1462-
!opts.patch_mode &&
1463+
!opts->patch_mode &&
14631464
dwim_new_local_branch &&
1464-
opts.track == BRANCH_TRACK_UNSPECIFIED &&
1465-
!opts.new_branch;
1465+
opts->track == BRANCH_TRACK_UNSPECIFIED &&
1466+
!opts->new_branch;
14661467
int n = parse_branchname_arg(argc, argv, dwim_ok,
1467-
&new_branch_info, &opts, &rev,
1468+
&new_branch_info, opts, &rev,
14681469
&dwim_remotes_matched);
14691470
argv += n;
14701471
argc -= n;
14711472
}
14721473

14731474
if (argc) {
1474-
parse_pathspec(&opts.pathspec, 0,
1475-
opts.patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,
1475+
parse_pathspec(&opts->pathspec, 0,
1476+
opts->patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,
14761477
prefix, argv);
14771478

1478-
if (!opts.pathspec.nr)
1479+
if (!opts->pathspec.nr)
14791480
die(_("invalid path specification"));
14801481

14811482
/*
14821483
* Try to give more helpful suggestion.
14831484
* new_branch && argc > 1 will be caught later.
14841485
*/
1485-
if (opts.new_branch && argc == 1)
1486+
if (opts->new_branch && argc == 1)
14861487
die(_("'%s' is not a commit and a branch '%s' cannot be created from it"),
1487-
argv[0], opts.new_branch);
1488+
argv[0], opts->new_branch);
14881489

1489-
if (opts.force_detach)
1490+
if (opts->force_detach)
14901491
die(_("git checkout: --detach does not take a path argument '%s'"),
14911492
argv[0]);
14921493

1493-
if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge)
1494+
if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge)
14941495
die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"
14951496
"checking out of the index."));
14961497
}
14971498

1498-
if (opts.new_branch) {
1499+
if (opts->new_branch) {
14991500
struct strbuf buf = STRBUF_INIT;
15001501

1501-
if (opts.new_branch_force)
1502-
opts.branch_exists = validate_branchname(opts.new_branch, &buf);
1502+
if (opts->new_branch_force)
1503+
opts->branch_exists = validate_branchname(opts->new_branch, &buf);
15031504
else
1504-
opts.branch_exists =
1505-
validate_new_branchname(opts.new_branch, &buf, 0);
1505+
opts->branch_exists =
1506+
validate_new_branchname(opts->new_branch, &buf, 0);
15061507
strbuf_release(&buf);
15071508
}
15081509

15091510
UNLEAK(opts);
1510-
if (opts.patch_mode || opts.pathspec.nr) {
1511-
int ret = checkout_paths(&opts, new_branch_info.name);
1511+
if (opts->patch_mode || opts->pathspec.nr) {
1512+
int ret = checkout_paths(opts, new_branch_info.name);
15121513
if (ret && dwim_remotes_matched > 1 &&
15131514
advice_checkout_ambiguous_remote_branch_name)
15141515
advise(_("'%s' matched more than one remote tracking branch.\n"
@@ -1527,6 +1528,6 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
15271528
dwim_remotes_matched);
15281529
return ret;
15291530
} else {
1530-
return checkout_branch(&opts, &new_branch_info);
1531+
return checkout_branch(opts, &new_branch_info);
15311532
}
15321533
}

0 commit comments

Comments
 (0)