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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
diff --git a/include/git2/index.h b/include/git2/index.h
index 195e5b1..9d1fd17 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 0104ba1..2d3534f 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 fb0061c..86f44ca 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 f0dcd64..05123b5 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 469913d..a68a005 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 6692263..177ccb9 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 b67c552..e0bcd6a 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 3a08d45..9813eb6 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 4763792..c758583 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 46129f1..15356ef 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 b165905..c9e43a1 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);
}