diff options
| author | Philip Kelley <phkelley@hotmail.com> | 2013-01-27 14:17:07 -0500 |
|---|---|---|
| committer | Philip Kelley <phkelley@hotmail.com> | 2013-01-27 14:17:07 -0500 |
| commit | 11d9f6b30438a141def883b0115f7f764c03e990 (patch) | |
| tree | abe54e8085c4e3a1c7a822ee256f81e0d58e6b42 /src/index.c | |
| parent | aa3bf89df21c44f22fe70b4aac9109646fd06b48 (diff) | |
| download | libgit2-11d9f6b30438a141def883b0115f7f764c03e990.tar.gz | |
Vector improvements and their fallout
Diffstat (limited to 'src/index.c')
| -rw-r--r-- | src/index.c | 69 |
1 files changed, 35 insertions, 34 deletions
diff --git a/src/index.c b/src/index.c index e9dffab8d..1e00dd3fa 100644 --- a/src/index.c +++ b/src/index.c @@ -97,7 +97,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) static bool is_index_extended(git_index *index); static int write_index(git_index *index, git_filebuf *file); -static int index_find(git_index *index, const char *path, int stage); +static int index_find(size_t *at_pos, git_index *index, const char *path, int stage); static void index_entry_free(git_index_entry *entry); static void index_entry_reuc_free(git_index_reuc_entry *reuc); @@ -504,13 +504,13 @@ const git_index_entry *git_index_get_byindex( const git_index_entry *git_index_get_bypath( git_index *index, const char *path, int stage) { - int pos; + size_t pos; assert(index); git_vector_sort(&index->entries); - if((pos = index_find(index, path, stage)) < 0) + if (index_find(&pos, index, path, stage) < 0) return NULL; return git_index_get_byindex(index, pos); @@ -665,8 +665,7 @@ static void index_entry_free(git_index_entry *entry) static int index_insert(git_index *index, git_index_entry *entry, int replace) { - size_t path_length; - int position; + size_t path_length, position; git_index_entry **existing = NULL; assert(index && entry && entry->path != NULL); @@ -682,7 +681,7 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace) entry->flags |= GIT_IDXENTRY_NAMEMASK; /* look if an entry with this path already exists */ - if ((position = index_find(index, entry->path, index_entry_stage(entry))) >= 0) { + if (!index_find(&position, index, entry->path, index_entry_stage(entry))) { existing = (git_index_entry **)&index->entries.contents[position]; /* update filemode to existing values if stat is not trusted */ @@ -787,14 +786,14 @@ int git_index_add(git_index *index, const git_index_entry *source_entry) int git_index_remove(git_index *index, const char *path, int stage) { - int position; + size_t position; int error; git_index_entry *entry; git_vector_sort(&index->entries); - if ((position = index_find(index, path, stage)) < 0) - return position; + if (index_find(&position, index, path, stage) < 0) + return GIT_ENOTFOUND; entry = git_vector_get(&index->entries, position); if (entry != NULL) @@ -846,7 +845,7 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage) return error; } -static int index_find(git_index *index, const char *path, int stage) +static int index_find(size_t *at_pos, git_index *index, const char *path, int stage) { struct entry_srch_key srch_key; @@ -855,20 +854,18 @@ static int index_find(git_index *index, const char *path, int stage) srch_key.path = path; srch_key.stage = stage; - return git_vector_bsearch2(&index->entries, index->entries_search, &srch_key); + return git_vector_bsearch2(at_pos, &index->entries, index->entries_search, &srch_key); } -int git_index_find(git_index *index, const char *path) +int git_index_find(size_t *at_pos, git_index *index, const char *path) { - int pos; + size_t pos; assert(index && path); - if ((pos = git_vector_bsearch2( - &index->entries, index->entries_search_path, path)) < 0) - { + if (git_vector_bsearch2(&pos, &index->entries, index->entries_search_path, path) < 0) { giterr_set(GITERR_INDEX, "Index does not contain %s", path); - return pos; + return GIT_ENOTFOUND; } /* Since our binary search only looked at path, we may be in the @@ -883,7 +880,10 @@ int git_index_find(git_index *index, const char *path) --pos; } - return pos; + if (at_pos) + *at_pos = pos; + + return 0; } size_t git_index__prefix_position(git_index *index, const char *path) @@ -895,7 +895,7 @@ size_t git_index__prefix_position(git_index *index, const char *path) srch_key.stage = 0; git_vector_sort(&index->entries); - git_vector_bsearch3( + git_vector_bsearch2( &pos, &index->entries, index->entries_search, &srch_key); return pos; @@ -945,7 +945,8 @@ int git_index_conflict_get(git_index_entry **ancestor_out, git_index_entry **their_out, git_index *index, const char *path) { - int pos, posmax, stage; + size_t pos, posmax; + int stage; git_index_entry *conflict_entry; int error = GIT_ENOTFOUND; @@ -955,10 +956,10 @@ int git_index_conflict_get(git_index_entry **ancestor_out, *our_out = NULL; *their_out = NULL; - if ((pos = git_index_find(index, path)) < 0) - return pos; + if (git_index_find(&pos, index, path) < 0) + return GIT_ENOTFOUND; - for (posmax = (int)git_index_entrycount(index); pos < posmax; ++pos) { + for (posmax = git_index_entrycount(index); pos < posmax; ++pos) { conflict_entry = git_vector_get(&index->entries, pos); @@ -990,16 +991,16 @@ int git_index_conflict_get(git_index_entry **ancestor_out, int git_index_conflict_remove(git_index *index, const char *path) { - int pos, posmax; + size_t pos, posmax; git_index_entry *conflict_entry; int error = 0; assert(index && path); - if ((pos = git_index_find(index, path)) < 0) - return pos; + if (git_index_find(&pos, index, path) < 0) + return GIT_ENOTFOUND; - posmax = (int)git_index_entrycount(index); + posmax = git_index_entrycount(index); while (pos < posmax) { conflict_entry = git_vector_get(&index->entries, pos); @@ -1012,7 +1013,7 @@ int git_index_conflict_remove(git_index *index, const char *path) continue; } - if ((error = git_vector_remove(&index->entries, (unsigned int)pos)) < 0) + if ((error = git_vector_remove(&index->entries, pos)) < 0) return error; index_entry_free(conflict_entry); @@ -1064,11 +1065,11 @@ unsigned int git_index_reuc_entrycount(git_index *index) static int index_reuc_insert(git_index *index, git_index_reuc_entry *reuc, int replace) { git_index_reuc_entry **existing = NULL; - int position; + size_t position; assert(index && reuc && reuc->path != NULL); - if ((position = git_index_reuc_find(index, reuc->path)) >= 0) + if (!git_index_reuc_find(&position, index, reuc->path)) existing = (git_index_reuc_entry **)&index->reuc.contents[position]; if (!replace || !existing) @@ -1102,15 +1103,15 @@ int git_index_reuc_add(git_index *index, const char *path, return error; } -int git_index_reuc_find(git_index *index, const char *path) +int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path) { - return git_vector_bsearch2(&index->reuc, index->reuc_search, path); + return git_vector_bsearch2(at_pos, &index->reuc, index->reuc_search, path); } const git_index_reuc_entry *git_index_reuc_get_bypath( git_index *index, const char *path) { - int pos; + size_t pos; assert(index && path); if (!index->reuc.length) @@ -1118,7 +1119,7 @@ const git_index_reuc_entry *git_index_reuc_get_bypath( git_vector_sort(&index->reuc); - if ((pos = git_index_reuc_find(index, path)) < 0) + if (git_index_reuc_find(&pos, index, path) < 0) return NULL; return git_vector_get(&index->reuc, pos); |
