Skip to content

Commit 5dd6e23

Browse files
sunshinecogitster
authored andcommitted
worktree: introduce options container
add_worktree() will eventually need to deal with some options itself, so introduce a structure into which options can be conveniently bundled, and pass it along to add_worktree(). Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eef005d commit 5dd6e23

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

builtin/worktree.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ static const char * const worktree_usage[] = {
1212
NULL
1313
};
1414

15+
struct add_opts {
16+
int force;
17+
int detach;
18+
const char *new_branch;
19+
int force_new_branch;
20+
};
21+
1522
static int show_only;
1623
static int verbose;
1724
static unsigned long expire;
@@ -171,7 +178,8 @@ static const char *worktree_basename(const char *path, int *olen)
171178
return name;
172179
}
173180

174-
static int add_worktree(const char *path, const char **child_argv)
181+
static int add_worktree(const char *path, const char **child_argv,
182+
const struct add_opts *opts)
175183
{
176184
struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
177185
struct strbuf sb = STRBUF_INIT;
@@ -272,50 +280,51 @@ static int add_worktree(const char *path, const char **child_argv)
272280

273281
static int add(int ac, const char **av, const char *prefix)
274282
{
275-
int force = 0, detach = 0, force_new_branch;
276-
const char *new_branch = NULL, *new_branch_force = NULL;
283+
struct add_opts opts;
284+
const char *new_branch_force = NULL;
277285
const char *path, *branch;
278286
struct argv_array cmd = ARGV_ARRAY_INIT;
279287
struct option options[] = {
280-
OPT__FORCE(&force, N_("checkout <branch> even if already checked out in other worktree")),
281-
OPT_STRING('b', NULL, &new_branch, N_("branch"),
288+
OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
289+
OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
282290
N_("create a new branch")),
283291
OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
284292
N_("create or reset a branch")),
285-
OPT_BOOL(0, "detach", &detach, N_("detach HEAD at named commit")),
293+
OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
286294
OPT_END()
287295
};
288296

297+
memset(&opts, 0, sizeof(opts));
289298
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
290-
if (new_branch && new_branch_force)
299+
if (opts.new_branch && new_branch_force)
291300
die(_("-b and -B are mutually exclusive"));
292301
if (ac < 1 || ac > 2)
293302
usage_with_options(worktree_usage, options);
294303

295304
path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
296305
branch = ac < 2 ? "HEAD" : av[1];
297306

298-
force_new_branch = !!new_branch_force;
299-
if (force_new_branch)
300-
new_branch = new_branch_force;
307+
opts.force_new_branch = !!new_branch_force;
308+
if (opts.force_new_branch)
309+
opts.new_branch = new_branch_force;
301310

302-
if (ac < 2 && !new_branch) {
311+
if (ac < 2 && !opts.new_branch) {
303312
int n;
304313
const char *s = worktree_basename(path, &n);
305-
new_branch = xstrndup(s, n);
314+
opts.new_branch = xstrndup(s, n);
306315
}
307316

308317
argv_array_push(&cmd, "checkout");
309-
if (force)
318+
if (opts.force)
310319
argv_array_push(&cmd, "--ignore-other-worktrees");
311-
if (new_branch)
312-
argv_array_pushl(&cmd, force_new_branch ? "-B" : "-b",
313-
new_branch, NULL);
314-
if (detach)
320+
if (opts.new_branch)
321+
argv_array_pushl(&cmd, opts.force_new_branch ? "-B" : "-b",
322+
opts.new_branch, NULL);
323+
if (opts.detach)
315324
argv_array_push(&cmd, "--detach");
316325
argv_array_push(&cmd, branch);
317326

318-
return add_worktree(path, cmd.argv);
327+
return add_worktree(path, cmd.argv, &opts);
319328
}
320329

321330
int cmd_worktree(int ac, const char **av, const char *prefix)

0 commit comments

Comments
 (0)