vendor/libgit2/src/attrcache.c in rugged-0.28.4 vs vendor/libgit2/src/attrcache.c in rugged-0.28.4.1

- old
+ new

@@ -31,11 +31,16 @@ } GIT_INLINE(git_attr_file_entry *) attr_cache_lookup_entry( git_attr_cache *cache, const char *path) { - return git_strmap_get(cache->files, path); + size_t pos = git_strmap_lookup_index(cache->files, path); + + if (git_strmap_valid_index(cache->files, pos)) + return git_strmap_value_at(cache->files, pos); + else + return NULL; } int git_attr_cache__alloc_file_entry( git_attr_file_entry **out, const char *base, @@ -52,11 +57,11 @@ if (baselen && base[baselen - 1] != '/') cachesize++; } - ce = git_pool_mallocz(pool, cachesize); + ce = git_pool_mallocz(pool, (uint32_t)cachesize); GIT_ERROR_CHECK_ALLOC(ce); if (baselen) { memcpy(ce->fullpath, base, baselen); @@ -73,20 +78,22 @@ /* call with attrcache locked */ static int attr_cache_make_entry( git_attr_file_entry **out, git_repository *repo, const char *path) { + int error = 0; git_attr_cache *cache = git_repository_attr_cache(repo); git_attr_file_entry *entry = NULL; - int error; - if ((error = git_attr_cache__alloc_file_entry(&entry, git_repository_workdir(repo), - path, &cache->pool)) < 0) - return error; + error = git_attr_cache__alloc_file_entry( + &entry, git_repository_workdir(repo), path, &cache->pool); - if ((error = git_strmap_set(cache->files, entry->path, entry)) < 0) - return error; + if (!error) { + git_strmap_insert(cache->files, entry->path, entry, &error); + if (error > 0) + error = 0; + } *out = entry; return error; } @@ -206,12 +213,11 @@ git_repository *repo, git_attr_session *attr_session, git_attr_file_source source, const char *base, const char *filename, - git_attr_file_parser parser, - bool allow_macros) + git_attr_file_parser parser) { int error = 0; git_attr_cache *cache = git_repository_attr_cache(repo); git_attr_file_entry *entry = NULL; git_attr_file *file = NULL, *updated = NULL; @@ -220,11 +226,11 @@ &file, &entry, repo, attr_session, source, base, filename)) < 0) return error; /* load file if we don't have one or if existing one is out of date */ if (!file || (error = git_attr_file__out_of_date(repo, attr_session, file)) > 0) - error = git_attr_file__load(&updated, repo, attr_session, entry, source, parser, allow_macros); + error = git_attr_file__load(&updated, repo, attr_session, entry, source, parser); /* if we loaded the file, insert into and/or update cache */ if (updated) { if ((error = attr_cache_upsert(cache, updated)) < 0) git_attr_file__free(updated); @@ -257,19 +263,23 @@ git_repository *repo, git_attr_file_source source, const char *filename) { git_attr_cache *cache = git_repository_attr_cache(repo); - git_attr_file_entry *entry; git_strmap *files; + size_t pos; + git_attr_file_entry *entry; if (!cache || !(files = cache->files)) return false; - if ((entry = git_strmap_get(files, filename)) == NULL) + pos = git_strmap_lookup_index(files, filename); + if (!git_strmap_valid_index(files, pos)) return false; + entry = git_strmap_value_at(files, pos); + return entry && (entry->file[source] != NULL); } static int attr_cache__lookup_path( @@ -388,12 +398,12 @@ goto cancel; /* allocate hashtable for attribute and ignore file contents, * hashtable for attribute macros, and string pool */ - if ((ret = git_strmap_new(&cache->files)) < 0 || - (ret = git_strmap_new(&cache->macros)) < 0) + if ((ret = git_strmap_alloc(&cache->files)) < 0 || + (ret = git_strmap_alloc(&cache->macros)) < 0) goto cancel; git_pool_init(&cache->pool, 1); cache = git__compare_and_swap(&repo->attrcache, NULL, cache); @@ -423,45 +433,37 @@ } int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro) { git_attr_cache *cache = git_repository_attr_cache(repo); - git_attr_rule *preexisting; - bool locked = false; - int error = 0; + git_strmap *macros = cache->macros; + int error; - /* - * Callers assume that if we return success, that the - * macro will have been adopted by the attributes cache. - * Thus, we have to free the macro here if it's not being - * added to the cache. - * - * TODO: generate warning log if (macro->assigns.length == 0) - */ - if (macro->assigns.length == 0) { - git_attr_rule__free(macro); - goto out; + /* TODO: generate warning log if (macro->assigns.length == 0) */ + if (macro->assigns.length == 0) + return 0; + + if (attr_cache_lock(cache) < 0) { + git_error_set(GIT_ERROR_OS, "unable to get attr cache lock"); + error = -1; + } else { + git_strmap_insert(macros, macro->match.pattern, macro, &error); + git_mutex_unlock(&cache->lock); } - if ((error = attr_cache_lock(cache)) < 0) - goto out; - locked = true; - - if ((preexisting = git_strmap_get(cache->macros, macro->match.pattern)) != NULL) - git_attr_rule__free(preexisting); - - if ((error = git_strmap_set(cache->macros, macro->match.pattern, macro)) < 0) - goto out; - -out: - if (locked) - attr_cache_unlock(cache); - return error; + return (error < 0) ? -1 : 0; } git_attr_rule *git_attr_cache__lookup_macro( git_repository *repo, const char *name) { git_strmap *macros = git_repository_attr_cache(repo)->macros; + size_t pos; - return git_strmap_get(macros, name); + pos = git_strmap_lookup_index(macros, name); + + if (!git_strmap_valid_index(macros, pos)) + return NULL; + + return (git_attr_rule *)git_strmap_value_at(macros, pos); } +