diff options
| author | Vicent Martà <tanoku@gmail.com> | 2011-06-28 10:44:19 -0700 |
|---|---|---|
| committer | Vicent Martà <tanoku@gmail.com> | 2011-06-28 10:44:19 -0700 |
| commit | ccd59372d46759a92c8197717ca02ba0eb31b7be (patch) | |
| tree | 9874e7a48cd3a18dca32f10d1555fde3044cd544 /src/repository.c | |
| parent | 9525e47df91c42922b5bbcd1a1408899c5dc89be (diff) | |
| parent | f5e09d608062d46b773a72113656b39053e09a7f (diff) | |
| download | libgit2-ccd59372d46759a92c8197717ca02ba0eb31b7be.tar.gz | |
Merge pull request #279 from carlosmn/detached-orphan
Add detached and orphan convenience functions
Diffstat (limited to 'src/repository.c')
| -rw-r--r-- | src/repository.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/repository.c b/src/repository.c index 7e3f26e1b..a99c3018e 100644 --- a/src/repository.c +++ b/src/repository.c @@ -752,6 +752,47 @@ cleanup: return git__rethrow(error, "Failed to (re)init the repository `%s`", path); } +int git_repository_is_detached(git_repository *repo) +{ + git_reference *ref; + int error; + size_t GIT_UNUSED(_size); + git_otype type; + + error = git_reference_lookup(&ref, repo, GIT_HEAD_FILE); + if (error < GIT_SUCCESS) + return error; + + if (git_reference_type(ref) == GIT_REF_SYMBOLIC) + return 0; + + error = git_odb_read_header(&_size, &type, repo->db, git_reference_oid(ref)); + if (error < GIT_SUCCESS) + return error; + + if (type != GIT_OBJ_COMMIT) + return git__throw(GIT_EOBJCORRUPTED, "HEAD is not a commit"); + + return 1; +} + +int git_repository_is_orphan(git_repository *repo) +{ + git_reference *ref; + int error; + + error = git_reference_lookup(&ref, repo, GIT_HEAD_FILE); + if (error < GIT_SUCCESS) + return error; + + if (git_reference_type(ref) == GIT_REF_OID) + return 0; + + error = git_reference_resolve(&ref, ref); + + return error == GIT_ENOTFOUND ? 1 : error; +} + int git_repository_is_empty(git_repository *repo) { git_reference *head, *branch; |
