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>
Nguyễn Thái Ngọc Duy committed
Mar 29, 2019 at 17:39 UTC
b3edccb967ab4531e6a5fd20610a3be93be01c70
1 file changed
+58
-57
builtin/checkout.c
+58
-57
@@ -1341,82 +1341,83 @@ static int checkout_branch(struct checkout_opts *opts,
1341
1342
int cmd_checkout(int argc, const char **argv, const char *prefix)
1343
{
1344
- struct checkout_opts opts;
1344
+ struct checkout_opts real_opts;
1345
+ struct checkout_opts *opts = &real_opts;
1346
struct branch_info new_branch_info;
1347
char *conflict_style = NULL;
1348
int dwim_new_local_branch, no_dwim_new_local_branch = 0;
1349
int dwim_remotes_matched = 0;
1350
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"),
1353
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"),
1355
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"),
1359
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,
1362
N_("checkout our version for unmerged files"),
1363
2, PARSE_OPT_NONEG),
1363
- OPT_SET_INT_F('3', "theirs", &opts.writeout_stage,
1364
+ OPT_SET_INT_F('3', "theirs", &opts->writeout_stage,
1365
N_("checkout their version for unmerged files"),
1366
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)"),
1368
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,
1371
N_("update ignored files (default)"),
1372
PARSE_OPT_NOCOMPLETE),
1373
OPT_STRING(0, "conflict", &conflict_style, N_("style"),
1374
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,
1377
N_("do not limit pathspecs to sparse entries only")),
1378
OPT_BOOL(0, "no-guess", &no_dwim_new_local_branch,
1379
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,
1381
N_("do not check if another worktree is holding the given ref")),
1382
{ OPTION_CALLBACK, 0, "recurse-submodules", NULL,
1383
"checkout", "control recursive updating of submodules",
1384
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)")),
1387
OPT_END(),
1388
};
1389
1389
- memset(&opts, 0, sizeof(opts));
1390
+ memset(opts, 0, sizeof(*opts));
1391
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;
1396
1396
- git_config(git_checkout_config, &opts);
1397
+ git_config(git_checkout_config, opts);
1398
1398
- opts.track = BRANCH_TRACK_UNSPECIFIED;
1399
+ opts->track = BRANCH_TRACK_UNSPECIFIED;
1400
1401
argc = parse_options(argc, argv, prefix, options, checkout_usage,
1402
PARSE_OPT_KEEP_DASHDASH);
1403
1404
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;
1408
else
1408
- opts.show_progress = isatty(2);
1409
+ opts->show_progress = isatty(2);
1410
}
1411
1412
if (conflict_style) {
1412
- opts.merge = 1; /* implied */
1413
+ opts->merge = 1; /* implied */
1414
git_xmerge_config("merge.conflictstyle", conflict_style, NULL);
1415
}
1416
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)
1418
die(_("-b, -B and --orphan are mutually exclusive"));
1419
1419
- if (opts.overlay_mode == 1 && opts.patch_mode)
1420
+ if (opts->overlay_mode == 1 && opts->patch_mode)
1421
die(_("-p and --overlay are mutually exclusive"));
1422
1423
/*
@@ -1424,14 +1425,14 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1425
* and new_branch_force and new_orphan_branch will tell us which one of
1426
* -b/-B/--orphan is being used.
1427
*/
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;
1430
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;
1433
1434
/* --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) {
1436
const char *argv0 = argv[0];
1437
if (!argc || !strcmp(argv0, "--"))
1438
die(_("--track needs a branch name"));
@@ -1440,7 +1441,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1441
argv0 = strchr(argv0, '/');
1442
if (!argv0 || !argv0[1])
1443
die(_("missing branch name; try -b"));
1443
- opts.new_branch = argv0 + 1;
1444
+ opts->new_branch = argv0 + 1;
1445
}
1446
1447
/*
@@ -1459,56 +1460,56 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1460
if (argc) {
1461
struct object_id rev;
1462
int dwim_ok =
1462
- !opts.patch_mode &&
1463
+ !opts->patch_mode &&
1464
dwim_new_local_branch &&
1464
- opts.track == BRANCH_TRACK_UNSPECIFIED &&
1465
- !opts.new_branch;
1465
+ opts->track == BRANCH_TRACK_UNSPECIFIED &&
1466
+ !opts->new_branch;
1467
int n = parse_branchname_arg(argc, argv, dwim_ok,
1467
- &new_branch_info, &opts, &rev,
1468
+ &new_branch_info, opts, &rev,
1469
&dwim_remotes_matched);
1470
argv += n;
1471
argc -= n;
1472
}
1473
1474
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,
1477
prefix, argv);
1478
1478
- if (!opts.pathspec.nr)
1479
+ if (!opts->pathspec.nr)
1480
die(_("invalid path specification"));
1481
1482
/*
1483
* Try to give more helpful suggestion.
1484
* new_branch && argc > 1 will be caught later.
1485
*/
1485
- if (opts.new_branch && argc == 1)
1486
+ if (opts->new_branch && argc == 1)
1487
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);
1489
1489
- if (opts.force_detach)
1490
+ if (opts->force_detach)
1491
die(_("git checkout: --detach does not take a path argument '%s'"),
1492
argv[0]);
1493
1493
- if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge)
1494
+ if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge)
1495
die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"
1496
"checking out of the index."));
1497
}
1498
1498
- if (opts.new_branch) {
1499
+ if (opts->new_branch) {
1500
struct strbuf buf = STRBUF_INIT;
1501
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);
1504
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);
1507
strbuf_release(&buf);
1508
}
1509
1510
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);
1513
if (ret && dwim_remotes_matched > 1 &&
1514
advice_checkout_ambiguous_remote_branch_name)
1515
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)
1528
dwim_remotes_matched);
1529
return ret;
1530
} else {
1530
- return checkout_branch(&opts, &new_branch_info);
1531
+ return checkout_branch(opts, &new_branch_info);
1532
}
1533
}