diff options
author | Torsten Bögershausen <tboegi@web.de> | 2016-06-28 10:01:16 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2016-07-07 09:17:12 -0700 |
commit | 9e2be614c1fa594f786b408e717159f1ed52bb37 (patch) | |
tree | e258b374c5ebb6ec538c602429997d31592052bf /convert.c | |
parent | 75964d2722cb95f0a9a04f9c0a5814faabbd232f (diff) | |
download | git-tb/convert-peek-in-index.tar.gz |
correct ce_compare_data() in a middle of a mergetb/convert-peek-in-index
The following didn't work as expected:
- In a middle of a merge
- merge.renormalize is true,
- .gitattributes = "* text=auto"
- core.eol = crlf
Merge a blob with CRLF "first line\r\nsame line\r\n" and a blob
with LF "first line\nsame line\n".
The expected result of the merge is "first line\nsame line\n".
The content in the working tree is "first line\r\nsame line\r\n",
and ce_compare_data() should find that the content is clean and return 0.
Deep down crlf_to_git() is invoked, to check if CRLF are converted or not.
The "new safer autocrlf handling" calls blob_has_cr().
Instead of using the sha1 of the blob, (CRLF in this example),
the function get_sha1_from_index() is invoked.
get_sha1_from_index() decides to return "ours" when in the middle of
the merge, which is LF.
As a result, the CRLF in the worktree are converted into LF before
the comparison.
The contents of LF and CRLF don't match any more.
The problem is that ce_compare_data() has ce->sha1, but the sha1 is lost
on it's way into blob_has_cr().
Forwarding ce->sha1 from ce_compare_data() into crlf_to_git() makes sure
that blob_has_cr() looks at the appropriate blob.
Add a new parameter index_blob_sha1 to convert_to_git(), and forward the
sha1 from ce_compare_data() into convert_to_git(). Other callers use NULL
for index_blob_sha1, and the sha1 is determined from path
using get_sha1_from_cache(path). This is the same handling as before.
In the same spirit, forward the sha1 into would_convert_to_git().
While at it, rename has_cr_in_index() into blob_has_cr()
and replace 0 with SAFE_CRLF_FALSE.
Add a TC in t6038 to have a test coverage under Linux.
Signed-off-by: Torsten Bögershausen <tboegi@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'convert.c')
-rw-r--r-- | convert.c | 43 |
1 files changed, 30 insertions, 13 deletions
@@ -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); } /***************************************************************** |