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
6091457e
Commit
6091457e
authored
Nov 17, 2012
by
nulltoken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
repo: ensure is_empty() checks there are no refs
parent
f5a0e734
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
29 deletions
+50
-29
include/git2/repository.h
+1
-1
src/repository.c
+31
-20
tests-clar/repo/getters.c
+18
-8
No files found.
include/git2/repository.h
View file @
6091457e
...
...
@@ -305,7 +305,7 @@ GIT_EXTERN(int) git_repository_head_orphan(git_repository *repo);
* Check if a repository is empty
*
* An empty repository has just been initialized and contains
* no
commit
s.
* no
reference
s.
*
* @param repo Repo to test
* @return 1 if the repository is empty, 0 if it isn't, error code
...
...
src/repository.c
View file @
6091457e
...
...
@@ -1241,36 +1241,47 @@ int git_repository_head_orphan(git_repository *repo)
return
0
;
}
int
git_repository_is_empty
(
git_repository
*
repo
)
int
at_least_one_cb
(
const
char
*
refname
,
void
*
payload
)
{
git_reference
*
head
=
NULL
,
*
branch
=
NULL
;
int
error
;
GIT_UNUSED
(
refname
)
;
GIT_UNUSED
(
payload
)
;
if
(
git_reference_lookup
(
&
head
,
repo
,
GIT_HEAD_FILE
)
<
0
)
return
-
1
;
return
GIT_EUSER
;
}
if
(
git_reference_type
(
head
)
!=
GIT_REF_SYMBOLIC
)
{
git_reference_free
(
head
);
return
0
;
}
static
int
repo_contains_no_reference
(
git_repository
*
repo
)
{
int
error
;
error
=
git_reference_foreach
(
repo
,
GIT_REF_LISTALL
,
at_least_one_cb
,
NULL
);
if
(
strcmp
(
git_reference_target
(
head
),
GIT_REFS_HEADS_DIR
"master"
)
!=
0
)
{
git_reference_free
(
head
);
if
(
error
==
GIT_EUSER
)
return
0
;
}
error
=
git_reference_resolve
(
&
branch
,
head
);
git_reference_free
(
head
);
git_reference_free
(
branch
);
return
error
==
0
?
1
:
error
;
}
if
(
error
==
GIT_ENOTFOUND
)
return
1
;
int
git_repository_is_empty
(
git_repository
*
repo
)
{
git_reference
*
head
=
NULL
;
int
error
,
ref_count
=
0
;
if
(
error
<
0
)
if
(
git_reference_lookup
(
&
head
,
repo
,
GIT_HEAD_FILE
)
<
0
)
return
-
1
;
return
0
;
if
(
!
(
error
=
git_reference_type
(
head
)
==
GIT_REF_SYMBOLIC
))
goto
cleanup
;
if
(
!
(
error
=
strcmp
(
git_reference_target
(
head
),
GIT_REFS_HEADS_DIR
"master"
)
==
0
))
goto
cleanup
;
error
=
repo_contains_no_reference
(
repo
);
cleanup:
git_reference_free
(
head
);
return
error
<
0
?
-
1
:
error
;
}
const
char
*
git_repository_path
(
git_repository
*
repo
)
...
...
tests-clar/repo/getters.c
View file @
6091457e
#include "clar_libgit2.h"
void
test_repo_getters__
empty
(
void
)
void
test_repo_getters__
is_empty_correctly_deals_with_pristine_looking_repos
(
void
)
{
git_repository
*
repo_empty
,
*
repo_normal
;
git_repository
*
repo
;
repo
=
cl_git_sandbox_init
(
"empty_bare.git"
);
cl_git_remove_placeholders
(
git_repository_path
(
repo
),
"dummy-marker.txt"
);
cl_assert_equal_i
(
true
,
git_repository_is_empty
(
repo
));
cl_git_pass
(
git_repository_open
(
&
repo_normal
,
cl_fixture
(
"testrepo.git"
)));
cl_assert
(
git_repository_is_empty
(
repo_normal
)
==
0
);
git_repository_free
(
repo_normal
);
cl_git_sandbox_cleanup
();
}
cl_git_pass
(
git_repository_open
(
&
repo_empty
,
cl_fixture
(
"empty_bare.git"
)));
cl_assert
(
git_repository_is_empty
(
repo_empty
)
==
1
);
git_repository_free
(
repo_empty
);
void
test_repo_getters__is_empty_can_detect_used_repositories
(
void
)
{
git_repository
*
repo
;
cl_git_pass
(
git_repository_open
(
&
repo
,
cl_fixture
(
"testrepo.git"
)));
cl_assert_equal_i
(
false
,
git_repository_is_empty
(
repo
));
git_repository_free
(
repo
);
}
void
test_repo_getters__retrieving_the_odb_honors_the_refcount
(
void
)
...
...
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