http: avoid disconnecting on 404s for loose objects
[gitweb.git] / name-hash.c
index effe96db0b030ff9626a61345f296424c3641010..6d9f23e932559c58c9ebf4679a6035889f726ed2 100644 (file)
 struct dir_entry {
        struct hashmap_entry ent;
        struct dir_entry *parent;
-       struct cache_entry *ce;
        int nr;
        unsigned int namelen;
+       char name[FLEX_ARRAY];
 };
 
 static int dir_entry_cmp(const struct dir_entry *e1,
                const struct dir_entry *e2, const char *name)
 {
-       return e1->namelen != e2->namelen || strncasecmp(e1->ce->name,
-                       name ? name : e2->ce->name, e1->namelen);
+       return e1->namelen != e2->namelen || strncasecmp(e1->name,
+                       name ? name : e2->name, e1->namelen);
 }
 
 static struct dir_entry *find_dir_entry(struct index_state *istate,
@@ -41,14 +41,6 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
         * closing slash.  Despite submodules being a directory, they never
         * reach this point, because they are stored
         * in index_state.name_hash (as ordinary cache_entries).
-        *
-        * Note that the cache_entry stored with the dir_entry merely
-        * supplies the name of the directory (up to dir_entry.namelen). We
-        * track the number of 'active' files in a directory in dir_entry.nr,
-        * so we can tell if the directory is still relevant, e.g. for git
-        * status. However, if cache_entries are removed, we cannot pinpoint
-        * an exact cache_entry that's still active. It is very possible that
-        * multiple dir_entries point to the same cache_entry.
         */
        struct dir_entry *dir;
 
@@ -63,10 +55,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
        dir = find_dir_entry(istate, ce->name, namelen);
        if (!dir) {
                /* not found, create it and add to hash table */
-               dir = xcalloc(1, sizeof(struct dir_entry));
+               FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
                hashmap_entry_init(dir, memihash(ce->name, namelen));
                dir->namelen = namelen;
-               dir->ce = ce;
                hashmap_add(&istate->dir_hash, dir);
 
                /* recursively add missing parent directories */
@@ -100,32 +91,35 @@ static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
 
 static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
 {
-       void **pos;
-       unsigned int hash;
-
        if (ce->ce_flags & CE_HASHED)
                return;
        ce->ce_flags |= CE_HASHED;
-       ce->next = NULL;
-       hash = memihash(ce->name, ce_namelen(ce));
-       pos = insert_hash(hash, ce, &istate->name_hash);
-       if (pos) {
-               ce->next = *pos;
-               *pos = ce;
-       }
+       hashmap_entry_init(ce, memihash(ce->name, ce_namelen(ce)));
+       hashmap_add(&istate->name_hash, ce);
 
-       if (ignore_case && !(ce->ce_flags & CE_UNHASHED))
+       if (ignore_case)
                add_dir_entry(istate, ce);
 }
 
+static int cache_entry_cmp(const struct cache_entry *ce1,
+               const struct cache_entry *ce2, const void *remove)
+{
+       /*
+        * For remove_name_hash, find the exact entry (pointer equality); for
+        * index_file_exists, find all entries with matching hash code and
+        * decide whether the entry matches in same_name.
+        */
+       return remove ? !(ce1 == ce2) : 0;
+}
+
 static void lazy_init_name_hash(struct index_state *istate)
 {
        int nr;
 
        if (istate->name_hash_initialized)
                return;
-       if (istate->cache_nr)
-               preallocate_hash(&istate->name_hash, istate->cache_nr);
+       hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
+                       istate->cache_nr);
        hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp, 0);
        for (nr = 0; nr < istate->cache_nr; nr++)
                hash_index_entry(istate, istate->cache[nr]);
@@ -134,31 +128,19 @@ static void lazy_init_name_hash(struct index_state *istate)
 
 void add_name_hash(struct index_state *istate, struct cache_entry *ce)
 {
-       /* if already hashed, add reference to directory entries */
-       if (ignore_case && (ce->ce_flags & CE_STATE_MASK) == CE_STATE_MASK)
-               add_dir_entry(istate, ce);
-
-       ce->ce_flags &= ~CE_UNHASHED;
        if (istate->name_hash_initialized)
                hash_index_entry(istate, ce);
 }
 
-/*
- * We don't actually *remove* it, we can just mark it invalid so that
- * we won't find it in lookups.
- *
- * Not only would we have to search the lists (simple enough), but
- * we'd also have to rehash other hash buckets in case this makes the
- * hash bucket empty (common). So it's much better to just mark
- * it.
- */
 void remove_name_hash(struct index_state *istate, struct cache_entry *ce)
 {
-       /* if already hashed, release reference to directory entries */
-       if (ignore_case && (ce->ce_flags & CE_STATE_MASK) == CE_HASHED)
-               remove_dir_entry(istate, ce);
+       if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED))
+               return;
+       ce->ce_flags &= ~CE_HASHED;
+       hashmap_remove(&istate->name_hash, ce, ce);
 
