diff options
author | Russell Belfer <rb@github.com> | 2012-11-21 11:03:07 -0800 |
---|---|---|
committer | Ben Straub <bs@github.com> | 2012-11-27 13:18:29 -0800 |
commit | 16248ee2d1d67bd386277bca67f04049afef875e (patch) | |
tree | 2ae7078fbe8aa91dd0d094afc0a38c33a4cb6732 | |
parent | f45d51ff8e04c6849413c13aedcf5abacc3c69bd (diff) | |
download | libgit2-16248ee2d1d67bd386277bca67f04049afef875e.tar.gz |
Fix up some missing consts in tree & index
This fixes some missed places where we can apply const-ness to
various public APIs.
There are still some index and tree APIs that cannot take const
pointers because we sort our `git_vectors` lazily and so we can't
reliably bsearch the index and tree content without applying a
`git_vector_sort()` first.
This also fixes some missed places where size_t can be used and
where const can be applied to a couple internal functions.
-rw-r--r-- | include/git2/index.h | 10 | ||||
-rw-r--r-- | include/git2/tree.h | 17 | ||||
-rw-r--r-- | src/index.c | 38 | ||||
-rw-r--r-- | src/index.h | 2 | ||||
-rw-r--r-- | src/iterator.c | 8 | ||||
-rw-r--r-- | src/tree.c | 33 | ||||
-rw-r--r-- | src/tree.h | 3 | ||||
-rw-r--r-- | src/util.c | 2 | ||||
-rw-r--r-- | src/vector.c | 35 | ||||
-rw-r--r-- | src/vector.h | 20 | ||||
-rw-r--r-- | tests-clar/core/vector.c | 2 |
11 files changed, 88 insertions, 82 deletions
diff --git a/include/git2/index.h b/include/git2/index.h index 195e5b13f..9d1fd17d0 100644 --- a/include/git2/index.h +++ b/include/git2/index.h @@ -258,7 +258,7 @@ GIT_EXTERN(int) git_index_write_tree_to(git_oid *out, git_index *index, git_repo * @param index an existing index object * @return integer of count of current entries */ -GIT_EXTERN(unsigned int) git_index_entrycount(git_index *index); +GIT_EXTERN(unsigned int) git_index_entrycount(const git_index *index); /** * Clear the contents (all the entries) of an index object. @@ -282,7 +282,8 @@ GIT_EXTERN(void) git_index_clear(git_index *index); * @param n the position of the entry * @return a pointer to the entry; NULL if out of bounds */ -GIT_EXTERN(const git_index_entry *) git_index_get_byindex(git_index *index, size_t n); +GIT_EXTERN(const git_index_entry *) git_index_get_byindex( + git_index *index, size_t n); /** * Get a pointer to one of the entries in the index @@ -298,7 +299,8 @@ GIT_EXTERN(const git_index_entry *) git_index_get_byindex(git_index *index, size * @param stage stage to search * @return a pointer to the entry; NULL if it was not found */ -GIT_EXTERN(const git_index_entry *) git_index_get_bypath(git_index *index, const char *path, int stage); +GIT_EXTERN(const git_index_entry *) git_index_get_bypath( + git_index *index, const char *path, int stage); /** * Remove an entry from the index @@ -443,7 +445,7 @@ GIT_EXTERN(void) git_index_conflict_cleanup(git_index *index); * * @return 1 if at least one conflict is found, 0 otherwise. */ -GIT_EXTERN(int) git_index_has_conflicts(git_index *index); +GIT_EXTERN(int) git_index_has_conflicts(const git_index *index); /**@}*/ diff --git a/include/git2/tree.h b/include/git2/tree.h index 0104ba144..2d3534fab 100644 --- a/include/git2/tree.h +++ b/include/git2/tree.h @@ -127,7 +127,7 @@ GIT_EXTERN(const git_tree_entry *) git_tree_entry_byindex( * @return the tree entry; NULL if not found */ GIT_EXTERN(const git_tree_entry *) git_tree_entry_byoid( - git_tree *tree, const git_oid *oid); + const git_tree *tree, const git_oid *oid); /** * Retrieve a tree entry contained in a tree or in any of its subtrees, @@ -351,18 +351,15 @@ typedef enum { } git_treewalk_mode; /** - * Traverse the entries in a tree and its subtrees in - * post or pre order + * Traverse the entries in a tree and its subtrees in post or pre order. * - * The entries will be traversed in the specified order, - * children subtrees will be automatically loaded as required, - * and the `callback` will be called once per entry with - * the current (relative) root for the entry and the entry - * data itself. + * The entries will be traversed in the specified order, children subtrees + * will be automatically loaded as required, and the `callback` will be + * called once per entry with the current (relative) root for the entry and + * the entry data itself. * * If the callback returns a positive value, the passed entry will be - * skipped on the traversal (in pre mode). A negative value stops the - * walk. + * skipped on the traversal (in pre mode). A negative value stops the walk. * * @param tree The tree to walk * @param mode Traversal mode (pre or post-order) diff --git a/src/index.c b/src/index.c index fb0061cea..86f44ca52 100644 --- a/src/index.c +++ b/src/index.c @@ -298,7 +298,7 @@ static void index_free(git_index *index) { git_index_entry *e; git_index_reuc_entry *reuc; - unsigned int i; + size_t i; git_index_clear(index); git_vector_foreach(&index->entries, i, e) { @@ -488,20 +488,22 @@ int git_index_write_tree_to(git_oid *oid, git_index *index, git_repository *repo return git_tree__write_index(oid, index, repo); } -unsigned int git_index_entrycount(git_index *index) +unsigned int git_index_entrycount(const git_index *index) { assert(index); return (unsigned int)index->entries.length; } -const git_index_entry *git_index_get_byindex(git_index *index, size_t n) +const git_index_entry *git_index_get_byindex( + git_index *index, size_t n) { assert(index); git_vector_sort(&index->entries); return git_vector_get(&index->entries, n); } -const git_index_entry *git_index_get_bypath(git_index *index, const char *path, int stage) +const git_index_entry *git_index_get_bypath( + git_index *index, const char *path, int stage) { int pos; @@ -810,13 +812,15 @@ int git_index_find(git_index *index, const char *path) assert(index && path); - if ((pos = git_vector_bsearch2(&index->entries, index->entries_search_path, path)) < 0) + if ((pos = git_vector_bsearch2( + &index->entries, index->entries_search_path, path)) < 0) return pos; /* Since our binary search only looked at path, we may be in the - * middle of a list of stages. */ + * middle of a list of stages. + */ while (pos > 0) { - git_index_entry *prev = git_vector_get(&index->entries, pos-1); + const git_index_entry *prev = git_vector_get(&index->entries, pos-1); if (index->entries_cmp_path(prev->path, path) != 0) break; @@ -827,10 +831,10 @@ int git_index_find(git_index *index, const char *path) return pos; } -unsigned int git_index__prefix_position(git_index *index, const char *path) +size_t git_index__prefix_position(git_index *index, const char *path) { struct entry_srch_key srch_key; - unsigned int pos; + size_t pos; srch_key.path = path; srch_key.stage = 0; @@ -961,7 +965,7 @@ int git_index_conflict_remove(git_index *index, const char *path) return error; } -static int index_conflicts_match(git_vector *v, size_t idx) +static int index_conflicts_match(const git_vector *v, size_t idx) { git_index_entry *entry = git_vector_get(v, idx); @@ -979,9 +983,9 @@ void git_index_conflict_cleanup(git_index *index) git_vector_remove_matching(&index->entries, index_conflicts_match); } -int git_index_has_conflicts(git_index *index) +int git_index_has_conflicts(const git_index *index) { - unsigned int i; + size_t i; git_index_entry *entry; assert(index); @@ -1361,7 +1365,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) static int is_index_extended(git_index *index) { - unsigned int i, extended; + size_t i, extended; git_index_entry *entry; extended = 0; @@ -1374,7 +1378,7 @@ static int is_index_extended(git_index *index) } } - return extended; + return (int)extended; } static int write_disk_entry(git_filebuf *file, git_index_entry *entry) @@ -1440,7 +1444,7 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry) static int write_entries(git_index *index, git_filebuf *file) { int error = 0; - unsigned int i; + size_t i; git_vector case_sorted; git_index_entry *entry; git_vector *out = &index->entries; @@ -1506,7 +1510,7 @@ static int write_reuc_extension(git_index *index, git_filebuf *file) git_vector *out = &index->reuc; git_index_reuc_entry *reuc; struct index_extension extension; - unsigned int i; + size_t i; int error = 0; git_vector_foreach(out, i, reuc) { @@ -1529,9 +1533,7 @@ done: static int write_index(git_index *index, git_filebuf *file) { git_oid hash_final; - struct index_header header; - int is_extended; assert(index && file); diff --git a/src/index.h b/src/index.h index f0dcd64d5..05123b580 100644 --- a/src/index.h +++ b/src/index.h @@ -43,7 +43,7 @@ struct git_index { extern void git_index_entry__init_from_stat(git_index_entry *entry, struct stat *st); -extern unsigned int git_index__prefix_position(git_index *index, const char *path); +extern size_t git_index__prefix_position(git_index *index, const char *path); extern int git_index_entry__cmp(const void *a, const void *b); extern int git_index_entry__cmp_icase(const void *a, const void *b); diff --git a/src/iterator.c b/src/iterator.c index 469913d3b..a68a0050b 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -434,7 +434,7 @@ typedef struct workdir_iterator_frame workdir_iterator_frame; struct workdir_iterator_frame { workdir_iterator_frame *next; git_vector entries; - unsigned int index; + size_t index; char *start; }; @@ -761,7 +761,8 @@ static int spoolandsort_iterator__current( spoolandsort_iterator *si = (spoolandsort_iterator *)self; if (si->position < si->entries.length) - *entry = (const git_index_entry *)git_vector_get_const(&si->entries, si->position); + *entry = (const git_index_entry *)git_vector_get( + &si->entries, si->position); else *entry = NULL; @@ -781,7 +782,8 @@ static int spoolandsort_iterator__advance( spoolandsort_iterator *si = (spoolandsort_iterator *)self; if (si->position < si->entries.length) - *entry = (const git_index_entry *)git_vector_get_const(&si->entries, ++si->position); + *entry = (const git_index_entry *)git_vector_get( + &si->entries, ++si->position); else *entry = NULL; diff --git a/src/tree.c b/src/tree.c index 6692263c5..177ccb98d 100644 --- a/src/tree.c +++ b/src/tree.c @@ -98,11 +98,11 @@ static int homing_search_cmp(const void *key, const void *array_member) * ambiguous because of folder vs file sorting, we look linearly * around the area for our target file. */ -static int tree_key_search(git_vector *entries, const char *filename, size_t filename_len) +static int tree_key_search( + git_vector *entries, const char *filename, size_t filename_len) { struct tree_key_search ksearch; const git_tree_entry *entry; - int homing, i; ksearch.filename = filename; @@ -226,7 +226,8 @@ int git_tree_entry_to_object( return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY); } -static git_tree_entry *entry_fromname(git_tree *tree, const char *name, size_t name_len) +static const git_tree_entry *entry_fromname( + git_tree *tree, const char *name, size_t name_len) { int idx = tree_key_search(&tree->entries, name, name_len); if (idx < 0) @@ -235,22 +236,25 @@ static git_tree_entry *entry_fromname(git_tree *tree, const char *name, size_t n return git_vector_get(&tree->entries, idx); } -const git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename) +const git_tree_entry *git_tree_entry_byname( + 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_entry *git_tree_entry_byindex( + git_tree *tree, size_t idx) { assert(tree); return git_vector_get(&tree->entries, idx); } -const git_tree_entry *git_tree_entry_byoid(git_tree *tree, const git_oid *oid) +const git_tree_entry *git_tree_entry_byoid( + const git_tree *tree, const git_oid *oid) { - unsigned int i; - git_tree_entry *e; + size_t i; + const git_tree_entry *e; assert(tree); @@ -266,7 +270,7 @@ int git_tree__prefix_position(git_tree *tree, const char *path) { git_vector *entries = &tree->entries; struct tree_key_search ksearch; - unsigned int at_pos; + size_t at_pos; ksearch.filename = path; ksearch.filename_len = strlen(path); @@ -286,7 +290,7 @@ int git_tree__prefix_position(git_tree *tree, const char *path) break; } - return at_pos; + return (int)at_pos; } size_t git_tree_entrycount(const git_tree *tree) @@ -422,7 +426,7 @@ static int write_tree( */ for (i = start; i < entries; ++i) { const git_index_entry *entry = git_index_get_byindex(index, i); - char *filename, *next_slash; + const char *filename, *next_slash; /* * If we've left our (sub)tree, exit the loop and return. The @@ -497,7 +501,8 @@ on_error: return -1; } -int git_tree__write_index(git_oid *oid, git_index *index, git_repository *repo) +int git_tree__write_index( + git_oid *oid, git_index *index, git_repository *repo) { int ret; @@ -814,10 +819,10 @@ static int tree_walk( bool preorder) { int error = 0; - unsigned int i; + size_t i; for (i = 0; i < tree->entries.length; ++i) { - git_tree_entry *entry = tree->entries.contents[i]; + const git_tree_entry *entry = tree->entries.contents[i]; if (preorder) { error = callback(path->ptr, entry, payload); diff --git a/src/tree.h b/src/tree.h index b67c55202..e0bcd6acf 100644 --- a/src/tree.h +++ b/src/tree.h @@ -51,7 +51,8 @@ int git_tree__prefix_position(git_tree *tree, const char *prefix); /** * Write a tree to the given repository */ -int git_tree__write_index(git_oid *oid, git_index *index, git_repository *repo); +int git_tree__write_index( + git_oid *oid, git_index *index, git_repository *repo); /** * Obsolete mode kept for compatibility reasons diff --git a/src/util.c b/src/util.c index 3a08d4554..9813eb694 100644 --- a/src/util.c +++ b/src/util.c @@ -447,7 +447,7 @@ int git__bsearch( /** * A strcmp wrapper - * + * * We don't want direct pointers to the CRT on Windows, we may * get stdcall conflicts. */ diff --git a/src/vector.c b/src/vector.c index 4763792f5..c758583b6 100644 --- a/src/vector.c +++ b/src/vector.c @@ -9,14 +9,14 @@ #include "repository.h" #include "vector.h" -static const double resize_factor = 1.75; -static const unsigned int minimum_size = 8; +static const double git_vector_resize_factor = 1.75; +static const size_t git_vector_minimum_size = 8; static int resize_vector(git_vector *v) { - v->_alloc_size = ((unsigned int)(v->_alloc_size * resize_factor)) + 1; - if (v->_alloc_size < minimum_size) - v->_alloc_size = minimum_size; + v->_alloc_size = (size_t)(v->_alloc_size * git_vector_resize_factor) + 1; + if (v->_alloc_size < git_vector_minimum_size) + v->_alloc_size = git_vector_minimum_size; v->contents = git__realloc(v->contents, v->_alloc_size * sizeof(void *)); GITERR_CHECK_ALLOC(v->contents); @@ -24,7 +24,7 @@ static int resize_vector(git_vector *v) return 0; } -int git_vector_dup(git_vector *v, git_vector *src, git_vector_cmp cmp) +int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp) { assert(v && src); @@ -58,7 +58,7 @@ int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp) memset(v, 0x0, sizeof(git_vector)); if (initial_size == 0) - initial_size = minimum_size; + initial_size = git_vector_minimum_size; v->_alloc_size = initial_size; v->_cmp = cmp; @@ -133,7 +133,7 @@ void git_vector_sort(git_vector *v) } int git_vector_bsearch3( - unsigned int *at_pos, + size_t *at_pos, git_vector *v, git_vector_cmp key_lookup, const void *key) @@ -151,15 +151,15 @@ int git_vector_bsearch3( rval = git__bsearch(v->contents, v->length, key, key_lookup, &pos); if (at_pos != NULL) - *at_pos = (unsigned int)pos; + *at_pos = pos; return (rval >= 0) ? (int)pos : GIT_ENOTFOUND; } int git_vector_search2( - git_vector *v, git_vector_cmp key_lookup, const void *key) + const git_vector *v, git_vector_cmp key_lookup, const void *key) { - unsigned int i; + size_t i; assert(v && key && key_lookup); @@ -176,14 +176,14 @@ static int strict_comparison(const void *a, const void *b) return (a == b) ? 0 : -1; } -int git_vector_search(git_vector *v, const void *entry) +int git_vector_search(const git_vector *v, const void *entry) { return git_vector_search2(v, v->_cmp ? v->_cmp : strict_comparison, entry); } -int git_vector_remove(git_vector *v, unsigned int idx) +int git_vector_remove(git_vector *v, size_t idx) { - unsigned int i; + size_t i; assert(v); @@ -206,7 +206,7 @@ void git_vector_pop(git_vector *v) void git_vector_uniq(git_vector *v) { git_vector_cmp cmp; - unsigned int i, j; + size_t i, j; if (v->length <= 1) return; @@ -223,9 +223,10 @@ void git_vector_uniq(git_vector *v) v->length -= j - i - 1; } -void git_vector_remove_matching(git_vector *v, int (*match)(git_vector *v, size_t idx)) +void git_vector_remove_matching( + git_vector *v, int (*match)(const git_vector *v, size_t idx)) { - unsigned int i, j; + size_t i, j; for (i = 0, j = 0; j < v->length; ++j) { v->contents[i] = v->contents[j]; diff --git a/src/vector.h b/src/vector.h index 46129f17b..15356ef16 100644 --- a/src/vector.h +++ b/src/vector.h @@ -24,23 +24,23 @@ typedef struct git_vector { int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp); void git_vector_free(git_vector *v); void git_vector_clear(git_vector *v); -int git_vector_dup(git_vector *v, git_vector *src, git_vector_cmp cmp); +int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp); void git_vector_swap(git_vector *a, git_vector *b); void git_vector_sort(git_vector *v); /** Linear search for matching entry using internal comparison function */ -int git_vector_search(git_vector *v, const void *entry); +int git_vector_search(const git_vector *v, const void *entry); /** Linear search for matching entry using explicit comparison function */ -int git_vector_search2(git_vector *v, git_vector_cmp cmp, const void *key); +int git_vector_search2(const git_vector *v, git_vector_cmp cmp, const void *key); /** * Binary search for matching entry using explicit comparison function that * returns position where item would go if not found. */ int git_vector_bsearch3( - unsigned int *at_pos, git_vector *v, git_vector_cmp cmp, const void *key); + size_t *at_pos, git_vector *v, git_vector_cmp cmp, const void *key); /** Binary search for matching entry using internal comparison function */ GIT_INLINE(int) git_vector_bsearch(git_vector *v, const void *key) @@ -60,14 +60,9 @@ GIT_INLINE(void *) git_vector_get(const git_vector *v, size_t position) return (position < v->length) ? v->contents[position] : NULL; } -GIT_INLINE(const void *) git_vector_get_const(const git_vector *v, size_t position) -{ - return (position < v->length) ? v->contents[position] : NULL; -} - #define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL) -GIT_INLINE(void *) git_vector_last(git_vector *v) +GIT_INLINE(void *) git_vector_last(const git_vector *v) { return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL; } @@ -81,10 +76,11 @@ GIT_INLINE(void *) git_vector_last(git_vector *v) int git_vector_insert(git_vector *v, void *element); int git_vector_insert_sorted(git_vector *v, void *element, int (*on_dup)(void **old, void *new)); -int git_vector_remove(git_vector *v, unsigned int idx); +int git_vector_remove(git_vector *v, size_t idx); void git_vector_pop(git_vector *v); void git_vector_uniq(git_vector *v); -void git_vector_remove_matching(git_vector *v, int (*match)(git_vector *v, size_t idx)); +void git_vector_remove_matching( + git_vector *v, int (*match)(const git_vector *v, size_t idx)); int git_vector_resize_to(git_vector *v, size_t new_length); int git_vector_set(void **old, git_vector *v, size_t position, void *value); diff --git a/tests-clar/core/vector.c b/tests-clar/core/vector.c index b165905ae..c9e43a149 100644 --- a/tests-clar/core/vector.c +++ b/tests-clar/core/vector.c @@ -190,7 +190,7 @@ void test_core_vector__5(void) git_vector_free(&x); } -static int remove_ones(git_vector *v, size_t idx) +static int remove_ones(const git_vector *v, size_t idx) { return (git_vector_get(v, idx) == (void *)0x001); } |