{"thread":{"id":"1291","subject":"[PATCH 1/2] apply.c: handle incomplete lines correctly.","startedAt":"2005-07-22T16:56:39Z","lastAt":"2005-07-22T16:56:39Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"6305","messageId":"7vsly6wzi0.fsf@assigned-by-dhcp.cox.net","threadId":"1291","inReplyTo":null,"subject":"[PATCH 1/2] apply.c: handle incomplete lines correctly.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-07-22T16:56:39Z","receivedAt":"2005-07-22T16:56:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"The parsing code had a bug that failed to recognize an\nincomplete line at the end of a fragment, and the fragment\napplication code had a comparison bug to recognize such.  Fix\nthem to handle incomplete lines correctly.\n\nAdd a test script for patches with various combinations of\ncomplete and incomplete lines to make sure the fix works.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n apply.c               |    9 ++++++++-\n t/t4101-apply-nonl.sh |   32 ++++++++++++++++++++++++++++++++\n 2 files changed, 40 insertions(+), 1 deletions(-)\n create mode 100755 t/t4101-apply-nonl.sh\n\n9b378103b8ba89d697cdf368d29a6113929b127a\ndiff --git a/apply.c b/apply.c\n--- a/apply.c\n+++ b/apply.c\n@@ -679,6 +679,13 @@ static int parse_fragment(char *line, un\n \t\t\tbreak;\n \t\t}\n \t}\n+\t/* If a fragment ends with an incomplete line, we failed to include\n+\t * it in the above loop because we hit oldlines == newlines == 0\n+\t * before seeing it.\n+\t */\n+\tif (12 < size && !memcmp(line, \"\\\\ No newline\", 12))\n+\t\toffset += linelen(line, size);\n+\n \tpatch->lines_added += added;\n \tpatch->lines_deleted += deleted;\n \treturn offset;\n@@ -900,7 +907,7 @@ static int apply_one_fragment(struct buf\n \t\t * last one (which is the newline, of course).\n \t\t */\n \t\tplen = len-1;\n-\t\tif (len > size && patch[len] == '\\\\')\n+\t\tif (len < size && patch[len] == '\\\\')\n \t\t\tplen--;\n \t\tswitch (*patch) {\n \t\tcase ' ':\ndiff --git a/t/t4101-apply-nonl.sh b/t/t4101-apply-nonl.sh\nnew file mode 100755\n--- /dev/null\n+++ b/t/t4101-apply-nonl.sh\n@@ -0,0 +1,32 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2005 Junio C Hamano\n+#\n+\n+test_description='git-apply should handle files with incomplete lines.\n+\n+'\n+. ./test-lib.sh\n+\n+# setup\n+\n+(echo a; echo b) >frotz.0\n+(echo a; echo b; echo c) >frotz.1\n+(echo a; echo b | tr -d '\\012') >frotz.2\n+(echo a; echo c; echo b | tr -d '\\012') >frotz.3\n+\n+for i in 0 1 2 3\n+do\n+  for j in 0 1 2 3\n+  do\n+    test $i -eq $j && continue\n+    diff -u frotz.$i frotz.$j |\n+    sed -e '\n+\t/^---/s|.*|--- a/frotz|\n+\t/^+++/s|.*|+++ b/frotz|' >diff.$i-$j\n+    cat frotz.$i >frotz\n+    test_expect_success \\\n+        \"apply diff between $i and $j\" \\\n+\t\"git-apply <diff.$i-$j && diff frotz.$j frotz\"\n+  done\n+done\n"}]}