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.
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
diff --git a/src/checkout.c b/src/checkout.c
index a251c08..0b38522 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -279,17 +279,17 @@ static int checkout_action_wd_only(
if (data->index != NULL) {
size_t pos;
- error = git_index__find(
+ error = git_index__find_pos(
&pos, data->index, wd->path, 0, GIT_INDEX_STAGE_ANY);
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;
remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
} else if (error != GIT_ENOTFOUND)
return error;
else
- error = 0; /* git_index__find does not set error msg */
+ error = 0; /* git_index__find_pos does not set error msg */
} else {
/* for tree entries, we have to see if there are any index
* entries that are contained inside that tree
diff --git a/src/index.c b/src/index.c
index 094328b..10fb1fe 100644
--- a/src/index.c
+++ b/src/index.c
@@ -342,6 +342,28 @@ static int index_sort_if_needed(git_index *index, bool need_lock)
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)
{
index->ignore_case = ignore_case;
@@ -687,16 +709,6 @@ size_t git_index_entrycount(const git_index *index)
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(
git_index *index, size_t n)
{
@@ -713,7 +725,7 @@ const git_index_entry *git_index_get_bypath(
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);
return NULL;
}
@@ -896,7 +908,7 @@ static int has_dir_name(git_index *index,
}
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;
if (!ok_to_replace)
break;
@@ -976,7 +988,7 @@ static int index_insert(
git_vector_sort(&index->entries);
/* 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)) {
existing = index->entries.contents[position];
/* 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)
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_INDEX, "Index does not contain %s at stage %d", path, stage);
error = GIT_ENOTFOUND;
@@ -1124,8 +1136,7 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage)
if (!(error = git_buf_sets(&pfx, dir)) &&
!(error = git_path_to_dir(&pfx)))
- git_index__find_internal(
- &pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY, false);
+ index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY, false);
while (!error) {
entry = git_vector_get(&index->entries, pos);
@@ -1148,22 +1159,11 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage)
return error;
}
-int git_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);
-}
-
-int git_index__find(
+int git_index__find_pos(
size_t *out, git_index *index, const char *path, size_t path_len, int stage)
{
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)
@@ -1177,7 +1177,8 @@ int git_index_find(size_t *at_pos, git_index *index, const char *path)
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);
giterr_set(GITERR_INDEX, "Index does not contain %s", path);
return GIT_ENOTFOUND;
@@ -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
* middle of a list of stages.
*/
- while (pos > 0) {
- const git_index_entry *prev = git_vector_get(&index->entries, pos-1);
+ for (; pos > 0; --pos) {
+ const git_index_entry *prev = git_vector_get(&index->entries, pos - 1);
if (index->entries_cmp_path(prev->path, path) != 0)
break;
-
- --pos;
}
if (at_pos)
@@ -2269,7 +2268,7 @@ static int read_tree_cb(
/* look for corresponding old entry and copy data to new entry */
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) &&
(old_entry = git_vector_get(data->old_entries, pos)) != NULL &&
entry->mode == old_entry->mode &&
@@ -2394,7 +2393,7 @@ int git_index_add_all(
/* skip ignored items that are not already in the index */
if ((flags & GIT_INDEX_ADD_FORCE) == 0 &&
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;
/* issue notification callback if requested */
@@ -2556,7 +2555,7 @@ int git_index_update_all(
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;
@@ -2570,7 +2569,7 @@ int git_index__snapshot(git_vector *entries, git_index *index)
git_atomic_inc(&index->readers);
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);
@@ -2580,8 +2579,10 @@ int git_index__snapshot(git_vector *entries, git_index *index)
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);
if (!git_mutex_lock(&index->lock)) {
@@ -2591,3 +2592,10 @@ void git_index__release_snapshot(git_index *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);
+}
diff --git a/src/index.h b/src/index.h
index cb44258..50a0b4b 100644
--- a/src/index.h
+++ b/src/index.h
@@ -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_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.
* 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);
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
/* Copy the current entries vector *and* increment the index refcount.
* Call `git_index__release_snapshot` when done.
*/
-extern int git_index__snapshot(git_vector *entries, git_index *index);
-extern void git_index__release_snapshot(git_index *index);
+extern int git_index_snapshot_new(git_vector *snap, 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! */
-extern int git_index__find_in_entries(
- size_t *at_pos,
- git_vector *entries, git_vector_cmp entry_srch,
+extern int git_index_snapshot_find(
+ size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
const char *path, size_t path_len, int stage);
+
#endif
diff --git a/src/iterator.c b/src/iterator.c
index 53ef278..63c14f9 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -820,7 +820,7 @@ static int index_iterator__reset(
ii->current = 0;
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);
if ((ie = index_iterator__skip_conflicts(ii)) == NULL)
@@ -846,9 +846,8 @@ static int index_iterator__reset(
static void index_iterator__free(git_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;
- git_vector_free(&ii->entries);
git_buf_free(&ii->partial);
}
@@ -863,7 +862,7 @@ int git_iterator_for_index(
index_iterator *ii = git__calloc(1, sizeof(index_iterator));
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);
return error;
}
diff --git a/src/pathspec.c b/src/pathspec.c
index 4714884..09650de 100644
--- a/src/pathspec.c
+++ b/src/pathspec.c
@@ -445,7 +445,7 @@ static int pathspec_match_from_iterator(
/* check if path is ignored and untracked */
if (index != NULL &&
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;
/* mark the matched pattern as used */