don't convert CRLF to CRCRLF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
diff --git a/src/crlf.c b/src/crlf.c
index 84347ac..cd6d982 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -223,12 +223,17 @@ static int crlf_apply_to_odb(
static int convert_line_endings(git_buf *dest, const git_buf *source, const char *ending)
{
const char *scan = git_buf_cstr(source),
- *next,
- *scan_end = git_buf_cstr(source) + git_buf_len(source);
+ *next,
+ *line_end,
+ *scan_end = git_buf_cstr(source) + git_buf_len(source);
while ((next = memchr(scan, '\n', scan_end - scan)) != NULL) {
- if (next > scan)
- git_buf_put(dest, scan, next-scan);
+ if (next > scan) {
+ line_end = *(next - 1) == '\r' ? next - 1 : next;
+ git_buf_put(dest, scan, line_end - scan);
+ scan = next + 1;
+ }
+
git_buf_puts(dest, ending);
scan = next + 1;
}
diff --git a/tests-clar/checkout/crlf.c b/tests-clar/checkout/crlf.c
index 39889a1..40f083c 100644
--- a/tests-clar/checkout/crlf.c
+++ b/tests-clar/checkout/crlf.c
@@ -10,7 +10,9 @@
#define MORE_CRLF_TEXT_RAW "crlf\r\ncrlf\r\nlf\ncrlf\r\ncrlf\r\n"
#define MORE_LF_TEXT_RAW "lf\nlf\ncrlf\r\nlf\nlf\n"
-#define ALL_LF_TEXT_AS_CRLF "lf\r\nlf\r\nlf\r\nlf\r\nlf\r\n"
+#define ALL_LF_TEXT_AS_CRLF "lf\r\nlf\r\nlf\r\nlf\r\nlf\r\n"
+#define MORE_CRLF_TEXT_AS_CRLF "crlf\r\ncrlf\r\nlf\r\ncrlf\r\ncrlf\r\n"
+#define MORE_LF_TEXT_AS_CRLF "lf\r\nlf\r\ncrlf\r\nlf\r\nlf\r\n"
static git_repository *g_repo;
@@ -78,6 +80,48 @@ void test_checkout_crlf__detect_crlf_autocrlf_true(void)
#endif
}
+void test_checkout_crlf__more_lf_autocrlf_true(void)
+{
+#ifdef GIT_WIN32
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", true);
+
+ git_checkout_head(g_repo, &opts);
+
+ test_file_contents("./crlf/more-lf", MORE_LF_TEXT_AS_CRLF);
+#endif
+}
+
+void test_checkout_crlf__more_crlf_autocrlf_true(void)
+{
+#ifdef GIT_WIN32
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", true);
+
+ git_checkout_head(g_repo, &opts);
+
+ test_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_AS_CRLF);
+#endif
+}
+
+void test_checkout_crlf__all_crlf_autocrlf_true(void)
+{
+#ifdef GIT_WIN32
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", true);
+
+ git_checkout_head(g_repo, &opts);
+
+ test_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
+#endif
+}
+
void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
{
#ifdef GIT_WIN32