Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
8529ac9b
Unverified
Commit
8529ac9b
authored
Apr 17, 2018
by
Edward Thomson
Committed by
GitHub
Apr 17, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4524 from pks-t/pks/worktree-refs
worktree: add ability to create worktree with pre-existing branch
parents
1fd26760
a22f19e6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
8 deletions
+56
-8
include/git2/worktree.h
+2
-1
src/worktree.c
+24
-7
tests/worktree/worktree.c
+30
-0
No files found.
include/git2/worktree.h
View file @
8529ac9b
...
...
@@ -78,10 +78,11 @@ typedef struct git_worktree_add_options {
unsigned
int
version
;
int
lock
;
/**< lock newly created worktree */
git_reference
*
ref
;
/**< reference to use for the new worktree HEAD */
}
git_worktree_add_options
;
#define GIT_WORKTREE_ADD_OPTIONS_VERSION 1
#define GIT_WORKTREE_ADD_OPTIONS_INIT {GIT_WORKTREE_ADD_OPTIONS_VERSION,0}
#define GIT_WORKTREE_ADD_OPTIONS_INIT {GIT_WORKTREE_ADD_OPTIONS_VERSION,0
,NULL
}
/**
* Initializes a `git_worktree_add_options` with default vaules.
...
...
src/worktree.c
View file @
8529ac9b
...
...
@@ -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
)
...
...
tests/worktree/worktree.c
View file @
8529ac9b
...
...
@@ -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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment