Skip to content

Commit 86e5003

Browse files
committed
Merge branch 'pr/6062'
2 parents a50bbba + 942cfac commit 86e5003

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

include/git2/worktree.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,15 @@ typedef struct git_worktree_add_options {
8686

8787
int lock; /**< lock newly created worktree */
8888
git_reference *ref; /**< reference to use for the new worktree HEAD */
89+
90+
/**
91+
* Options for the checkout.
92+
*/
93+
git_checkout_options checkout_options;
8994
} git_worktree_add_options;
9095

9196
#define GIT_WORKTREE_ADD_OPTIONS_VERSION 1
92-
#define GIT_WORKTREE_ADD_OPTIONS_INIT {GIT_WORKTREE_ADD_OPTIONS_VERSION,0,NULL}
97+
#define GIT_WORKTREE_ADD_OPTIONS_INIT {GIT_WORKTREE_ADD_OPTIONS_VERSION,0,NULL,GIT_CHECKOUT_OPTIONS_INIT}
9398

9499
/**
95100
* Initialize git_worktree_add_options structure

src/worktree.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,23 +304,25 @@ int git_worktree_add(git_worktree **out, git_repository *repo,
304304
git_reference *ref = NULL, *head = NULL;
305305
git_commit *commit = NULL;
306306
git_repository *wt = NULL;
307-
git_checkout_options coopts = GIT_CHECKOUT_OPTIONS_INIT;
307+
git_checkout_options coopts;
308308
git_worktree_add_options wtopts = GIT_WORKTREE_ADD_OPTIONS_INIT;
309309
int err;
310310

311311
GIT_ERROR_CHECK_VERSION(
312312
opts, GIT_WORKTREE_ADD_OPTIONS_VERSION, "git_worktree_add_options");
313313

314-
if (opts)
315-
memcpy(&wtopts, opts, sizeof(wtopts));
316-
317314
GIT_ASSERT_ARG(out);
318315
GIT_ASSERT_ARG(repo);
319316
GIT_ASSERT_ARG(name);
320317
GIT_ASSERT_ARG(worktree);
321318

322319
*out = NULL;
323320

321+
if (opts)
322+
memcpy(&wtopts, opts, sizeof(wtopts));
323+
324+
memcpy(&coopts, &wtopts.checkout_options, sizeof(coopts));
325+
324326
if (wtopts.ref) {
325327
if (!git_reference_is_branch(wtopts.ref)) {
326328
git_error_set(GIT_ERROR_WORKTREE, "reference is not a branch");
@@ -405,7 +407,6 @@ int git_worktree_add(git_worktree **out, git_repository *repo,
405407
goto out;
406408

407409
/* Checkout worktree's HEAD */
408-
coopts.checkout_strategy = GIT_CHECKOUT_FORCE;
409410
if ((err = git_checkout_head(wt, &coopts)) < 0)
410411
goto out;
411412

tests/worktree/worktree.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,28 @@ void test_worktree_worktree__add_with_explicit_branch(void)
292292
git_worktree_free(wt);
293293
}
294294

295+
void test_worktree_worktree__add_no_checkout(void)
296+
{
297+
git_worktree *wt;
298+
git_repository *wtrepo;
299+
git_index *index;
300+
git_str path = GIT_STR_INIT;
301+
git_worktree_add_options opts = GIT_WORKTREE_ADD_OPTIONS_INIT;
302+
303+
opts.checkout_options.checkout_strategy = GIT_CHECKOUT_NONE;
304+
305+
cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-no-checkout"));
306+
cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-no-checkout", path.ptr, &opts));
307+
308+
cl_git_pass(git_repository_open(&wtrepo, path.ptr));
309+
cl_git_pass(git_repository_index(&index, wtrepo));
310+
cl_assert_equal_i(git_index_entrycount(index), 0);
311+
312+
git_str_dispose(&path);
313+
git_worktree_free(wt);
314+
git_index_free(index);
315+
git_repository_free(wtrepo);
316+
}
295317

296318
void test_worktree_worktree__init_existing_worktree(void)
297319
{

0 commit comments

Comments
 (0)