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
fcb2164f
Commit
fcb2164f
authored
Apr 13, 2012
by
Vicent Martí
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #623 from arrbee/refactor-open
Update git_repository_open
parents
6a625435
7784bcbb
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
307 additions
and
59 deletions
+307
-59
include/git2/repository.h
+14
-0
src/attr.c
+11
-9
src/config.c
+9
-18
src/diff.c
+1
-2
src/errors.c
+5
-1
src/path.c
+10
-13
src/repository.c
+0
-0
tests-clar/clar_helpers.c
+16
-5
tests-clar/clar_libgit2.h
+2
-0
tests-clar/repo/open.c
+239
-11
No files found.
include/git2/repository.h
View file @
fcb2164f
...
@@ -70,6 +70,20 @@ GIT_EXTERN(int) git_repository_discover(
...
@@ -70,6 +70,20 @@ GIT_EXTERN(int) git_repository_discover(
int
across_fs
,
int
across_fs
,
const
char
*
ceiling_dirs
);
const
char
*
ceiling_dirs
);
enum
{
GIT_REPOSITORY_OPEN_NO_SEARCH
=
(
1
<<
0
),
GIT_REPOSITORY_OPEN_CROSS_FS
=
(
1
<<
1
),
};
/**
* Find and open a repository with extended controls.
*/
GIT_EXTERN
(
int
)
git_repository_open_ext
(
git_repository
**
repo
,
const
char
*
start_path
,
uint32_t
flags
,
const
char
*
ceiling_dirs
);
/**
/**
* Free a previously allocated repository
* Free a previously allocated repository
*
*
...
...
src/attr.c
View file @
fcb2164f
...
@@ -347,6 +347,7 @@ static int collect_attr_files(
...
@@ -347,6 +347,7 @@ static int collect_attr_files(
int
git_attr_cache__init
(
git_repository
*
repo
)
int
git_attr_cache__init
(
git_repository
*
repo
)
{
{
int
ret
;
git_attr_cache
*
cache
=
git_repository_attr_cache
(
repo
);
git_attr_cache
*
cache
=
git_repository_attr_cache
(
repo
);
git_config
*
cfg
;
git_config
*
cfg
;
...
@@ -354,17 +355,18 @@ int git_attr_cache__init(git_repository *repo)
...
@@ -354,17 +355,18 @@ int git_attr_cache__init(git_repository *repo)
return
0
;
return
0
;
/* cache config settings for attributes and ignores */
/* cache config settings for attributes and ignores */
if
(
git_repository_config
(
&
cfg
,
repo
)
<
0
)
if
(
git_repository_config
__weakptr
(
&
cfg
,
repo
)
<
0
)
return
-
1
;
return
-
1
;
if
(
git_config_get_string
(
cfg
,
GIT_ATTR_CONFIG
,
&
cache
->
cfg_attr_file
))
{
giterr_clear
();
ret
=
git_config_get_string
(
cfg
,
GIT_ATTR_CONFIG
,
&
cache
->
cfg_attr_file
);
cache
->
cfg_attr_file
=
NULL
;
if
(
ret
<
0
&&
ret
!=
GIT_ENOTFOUND
)
}
return
ret
;
if
(
git_config_get_string
(
cfg
,
GIT_IGNORE_CONFIG
,
&
cache
->
cfg_excl_file
))
{
ret
=
git_config_get_string
(
cfg
,
GIT_IGNORE_CONFIG
,
&
cache
->
cfg_excl_file
);
if
(
ret
<
0
&&
ret
!=
GIT_ENOTFOUND
)
return
ret
;
giterr_clear
();
giterr_clear
();
cache
->
cfg_excl_file
=
NULL
;
}
git_config_free
(
cfg
);
/* allocate hashtable for attribute and ignore file contents */
/* allocate hashtable for attribute and ignore file contents */
if
(
cache
->
files
==
NULL
)
{
if
(
cache
->
files
==
NULL
)
{
...
...
src/config.c
View file @
fcb2164f
...
@@ -327,11 +327,11 @@ int git_config_lookup_map_value(
...
@@ -327,11 +327,11 @@ int git_config_lookup_map_value(
int
git_config_get_mapped
(
git_config
*
cfg
,
const
char
*
name
,
git_cvar_map
*
maps
,
size_t
map_n
,
int
*
out
)
int
git_config_get_mapped
(
git_config
*
cfg
,
const
char
*
name
,
git_cvar_map
*
maps
,
size_t
map_n
,
int
*
out
)
{
{
const
char
*
value
;
const
char
*
value
;
int
error
;
int
ret
;
error
=
git_config_get_string
(
cfg
,
name
,
&
value
);
ret
=
git_config_get_string
(
cfg
,
name
,
&
value
);
if
(
error
<
0
)
if
(
ret
<
0
)
return
error
;
return
ret
;
if
(
!
git_config_lookup_map_value
(
maps
,
map_n
,
value
,
out
))
if
(
!
git_config_lookup_map_value
(
maps
,
map_n
,
value
,
out
))
return
0
;
return
0
;
...
@@ -399,25 +399,16 @@ int git_config_get_bool(git_config *cfg, const char *name, int *out)
...
@@ -399,25 +399,16 @@ int git_config_get_bool(git_config *cfg, const char *name, int *out)
int
git_config_get_string
(
git_config
*
cfg
,
const
char
*
name
,
const
char
**
out
)
int
git_config_get_string
(
git_config
*
cfg
,
const
char
*
name
,
const
char
**
out
)
{
{
file_internal
*
internal
;
file_internal
*
internal
;
git_config_file
*
file
;
int
ret
=
GIT_ENOTFOUND
;
unsigned
int
i
;
unsigned
int
i
;
assert
(
cfg
->
files
.
length
);
assert
(
cfg
->
files
.
length
);
for
(
i
=
0
;
i
<
cfg
->
files
.
length
;
++
i
)
{
*
out
=
NULL
;
internal
=
git_vector_get
(
&
cfg
->
files
,
i
);
file
=
internal
->
file
;
ret
=
file
->
get
(
file
,
name
,
out
);
if
(
ret
==
0
)
return
0
;
/* File backend doesn't set error message on variable
* not found */
if
(
ret
==
GIT_ENOTFOUND
)
continue
;
git_vector_foreach
(
&
cfg
->
files
,
i
,
internal
)
{
git_config_file
*
file
=
internal
->
file
;
int
ret
=
file
->
get
(
file
,
name
,
out
);
if
(
ret
!=
GIT_ENOTFOUND
)
return
ret
;
return
ret
;
}
}
...
...
src/diff.c
View file @
fcb2164f
...
@@ -253,7 +253,7 @@ static git_diff_list *git_diff_list_alloc(
...
@@ -253,7 +253,7 @@ static git_diff_list *git_diff_list_alloc(
diff
->
repo
=
repo
;
diff
->
repo
=
repo
;
/* load config values that affect diff behavior */
/* load config values that affect diff behavior */
if
(
git_repository_config
(
&
cfg
,
repo
)
<
0
)
if
(
git_repository_config
__weakptr
(
&
cfg
,
repo
)
<
0
)
goto
fail
;
goto
fail
;
if
(
config_bool
(
cfg
,
"core.symlinks"
,
1
))
if
(
config_bool
(
cfg
,
"core.symlinks"
,
1
))
diff
->
diffcaps
=
diff
->
diffcaps
|
GIT_DIFFCAPS_HAS_SYMLINKS
;
diff
->
diffcaps
=
diff
->
diffcaps
|
GIT_DIFFCAPS_HAS_SYMLINKS
;
...
@@ -264,7 +264,6 @@ static git_diff_list *git_diff_list_alloc(
...
@@ -264,7 +264,6 @@ static git_diff_list *git_diff_list_alloc(
if
(
config_bool
(
cfg
,
"core.trustctime"
,
1
))
if
(
config_bool
(
cfg
,
"core.trustctime"
,
1
))
diff
->
diffcaps
=
diff
->
diffcaps
|
GIT_DIFFCAPS_TRUST_CTIME
;
diff
->
diffcaps
=
diff
->
diffcaps
|
GIT_DIFFCAPS_TRUST_CTIME
;
/* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
/* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
git_config_free
(
cfg
);
if
(
opts
==
NULL
)
if
(
opts
==
NULL
)
return
diff
;
return
diff
;
...
...
src/errors.c
View file @
fcb2164f
...
@@ -92,8 +92,12 @@ const char *git_lasterror(void)
...
@@ -92,8 +92,12 @@ const char *git_lasterror(void)
{
{
char
*
last_error
=
GIT_GLOBAL
->
error
.
last
;
char
*
last_error
=
GIT_GLOBAL
->
error
.
last
;
if
(
!
last_error
[
0
])
if
(
!
last_error
[
0
])
{
const
git_error
*
err
=
git_error_last
();
if
(
err
!=
NULL
)
return
err
->
message
;
return
NULL
;
return
NULL
;
}
return
last_error
;
return
last_error
;
}
}
...
...
src/path.c
View file @
fcb2164f
...
@@ -172,20 +172,22 @@ int git_path_root(const char *path)
...
@@ -172,20 +172,22 @@ int git_path_root(const char *path)
if
(
isalpha
(
path
[
0
])
&&
(
path
[
1
]
==
':'
))
if
(
isalpha
(
path
[
0
])
&&
(
path
[
1
]
==
':'
))
offset
+=
2
;
offset
+=
2
;
/* Are we dealing with a network path? */
/* Are we dealing with a windows network path? */
else
if
(
path
[
0
]
==
'/'
&&
path
[
1
]
==
'/'
)
{
else
if
((
path
[
0
]
==
'/'
&&
path
[
1
]
==
'/'
)
||
(
path
[
0
]
==
'\\'
&&
path
[
1
]
==
'\\'
))
{
offset
+=
2
;
offset
+=
2
;
/* Skip the computer name segment */
/* Skip the computer name segment */
while
(
*
(
path
+
offset
)
&&
*
(
path
+
offset
)
!=
'/
'
)
while
(
path
[
offset
]
&&
path
[
offset
]
!=
'/'
&&
path
[
offset
]
!=
'\\
'
)
offset
++
;
offset
++
;
}
}
#endif
#endif
if
(
*
(
path
+
offset
)
==
'/
'
)
if
(
path
[
offset
]
==
'/'
||
path
[
offset
]
==
'\\
'
)
return
offset
;
return
offset
;
return
-
1
;
/* Not a real error
. Rather a signal than the
path is not rooted */
return
-
1
;
/* Not a real error
- signals that
path is not rooted */
}
}
int
git_path_prettify
(
git_buf
*
path_out
,
const
char
*
path
,
const
char
*
base
)
int
git_path_prettify
(
git_buf
*
path_out
,
const
char
*
path
,
const
char
*
base
)
...
@@ -193,26 +195,21 @@ int git_path_prettify(git_buf *path_out, const char *path, const char *base)
...
@@ -193,26 +195,21 @@ int git_path_prettify(git_buf *path_out, const char *path, const char *base)
char
buf
[
GIT_PATH_MAX
];
char
buf
[
GIT_PATH_MAX
];
assert
(
path
&&
path_out
);
assert
(
path
&&
path_out
);
git_buf_clear
(
path_out
);
/* construct path if needed */
/* construct path if needed */
if
(
base
!=
NULL
&&
git_path_root
(
path
)
<
0
)
{
if
(
base
!=
NULL
&&
git_path_root
(
path
)
<
0
)
{
if
(
git_buf_joinpath
(
path_out
,
base
,
path
)
<
0
)
if
(
git_buf_joinpath
(
path_out
,
base
,
path
)
<
0
)
return
-
1
;
return
-
1
;
path
=
path_out
->
ptr
;
path
=
path_out
->
ptr
;
}
}
if
(
p_realpath
(
path
,
buf
)
==
NULL
)
{
if
(
p_realpath
(
path
,
buf
)
==
NULL
)
{
giterr_set
(
GITERR_OS
,
"Failed to resolve path '%s'
: %s"
,
giterr_set
(
GITERR_OS
,
"Failed to resolve path '%s'
"
,
path
);
path
,
strerror
(
errno
)
);
git_buf_clear
(
path_out
);
return
(
errno
==
ENOENT
)
?
GIT_ENOTFOUND
:
-
1
;
return
(
errno
==
ENOENT
)
?
GIT_ENOTFOUND
:
-
1
;
}
}
if
(
git_buf_sets
(
path_out
,
buf
)
<
0
)
return
git_buf_sets
(
path_out
,
buf
);
return
-
1
;
return
0
;
}
}
int
git_path_prettify_dir
(
git_buf
*
path_out
,
const
char
*
path
,
const
char
*
base
)
int
git_path_prettify_dir
(
git_buf
*
path_out
,
const
char
*
path
,
const
char
*
base
)
...
...
src/repository.c
View file @
fcb2164f
This diff is collapsed.
Click to expand it.
tests-clar/clar_helpers.c
View file @
fcb2164f
...
@@ -28,9 +28,9 @@ void cl_git_mkfile(const char *filename, const char *content)
...
@@ -28,9 +28,9 @@ void cl_git_mkfile(const char *filename, const char *content)
cl_must_pass
(
p_close
(
fd
));
cl_must_pass
(
p_close
(
fd
));
}
}
void
cl_git_
append2file
(
const
char
*
filename
,
const
char
*
new_content
)
void
cl_git_
write2file
(
const
char
*
filename
,
const
char
*
new_content
,
int
flags
)
{
{
int
fd
=
p_open
(
filename
,
O_WRONLY
|
O_APPEND
|
O_CREAT
);
int
fd
=
p_open
(
filename
,
flags
);
cl_assert
(
fd
!=
0
);
cl_assert
(
fd
!=
0
);
if
(
!
new_content
)
if
(
!
new_content
)
new_content
=
"
\n
"
;
new_content
=
"
\n
"
;
...
@@ -39,6 +39,16 @@ void cl_git_append2file(const char *filename, const char *new_content)
...
@@ -39,6 +39,16 @@ void cl_git_append2file(const char *filename, const char *new_content)
cl_must_pass
(
p_chmod
(
filename
,
0644
));
cl_must_pass
(
p_chmod
(
filename
,
0644
));
}
}
void
cl_git_append2file
(
const
char
*
filename
,
const
char
*
new_content
)
{
cl_git_write2file
(
filename
,
new_content
,
O_WRONLY
|
O_APPEND
|
O_CREAT
);
}
void
cl_git_rewritefile
(
const
char
*
filename
,
const
char
*
new_content
)
{
cl_git_write2file
(
filename
,
new_content
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
);
}
static
const
char
*
_cl_sandbox
=
NULL
;
static
const
char
*
_cl_sandbox
=
NULL
;
static
git_repository
*
_cl_repo
=
NULL
;
static
git_repository
*
_cl_repo
=
NULL
;
...
@@ -52,10 +62,11 @@ git_repository *cl_git_sandbox_init(const char *sandbox)
...
@@ -52,10 +62,11 @@ git_repository *cl_git_sandbox_init(const char *sandbox)
p_chdir
(
sandbox
);
p_chdir
(
sandbox
);
/*
Rename `sandbox/.gitted` to `sandbox/.git` which must be done since
/*
If this is not a bare repo, then rename `sandbox/.gitted` to
*
we cannot store a folder named `.git` inside the fixtures folder of
*
`sandbox/.git` which must be done since we cannot store a folder
* our libgit2 repo.
*
named `.git` inside the fixtures folder of
our libgit2 repo.
*/
*/
if
(
p_access
(
".gitted"
,
F_OK
)
==
0
)
cl_git_pass
(
p_rename
(
".gitted"
,
".git"
));
cl_git_pass
(
p_rename
(
".gitted"
,
".git"
));
/* If we have `gitattributes`, rename to `.gitattributes`. This may
/* If we have `gitattributes`, rename to `.gitattributes`. This may
...
...
tests-clar/clar_libgit2.h
View file @
fcb2164f
...
@@ -57,6 +57,8 @@ GIT_INLINE(void) cl_assert_strequal_internal(
...
@@ -57,6 +57,8 @@ GIT_INLINE(void) cl_assert_strequal_internal(
/* Write the contents of a buffer to disk */
/* Write the contents of a buffer to disk */
void
cl_git_mkfile
(
const
char
*
filename
,
const
char
*
content
);
void
cl_git_mkfile
(
const
char
*
filename
,
const
char
*
content
);
void
cl_git_append2file
(
const
char
*
filename
,
const
char
*
new_content
);
void
cl_git_append2file
(
const
char
*
filename
,
const
char
*
new_content
);
void
cl_git_rewritefile
(
const
char
*
filename
,
const
char
*
new_content
);
void
cl_git_write2file
(
const
char
*
filename
,
const
char
*
new_content
,
int
mode
);
/* Git sandbox setup helpers */
/* Git sandbox setup helpers */
...
...
tests-clar/repo/open.c
View file @
fcb2164f
#include "clar_libgit2.h"
#include "clar_libgit2.h"
#include "posix.h"
#include "fileops.h"
static
git_repository
*
repo
;
void
test_repo_open__cleanup
(
void
)
void
test_repo_open__cleanup
(
void
)
{
{
git_repository_free
(
repo
);
cl_git_sandbox_cleanup
();
if
(
git_path_isdir
(
"alternate"
))
git_futils_rmdir_r
(
"alternate"
,
1
);
}
}
void
test_repo_open__bare_empty_repo
(
void
)
void
test_repo_open__bare_empty_repo
(
void
)
{
{
cl_git_pass
(
git_repository_open
(
&
repo
,
cl_fixture
(
"empty_bare.git"
))
);
git_repository
*
repo
=
cl_git_sandbox_init
(
"empty_bare.git"
);
cl_assert
(
git_repository_path
(
repo
)
!=
NULL
);
cl_assert
(
git_repository_path
(
repo
)
!=
NULL
);
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo
),
"/"
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo
),
"/"
)
==
0
);
cl_assert
(
git_repository_workdir
(
repo
)
==
NULL
);
cl_assert
(
git_repository_workdir
(
repo
)
==
NULL
);
}
}
void
test_repo_open__standard_empty_repo_through_gitdir
(
void
)
void
test_repo_open__standard_empty_repo_through_gitdir
(
void
)
{
{
git_repository
*
repo
;
cl_git_pass
(
git_repository_open
(
&
repo
,
cl_fixture
(
"empty_standard_repo/.gitted"
)));
cl_git_pass
(
git_repository_open
(
&
repo
,
cl_fixture
(
"empty_standard_repo/.gitted"
)));
cl_assert
(
git_repository_path
(
repo
)
!=
NULL
);
cl_assert
(
git_repository_path
(
repo
)
!=
NULL
);
...
@@ -27,20 +29,246 @@ void test_repo_open__standard_empty_repo_through_gitdir(void)
...
@@ -27,20 +29,246 @@ void test_repo_open__standard_empty_repo_through_gitdir(void)
cl_assert
(
git_repository_workdir
(
repo
)
!=
NULL
);
cl_assert
(
git_repository_workdir
(
repo
)
!=
NULL
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo
),
"/"
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo
),
"/"
)
==
0
);
git_repository_free
(
repo
);
}
}
void
test_repo_open__standard_empty_repo_through_workdir
(
void
)
void
test_repo_open__standard_empty_repo_through_workdir
(
void
)
{
{
cl_fixture_sandbox
(
"empty_standard_repo"
);
git_repository
*
repo
=
cl_git_sandbox_init
(
"empty_standard_repo"
);
cl_git_pass
(
p_rename
(
"empty_standard_repo/.gitted"
,
"empty_standard_repo/.git"
));
cl_git_pass
(
git_repository_open
(
&
repo
,
"empty_standard_repo"
));
cl_assert
(
git_repository_path
(
repo
)
!=
NULL
);
cl_assert
(
git_repository_path
(
repo
)
!=
NULL
);
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo
),
"/"
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo
),
"/"
)
==
0
);
cl_assert
(
git_repository_workdir
(
repo
)
!=
NULL
);
cl_assert
(
git_repository_workdir
(
repo
)
!=
NULL
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo
),
"/"
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo
),
"/"
)
==
0
);
}
void
test_repo_open__open_with_discover
(
void
)
{
static
const
char
*
variants
[]
=
{
"attr"
,
"attr/"
,
"attr/.git"
,
"attr/.git/"
,
"attr/sub"
,
"attr/sub/"
,
"attr/sub/sub"
,
"attr/sub/sub/"
,
NULL
};
git_repository
*
repo
;
const
char
**
scan
;
cl_fixture_sandbox
(
"attr"
);
cl_git_pass
(
p_rename
(
"attr/.gitted"
,
"attr/.git"
));
for
(
scan
=
variants
;
*
scan
!=
NULL
;
scan
++
)
{
cl_git_pass
(
git_repository_open_ext
(
&
repo
,
*
scan
,
0
,
NULL
));
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo
),
"attr/.git/"
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo
),
"attr/"
)
==
0
);
git_repository_free
(
repo
);
}
cl_fixture_cleanup
(
"attr"
);
}
void
test_repo_open__gitlinked
(
void
)
{
/* need to have both repo dir and workdir set up correctly */
git_repository
*
repo
=
cl_git_sandbox_init
(
"empty_standard_repo"
);
git_repository
*
repo2
;
cl_must_pass
(
p_mkdir
(
"alternate"
,
0777
));
cl_git_mkfile
(
"alternate/.git"
,
"gitdir: ../empty_standard_repo/.git"
);
cl_git_pass
(
git_repository_open
(
&
repo2
,
"alternate"
));
cl_assert
(
git_repository_path
(
repo2
)
!=
NULL
);
cl_assert_
(
git__suffixcmp
(
git_repository_path
(
repo2
),
"empty_standard_repo/.git/"
)
==
0
,
git_repository_path
(
repo2
));
cl_assert_equal_s
(
git_repository_path
(
repo
),
git_repository_path
(
repo2
));
cl_assert
(
git_repository_workdir
(
repo2
)
!=
NULL
);
cl_assert_
(
git__suffixcmp
(
git_repository_workdir
(
repo2
),
"alternate/"
)
==
0
,
git_repository_workdir
(
repo2
));
git_repository_free
(
repo2
);
}
void
test_repo_open__from_git_new_workdir
(
void
)
{
/* The git-new-workdir script that ships with git sets up a bunch of
* symlinks to create a second workdir that shares the object db with
* another checkout. Libgit2 can open a repo that has been configured
* this way.
*/
cl_git_sandbox_init
(
"empty_standard_repo"
);
#ifndef GIT_WIN32
git_repository
*
repo2
;
git_buf
link_tgt
=
GIT_BUF_INIT
,
link
=
GIT_BUF_INIT
,
body
=
GIT_BUF_INIT
;
const
char
**
scan
;
int
link_fd
;
static
const
char
*
links
[]
=
{
"config"
,
"refs"
,
"logs/refs"
,
"objects"
,
"info"
,
"hooks"
,
"packed-refs"
,
"remotes"
,
"rr-cache"
,
"svn"
,
NULL
};
static
const
char
*
copies
[]
=
{
"HEAD"
,
NULL
};
cl_git_pass
(
p_mkdir
(
"alternate"
,
0777
));
cl_git_pass
(
p_mkdir
(
"alternate/.git"
,
0777
));
for
(
scan
=
links
;
*
scan
!=
NULL
;
scan
++
)
{
git_buf_joinpath
(
&
link_tgt
,
"empty_standard_repo/.git"
,
*
scan
);
if
(
git_path_exists
(
link_tgt
.
ptr
))
{
git_buf_joinpath
(
&
link_tgt
,
"../../empty_standard_repo/.git"
,
*
scan
);
git_buf_joinpath
(
&
link
,
"alternate/.git"
,
*
scan
);
if
(
strchr
(
*
scan
,
'/'
))
git_futils_mkpath2file
(
link
.
ptr
,
0777
);
cl_assert_
(
symlink
(
link_tgt
.
ptr
,
link
.
ptr
)
==
0
,
strerror
(
errno
));
}
}
for
(
scan
=
copies
;
*
scan
!=
NULL
;
scan
++
)
{
git_buf_joinpath
(
&
link_tgt
,
"empty_standard_repo/.git"
,
*
scan
);
if
(
git_path_exists
(
link_tgt
.
ptr
))
{
git_buf_joinpath
(
&
link
,
"alternate/.git"
,
*
scan
);
cl_git_pass
(
git_futils_readbuffer
(
&
body
,
link_tgt
.
ptr
));
cl_assert
((
link_fd
=
git_futils_creat_withpath
(
link
.
ptr
,
0777
,
0666
))
>=
0
);
cl_must_pass
(
p_write
(
link_fd
,
body
.
ptr
,
body
.
size
));
p_close
(
link_fd
);
}
}
git_buf_free
(
&
link_tgt
);
git_buf_free
(
&
link
);
git_buf_free
(
&
body
);
cl_git_pass
(
git_repository_open
(
&
repo2
,
"alternate"
));
cl_assert
(
git_repository_path
(
repo2
)
!=
NULL
);
cl_assert_
(
git__suffixcmp
(
git_repository_path
(
repo2
),
"alternate/.git/"
)
==
0
,
git_repository_path
(
repo2
));
cl_assert
(
git_repository_workdir
(
repo2
)
!=
NULL
);
cl_assert_
(
git__suffixcmp
(
git_repository_workdir
(
repo2
),
"alternate/"
)
==
0
,
git_repository_workdir
(
repo2
));
git_repository_free
(
repo2
);
#endif
}
void
test_repo_open__failures
(
void
)
{
git_repository
*
base
,
*
repo
;
git_buf
ceiling
=
GIT_BUF_INIT
;
base
=
cl_git_sandbox_init
(
"attr"
);
cl_git_pass
(
git_buf_sets
(
&
ceiling
,
git_repository_workdir
(
base
)));
/* fail with no searching */
cl_git_fail
(
git_repository_open
(
&
repo
,
"attr/sub"
));
cl_git_fail
(
git_repository_open_ext
(
&
repo
,
"attr/sub"
,
GIT_REPOSITORY_OPEN_NO_SEARCH
,
NULL
));
/* fail with ceiling too low */
cl_git_pass
(
git_buf_joinpath
(
&
ceiling
,
ceiling
.
ptr
,
"sub"
));
cl_git_fail
(
git_repository_open_ext
(
&
repo
,
"attr/sub"
,
0
,
ceiling
.
ptr
));
/* fail with no repo */
cl_git_pass
(
p_mkdir
(
"alternate"
,
0777
));
cl_git_pass
(
p_mkdir
(
"alternate/.git"
,
0777
));
cl_git_fail
(
git_repository_open_ext
(
&
repo
,
"alternate"
,
0
,
NULL
));
cl_git_fail
(
git_repository_open_ext
(
&
repo
,
"alternate/.git"
,
0
,
NULL
));
git_buf_free
(
&
ceiling
);
}
void
test_repo_open__bad_gitlinks
(
void
)
{
git_repository
*
repo
;
static
const
char
*
bad_links
[]
=
{
"garbage
\n
"
,
"gitdir"
,
"gitdir:
\n
"
,
"gitdir: foobar"
,
"gitdir: ../invalid"
,
"gitdir: ../invalid2"
,
"gitdir: ../attr/.git with extra stuff"
,
NULL
};
const
char
**
scan
;
cl_git_sandbox_init
(
"attr"
);
cl_git_pass
(
p_mkdir
(
"alternate"
,
0777
));
cl_git_pass
(
p_mkdir
(
"invalid"
,
0777
));
cl_git_pass
(
git_futils_mkdir_r
(
"invalid2/.git"
,
NULL
,
0777
));
for
(
scan
=
bad_links
;
*
scan
!=
NULL
;
scan
++
)
{
cl_git_rewritefile
(
"alternate/.git"
,
*
scan
);
cl_git_fail
(
git_repository_open_ext
(
&
repo
,
"alternate"
,
0
,
NULL
));
}
git_futils_rmdir_r
(
"invalid"
,
1
);
git_futils_rmdir_r
(
"invalid2"
,
1
);
}
static
void
unposix_path
(
git_buf
*
path
)
{
char
*
src
,
*
tgt
;
src
=
tgt
=
path
->
ptr
;
/* convert "/d/..." to "d:\..." */
if
(
src
[
0
]
==
'/'
&&
isalpha
(
src
[
1
])
&&
src
[
2
]
==
'/'
)
{
*
tgt
++
=
src
[
1
];
*
tgt
++
=
':'
;
*
tgt
++
=
'\\'
;
src
+=
3
;
}
while
(
*
src
)
{
*
tgt
++
=
(
*
src
==
'/'
)
?
'\\'
:
*
src
;
src
++
;
}
*
tgt
=
'\0'
;
}
void
test_repo_open__win32_path
(
void
)
{
#ifdef GIT_WIN32
git_repository
*
repo
=
cl_git_sandbox_init
(
"empty_standard_repo"
),
*
repo2
;
git_buf
winpath
=
GIT_BUF_INIT
;
char
*
src
,
*
tgt
;
static
const
char
*
repo_path
=
"empty_standard_repo/.git/"
;
static
const
char
*
repo_wd
=
"empty_standard_repo/"
;
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo
),
repo_path
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo
),
repo_wd
)
==
0
);
cl_git_pass
(
git_buf_sets
(
&
winpath
,
git_repository_path
(
repo
)));
unposix_path
(
&
winpath
);
cl_git_pass
(
git_repository_open
(
&
repo2
,
winpath
.
ptr
));
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo2
),
repo_path
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo2
),
repo_wd
)
==
0
);
git_repository_free
(
repo2
);
cl_git_pass
(
git_buf_sets
(
&
winpath
,
git_repository_path
(
repo
)));
git_buf_truncate
(
&
winpath
,
winpath
.
size
-
1
);
/* remove trailing '/' */
unposix_path
(
&
winpath
);
cl_git_pass
(
git_repository_open
(
&
repo2
,
winpath
.
ptr
));
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo2
),
repo_path
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo2
),
repo_wd
)
==
0
);
git_repository_free
(
repo2
);
cl_git_pass
(
git_buf_sets
(
&
winpath
,
git_repository_workdir
(
repo
)));
unposix_path
(
&
winpath
);
cl_git_pass
(
git_repository_open
(
&
repo2
,
winpath
.
ptr
));
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo2
),
repo_path
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo2
),
repo_wd
)
==
0
);
git_repository_free
(
repo2
);
cl_git_pass
(
git_buf_sets
(
&
winpath
,
git_repository_workdir
(
repo
)));
git_buf_truncate
(
&
winpath
,
winpath
.
size
-
1
);
/* remove trailing '/' */
unposix_path
(
&
winpath
);
cl_git_pass
(
git_repository_open
(
&
repo2
,
winpath
.
ptr
));
cl_assert
(
git__suffixcmp
(
git_repository_path
(
repo2
),
repo_path
)
==
0
);
cl_assert
(
git__suffixcmp
(
git_repository_workdir
(
repo2
),
repo_wd
)
==
0
);
git_repository_free
(
repo2
);
cl_fixture_cleanup
(
"empty_standard_repo"
);
git_buf_free
(
&
winpath
);
#endif
}
}
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