The index now keeps track of the conversion mode that was active when the entry was created. This can be used to detect the most common cases of when the conversion mode has changed. Signed-off-by: Henrik Grubbström <grubba@xxxxxxxxxx> --- No longer keeps track of the normalized sha1. Instead CONV_NORM_NEEDED is set when the conversion mode affects the normalized sha1, and the normalized sha1 (re-)generated on demand. The cache_entry is now smudged when CONV_NORM_NEEDED is set or reset, since we don't know the size of the corresponding working tree file in such cases. Removed the prefix NORM_ from the conversion mode flags. cache.h | 10 ++++++++++ convert.c | 31 +++++++++++++++++++++++++++++++ read-cache.c | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/cache.h b/cache.h index 1fe2d7d..e5f54f2 100644 --- a/cache.h +++ b/cache.h @@ -151,10 +151,19 @@ struct cache_entry { unsigned int ce_size; unsigned int ce_flags; unsigned char sha1[20]; + unsigned int ce_conv_flags; struct cache_entry *next; char name[FLEX_ARRAY]; /* more */ }; +#define CONV_CRLF_GIT 0x0001 +#define CONV_CRLF_WT 0x0002 +#define CONV_CRLF_GUESS 0x0004 +#define CONV_IDENT 0x0008 +#define CONV_FILT 0x0010 +#define CONV_MASK 0x001f +#define CONV_NORM_NEEDED 0x010000 + #define CE_NAMEMASK (0x0fff) #define CE_STAGEMASK (0x3000) #define CE_EXTENDED (0x4000) @@ -1014,6 +1023,7 @@ extern void trace_argv_printf(const char **argv, const char *format, ...); /* convert.c */ /* returns 1 if *dst was used */ +extern unsigned int git_conv_flags(const char *path); extern int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst, enum safe_crlf checksafe); extern int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst); diff --git a/convert.c b/convert.c index 4f8fcb7..9c063c8 100644 --- a/convert.c +++ b/convert.c @@ -568,6 +568,37 @@ static int git_path_check_ident(const char *path, struct git_attr_check *check) return !!ATTR_TRUE(value); } +unsigned int git_conv_flags(const char *path) +{ + struct git_attr_check check[3]; + int crlf = CRLF_GUESS; + int ident = 0; + unsigned ret = 0; + struct convert_driver *drv = NULL; + + setup_convert_check(check); + if (!git_checkattr(path, ARRAY_SIZE(check), check)) { + crlf = git_path_check_crlf(path, check + 0); + ident = git_path_check_ident(path, check + 1); + drv = git_path_check_convert(path, check + 2); + } + + if (auto_crlf && (crlf != CRLF_BINARY)) { + ret |= CONV_CRLF_GIT; + if (crlf != CRLF_INPUT && auto_crlf > 0) + ret |= CONV_CRLF_WT; + if (crlf == CRLF_GUESS) + ret |= CONV_CRLF_GUESS; + } + if (ident) { + ret |= CONV_IDENT; + } + if (drv) { + ret |= CONV_FILT; + } + return ret; +} + int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst, enum safe_crlf checksafe) { diff --git a/read-cache.c b/read-cache.c index f1f789b..eeda928 100644 --- a/read-cache.c +++ b/read-cache.c @@ -88,12 +88,38 @@ void fill_stat_cache_info(struct cache_entry *ce, struct stat *st) static int ce_compare_data(struct cache_entry *ce, struct stat *st) { int match = -1; - int fd = open(ce->name, O_RDONLY); - + int fd; + unsigned char norm_sha1[20]; + unsigned int conv_flags = git_conv_flags(ce->name); + const unsigned char *cmp_sha1 = ce->sha1; + + if ((conv_flags ^ ce->ce_conv_flags) & CONV_MASK) { + if (ce->ce_conv_flags & CONV_NORM_NEEDED) { + /* Smudge the entry since it was only correct + * for the old conversion mode. */ + ce->ce_size = 0; + } + ce->ce_conv_flags = conv_flags; + } else + conv_flags = ce->ce_conv_flags & CONV_NORM_NEEDED; + + if (conv_flags) { + index_blob(norm_sha1, ce->sha1, 0, ce->name); + if (!(conv_flags & CONV_NORM_NEEDED) && + hashcmp(norm_sha1, ce->sha1)) { + ce->ce_conv_flags = conv_flags | CONV_NORM_NEEDED; + /* Smudge the entry since we don't know + * the correct value. */ + ce->ce_size = 0; + } + cmp_sha1 = norm_sha1; + } + + fd = open(ce->name, O_RDONLY); if (fd >= 0) { unsigned char sha1[20]; if (!index_fd(sha1, fd, st, 0, OBJ_BLOB, ce->name)) - match = hashcmp(sha1, ce->sha1); + match = hashcmp(sha1, cmp_sha1); /* index_fd() closed the file descriptor already */ } return match; @@ -227,6 +253,11 @@ static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st) changed |= INODE_CHANGED; #endif + /* ce_size can not be trusted if the conversion mode has changed. */ + if ((ce->ce_mode & S_IFMT) == S_IFREG && + ((ce->ce_conv_flags ^ git_conv_flags(ce->name)) & CONV_MASK)) + return changed; + if (ce->ce_size != (unsigned int) st->st_size) changed |= DATA_CHANGED; -- 1.7.0.4.369.g81e89 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html