Fix tests with new attr cache code
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
diff --git a/src/attr.c b/src/attr.c
index c53a728..2e31499 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -60,6 +60,7 @@ int git_attr_get(
if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0)
goto cleanup;
+ memset(&attr, 0, sizeof(attr));
attr.name = name;
attr.name_hash = git_attr_file__name_hash(name);
@@ -295,11 +296,15 @@ static int push_attr_file(
int error = 0;
git_attr_file *file = NULL;
- if ((error = git_attr_cache__get(
- &file, repo, source, base, filename,
- git_attr_file__parse_buffer, NULL)) < 0 ||
- (error = git_vector_insert(list, file)) < 0)
- git_attr_file__free(file);
+ error = git_attr_cache__get(
+ &file, repo, source, base, filename, git_attr_file__parse_buffer, NULL);
+ if (error < 0)
+ return error;
+
+ if (file != NULL) {
+ if ((error = git_vector_insert(list, file)) < 0)
+ git_attr_file__free(file);
+ }
return error;
}
@@ -343,9 +348,8 @@ static int collect_attr_files(
const char *workdir = git_repository_workdir(repo);
attr_walk_up_info info = { NULL };
- if (git_attr_cache__init(repo) < 0 ||
- git_vector_init(files, 4, NULL) < 0)
- return -1;
+ if ((error = git_attr_cache__init(repo)) < 0)
+ return error;
/* Resolve path in a non-bare repo */
if (workdir != NULL)
diff --git a/src/attr_file.c b/src/attr_file.c
index 86b3448..66f71ad 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -23,9 +23,7 @@ int git_attr_file__new(
git_attr_file *attrs = git__calloc(1, sizeof(git_attr_file));
GITERR_CHECK_ALLOC(attrs);
- if (git_pool_init(&attrs->pool, 1, 0) < 0 ||
- git_vector_init(&attrs->rules, 0, NULL) < 0)
- {
+ if (git_pool_init(&attrs->pool, 1, 0) < 0) {
attr_file_free(attrs);
return -1;
}
diff --git a/src/attrcache.c b/src/attrcache.c
index 6d09723..a7dc0c8 100644
--- a/src/attrcache.c
+++ b/src/attrcache.c
@@ -41,16 +41,29 @@ int git_attr_cache_entry__new(
const char *path,
git_pool *pool)
{
- size_t baselen = base ? strlen(base) : 0, pathlen = strlen(path);
- size_t cachesize = sizeof(git_attr_cache_entry) + baselen + pathlen + 1;
+ size_t baselen = 0, pathlen = strlen(path);
+ size_t cachesize = sizeof(git_attr_cache_entry) + pathlen + 1;
git_attr_cache_entry *ce;
+ if (base != NULL && git_path_root(path) < 0) {
+ baselen = strlen(base);
+ cachesize += baselen;
+
+ if (baselen && base[baselen - 1] != '/')
+ cachesize++;
+ }
+
ce = git_pool_mallocz(pool, cachesize);
GITERR_CHECK_ALLOC(ce);
- if (baselen)
+ if (baselen) {
memcpy(ce->fullpath, base, baselen);
+
+ if (base[baselen - 1] != '/')
+ ce->fullpath[baselen++] = '/';
+ }
memcpy(&ce->fullpath[baselen], path, pathlen);
+
ce->path = &ce->fullpath[baselen];
*out = ce;
@@ -119,6 +132,7 @@ static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
ce->file[file->source] == file)
{
ce->file[file->source] = NULL;
+ GIT_REFCOUNT_OWN(file, NULL);
found = true;
}
@@ -130,14 +144,13 @@ static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
return error;
}
-int git_attr_cache__get(
- git_attr_file **out,
+static int attr_cache_lookup(
+ git_attr_file **out_file,
+ git_attr_cache_entry **out_ce,
git_repository *repo,
git_attr_cache_source source,
const char *base,
- const char *filename,
- git_attr_cache_parser parser,
- void *payload)
+ const char *filename)
{
int error = 0;
git_buf path = GIT_BUF_INIT;
@@ -172,36 +185,61 @@ int git_attr_cache__get(
attr_cache_unlock(cache);
+cleanup:
+ *out_file = file;
+ *out_ce = ce;
+
+ git_buf_free(&path);
+ return error;
+}
+
+int git_attr_cache__get(
+ git_attr_file **out,
+ git_repository *repo,
+ git_attr_cache_source source,
+ const char *base,
+ const char *filename,
+ git_attr_cache_parser parser,
+ void *payload)
+{
+ int error = 0;
+ git_attr_cache *cache = git_repository_attr_cache(repo);
+ git_attr_cache_entry *ce = NULL;
+ git_attr_file *file = NULL;
+
+ if ((error = attr_cache_lookup(&file, &ce, repo, source, base, filename)) < 0)
+ goto cleanup;
+
/* if this is not a file backed entry, just create a new empty one */
if (!parser) {
- error = git_attr_file__new(&file, ce, source);
- goto cleanup;
+ if (!file && !(error = git_attr_file__new(&file, ce, source)))
+ error = attr_cache_upsert(cache, file);
}
-
/* otherwise load and/or reload as needed */
- switch (git_attr_file__out_of_date(repo, file)) {
- case 1:
+ else if (!file || (error = git_attr_file__out_of_date(repo, file)) > 0) {
if (!(error = git_attr_file__load(
- &file, repo, ce, source, parser, payload)))
+ &file, repo, ce, source, parser, payload)))
error = attr_cache_upsert(cache, file);
- break;
- case 0:
- /* just use the file */
- break;
- case GIT_ENOTFOUND:
- /* did exist and now does not - remove from cache */
- error = attr_cache_remove(cache, file);
- file = NULL;
- break;
- default:
- /* other error (e.g. out of memory, can't read index) */
+ }
+
+ /* GIT_ENOTFOUND is okay when probing for the file. If the file did
+ * exist and now does not, we have to remove it from cache, however.
+ */
+ if (error == GIT_ENOTFOUND) {
giterr_clear();
- break;
+ error = 0;
+
+ if (file != NULL)
+ error = attr_cache_remove(cache, file);
}
cleanup:
- *out = error ? NULL : file;
- git_buf_free(&path);
+ if (error < 0 && file != NULL) {
+ git_attr_file__free(file);
+ file = NULL;
+ }
+ *out = file;
+
return error;
}
@@ -250,7 +288,6 @@ static int attr_cache__lookup_path(
*out = git_buf_detach(&buf);
else if (cfgval)
*out = git__strdup(cfgval);
-
}
else if (!git_sysdir_find_xdg_file(&buf, fallback))
*out = git_buf_detach(&buf);
@@ -262,14 +299,24 @@ static int attr_cache__lookup_path(
static void attr_cache__free(git_attr_cache *cache)
{
+ bool unlock;
+
if (!cache)
return;
- if (cache->files != NULL) {
- git_attr_file *file;
+ unlock = (git_mutex_lock(&cache->lock) == 0);
- git_strmap_foreach_value(cache->files, file, {
- git_attr_file__free(file);
+ if (cache->files != NULL) {
+ git_attr_cache_entry *ce;
+ int i;
+
+ git_strmap_foreach_value(cache->files, ce, {
+ for (i = 0; i < GIT_ATTR_CACHE_NUM_SOURCES; ++i) {
+ if (ce->file[i]) {
+ GIT_REFCOUNT_OWN(ce->file[i], NULL);
+ git_attr_file__free(ce->file[i]);
+ }
+ }
});
git_strmap_free(cache->files);
}
@@ -291,6 +338,8 @@ static void attr_cache__free(git_attr_cache *cache)
git__free(cache->cfg_excl_file);
cache->cfg_excl_file = NULL;
+ if (unlock)
+ git_mutex_unlock(&cache->lock);
git_mutex_free(&cache->lock);
git__free(cache);
diff --git a/src/ignore.c b/src/ignore.c
index 3ee7ba0..1f9df8a 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -77,11 +77,16 @@ static int push_ignore_file(
int error = 0;
git_attr_file *file = NULL;
- if ((error = git_attr_cache__get(
- &file, ignores->repo, GIT_ATTR_CACHE__FROM_FILE,
- base, filename, parse_ignore_file, ignores)) < 0 ||
- (error = git_vector_insert(which_list, file)) < 0)
- git_attr_file__free(file);
+ error = git_attr_cache__get(
+ &file, ignores->repo, GIT_ATTR_CACHE__FROM_FILE,
+ base, filename, parse_ignore_file, ignores);
+ if (error < 0)
+ return error;
+
+ if (file != NULL) {
+ if ((error = git_vector_insert(which_list, file)) < 0)
+ git_attr_file__free(file);
+ }
return error;
}
@@ -122,19 +127,15 @@ int git_ignore__for_path(
assert(ignores);
+ memset(ignores, 0, sizeof(*ignores));
ignores->repo = repo;
- git_buf_init(&ignores->dir, 0);
- ignores->ign_internal = NULL;
- ignores->depth = 0;
/* Read the ignore_case flag */
if ((error = git_repository__cvar(
&ignores->ignore_case, repo, GIT_CVAR_IGNORECASE)) < 0)
goto cleanup;
- if ((error = git_vector_init(&ignores->ign_path, 8, NULL)) < 0 ||
- (error = git_vector_init(&ignores->ign_global, 2, NULL)) < 0 ||
- (error = git_attr_cache__init(repo)) < 0)
+ if ((error = git_attr_cache__init(repo)) < 0)
goto cleanup;
/* given a unrooted path in a non-bare repo, resolve it */
@@ -304,7 +305,7 @@ int git_ignore_add_rule(
git_attr_file *ign_internal = NULL;
if (!(error = get_internal_ignores(&ign_internal, repo))) {
- error = parse_ignore_file(repo, NULL, rules, ign_internal);
+ error = parse_ignore_file(repo, ign_internal, rules, NULL);
git_attr_file__free(ign_internal);
}
@@ -321,7 +322,7 @@ int git_ignore_clear_internal_rules(
git_attr_file__clear_rules(ign_internal);
error = parse_ignore_file(
- repo, NULL, GIT_IGNORE_DEFAULT_RULES, ign_internal);
+ repo, ign_internal, GIT_IGNORE_DEFAULT_RULES, NULL);
git_attr_file__free(ign_internal);
}