Skip to content

Commit 30c06b6

Browse files
committed
patch_parse.c: Handle CRLF in parse_header_start
1 parent aeea1c4 commit 30c06b6

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/patch_parse.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ static int parse_header_start(git_patch_parsed *patch, git_patch_parse_ctx *ctx)
328328
* proceeed here. We then hope for the "---" and "+++" lines to fix that
329329
* for us.
330330
*/
331-
if (!git_parse_ctx_contains(&ctx->parse_ctx, "\n", 1)) {
331+
if (!git_parse_ctx_contains(&ctx->parse_ctx, "\n", 1) &&
332+
!git_parse_ctx_contains(&ctx->parse_ctx, "\r\n", 2)) {
332333
git_parse_advance_chars(&ctx->parse_ctx, ctx->parse_ctx.line_len - 1);
333334

334335
git__free(patch->header_old_path);

tests/diff/parse.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ void test_diff_parse__lineinfo(void)
360360
git_diff_free(diff);
361361
}
362362

363+
363364
void test_diff_parse__new_file_with_space(void)
364365
{
365366
const char *content = PATCH_ORIGINAL_NEW_FILE_WITH_SPACE;
@@ -377,3 +378,21 @@ void test_diff_parse__new_file_with_space(void)
377378
git_patch_free(patch);
378379
git_diff_free(diff);
379380
}
381+
382+
void test_diff_parse__crlf(void)
383+
{
384+
const char *text = PATCH_CRLF;
385+
git_diff *diff;
386+
git_patch *patch;
387+
const git_diff_delta *delta;
388+
389+
cl_git_pass(git_diff_from_buffer(&diff, text, strlen(text)));
390+
cl_git_pass(git_patch_from_diff(&patch, diff, 0));
391+
delta = git_patch_get_delta(patch);
392+
393+
cl_assert_equal_s(delta->old_file.path, "test-file");
394+
cl_assert_equal_s(delta->new_file.path, "test-file");
395+
396+
git_patch_free(patch);
397+
git_diff_free(diff);
398+
}

tests/patch/patch_common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,3 +850,12 @@
850850
"+++ b/sp ace.txt\n" \
851851
"@@ -0,0 +1 @@\n" \
852852
"+a\n"
853+
854+
#define PATCH_CRLF \
855+
"diff --git a/test-file b/test-file\r\n" \
856+
"new file mode 100644\r\n" \
857+
"index 0000000..af431f2 100644\r\n" \
858+
"--- /dev/null\r\n" \
859+
"+++ b/test-file\r\n" \
860+
"@@ -0,0 +1 @@\r\n" \
861+
"+a contents\r\n"

0 commit comments

Comments
 (0)