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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Eric Sunshine committed
Jul 17, 2015 at 19:00 UTC
5dd6e234a75bb96e159c4e7d30f6acb28c3283a0
1 file changed
+27
-18
builtin/worktree.c
+27
-18
@@ -12,6 +12,13 @@ static const char * const worktree_usage[] = {
12
NULL
13
};
14
15
+struct add_opts {
16
+ int force;
17
+ int detach;
18
+ const char *new_branch;
19
+ int force_new_branch;
20
+};
21
+
22
static int show_only;
23
static int verbose;
24
static unsigned long expire;
@@ -171,7 +178,8 @@ static const char *worktree_basename(const char *path, int *olen)
178
return name;
179
}
180
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)
183
{
184
struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
185
struct strbuf sb = STRBUF_INIT;
@@ -272,22 +280,23 @@ static int add_worktree(const char *path, const char **child_argv)
280
281
static int add(int ac, const char **av, const char *prefix)
282
{
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;
285
const char *path, *branch;
286
struct argv_array cmd = ARGV_ARRAY_INIT;
287
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"),
290
N_("create a new branch")),
291
OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
292
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")),
294
OPT_END()
295
};
296
297
+ memset(&opts, 0, sizeof(opts));
298
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)
300
die(_("-b and -B are mutually exclusive"));
301
if (ac < 1 || ac > 2)
302
usage_with_options(worktree_usage, options);
@@ -295,27 +304,27 @@ static int add(int ac, const char **av, const char *prefix)
304
path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
305
branch = ac < 2 ? "HEAD" : av[1];
306
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;
310
302
- if (ac < 2 && !new_branch) {
311
+ if (ac < 2 && !opts.new_branch) {
312
int n;
313
const char *s = worktree_basename(path, &n);
305
- new_branch = xstrndup(s, n);
314
+ opts.new_branch = xstrndup(s, n);
315
}
316
317
argv_array_push(&cmd, "checkout");
309
- if (force)
318
+ if (opts.force)
319
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)
324
argv_array_push(&cmd, "--detach");
325
argv_array_push(&cmd, branch);
326
318
- return add_worktree(path, cmd.argv);
327
+ return add_worktree(path, cmd.argv, &opts);
328
}
329
330
int cmd_worktree(int ac, const char **av, const char *prefix)