-       ce->ce_flags |= CE_UNHASHED;
+       if (ignore_case)
+               remove_dir_entry(istate, ce);
 }
 
 static int slow_same_name(const char *name1, int len1, const char *name2, int len2)
@@ -188,7 +170,7 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
         * Always do exact compare, even if we want a case-ignoring comparison;
         * we do the quick exact one first, because it will be the common case.
         */
-       if (len == namelen && !cache_name_compare(name, namelen, ce->name, len))
+       if (len == namelen && !memcmp(name, ce->name, len))
                return 1;
 
        if (!icase)
@@ -197,59 +179,60 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
        return slow_same_name(name, namelen, ce->name, len);
 }
 
-struct cache_entry *index_dir_exists(struct index_state *istate, const char *name, int namelen)
+int index_dir_exists(struct index_state *istate, const char *name, int namelen)
 {
-       struct cache_entry *ce;
        struct dir_entry *dir;
 
        lazy_init_name_hash(istate);
        dir = find_dir_entry(istate, name, namelen);
-       if (dir && dir->nr)
-               return dir->ce;
+       return dir && dir->nr;
+}
 
-       /*
-        * It might be a submodule. Unlike plain directories, which are stored
-        * in the dir-hash, submodules are stored in the name-hash, so check
-        * there, as well.
-        */
-       ce = index_file_exists(istate, name, namelen, 1);
-       if (ce && S_ISGITLINK(ce->ce_mode))
-               return ce;
+void adjust_dirname_case(struct index_state *istate, char *name)
+{
+       const char *startPtr = name;
+       const char *ptr = startPtr;
 
-       return NULL;
+       lazy_init_name_hash(istate);
+       while (*ptr) {
+               while (*ptr && *ptr != '/')
+                       ptr++;
+
+               if (*ptr == '/') {
+                       struct dir_entry *dir;
+
+                       ptr++;
+                       dir = find_dir_entry(istate, name, ptr - name + 1);
+                       if (dir) {
+                               memcpy((void *)startPtr, dir->name + (startPtr - name), ptr - startPtr);
+                               startPtr = ptr;
+                       }
+               }
+       }
 }
 
 struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
 {
-       unsigned int hash = memihash(name, namelen);
        struct cache_entry *ce;
 
        lazy_init_name_hash(istate);
-       ce = lookup_hash(hash, &istate->name_hash);
 
+       ce = hashmap_get_from_hash(&istate->name_hash,
+                                  memihash(name, namelen), NULL);
        while (ce) {
-               if (!(ce->ce_flags & CE_UNHASHED)) {
-                       if (same_name(ce, name, namelen, icase))
-                               return ce;
-               }
-               ce = ce->next;
+               if (same_name(ce, name, namelen, icase))
+                       return ce;
+               ce = hashmap_get_next(&istate->name_hash, ce);
        }
        return NULL;
 }
 
-struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
-{
-       if (namelen > 0 && name[namelen - 1] == '/')
-               return index_dir_exists(istate, name, namelen - 1);
-       return index_file_exists(istate, name, namelen, icase);
-}
-
 void free_name_hash(struct index_state *istate)
 {
        if (!istate->name_hash_initialized)
                return;
        istate->name_hash_initialized = 0;
 
-       free_hash(&istate->name_hash);
+       hashmap_free(&istate->name_hash, 0);
        hashmap_free(&istate->dir_hash, 1);
 }