diff options
| author | Patrick Steinhardt <ps@pks.im> | 2018-02-09 10:38:11 +0000 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2018-02-09 10:40:59 +0000 |
| commit | a22f19e6c4c0bf5205cd47dd66bfacd5c7d18136 (patch) | |
| tree | fccaf92a98bd4a776f542c48326afbd17a35c4a5 /tests/worktree/worktree.c | |
| parent | f7225946edeae35f48e3d402e2e0c94ea07f9666 (diff) | |
| download | libgit2-a22f19e6c4c0bf5205cd47dd66bfacd5c7d18136.tar.gz | |
worktree: add ability to create worktree with pre-existing branch
Currently, we always create a new branch after the new worktree's name
when creating a worktree. In some workflows, though, the caller may want
to check out an already existing reference instead of creating a new
one, which is impossible to do right now.
Add a new option `ref` to the options structure for adding worktrees. In
case it is set, a branch and not already checked out by another
worktree, we will re-use this reference instead of creating a new one.
Diffstat (limited to 'tests/worktree/worktree.c')
| -rw-r--r-- | tests/worktree/worktree.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/worktree/worktree.c b/tests/worktree/worktree.c index 4ac3b8bba..9b932d8ae 100644 --- a/tests/worktree/worktree.c +++ b/tests/worktree/worktree.c @@ -273,6 +273,36 @@ void test_worktree_worktree__init_existing_branch(void) git_reference_free(branch); } +void test_worktree_worktree__add_with_explicit_branch(void) +{ + git_reference *head, *branch, *wthead; + git_commit *commit; + git_worktree *wt; + git_repository *wtrepo; + git_buf path = GIT_BUF_INIT; + git_worktree_add_options opts = GIT_WORKTREE_ADD_OPTIONS_INIT; + + cl_git_pass(git_repository_head(&head, fixture.repo)); + cl_git_pass(git_commit_lookup(&commit, fixture.repo, &head->target.oid)); + cl_git_pass(git_branch_create(&branch, fixture.repo, "worktree-with-ref", commit, false)); + + opts.ref = branch; + + cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-with-different-name")); + cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-with-different-name", path.ptr, &opts)); + cl_git_pass(git_repository_open_from_worktree(&wtrepo, wt)); + cl_git_pass(git_repository_head(&wthead, wtrepo)); + cl_assert_equal_s(git_reference_name(wthead), "refs/heads/worktree-with-ref"); + + git_buf_free(&path); + git_commit_free(commit); + git_reference_free(head); + git_reference_free(branch); + git_reference_free(wthead); + git_repository_free(wtrepo); +} + + void test_worktree_worktree__init_existing_worktree(void) { git_worktree *wt; |
