]> git.proxmox.com Git - libgit2.git/commitdiff
git_buf_text_lf_to_crlf: allow mixed line endings
authorEdward Thomson <ethomson@microsoft.com>
Mon, 8 Jun 2015 17:51:28 +0000 (13:51 -0400)
committerEdward Thomson <ethomson@microsoft.com>
Mon, 22 Jun 2015 16:00:23 +0000 (12:00 -0400)
Allow files to have mixed line endings instead of skipping processing
on them.

src/buf_text.c
src/buf_text.h
tests/core/buffer.c

index 864e39caba71863fc06bd86f84b58bf1b851068a..7e6779d2dc61b2bd62e4c0fc5a2672aece0f4b05 100644 (file)
@@ -131,17 +131,15 @@ int git_buf_text_lf_to_crlf(git_buf *tgt, const git_buf *src)
        for (; next; scan = next + 1, next = memchr(scan, '\n', end - scan)) {
                size_t copylen = next - scan;
 
-               /* if we find mixed line endings, bail */
-               if (next > start && next[-1] == '\r') {
-                       git_buf_free(tgt);
-                       return GIT_PASSTHROUGH;
-               }
+               /* if we find mixed line endings, carry on */
+               if (copylen && next[-1] == '\r')
+                       copylen--;
 
                GITERR_CHECK_ALLOC_ADD(&alloclen, copylen, 3);
                if (git_buf_grow_by(tgt, alloclen) < 0)
                        return -1;
 
-               if (next > scan) {
+               if (copylen) {
                        memcpy(tgt->ptr + tgt->size, scan, copylen);
                        tgt->size += copylen;
                }
index e753a0244ec8ac4b1509e950da9ff76f904d9a58..c9c55af8931e035b323537d2e11af679ef0da054 100644 (file)
@@ -58,8 +58,7 @@ extern void git_buf_text_unescape(git_buf *buf);
 /**
  * Replace all \r\n with \n.
  *
- * @return 0 on success, -1 on memory error, GIT_PASSTHROUGH if the
- * source buffer has mixed line endings.
+ * @return 0 on success, -1 on memory error
  */
 extern int git_buf_text_crlf_to_lf(git_buf *tgt, const git_buf *src);
 
index d28aa218f3dbabc01a4a136bcab3e3317b9c8543..fef37f8f78010a483e113c10ff68c4f91bc49747 100644 (file)
@@ -1054,14 +1054,20 @@ void test_core_buffer__lf_and_crlf_conversions(void)
 
        git_buf_sets(&src, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n");
 
-       cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src));
+       cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
+       check_buf("crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n", tgt);
+
+       git_buf_sets(&src, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n");
 
        cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
        check_buf("crlf\ncrlf\ncrlf\ncrlf\n", tgt);
 
        git_buf_sets(&src, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf");
 
-       cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src));
+       cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
+       check_buf("\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf", tgt);
+
+       git_buf_sets(&src, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf");
 
        cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
        check_buf("\ncrlf\ncrlf\ncrlf\ncrlf\ncrlf", tgt);
@@ -1070,7 +1076,11 @@ void test_core_buffer__lf_and_crlf_conversions(void)
 
        git_buf_sets(&src, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n");
 
-       cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src));
+       cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
+       check_buf("\r\nlf\r\nlf\r\ncrlf\r\nlf\r\nlf\r\ncrlf\r\n", tgt);
+
+       git_buf_sets(&src, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n");
+
        cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
        check_buf("\nlf\nlf\ncrlf\nlf\nlf\ncrlf\n", tgt);
 
@@ -1078,7 +1088,9 @@ void test_core_buffer__lf_and_crlf_conversions(void)
 
        git_buf_sets(&src, "\ncrlf\r\ncrlf\r\nlf\ncrlf\r\ncrlf");
 
-       cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src));
+       cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
+       check_buf("\r\ncrlf\r\ncrlf\r\nlf\r\ncrlf\r\ncrlf", tgt);
+
        cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
        check_buf("\ncrlf\ncrlf\nlf\ncrlf\ncrlf", tgt);
 
@@ -1086,7 +1098,11 @@ void test_core_buffer__lf_and_crlf_conversions(void)
 
        git_buf_sets(&src, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r");
 
-       cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src));
+       cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
+       check_buf("\rcrlf\r\nlf\r\nlf\r\ncr\rcrlf\r\nlf\r\ncr\r", tgt);
+
+       git_buf_sets(&src, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r");
+
        cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
        check_buf("\rcrlf\nlf\nlf\ncr\rcrlf\nlf\ncr\r", tgt);
 
@@ -1102,7 +1118,9 @@ void test_core_buffer__lf_and_crlf_conversions(void)
        /* blob correspondence tests */
 
        git_buf_sets(&src, ALL_CRLF_TEXT_RAW);
-       cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src));
+       cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
+       check_buf(ALL_CRLF_TEXT_AS_CRLF, tgt);
+       git_buf_sets(&src, ALL_CRLF_TEXT_RAW);
        cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
        check_buf(ALL_CRLF_TEXT_AS_LF, tgt);
        git_buf_free(&src);
@@ -1111,20 +1129,25 @@ void test_core_buffer__lf_and_crlf_conversions(void)
        git_buf_sets(&src, ALL_LF_TEXT_RAW);
        cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
        check_buf(ALL_LF_TEXT_AS_CRLF, tgt);
+       git_buf_sets(&src, ALL_LF_TEXT_RAW);
        cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
        check_buf(ALL_LF_TEXT_AS_LF, tgt);
        git_buf_free(&src);
        git_buf_free(&tgt);
 
        git_buf_sets(&src, MORE_CRLF_TEXT_RAW);
-       cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src));
+       cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
+       check_buf(MORE_CRLF_TEXT_AS_CRLF, tgt);
+       git_buf_sets(&src, MORE_CRLF_TEXT_RAW);
        cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
        check_buf(MORE_CRLF_TEXT_AS_LF, tgt);
        git_buf_free(&src);
        git_buf_free(&tgt);
 
        git_buf_sets(&src, MORE_LF_TEXT_RAW);
-       cl_git_fail_with(GIT_PASSTHROUGH, git_buf_text_lf_to_crlf(&tgt, &src));
+       cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
+       check_buf(MORE_LF_TEXT_AS_CRLF, tgt);
+       git_buf_sets(&src, MORE_LF_TEXT_RAW);
        cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
        check_buf(MORE_LF_TEXT_AS_LF, tgt);
        git_buf_free(&src);