summaryrefslogtreecommitdiff
path: root/src/patch_parse.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-07-05 11:06:33 +0200
committerPatrick Steinhardt <ps@pks.im>2019-07-11 12:11:11 +0200
commit3f855fe863e7260ea4b7bc36a0c78ea2bff37b68 (patch)
treec6fed7a801144713282860976ed1cd66510957d9 /src/patch_parse.c
parentb30dab8fa7e1c2d6bd938cd5941c9d1cd55a0004 (diff)
downloadlibgit2-3f855fe863e7260ea4b7bc36a0c78ea2bff37b68.tar.gz
patch_parse: handle missing newline indicator in old file
When either the old or new file contents have no newline at the end of the file, then git-diff(1) will print out a "\ No newline at end of file" indicator. While we do correctly handle this in the case where the new file has this indcator, we fail to parse patches where the old file is missing a newline at EOF. Fix this bug by handling and missing newline indicators in the old file. Add tests to verify that we can parse such files.
Diffstat (limited to 'src/patch_parse.c')
-rw-r--r--src/patch_parse.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/patch_parse.c b/src/patch_parse.c
index b44d4f02f..9f5bef5b1 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -578,6 +578,16 @@ static int parse_hunk_body(
old_lineno = -1;
break;
+ case '\\':
+ /*
+ * If there are no oldlines left, then this is probably
+ * the "\ No newline at end of file" marker. Do not
+ * verify its format, as it may be localized.
+ */
+ if (!oldlines)
+ continue;
+ /* fall through */
+
default:
error = git_parse_err("invalid patch hunk at line %"PRIuZ, ctx->parse_ctx.line_num);
goto done;
@@ -606,7 +616,8 @@ static int parse_hunk_body(
goto done;
}
- /* Handle "\ No newline at end of file". Only expect the leading
+ /*
+ * Handle "\ No newline at end of file". Only expect the leading
* backslash, though, because the rest of the string could be
* localized. Because `diff` optimizes for the case where you
* want to apply the patch by hand.