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
04de614b
Commit
04de614b
authored
Jun 04, 2017
by
Edward Thomson
Committed by
GitHub
Jun 04, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4243 from pks-t/pks/submodule-workdir
Submodule working directory
parents
a1023a43
2696c5c3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
149 additions
and
38 deletions
+149
-38
src/repository.c
+59
-38
tests/submodule/open.c
+90
-0
No files found.
src/repository.c
View file @
04de614b
...
...
@@ -422,10 +422,10 @@ static int read_gitfile(git_buf *path_out, const char *file_path)
}
static
int
find_repo
(
git_buf
*
repo
_path
,
git_buf
*
parent
_path
,
git_buf
*
link_path
,
git_buf
*
common_path
,
git_buf
*
gitdir
_path
,
git_buf
*
workdir
_path
,
git_buf
*
git
link_path
,
git_buf
*
common
dir
_path
,
const
char
*
start_path
,
uint32_t
flags
,
const
char
*
ceiling_dirs
)
...
...
@@ -440,7 +440,7 @@ static int find_repo(
bool
in_dot_git
;
size_t
ceiling_offset
=
0
;
git_buf_
free
(
repo
_path
);
git_buf_
clear
(
gitdir
_path
);
error
=
git_path_prettify
(
&
path
,
start_path
,
NULL
);
if
(
error
<
0
)
...
...
@@ -482,13 +482,13 @@ static int find_repo(
if
(
S_ISDIR
(
st
.
st_mode
))
{
if
(
valid_repository_path
(
&
path
,
&
common_link
))
{
git_path_to_dir
(
&
path
);
git_buf_set
(
repo
_path
,
path
.
ptr
,
path
.
size
);
git_buf_set
(
gitdir
_path
,
path
.
ptr
,
path
.
size
);
if
(
link_path
)
git_buf_attach
(
link_path
,
if
(
git
link_path
)
git_buf_attach
(
git
link_path
,
git_worktree__read_link
(
path
.
ptr
,
GIT_GITDIR_FILE
),
0
);
if
(
common_path
)
git_buf_swap
(
&
common_link
,
common_path
);
if
(
common
dir
_path
)
git_buf_swap
(
&
common_link
,
common
dir
_path
);
break
;
}
...
...
@@ -498,12 +498,12 @@ static int find_repo(
if
(
error
<
0
)
break
;
if
(
valid_repository_path
(
&
repo_link
,
&
common_link
))
{
git_buf_swap
(
repo
_path
,
&
repo_link
);
git_buf_swap
(
gitdir
_path
,
&
repo_link
);
if
(
link_path
)
error
=
git_buf_put
(
link_path
,
path
.
ptr
,
path
.
size
);
if
(
common_path
)
git_buf_swap
(
&
common_link
,
common_path
);
if
(
git
link_path
)
error
=
git_buf_put
(
git
link_path
,
path
.
ptr
,
path
.
size
);
if
(
common
dir
_path
)
git_buf_swap
(
&
common_link
,
common
dir
_path
);
}
break
;
}
...
...
@@ -529,20 +529,20 @@ static int find_repo(
break
;
}
if
(
!
error
&&
parent
_path
&&
!
(
flags
&
GIT_REPOSITORY_OPEN_BARE
))
{
if
(
!
git_buf_len
(
repo
_path
))
git_buf_clear
(
parent
_path
);
if
(
!
error
&&
workdir
_path
&&
!
(
flags
&
GIT_REPOSITORY_OPEN_BARE
))
{
if
(
!
git_buf_len
(
gitdir
_path
))
git_buf_clear
(
workdir
_path
);
else
{
git_path_dirname_r
(
parent
_path
,
path
.
ptr
);
git_path_to_dir
(
parent
_path
);
git_path_dirname_r
(
workdir
_path
,
path
.
ptr
);
git_path_to_dir
(
workdir
_path
);
}
if
(
git_buf_oom
(
parent
_path
))
if
(
git_buf_oom
(
workdir
_path
))
return
-
1
;
}
/* If we didn't find the repository, and we don't have any other error
* to report, report that. */
if
(
!
git_buf_len
(
repo
_path
)
&&
!
error
)
{
if
(
!
git_buf_len
(
gitdir
_path
)
&&
!
error
)
{
giterr_set
(
GITERR_REPOSITORY
,
"could not find repository from '%s'"
,
start_path
);
error
=
GIT_ENOTFOUND
;
...
...
@@ -758,6 +758,29 @@ success:
return
error
;
}
static
int
repo_is_worktree
(
unsigned
*
out
,
const
git_repository
*
repo
)
{
git_buf
gitdir_link
=
GIT_BUF_INIT
;
int
error
;
/* Worktrees cannot have the same commondir and gitdir */
if
(
repo
->
commondir
&&
repo
->
gitdir
&&
!
strcmp
(
repo
->
commondir
,
repo
->
gitdir
))
{
*
out
=
0
;
return
0
;
}
if
((
error
=
git_buf_joinpath
(
&
gitdir_link
,
repo
->
gitdir
,
"gitdir"
))
<
0
)
return
-
1
;
/* A 'gitdir' file inside a git directory is currently
* only used when the repository is a working tree. */
*
out
=
!!
git_path_exists
(
gitdir_link
.
ptr
);
git_buf_free
(
&
gitdir_link
);
return
error
;
}
int
git_repository_open_ext
(
git_repository
**
repo_ptr
,
const
char
*
start_path
,
...
...
@@ -765,8 +788,9 @@ int git_repository_open_ext(
const
char
*
ceiling_dirs
)
{
int
error
;
git_buf
path
=
GIT_BUF_INIT
,
parent
=
GIT_BUF_INIT
,
link_path
=
GIT_BUF_INIT
,
common_path
=
GIT_BUF_INIT
;
unsigned
is_worktree
;
git_buf
gitdir
=
GIT_BUF_INIT
,
workdir
=
GIT_BUF_INIT
,
gitlink
=
GIT_BUF_INIT
,
commondir
=
GIT_BUF_INIT
;
git_repository
*
repo
;
git_config
*
config
=
NULL
;
...
...
@@ -777,7 +801,7 @@ int git_repository_open_ext(
*
repo_ptr
=
NULL
;
error
=
find_repo
(
&
path
,
&
parent
,
&
link_path
,
&
common_path
,
start_path
,
flags
,
ceiling_dirs
);
&
gitdir
,
&
workdir
,
&
gitlink
,
&
commondir
,
start_path
,
flags
,
ceiling_dirs
);
if
(
error
<
0
||
!
repo_ptr
)
return
error
;
...
...
@@ -785,24 +809,21 @@ int git_repository_open_ext(
repo
=
repository_alloc
();
GITERR_CHECK_ALLOC
(
repo
);
repo
->
gitdir
=
git_buf_detach
(
&
path
);
repo
->
gitdir
=
git_buf_detach
(
&
gitdir
);
GITERR_CHECK_ALLOC
(
repo
->
gitdir
);
if
(
link_path
.
size
)
{
repo
->
gitlink
=
git_buf_detach
(
&
link_path
);
if
(
gitlink
.
size
)
{
repo
->
gitlink
=
git_buf_detach
(
&
gitlink
);
GITERR_CHECK_ALLOC
(
repo
->
gitlink
);
}
if
(
common
_path
.
size
)
{
repo
->
commondir
=
git_buf_detach
(
&
common
_path
);
if
(
common
dir
.
size
)
{
repo
->
commondir
=
git_buf_detach
(
&
common
dir
);
GITERR_CHECK_ALLOC
(
repo
->
commondir
);
}
if
((
error
=
git_buf_joinpath
(
&
path
,
repo
->
gitdir
,
"gitdir"
))
<
0
)
if
((
error
=
repo_is_worktree
(
&
is_worktree
,
repo
))
<
0
)
goto
cleanup
;
/* A 'gitdir' file inside a git directory is currently
* only used when the repository is a working tree. */
if
(
git_path_exists
(
path
.
ptr
))
repo
->
is_worktree
=
1
;
repo
->
is_worktree
=
is_worktree
;
/*
* We'd like to have the config, but git doesn't particularly
...
...
@@ -822,13 +843,13 @@ int git_repository_open_ext(
if
(
config
&&
((
error
=
load_config_data
(
repo
,
config
))
<
0
||
(
error
=
load_workdir
(
repo
,
config
,
&
parent
))
<
0
))
(
error
=
load_workdir
(
repo
,
config
,
&
workdir
))
<
0
))
goto
cleanup
;
}
cleanup:
git_buf_free
(
&
path
);
git_buf_free
(
&
parent
);
git_buf_free
(
&
gitdir
);
git_buf_free
(
&
workdir
);
git_config_free
(
config
);
if
(
error
<
0
)
...
...
tests/submodule/open.c
0 → 100644
View file @
04de614b
#include "clar_libgit2.h"
#include "submodule_helpers.h"
#include "path.h"
static
git_repository
*
g_parent
;
static
git_repository
*
g_child
;
static
git_submodule
*
g_module
;
void
test_submodule_open__initialize
(
void
)
{
g_parent
=
setup_fixture_submod2
();
}
void
test_submodule_open__cleanup
(
void
)
{
git_submodule_free
(
g_module
);
git_repository_free
(
g_child
);
cl_git_sandbox_cleanup
();
g_parent
=
NULL
;
g_child
=
NULL
;
g_module
=
NULL
;
}
static
void
assert_sm_valid
(
git_repository
*
parent
,
git_repository
*
child
,
const
char
*
sm_name
)
{
git_buf
expected
=
GIT_BUF_INIT
,
actual
=
GIT_BUF_INIT
;
/* assert working directory */
cl_git_pass
(
git_buf_joinpath
(
&
expected
,
git_repository_workdir
(
parent
),
sm_name
));
cl_git_pass
(
git_path_prettify_dir
(
&
expected
,
expected
.
ptr
,
NULL
));
cl_git_pass
(
git_buf_sets
(
&
actual
,
git_repository_workdir
(
child
)));
cl_git_pass
(
git_path_prettify_dir
(
&
actual
,
actual
.
ptr
,
NULL
));
cl_assert_equal_s
(
expected
.
ptr
,
actual
.
ptr
);
git_buf_clear
(
&
expected
);
git_buf_clear
(
&
actual
);
/* assert common directory */
cl_git_pass
(
git_buf_joinpath
(
&
expected
,
git_repository_commondir
(
parent
),
"modules"
));
cl_git_pass
(
git_buf_joinpath
(
&
expected
,
expected
.
ptr
,
sm_name
));
cl_git_pass
(
git_path_prettify_dir
(
&
expected
,
expected
.
ptr
,
NULL
));
cl_git_pass
(
git_buf_sets
(
&
actual
,
git_repository_commondir
(
child
)));
cl_git_pass
(
git_path_prettify_dir
(
&
actual
,
actual
.
ptr
,
NULL
));
cl_assert_equal_s
(
expected
.
ptr
,
actual
.
ptr
);
/* assert git directory */
cl_git_pass
(
git_buf_sets
(
&
actual
,
git_repository_path
(
child
)));
cl_git_pass
(
git_path_prettify_dir
(
&
actual
,
actual
.
ptr
,
NULL
));
cl_assert_equal_s
(
expected
.
ptr
,
actual
.
ptr
);
git_buf_free
(
&
expected
);
git_buf_free
(
&
actual
);
}
void
test_submodule_open__opening_via_lookup_succeeds
(
void
)
{
cl_git_pass
(
git_submodule_lookup
(
&
g_module
,
g_parent
,
"sm_unchanged"
));
cl_git_pass
(
git_submodule_open
(
&
g_child
,
g_module
));
assert_sm_valid
(
g_parent
,
g_child
,
"sm_unchanged"
);
}
void
test_submodule_open__direct_open_succeeds
(
void
)
{
git_buf
path
=
GIT_BUF_INIT
;
cl_git_pass
(
git_buf_joinpath
(
&
path
,
git_repository_workdir
(
g_parent
),
"sm_unchanged"
));
cl_git_pass
(
git_repository_open
(
&
g_child
,
path
.
ptr
));
assert_sm_valid
(
g_parent
,
g_child
,
"sm_unchanged"
);
git_buf_free
(
&
path
);
}
void
test_submodule_open__direct_open_succeeds_for_broken_sm_with_gitdir
(
void
)
{
git_buf
path
=
GIT_BUF_INIT
;
/*
* This is actually not a valid submodule, but we
* encountered at least one occasion where the gitdir
* file existed inside of a submodule's gitdir. As we are
* now able to open these submodules correctly, we still
* add a test for this.
*/
cl_git_mkfile
(
"submod2/.git/modules/sm_unchanged/gitdir"
,
".git"
);
cl_git_pass
(
git_buf_joinpath
(
&
path
,
git_repository_workdir
(
g_parent
),
"sm_unchanged"
));
cl_git_pass
(
git_repository_open
(
&
g_child
,
path
.
ptr
));
assert_sm_valid
(
g_parent
,
g_child
,
"sm_unchanged"
);
git_buf_free
(
&
path
);
}
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