2008-03-21 21:16:24 +01:00
|
|
|
/*
|
|
|
|
* name-hash.c
|
|
|
|
*
|
|
|
|
* Hashing names in the index state
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Linus Torvalds
|
|
|
|
*/
|
|
|
|
#define NO_THE_INDEX_COMPATIBILITY_MACROS
|
|
|
|
#include "cache.h"
|
|
|
|
|
2013-02-28 00:57:48 +01:00
|
|
|
struct dir_entry {
|
2013-11-14 20:20:58 +01:00
|
|
|
struct hashmap_entry ent;
|
2013-02-28 00:57:48 +01:00
|
|
|
struct dir_entry *parent;
|
|
|
|
int nr;
|
|
|
|
unsigned int namelen;
|
2015-10-21 19:54:11 +02:00
|
|
|
char name[FLEX_ARRAY];
|
2013-02-28 00:57:48 +01:00
|
|
|
};
|
|
|
|
|
2013-11-14 20:20:58 +01:00
|
|
|
static int dir_entry_cmp(const struct dir_entry *e1,
|
|
|
|
const struct dir_entry *e2, const char *name)
|
|
|
|
{
|
2015-10-21 19:54:11 +02:00
|
|
|
return e1->namelen != e2->namelen || strncasecmp(e1->name,
|
|
|
|
name ? name : e2->name, e1->namelen);
|
2013-11-14 20:20:58 +01:00
|
|
|
}
|
|
|
|
|
2013-02-28 00:57:48 +01:00
|
|
|
static struct dir_entry *find_dir_entry(struct index_state *istate,
|
|
|
|
const char *name, unsigned int namelen)
|
|
|
|
{
|
2013-11-14 20:20:58 +01:00
|
|
|
struct dir_entry key;
|
|
|
|
hashmap_entry_init(&key, memihash(name, namelen));
|
|
|
|
key.namelen = namelen;
|
|
|
|
return hashmap_get(&istate->dir_hash, &key, name);
|
2013-02-28 00:57:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct dir_entry *hash_dir_entry(struct index_state *istate,
|
|
|
|
struct cache_entry *ce, int namelen)
|
2010-10-03 11:56:43 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Throw each directory component in the hash for quick lookup
|
name-hash: stop storing trailing '/' on paths in index_state.dir_hash
When 5102c617 (Add case insensitivity support for directories when using
git status, 2010-10-03) added directories to the name-hash there was
only a single hash table in which both real cache entries and leading
directory prefixes were registered. To distinguish between the two types
of entries, directories were stored with a trailing '/'.
2092678c (name-hash.c: fix endless loop with core.ignorecase=true,
2013-02-28), however, moved directories to a separate hash table
(index_state.dir_hash) but retained the (now) redundant trailing '/',
thus callers continue to bear the burden of ensuring the slash's
presence before searching the index for a directory. Eliminate this
redundancy by storing paths in the dir-hash without the trailing '/'.
An important benefit of this change is that it eliminates undocumented
and dangerous behavior of dir.c:directory_exists_in_index_icase() in
which it assumes not only that it can validly access one character
beyond the end of its incoming directory argument, but also that that
character will unconditionally be a '/'. This perilous behavior was
"tolerated" because the string passed in by its lone caller always had a
'/' in that position, however, things broke [1] when 2eac2a4c (ls-files
-k: a directory only can be killed if the index has a non-directory,
2013-08-15) added a new caller which failed to respect the undocumented
assumption.
[1]: http://thread.gmane.org/gmane.comp.version-control.git/232727
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-09-17 09:06:16 +02:00
|
|
|
* during a git status. Directory components are stored without their
|
2010-10-03 11:56:43 +02:00
|
|
|
* closing slash. Despite submodules being a directory, they never
|
name-hash: stop storing trailing '/' on paths in index_state.dir_hash
When 5102c617 (Add case insensitivity support for directories when using
git status, 2010-10-03) added directories to the name-hash there was
only a single hash table in which both real cache entries and leading
directory prefixes were registered. To distinguish between the two types
of entries, directories were stored with a trailing '/'.
2092678c (name-hash.c: fix endless loop with core.ignorecase=true,
2013-02-28), however, moved directories to a separate hash table
(index_state.dir_hash) but retained the (now) redundant trailing '/',
thus callers continue to bear the burden of ensuring the slash's
presence before searching the index for a directory. Eliminate this
redundancy by storing paths in the dir-hash without the trailing '/'.
An important benefit of this change is that it eliminates undocumented
and dangerous behavior of dir.c:directory_exists_in_index_icase() in
which it assumes not only that it can validly access one character
beyond the end of its incoming directory argument, but also that that
character will unconditionally be a '/'. This perilous behavior was
"tolerated" because the string passed in by its lone caller always had a
'/' in that position, however, things broke [1] when 2eac2a4c (ls-files
-k: a directory only can be killed if the index has a non-directory,
2013-08-15) added a new caller which failed to respect the undocumented
assumption.
[1]: http://thread.gmane.org/gmane.comp.version-control.git/232727
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-09-17 09:06:16 +02:00
|
|
|
* reach this point, because they are stored
|
2013-02-28 00:57:48 +01:00
|
|
|
* in index_state.name_hash (as ordinary cache_entries).
|
2010-10-03 11:56:43 +02:00
|
|
|
*/
|
2013-02-28 00:57:48 +01:00
|
|
|
struct dir_entry *dir;
|
|
|
|
|
|
|
|
/* get length of parent directory */
|
|
|
|
while (namelen > 0 && !is_dir_sep(ce->name[namelen - 1]))
|
|
|
|
namelen--;
|
|
|
|
if (namelen <= 0)
|
|
|
|
return NULL;
|
name-hash: stop storing trailing '/' on paths in index_state.dir_hash
When 5102c617 (Add case insensitivity support for directories when using
git status, 2010-10-03) added directories to the name-hash there was
only a single hash table in which both real cache entries and leading
directory prefixes were registered. To distinguish between the two types
of entries, directories were stored with a trailing '/'.
2092678c (name-hash.c: fix endless loop with core.ignorecase=true,
2013-02-28), however, moved directories to a separate hash table
(index_state.dir_hash) but retained the (now) redundant trailing '/',
thus callers continue to bear the burden of ensuring the slash's
presence before searching the index for a directory. Eliminate this
redundancy by storing paths in the dir-hash without the trailing '/'.
An important benefit of this change is that it eliminates undocumented
and dangerous behavior of dir.c:directory_exists_in_index_icase() in
which it assumes not only that it can validly access one character
beyond the end of its incoming directory argument, but also that that
character will unconditionally be a '/'. This perilous behavior was
"tolerated" because the string passed in by its lone caller always had a
'/' in that position, however, things broke [1] when 2eac2a4c (ls-files
-k: a directory only can be killed if the index has a non-directory,
2013-08-15) added a new caller which failed to respect the undocumented
assumption.
[1]: http://thread.gmane.org/gmane.comp.version-control.git/232727
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-09-17 09:06:16 +02:00
|
|
|
namelen--;
|
2013-02-28 00:57:48 +01:00
|
|
|
|
|
|
|
/* lookup existing entry for that directory */
|
|
|
|
dir = find_dir_entry(istate, ce->name, namelen);
|
|
|
|
if (!dir) {
|
|
|
|
/* not found, create it and add to hash table */
|
2016-02-22 23:44:32 +01:00
|
|
|
FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
|
2013-11-14 20:20:58 +01:00
|
|
|
hashmap_entry_init(dir, memihash(ce->name, namelen));
|
2013-02-28 00:57:48 +01:00
|
|
|
dir->namelen = namelen;
|
2013-11-14 20:20:58 +01:00
|
|
|
hashmap_add(&istate->dir_hash, dir);
|
2013-02-28 00:57:48 +01:00
|
|
|
|
|
|
|
/* recursively add missing parent directories */
|
name-hash: stop storing trailing '/' on paths in index_state.dir_hash
When 5102c617 (Add case insensitivity support for directories when using
git status, 2010-10-03) added directories to the name-hash there was
only a single hash table in which both real cache entries and leading
directory prefixes were registered. To distinguish between the two types
of entries, directories were stored with a trailing '/'.
2092678c (name-hash.c: fix endless loop with core.ignorecase=true,
2013-02-28), however, moved directories to a separate hash table
(index_state.dir_hash) but retained the (now) redundant trailing '/',
thus callers continue to bear the burden of ensuring the slash's
presence before searching the index for a directory. Eliminate this
redundancy by storing paths in the dir-hash without the trailing '/'.
An important benefit of this change is that it eliminates undocumented
and dangerous behavior of dir.c:directory_exists_in_index_icase() in
which it assumes not only that it can validly access one character
beyond the end of its incoming directory argument, but also that that
character will unconditionally be a '/'. This perilous behavior was
"tolerated" because the string passed in by its lone caller always had a
'/' in that position, however, things broke [1] when 2eac2a4c (ls-files
-k: a directory only can be killed if the index has a non-directory,
2013-08-15) added a new caller which failed to respect the undocumented
assumption.
[1]: http://thread.gmane.org/gmane.comp.version-control.git/232727
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-09-17 09:06:16 +02:00
|
|
|
dir->parent = hash_dir_entry(istate, ce, namelen);
|
2010-10-03 11:56:43 +02:00
|
|
|
}
|
2013-02-28 00:57:48 +01:00
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_dir_entry(struct index_state *istate, struct cache_entry *ce)
|
|
|
|
{
|
|
|
|
/* Add reference to the directory entry (and parents if 0). */
|
|
|
|
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
|
|
|
|
while (dir && !(dir->nr++))
|
|
|
|
dir = dir->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
|
|
|
|
{
|
|
|
|
/*
|
2013-11-14 20:21:26 +01:00
|
|
|
* Release reference to the directory entry. If 0, remove and continue
|
|
|
|
* with parent directory.
|
2013-02-28 00:57:48 +01:00
|
|
|
*/
|
|
|
|
struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
|
2013-11-14 20:21:26 +01:00
|
|
|
while (dir && !(--dir->nr)) {
|
|
|
|
struct dir_entry *parent = dir->parent;
|
|
|
|
hashmap_remove(&istate->dir_hash, dir, NULL);
|
|
|
|
free(dir);
|
|
|
|
dir = parent;
|
|
|
|
}
|
2010-10-03 11:56:43 +02:00
|
|
|
}
|
|
|
|
|
2008-03-21 21:16:24 +01:00
|
|
|
static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
|
|
|
|
{
|
|
|
|
if (ce->ce_flags & CE_HASHED)
|
|
|
|
return;
|
|
|
|
ce->ce_flags |= CE_HASHED;
|
2013-11-14 20:21:58 +01:00
|
|
|
hashmap_entry_init(ce, memihash(ce->name, ce_namelen(ce)));
|
|
|
|
hashmap_add(&istate->name_hash, ce);
|
2010-10-03 11:56:43 +02:00
|
|
|
|
2013-11-14 20:22:27 +01:00
|
|
|
if (ignore_case)
|
2013-02-28 00:57:48 +01:00
|
|
|
add_dir_entry(istate, ce);
|
2008-03-21 21:16:24 +01:00
|
|
|
}
|
|
|
|
|
2013-11-14 20:22:27 +01:00
|
|
|
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
|
2014-01-02 22:57:12 +01:00
|
|
|
* index_file_exists, find all entries with matching hash code and
|
2013-11-14 20:22:27 +01:00
|
|
|
* decide whether the entry matches in same_name.
|
|
|
|
*/
|
|
|
|
return remove ? !(ce1 == ce2) : 0;
|
|
|
|
}
|
|
|
|
|
2008-03-21 21:16:24 +01:00
|
|
|
static void lazy_init_name_hash(struct index_state *istate)
|
|
|
|
{
|
|
|
|
int nr;
|
|
|
|
|
|
|
|
if (istate->name_hash_initialized)
|
|
|
|
return;
|
2013-11-14 20:22:27 +01:00
|
|
|
hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
|
|
|
|
istate->cache_nr);
|
2013-11-14 20:20:58 +01:00
|
|
|
hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp, 0);
|
2008-03-21 21:16:24 +01:00
|
|
|
for (nr = 0; nr < istate->cache_nr; nr++)
|
|
|
|
hash_index_entry(istate, istate->cache[nr]);
|
|
|
|
istate->name_hash_initialized = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_name_hash(struct index_state *istate, struct cache_entry *ce)
|
|
|
|
{
|
|
|
|
if (istate->name_hash_initialized)
|
|
|
|
hash_index_entry(istate, ce);
|
|
|
|
}
|
|
|
|
|
2013-02-28 00:57:48 +01:00
|
|
|
void remove_name_hash(struct index_state *istate, struct cache_entry *ce)
|
|
|
|
{
|
2013-11-14 20:22:27 +01:00
|
|
|
if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED))
|
|
|
|
return;
|
|
|
|
ce->ce_flags &= ~CE_HASHED;
|
|
|
|
hashmap_remove(&istate->name_hash, ce, ce);
|
2013-02-28 00:57:48 +01:00
|
|
|
|
2013-11-14 20:22:27 +01:00
|
|
|
if (ignore_case)
|
|
|
|
remove_dir_entry(istate, ce);
|
2013-02-28 00:57:48 +01:00
|
|
|
}
|
|
|
|
|
2008-03-21 23:55:19 +01:00
|
|
|
static int slow_same_name(const char *name1, int len1, const char *name2, int len2)
|
|
|
|
{
|
|
|
|
if (len1 != len2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (len1) {
|
|
|
|
unsigned char c1 = *name1++;
|
|
|
|
unsigned char c2 = *name2++;
|
|
|
|
len1--;
|
|
|
|
if (c1 != c2) {
|
|
|
|
c1 = toupper(c1);
|
|
|
|
c2 = toupper(c2);
|
|
|
|
if (c1 != c2)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase)
|
|
|
|
{
|
|
|
|
int len = ce_namelen(ce);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-06-20 04:06:43 +02:00
|
|
|
if (len == namelen && !memcmp(name, ce->name, len))
|
2008-03-21 23:55:19 +01:00
|
|
|
return 1;
|
|
|
|
|
2010-10-03 11:56:43 +02:00
|
|
|
if (!icase)
|
|
|
|
return 0;
|
|
|
|
|
2013-02-28 00:57:48 +01:00
|
|
|
return slow_same_name(name, namelen, ce->name, len);
|
2008-03-21 23:55:19 +01:00
|
|
|
}
|
|
|
|
|
2015-10-21 19:54:11 +02:00
|
|
|
int index_dir_exists(struct index_state *istate, const char *name, int namelen)
|
2013-09-17 09:06:14 +02:00
|
|
|
{
|
|
|
|
struct dir_entry *dir;
|
|
|
|
|
|
|
|
lazy_init_name_hash(istate);
|
|
|
|
dir = find_dir_entry(istate, name, namelen);
|
2015-10-21 19:54:11 +02:00
|
|
|
return dir && dir->nr;
|
|
|
|
}
|
2013-09-17 09:06:14 +02:00
|
|
|
|
2015-10-21 19:54:11 +02:00
|
|
|
void adjust_dirname_case(struct index_state *istate, char *name)
|
|
|
|
{
|
|
|
|
const char *startPtr = name;
|
|
|
|
const char *ptr = startPtr;
|
2013-09-17 09:06:14 +02:00
|
|
|
|
2015-10-21 19:54:11 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-17 09:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
|
2008-03-21 21:16:24 +01:00
|
|
|
{
|
|
|
|
struct cache_entry *ce;
|
|
|
|
|
|
|
|
lazy_init_name_hash(istate);
|
|
|
|
|
2014-07-03 00:22:11 +02:00
|
|
|
ce = hashmap_get_from_hash(&istate->name_hash,
|
|
|
|
memihash(name, namelen), NULL);
|
2008-03-21 21:16:24 +01:00
|
|
|
while (ce) {
|
2013-11-14 20:22:27 +01:00
|
|
|
if (same_name(ce, name, namelen, icase))
|
|
|
|
return ce;
|
2013-11-14 20:21:58 +01:00
|
|
|
ce = hashmap_get_next(&istate->name_hash, ce);
|
2008-03-21 21:16:24 +01:00
|
|
|
}
|
2008-03-21 23:53:00 +01:00
|
|
|
return NULL;
|
2008-03-21 21:16:24 +01:00
|
|
|
}
|
2013-02-28 00:57:48 +01:00
|
|
|
|
|
|
|
void free_name_hash(struct index_state *istate)
|
|
|
|
{
|
|
|
|
if (!istate->name_hash_initialized)
|
|
|
|
return;
|
|
|
|
istate->name_hash_initialized = 0;
|
|
|
|
|
2013-11-14 20:21:58 +01:00
|
|
|
hashmap_free(&istate->name_hash, 0);
|
2013-11-14 20:20:58 +01:00
|
|
|
hashmap_free(&istate->dir_hash, 1);
|
2013-02-28 00:57:48 +01:00
|
|
|
}
|