worktree: add: introduce --checkout option

By adding this option which defaults to true, we can use the corresponding --no-checkout to make some customizations before the checkout, like sparse checkout, etc. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Helped-by: Junio C Hamano <gitster@pobox.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Ray Zhang <zhanglei002@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ray Zhang committed Mar 29, 2016 at 10:11 UTC ef2a0ac9a00c5d82f1fbbef8304c662ff60e4235
3 files changed +37 -12
Documentation/git-worktree.txt
+7 -1
@@ -9,7 +9,7 @@ git-worktree - Manage multiple working trees
9 SYNOPSIS
10 --------
11 [verse]
12 -'git worktree add' [-f] [--detach] [-b <new-branch>] <path> [<branch>]
12 +'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] <path> [<branch>]
13 'git worktree prune' [-n] [-v] [--expire <expire>]
14 'git worktree list' [--porcelain]
15
@@ -87,6 +87,12 @@ OPTIONS
87 With `add`, detach HEAD in the new working tree. See "DETACHED HEAD"
88 in linkgit:git-checkout[1].
89
90 +--[no-]checkout::
91 + By default, `add` checks out `<branch>`, however, `--no-checkout` can
92 + be used to suppress checkout in order to make customizations,
93 + such as configuring sparse-checkout. See "Sparse checkout"
94 + in linkgit:git-read-tree[1].
95 +
96 -n::
97 --dry-run::
98 With `prune`, do not remove anything; just report what it would
builtin/worktree.c
+18 -11
@@ -21,6 +21,7 @@ static const char * const worktree_usage[] = {
21 struct add_opts {
22 int force;
23 int detach;
24 + int checkout;
25 const char *new_branch;
26 int force_new_branch;
27 };
@@ -284,18 +285,22 @@ static int add_worktree(const char *path, const char *refname,
285 if (ret)
286 goto done;
287
287 - cp.argv = NULL;
288 - argv_array_clear(&cp.args);
289 - argv_array_pushl(&cp.args, "reset", "--hard", NULL);
290 - cp.env = child_env.argv;
291 - ret = run_command(&cp);
292 - if (!ret) {
293 - is_junk = 0;
294 - free(junk_work_tree);
295 - free(junk_git_dir);
296 - junk_work_tree = NULL;
297 - junk_git_dir = NULL;
288 + if (opts->checkout) {
289 + cp.argv = NULL;
290 + argv_array_clear(&cp.args);
291 + argv_array_pushl(&cp.args, "reset", "--hard", NULL);
292 + cp.env = child_env.argv;
293 + ret = run_command(&cp);
294 + if (ret)
295 + goto done;
296 }
297 +
298 + is_junk = 0;
299 + free(junk_work_tree);
300 + free(junk_git_dir);
301 + junk_work_tree = NULL;
302 + junk_git_dir = NULL;
303 +
304 done:
305 strbuf_reset(&sb);
306 strbuf_addf(&sb, "%s/locked", sb_repo.buf);
@@ -320,10 +325,12 @@ static int add(int ac, const char **av, const char *prefix)
325 OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
326 N_("create or reset a branch")),
327 OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
328 + OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
329 OPT_END()
330 };
331
332 memset(&opts, 0, sizeof(opts));
333 + opts.checkout = 1;
334 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
335 if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
336 die(_("-b, -B, and --detach are mutually exclusive"));
t/t2025-worktree-add.sh
+12
@@ -213,4 +213,16 @@ test_expect_success 'local clone from linked checkout' '
213 ( cd here-clone && git fsck )
214 '
215
216 +test_expect_success '"add" worktree with --no-checkout' '
217 + git worktree add --no-checkout -b swamp swamp &&
218 + ! test -e swamp/init.t &&
219 + git -C swamp reset --hard &&
220 + test_cmp init.t swamp/init.t
221 +'
222 +
223 +test_expect_success '"add" worktree with --checkout' '
224 + git worktree add --checkout -b swmap2 swamp2 &&
225 + test_cmp init.t swamp2/init.t
226 +'
227 +
228 test_done