Commit 52bb0476 by Russell Belfer

Clean up index snapshot function naming

Clear up some of the various "find" functions and the snapshot
API naming to be things I like more.
parent 8a2834d3
...@@ -279,17 +279,17 @@ static int checkout_action_wd_only( ...@@ -279,17 +279,17 @@ static int checkout_action_wd_only(
if (data->index != NULL) { if (data->index != NULL) {
size_t pos; size_t pos;
error = git_index__find( error = git_index__find_pos(
&pos, data->index, wd->path, 0, GIT_INDEX_STAGE_ANY); &pos, data->index, wd->path, 0, GIT_INDEX_STAGE_ANY);
if (wd->mode != GIT_FILEMODE_TREE) { if (wd->mode != GIT_FILEMODE_TREE) {
if (!error) { /* found by git_index__find call */ if (!error) { /* found by git_index__find_pos call */
notify = GIT_CHECKOUT_NOTIFY_DIRTY; notify = GIT_CHECKOUT_NOTIFY_DIRTY;
remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0); remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
} else if (error != GIT_ENOTFOUND) } else if (error != GIT_ENOTFOUND)
return error; return error;
else else
error = 0; /* git_index__find does not set error msg */ error = 0; /* git_index__find_pos does not set error msg */
} else { } else {
/* for tree entries, we have to see if there are any index /* for tree entries, we have to see if there are any index
* entries that are contained inside that tree * entries that are contained inside that tree
......
...@@ -342,6 +342,28 @@ static int index_sort_if_needed(git_index *index, bool need_lock) ...@@ -342,6 +342,28 @@ static int index_sort_if_needed(git_index *index, bool need_lock)
return 0; return 0;
} }
GIT_INLINE(int) index_find_in_entries(
size_t *out, git_vector *entries, git_vector_cmp entry_srch,
const char *path, size_t path_len, int stage)
{
struct entry_srch_key srch_key;
srch_key.path = path;
srch_key.pathlen = !path_len ? strlen(path) : path_len;
srch_key.stage = stage;
return git_vector_bsearch2(out, entries, entry_srch, &srch_key);
}
GIT_INLINE(int) index_find(
size_t *out, git_index *index,
const char *path, size_t path_len, int stage, bool need_lock)
{
if (index_sort_if_needed(index, need_lock) < 0)
return -1;
return index_find_in_entries(
out, &index->entries, index->entries_search, path, path_len, stage);
}
void git_index__set_ignore_case(git_index *index, bool ignore_case) void git_index__set_ignore_case(git_index *index, bool ignore_case)
{ {
index->ignore_case = ignore_case; index->ignore_case = ignore_case;
...@@ -687,16 +709,6 @@ size_t git_index_entrycount(const git_index *index) ...@@ -687,16 +709,6 @@ size_t git_index_entrycount(const git_index *index)
return index->entries.length; return index->entries.length;
} }
GIT_INLINE(int) git_index__find_internal(
size_t *out, git_index *index, const char *path, size_t path_len, int stage,
bool need_lock)
{
if (index_sort_if_needed(index, need_lock) < 0)
return -1;
return git_index__find_in_entries(
out, &index->entries, index->entries_search, path, path_len, stage);
}
const git_index_entry *git_index_get_byindex( const git_index_entry *git_index_get_byindex(
git_index *index, size_t n) git_index *index, size_t n)
{ {
...@@ -713,7 +725,7 @@ const git_index_entry *git_index_get_bypath( ...@@ -713,7 +725,7 @@ const git_index_entry *git_index_get_bypath(
assert(index); assert(index);
if (git_index__find_internal(&pos, index, path, 0, stage, true) < 0) { if (index_find(&pos, index, path, 0, stage, true) < 0) {
giterr_set(GITERR_INDEX, "Index does not contain %s", path); giterr_set(GITERR_INDEX, "Index does not contain %s", path);
return NULL; return NULL;
} }
...@@ -896,7 +908,7 @@ static int has_dir_name(git_index *index, ...@@ -896,7 +908,7 @@ static int has_dir_name(git_index *index,
} }
len = slash - name; len = slash - name;
if (!git_index__find_internal(&pos, index, name, len, stage, false)) { if (!index_find(&pos, index, name, len, stage, false)) {
retval = -1; retval = -1;
if (!ok_to_replace) if (!ok_to_replace)
break; break;
...@@ -976,7 +988,7 @@ static int index_insert( ...@@ -976,7 +988,7 @@ static int index_insert(
git_vector_sort(&index->entries); git_vector_sort(&index->entries);
/* look if an entry with this path already exists */ /* look if an entry with this path already exists */
if (!git_index__find_internal( if (!index_find(
&position, index, entry->path, 0, GIT_IDXENTRY_STAGE(entry), false)) { &position, index, entry->path, 0, GIT_IDXENTRY_STAGE(entry), false)) {
existing = index->entries.contents[position]; existing = index->entries.contents[position];
/* update filemode to existing values if stat is not trusted */ /* update filemode to existing values if stat is not trusted */
...@@ -1098,7 +1110,7 @@ int git_index_remove(git_index *index, const char *path, int stage) ...@@ -1098,7 +1110,7 @@ int git_index_remove(git_index *index, const char *path, int stage)
return -1; return -1;
} }
if (git_index__find_internal(&position, index, path, 0, stage, false) < 0) { if (index_find(&position, index, path, 0, stage, false) < 0) {
giterr_set( giterr_set(
GITERR_INDEX, "Index does not contain %s at stage %d", path, stage); GITERR_INDEX, "Index does not contain %s at stage %d", path, stage);
error = GIT_ENOTFOUND; error = GIT_ENOTFOUND;
...@@ -1124,8 +1136,7 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage) ...@@ -1124,8 +1136,7 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage)
if (!(error = git_buf_sets(&pfx, dir)) && if (!(error = git_buf_sets(&pfx, dir)) &&
!(error = git_path_to_dir(&pfx))) !(error = git_path_to_dir(&pfx)))
git_index__find_internal( index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY, false);
&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY, false);
while (!error) { while (!error) {
entry = git_vector_get(&index->entries, pos); entry = git_vector_get(&index->entries, pos);
...@@ -1148,22 +1159,11 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage) ...@@ -1148,22 +1159,11 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage)
return error; return error;
} }
int git_index__find_in_entries( int git_index__find_pos(
size_t *out, git_vector *entries, git_vector_cmp entry_srch,
const char *path, size_t path_len, int stage)
{
struct entry_srch_key srch_key;
srch_key.path = path;
srch_key.pathlen = !path_len ? strlen(path) : path_len;
srch_key.stage = stage;
return git_vector_bsearch2(out, entries, entry_srch, &srch_key);
}
int git_index__find(
size_t *out, git_index *index, const char *path, size_t path_len, int stage) size_t *out, git_index *index, const char *path, size_t path_len, int stage)
{ {
assert(index && path); assert(index && path);
return git_index__find_internal(out, index, path, path_len, stage, true); return index_find(out, index, path, path_len, stage, true);
} }
int git_index_find(size_t *at_pos, git_index *index, const char *path) int git_index_find(size_t *at_pos, git_index *index, const char *path)
...@@ -1177,7 +1177,8 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path) ...@@ -1177,7 +1177,8 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path)
return -1; return -1;
} }
if (git_vector_bsearch2(&pos, &index->entries, index->entries_search_path, path) < 0) { if (git_vector_bsearch2(
&pos, &index->entries, index->entries_search_path, path) < 0) {
git_mutex_unlock(&index->lock); git_mutex_unlock(&index->lock);
giterr_set(GITERR_INDEX, "Index does not contain %s", path); giterr_set(GITERR_INDEX, "Index does not contain %s", path);
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
...@@ -1186,13 +1187,11 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path) ...@@ -1186,13 +1187,11 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path)
/* Since our binary search only looked at path, we may be in the /* 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) { for (; pos > 0; --pos) {
const 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) if (index->entries_cmp_path(prev->path, path) != 0)
break; break;
--pos;
} }
if (at_pos) if (at_pos)
...@@ -2269,7 +2268,7 @@ static int read_tree_cb( ...@@ -2269,7 +2268,7 @@ static int read_tree_cb(
/* look for corresponding old entry and copy data to new entry */ /* look for corresponding old entry and copy data to new entry */
if (data->old_entries != NULL && if (data->old_entries != NULL &&
!git_index__find_in_entries( !index_find_in_entries(
&pos, data->old_entries, data->entry_cmp, path.ptr, 0, 0) && &pos, data->old_entries, data->entry_cmp, path.ptr, 0, 0) &&
(old_entry = git_vector_get(data->old_entries, pos)) != NULL && (old_entry = git_vector_get(data->old_entries, pos)) != NULL &&
entry->mode == old_entry->mode && entry->mode == old_entry->mode &&
...@@ -2394,7 +2393,7 @@ int git_index_add_all( ...@@ -2394,7 +2393,7 @@ int git_index_add_all(
/* skip ignored items that are not already in the index */ /* skip ignored items that are not already in the index */
if ((flags & GIT_INDEX_ADD_FORCE) == 0 && if ((flags & GIT_INDEX_ADD_FORCE) == 0 &&
git_iterator_current_is_ignored(wditer) && git_iterator_current_is_ignored(wditer) &&
git_index__find(&existing, index, wd->path, 0, 0) < 0) index_find(&existing, index, wd->path, 0, 0, true) < 0)
continue; continue;
/* issue notification callback if requested */ /* issue notification callback if requested */
...@@ -2556,7 +2555,7 @@ int git_index_update_all( ...@@ -2556,7 +2555,7 @@ int git_index_update_all(
return error; return error;
} }
int git_index__snapshot(git_vector *entries, git_index *index) int git_index_snapshot_new(git_vector *snap, git_index *index)
{ {
int error; int error;
...@@ -2570,7 +2569,7 @@ int git_index__snapshot(git_vector *entries, git_index *index) ...@@ -2570,7 +2569,7 @@ int git_index__snapshot(git_vector *entries, git_index *index)
git_atomic_inc(&index->readers); git_atomic_inc(&index->readers);
git_vector_sort(&index->entries); git_vector_sort(&index->entries);
error = git_vector_dup(entries, &index->entries, index->entries._cmp); error = git_vector_dup(snap, &index->entries, index->entries._cmp);
git_mutex_unlock(&index->lock); git_mutex_unlock(&index->lock);
...@@ -2580,8 +2579,10 @@ int git_index__snapshot(git_vector *entries, git_index *index) ...@@ -2580,8 +2579,10 @@ int git_index__snapshot(git_vector *entries, git_index *index)
return error; return error;
} }
void git_index__release_snapshot(git_index *index) void git_index_snapshot_release(git_vector *snap, git_index *index)
{ {
git_vector_free(snap);
git_atomic_dec(&index->readers); git_atomic_dec(&index->readers);
if (!git_mutex_lock(&index->lock)) { if (!git_mutex_lock(&index->lock)) {
...@@ -2591,3 +2592,10 @@ void git_index__release_snapshot(git_index *index) ...@@ -2591,3 +2592,10 @@ void git_index__release_snapshot(git_index *index)
git_index_free(index); git_index_free(index);
} }
int git_index_snapshot_find(
size_t *out, git_vector *entries, git_vector_cmp entry_srch,
const char *path, size_t path_len, int stage)
{
return index_find_in_entries(out, entries, entry_srch, path, path_len, stage);
}
...@@ -61,11 +61,13 @@ extern int git_index_entry_icmp(const void *a, const void *b); ...@@ -61,11 +61,13 @@ extern int git_index_entry_icmp(const void *a, const void *b);
extern int git_index_entry_srch(const void *a, const void *b); extern int git_index_entry_srch(const void *a, const void *b);
extern int git_index_entry_isrch(const void *a, const void *b); extern int git_index_entry_isrch(const void *a, const void *b);
/* Search index for `path`, returning GIT_ENOTFOUND if it does not exist. /* Search index for `path`, returning GIT_ENOTFOUND if it does not exist
* (but not setting an error message).
*
* `at_pos` is set to the position where it is or would be inserted. * `at_pos` is set to the position where it is or would be inserted.
* Pass `path_len` as strlen of path or 0 to call strlen internally. * Pass `path_len` as strlen of path or 0 to call strlen internally.
*/ */
extern int git_index__find( extern int git_index__find_pos(
size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage); size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage);
extern void git_index__set_ignore_case(git_index *index, bool ignore_case); extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
...@@ -82,13 +84,13 @@ extern int git_index__changed_relative_to(git_index *index, const git_futils_fil ...@@ -82,13 +84,13 @@ extern int git_index__changed_relative_to(git_index *index, const git_futils_fil
/* Copy the current entries vector *and* increment the index refcount. /* Copy the current entries vector *and* increment the index refcount.
* Call `git_index__release_snapshot` when done. * Call `git_index__release_snapshot` when done.
*/ */
extern int git_index__snapshot(git_vector *entries, git_index *index); extern int git_index_snapshot_new(git_vector *snap, git_index *index);
extern void git_index__release_snapshot(git_index *index); extern void git_index_snapshot_release(git_vector *snap, git_index *index);
/* Allow searching in a snapshot; entries must already be sorted! */ /* Allow searching in a snapshot; entries must already be sorted! */
extern int git_index__find_in_entries( extern int git_index_snapshot_find(
size_t *at_pos, size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
git_vector *entries, git_vector_cmp entry_srch,
const char *path, size_t path_len, int stage); const char *path, size_t path_len, int stage);
#endif #endif
...@@ -820,7 +820,7 @@ static int index_iterator__reset( ...@@ -820,7 +820,7 @@ static int index_iterator__reset(
ii->current = 0; ii->current = 0;
if (ii->base.start) if (ii->base.start)
git_index__find_in_entries( git_index_snapshot_find(
&ii->current, &ii->entries, ii->entry_srch, ii->base.start, 0, 0); &ii->current, &ii->entries, ii->entry_srch, ii->base.start, 0, 0);
if ((ie = index_iterator__skip_conflicts(ii)) == NULL) if ((ie = index_iterator__skip_conflicts(ii)) == NULL)
...@@ -846,9 +846,8 @@ static int index_iterator__reset( ...@@ -846,9 +846,8 @@ static int index_iterator__reset(
static void index_iterator__free(git_iterator *self) static void index_iterator__free(git_iterator *self)
{ {
index_iterator *ii = (index_iterator *)self; index_iterator *ii = (index_iterator *)self;
git_index__release_snapshot(ii->index); git_index_snapshot_release(&ii->entries, ii->index);
ii->index = NULL; ii->index = NULL;
git_vector_free(&ii->entries);
git_buf_free(&ii->partial); git_buf_free(&ii->partial);
} }
...@@ -863,7 +862,7 @@ int git_iterator_for_index( ...@@ -863,7 +862,7 @@ int git_iterator_for_index(
index_iterator *ii = git__calloc(1, sizeof(index_iterator)); index_iterator *ii = git__calloc(1, sizeof(index_iterator));
GITERR_CHECK_ALLOC(ii); GITERR_CHECK_ALLOC(ii);
if ((error = git_index__snapshot(&ii->entries, index)) < 0) { if ((error = git_index_snapshot_new(&ii->entries, index)) < 0) {
git__free(ii); git__free(ii);
return error; return error;
} }
......
...@@ -445,7 +445,7 @@ static int pathspec_match_from_iterator( ...@@ -445,7 +445,7 @@ static int pathspec_match_from_iterator(
/* check if path is ignored and untracked */ /* check if path is ignored and untracked */
if (index != NULL && if (index != NULL &&
git_iterator_current_is_ignored(iter) && git_iterator_current_is_ignored(iter) &&
git_index__find(NULL, index, entry->path, 0, GIT_INDEX_STAGE_ANY) < 0) git_index__find_pos(NULL, index, entry->path, 0, GIT_INDEX_STAGE_ANY) < 0)
continue; continue;
/* mark the matched pattern as used */ /* mark the matched pattern as used */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment