Skip to content

Commit 0d8683c

Browse files
committed
Merge branch 'rz/worktree-no-checkout'
"git worktree add" can be given "--no-checkout" option to only create an empty worktree without checking out the files. * rz/worktree-no-checkout: worktree: add: introduce --checkout option
2 parents 5c788e7 + ef2a0ac commit 0d8683c

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

Documentation/git-worktree.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-worktree - Manage multiple working trees
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git worktree add' [-f] [--detach] [-b <new-branch>] <path> [<branch>]
12+
'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] <path> [<branch>]
1313
'git worktree prune' [-n] [-v] [--expire <expire>]
1414
'git worktree list' [--porcelain]
1515

@@ -87,6 +87,12 @@ OPTIONS
8787
With `add`, detach HEAD in the new working tree. See "DETACHED HEAD"
8888
in linkgit:git-checkout[1].
8989

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+
9096
-n::
9197
--dry-run::
9298
With `prune`, do not remove anything; just report what it would

builtin/worktree.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ static const char * const worktree_usage[] = {
2121
struct add_opts {
2222
int force;
2323
int detach;
24+
int checkout;
2425
const char *new_branch;
2526
int force_new_branch;
2627
};
@@ -284,18 +285,22 @@ static int add_worktree(const char *path, const char *refname,
284285
if (ret)
285286
goto done;
286287

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;
298296
}
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+
299304
done:
300305
strbuf_reset(&sb);
301306
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
@@ -320,10 +325,12 @@ static int add(int ac, const char **av, const char *prefix)
320325
OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
321326
N_("create or reset a branch")),
322327
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")),
323329
OPT_END()
324330
};
325331

326332
memset(&opts, 0, sizeof(opts));
333+
opts.checkout = 1;
327334
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
328335
if (!!opts.detach + !!opts.new_branch + !!new_branch_force > 1)
329336
die(_("-b, -B, and --detach are mutually exclusive"));

t/t2025-worktree-add.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,16 @@ test_expect_success 'local clone from linked checkout' '
213213
( cd here-clone && git fsck )
214214
'
215215

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+
216228
test_done

0 commit comments

Comments
 (0)