diff options
Diffstat (limited to 'attr.c')
-rw-r--r-- | attr.c | 97 |
1 files changed, 56 insertions, 41 deletions
@@ -9,7 +9,8 @@ #define NO_THE_INDEX_COMPATIBILITY_MACROS #include "cache.h" -#include "exec_cmd.h" +#include "config.h" +#include "exec-cmd.h" #include "attr.h" #include "dir.h" #include "utf8.h" @@ -75,17 +76,20 @@ struct attr_hash_entry { }; /* attr_hashmap comparison function */ -static int attr_hash_entry_cmp(const struct attr_hash_entry *a, - const struct attr_hash_entry *b, - void *unused) +static int attr_hash_entry_cmp(const void *unused_cmp_data, + const void *entry, + const void *entry_or_key, + const void *unused_keydata) { + const struct attr_hash_entry *a = entry; + const struct attr_hash_entry *b = entry_or_key; return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen); } /* Initialize an 'attr_hashmap' object */ static void attr_hashmap_init(struct attr_hashmap *map) { - hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, 0); + hashmap_init(&map->map, attr_hash_entry_cmp, NULL, 0); } /* @@ -147,11 +151,13 @@ struct all_attrs_item { static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check) { int i; + unsigned int size; hashmap_lock(map); - if (map->map.size < check->all_attrs_nr) - die("BUG: interned attributes shouldn't be deleted"); + size = hashmap_get_size(&map->map); + if (size < check->all_attrs_nr) + BUG("interned attributes shouldn't be deleted"); /* * If the number of attributes in the global dictionary has increased @@ -159,13 +165,13 @@ static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check) * field), reallocate the provided attr_check instance's all_attrs * field and fill each entry with its corresponding git_attr. */ - if (map->map.size != check->all_attrs_nr) { + if (size != check->all_attrs_nr) { struct attr_hash_entry *e; struct hashmap_iter iter; hashmap_iter_init(&map->map, &iter); - REALLOC_ARRAY(check->all_attrs, map->map.size); - check->all_attrs_nr = map->map.size; + REALLOC_ARRAY(check->all_attrs, size); + check->all_attrs_nr = size; while ((e = hashmap_iter_next(&iter))) { const struct git_attr *a = e->value; @@ -233,10 +239,11 @@ static const struct git_attr *git_attr_internal(const char *name, int namelen) if (!a) { FLEX_ALLOC_MEM(a, name, name, namelen); - a->attr_nr = g_attr_hashmap.map.size; + a->attr_nr = hashmap_get_size(&g_attr_hashmap.map); attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a); - assert(a->attr_nr == (g_attr_hashmap.map.size - 1)); + assert(a->attr_nr == + (hashmap_get_size(&g_attr_hashmap.map) - 1)); } hashmap_unlock(&g_attr_hashmap); @@ -534,7 +541,7 @@ static void check_vector_remove(struct attr_check *check) break; if (i >= check_vector.nr) - die("BUG: no entry found"); + BUG("no entry found"); /* shift entries over */ for (; i < check_vector.nr - 1; i++) @@ -592,11 +599,11 @@ struct attr_check *attr_check_initl(const char *one, ...) const struct git_attr *attr; param = va_arg(params, const char *); if (!param) - die("BUG: counted %d != ended at %d", + BUG("counted %d != ended at %d", check->nr, cnt); attr = git_attr(param); if (!attr) - die("BUG: %s: not a valid attribute name", param); + BUG("%s: not a valid attribute name", param); check->items[cnt].attr = attr; } va_end(params); @@ -638,13 +645,11 @@ void attr_check_reset(struct attr_check *check) void attr_check_clear(struct attr_check *check) { - free(check->items); - check->items = NULL; + FREE_AND_NULL(check->items); check->alloc = 0; check->nr = 0; - free(check->all_attrs); - check->all_attrs = NULL; + FREE_AND_NULL(check->all_attrs); check->all_attrs_nr = 0; drop_attr_stack(&check->stack); @@ -703,19 +708,16 @@ static struct attr_stack *read_attr_from_array(const char **list) * another thread could potentially be calling into the attribute system. */ static enum git_attr_direction direction; -static struct index_state *use_index; -void git_attr_set_direction(enum git_attr_direction new_direction, - struct index_state *istate) +void git_attr_set_direction(enum git_attr_direction new_direction) { if (is_bare_repository() && new_direction != GIT_ATTR_INDEX) - die("BUG: non-INDEX attr direction in a bare repo"); + BUG("non-INDEX attr direction in a bare repo"); if (new_direction != direction) drop_all_attr_stacks(); direction = new_direction; - use_index = istate; } static struct attr_stack *read_attr_from_file(const char *path, int macro_ok) @@ -738,13 +740,18 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok) return res; } -static struct attr_stack *read_attr_from_index(const char *path, int macro_ok) +static struct attr_stack *read_attr_from_index(const struct index_state *istate, + const char *path, + int macro_ok) { struct attr_stack *res; char *buf, *sp; int lineno = 0; - buf = read_blob_data_from_index(use_index ? use_index : &the_index, path, NULL); + if (!istate) + return NULL; + + buf = read_blob_data_from_index(istate, path, NULL); if (!buf) return NULL; @@ -763,15 +770,16 @@ static struct attr_stack *read_attr_from_index(const char *path, int macro_ok) return res; } -static struct attr_stack *read_attr(const char *path, int macro_ok) +static struct attr_stack *read_attr(const struct index_state *istate, + const char *path, int macro_ok) { struct attr_stack *res = NULL; if (direction == GIT_ATTR_INDEX) { - res = read_attr_from_index(path, macro_ok); + res = read_attr_from_index(istate, path, macro_ok); } else if (!is_bare_repository()) { if (direction == GIT_ATTR_CHECKOUT) { - res = read_attr_from_index(path, macro_ok); + res = read_attr_from_index(istate, path, macro_ok); if (!res) res = read_attr_from_file(path, macro_ok); } else if (direction == GIT_ATTR_CHECKIN) { @@ -783,7 +791,7 @@ static struct attr_stack *read_attr(const char *path, int macro_ok) * We allow operation in a sparsely checked out * work tree, so read from it. */ - res = read_attr_from_index(path, macro_ok); + res = read_attr_from_index(istate, path, macro_ok); } } @@ -854,7 +862,8 @@ static void push_stack(struct attr_stack **attr_stack_p, } } -static void bootstrap_attr_stack(struct attr_stack **stack) +static void bootstrap_attr_stack(const struct index_state *istate, + struct attr_stack **stack) { struct attr_stack *e; @@ -878,7 +887,7 @@ static void bootstrap_attr_stack(struct attr_stack **stack) } /* root directory */ - e = read_attr(GITATTRIBUTES_FILE, 1); + e = read_attr(istate, GITATTRIBUTES_FILE, 1); push_stack(stack, e, xstrdup(""), 0); /* info frame */ @@ -891,7 +900,8 @@ static void bootstrap_attr_stack(struct attr_stack **stack) push_stack(stack, e, NULL, 0); } -static void prepare_attr_stack(const char *path, int dirlen, +static void prepare_attr_stack(const struct index_state *istate, + const char *path, int dirlen, struct attr_stack **stack) { struct attr_stack *info; @@ -912,7 +922,7 @@ static void prepare_attr_stack(const char *path, int dirlen, * .gitattributes in deeper directories to shallower ones, * and finally use the built-in set as the default. */ - bootstrap_attr_stack(stack); + bootstrap_attr_stack(istate, stack); /* * Pop the "info" one that is always at the top of the stack. @@ -968,7 +978,7 @@ static void prepare_attr_stack(const char *path, int dirlen, strbuf_add(&pathbuf, path + pathbuf.len, (len - pathbuf.len)); strbuf_addf(&pathbuf, "/%s", GITATTRIBUTES_FILE); - next = read_attr(pathbuf.buf, 0); + next = read_attr(istate, pathbuf.buf, 0); /* reset the pathbuf to not include "/.gitattributes" */ strbuf_setlen(&pathbuf, len); @@ -1090,7 +1100,9 @@ static void determine_macros(struct all_attrs_item *all_attrs, * If check->check_nr is non-zero, only attributes in check[] are collected. * Otherwise all attributes are collected. */ -static void collect_some_attrs(const char *path, struct attr_check *check) +static void collect_some_attrs(const struct index_state *istate, + const char *path, + struct attr_check *check) { int i, pathlen, rem, dirlen; const char *cp, *last_slash = NULL; @@ -1109,7 +1121,7 @@ static void collect_some_attrs(const char *path, struct attr_check *check) dirlen = 0; } - prepare_attr_stack(path, dirlen, &check->stack); + prepare_attr_stack(istate, path, dirlen, &check->stack); all_attrs_init(&g_attr_hashmap, check); determine_macros(check->all_attrs, check->stack); @@ -1131,11 +1143,13 @@ static void collect_some_attrs(const char *path, struct attr_check *check) fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem); } -int git_check_attr(const char *path, struct attr_check *check) +int git_check_attr(const struct index_state *istate, + const char *path, + struct attr_check *check) { int i; - collect_some_attrs(path, check); + collect_some_attrs(istate, path, check); for (i = 0; i < check->nr; i++) { size_t n = check->items[i].attr->attr_nr; @@ -1148,12 +1162,13 @@ int git_check_attr(const char *path, struct attr_check *check) return 0; } -void git_all_attrs(const char *path, struct attr_check *check) +void git_all_attrs(const struct index_state *istate, + const char *path, struct attr_check *check) { int i; attr_check_reset(check); - collect_some_attrs(path, check); + collect_some_attrs(istate, path, check); for (i = 0; i < check->all_attrs_nr; i++) { const char *name = check->all_attrs[i].attr->name; |