summaryrefslogtreecommitdiff
path: root/convert.c
diff options
context:
space:
mode:
Diffstat (limited to 'convert.c')
-rw-r--r--convert.c43
1 files changed, 30 insertions, 13 deletions
diff --git a/convert.c b/convert.c
index 67d69b5c0e..802ee7cdaa 100644
--- a/convert.c
+++ b/convert.c
@@ -219,23 +219,28 @@ static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
}
}
-static int has_cr_in_index(const char *path)
+static int blob_has_cr(const unsigned char *index_blob_sha1)
{
unsigned long sz;
void *data;
- int has_cr;
-
- data = read_blob_data_from_cache(path, &sz);
+ int has_cr = 0;
+ enum object_type type;
+ if (!index_blob_sha1)
+ return 0;
+ data = read_sha1_file(index_blob_sha1, &type, &sz);
if (!data)
return 0;
- has_cr = memchr(data, '\r', sz) != NULL;
+ if (type == OBJ_BLOB)
+ has_cr = memchr(data, '\r', sz) != NULL;
+
free(data);
return has_cr;
}
static int crlf_to_git(const char *path, const char *src, size_t len,
struct strbuf *buf,
- enum crlf_action crlf_action, enum safe_crlf checksafe)
+ enum crlf_action crlf_action, enum safe_crlf checksafe,
+ const unsigned char *index_blob_sha1)
{
struct text_stat stats;
char *dst;
@@ -256,14 +261,23 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
if (convert_is_binary(len, &stats))
return 0;
+
/*
* If the file in the index has any CR in it, do not convert.
* This is the new safer autocrlf handling.
*/
if (checksafe == SAFE_CRLF_RENORMALIZE)
checksafe = SAFE_CRLF_FALSE;
- else if (has_cr_in_index(path))
- return 0;
+ else {
+ /*
+ * If the file in the index has any CR in it, do not convert.
+ * This is the new safer autocrlf handling.
+ */
+ if (!index_blob_sha1)
+ index_blob_sha1 = get_sha1_from_cache(path);
+ if (blob_has_cr(index_blob_sha1))
+ return 0;
+ }
}
check_safe_crlf(path, crlf_action, &stats, checksafe);
@@ -855,7 +869,8 @@ const char *get_convert_attr_ascii(const char *path)
}
int convert_to_git(const char *path, const char *src, size_t len,
- struct strbuf *dst, enum safe_crlf checksafe)
+ struct strbuf *dst, enum safe_crlf checksafe,
+ const unsigned char *index_blob_sha1)
{
int ret = 0;
const char *filter = NULL;
@@ -876,7 +891,7 @@ int convert_to_git(const char *path, const char *src, size_t len,
src = dst->buf;
len = dst->len;
}
- ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
+ ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe, index_blob_sha1);
if (ret && dst) {
src = dst->buf;
len = dst->len;
@@ -885,7 +900,8 @@ int convert_to_git(const char *path, const char *src, size_t len,
}
void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
- enum safe_crlf checksafe)
+ enum safe_crlf checksafe,
+ const unsigned char *index_blob_sha1)
{
struct conv_attrs ca;
convert_attrs(&ca, path);
@@ -896,7 +912,8 @@ void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
if (!apply_filter(path, NULL, 0, fd, dst, ca.drv->clean))
die("%s: clean filter '%s' failed", path, ca.drv->name);
- crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
+ crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action,
+ checksafe, index_blob_sha1);
ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
}
@@ -951,7 +968,7 @@ int renormalize_buffer(const char *path, const char *src, size_t len, struct str
src = dst->buf;
len = dst->len;
}
- return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_RENORMALIZE);
+ return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_RENORMALIZE, NULL);
}
/*****************************************************************