diff options
author | Russell Belfer <rb@github.com> | 2013-05-16 10:38:27 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-05-16 10:38:27 -0700 |
commit | 58206c9ae79af8ef675e30087e5430065b078bbb (patch) | |
tree | 68fa8a68d13a3d4cccf0551f9515b5578ec63f07 /src | |
parent | 54e489c21efc19a14eac0f6f83313e3a753688f8 (diff) | |
download | libgit2-58206c9ae79af8ef675e30087e5430065b078bbb.tar.gz |
Add cat-file example and increase const use in API
This adds an example implementation that emulates git cat-file.
It is a convenient and relatively simple example of getting data
out of a repository.
Implementing this also revealed that there are a number of APIs
that are still not using const pointers to objects that really
ought to be. The main cause of this is that `git_vector_bsearch`
may need to call `git_vector_sort` before doing the search, so a
const pointer to the vector is not allowed. However, for tree
objects, with a little care, we can ensure that the vector of
tree entries is always sorted and allow lookups to take a const
pointer. Also, the missing const in commit objects just looks
like an oversight.
Diffstat (limited to 'src')
-rw-r--r-- | src/commit.c | 6 | ||||
-rw-r--r-- | src/tree.c | 23 | ||||
-rw-r--r-- | src/tree.h | 2 |
3 files changed, 20 insertions, 11 deletions
diff --git a/src/commit.c b/src/commit.c index be6e32c76..1ab9b34f7 100644 --- a/src/commit.c +++ b/src/commit.c @@ -266,14 +266,16 @@ int git_commit_tree(git_tree **tree_out, const git_commit *commit) return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_id); } -const git_oid *git_commit_parent_id(git_commit *commit, unsigned int n) +const git_oid *git_commit_parent_id( + const git_commit *commit, unsigned int n) { assert(commit); return git_vector_get(&commit->parent_ids, n); } -int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n) +int git_commit_parent( + git_commit **parent, const git_commit *commit, unsigned int n) { const git_oid *parent_id; assert(commit); diff --git a/src/tree.c b/src/tree.c index a48b322d4..10d131438 100644 --- a/src/tree.c +++ b/src/tree.c @@ -271,25 +271,27 @@ int git_tree_entry_to_object( } static const git_tree_entry *entry_fromname( - git_tree *tree, const char *name, size_t name_len) + const git_tree *tree, const char *name, size_t name_len) { size_t idx; - if (tree_key_search(&idx, &tree->entries, name, name_len) < 0) + assert(tree->entries.sorted); /* be safe when we cast away constness */ + + if (tree_key_search(&idx, (git_vector *)&tree->entries, name, name_len) < 0) return NULL; return git_vector_get(&tree->entries, idx); } const git_tree_entry *git_tree_entry_byname( - git_tree *tree, const char *filename) + const git_tree *tree, const char *filename) { assert(tree && filename); return entry_fromname(tree, filename, strlen(filename)); } const git_tree_entry *git_tree_entry_byindex( - git_tree *tree, size_t idx) + const git_tree *tree, size_t idx) { assert(tree); return git_vector_get(&tree->entries, idx); @@ -311,9 +313,9 @@ const git_tree_entry *git_tree_entry_byoid( return NULL; } -int git_tree__prefix_position(git_tree *tree, const char *path) +int git_tree__prefix_position(const git_tree *tree, const char *path) { - git_vector *entries = &tree->entries; + const git_vector *entries = &tree->entries; struct tree_key_search ksearch; size_t at_pos; @@ -323,8 +325,11 @@ int git_tree__prefix_position(git_tree *tree, const char *path) ksearch.filename = path; ksearch.filename_len = strlen(path); + assert(tree->entries.sorted); /* be safe when we cast away constness */ + /* Find tree entry with appropriate prefix */ - git_vector_bsearch2(&at_pos, entries, &homing_search_cmp, &ksearch); + git_vector_bsearch2( + &at_pos, (git_vector *)entries, &homing_search_cmp, &ksearch); for (; at_pos < entries->length; ++at_pos) { const git_tree_entry *entry = entries->contents[at_pos]; @@ -408,6 +413,8 @@ int git_tree__parse(void *_tree, git_odb_object *odb_obj) buffer += GIT_OID_RAWSZ; } + git_vector_sort(&tree->entries); + return 0; } @@ -796,7 +803,7 @@ static size_t subpath_len(const char *path) int git_tree_entry_bypath( git_tree_entry **entry_out, - git_tree *root, + const git_tree *root, const char *path) { int error = 0; diff --git a/src/tree.h b/src/tree.h index 7cb2dd36c..f07039a07 100644 --- a/src/tree.h +++ b/src/tree.h @@ -47,7 +47,7 @@ int git_tree__parse(void *tree, git_odb_object *obj); * @param prefix the beginning of a path to find in the tree. * @return index of the first item at or after the given prefix. */ -int git_tree__prefix_position(git_tree *tree, const char *prefix); +int git_tree__prefix_position(const git_tree *tree, const char *prefix); /** |