summaryrefslogtreecommitdiff
path: root/src/worktree.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2018-04-17 23:38:46 +0100
committerGitHub <noreply@github.com>2018-04-17 23:38:46 +0100
commit8529ac9b98e9d05c2616b855a497ee88c4f5c955 (patch)
tree746400c37073424a3e9e3cbb627df047d2348b68 /src/worktree.c
parent1fd267608803f401a72380d26fbaf57ac3f4fe38 (diff)
parenta22f19e6c4c0bf5205cd47dd66bfacd5c7d18136 (diff)
downloadlibgit2-8529ac9b98e9d05c2616b855a497ee88c4f5c955.tar.gz
Merge pull request #4524 from pks-t/pks/worktree-refs
worktree: add ability to create worktree with pre-existing branch
Diffstat (limited to 'src/worktree.c')
-rw-r--r--src/worktree.c31
1 files changed, 24 insertions, 7 deletions
diff --git a/src/worktree.c b/src/worktree.c
index 4b18db7d6..b15cd534d 100644
--- a/src/worktree.c
+++ b/src/worktree.c
@@ -348,13 +348,30 @@ int git_worktree_add(git_worktree **out, git_repository *repo,
|| (err = write_wtfile(gitdir.ptr, "gitdir", &buf)) < 0)
goto out;
- /* Create new branch */
- if ((err = git_repository_head(&head, repo)) < 0)
- goto out;
- if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0)
- goto out;
- if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0)
- goto out;
+ /* Set up worktree reference */
+ if (wtopts.ref) {
+ if (!git_reference_is_branch(wtopts.ref)) {
+ giterr_set(GITERR_WORKTREE, "reference is not a branch");
+ err = -1;
+ goto out;
+ }
+
+ if (git_branch_is_checked_out(wtopts.ref)) {
+ giterr_set(GITERR_WORKTREE, "reference is already checked out");
+ err = -1;
+ goto out;
+ }
+
+ if ((err = git_reference_dup(&ref, wtopts.ref)) < 0)
+ goto out;
+ } else {
+ if ((err = git_repository_head(&head, repo)) < 0)
+ goto out;
+ if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0)
+ goto out;
+ if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0)
+ goto out;
+ }
/* Set worktree's HEAD */
if ((err = git_repository_create_head(gitdir.ptr, git_reference_name(ref))) < 0)