Commit Diff


commit - afbf14b354450516386d5af531a554eae463fd1f
commit + ff7f34d35c1ceac53ea203c97acadabfed48fca3
blob - 93ee4e2f11acb992d6fb5092844613caeb4d2112
blob + 5c820110e2837ee6a8cd741e88047a2a6b260bc7
--- lib/patch.c
+++ lib/patch.c
@@ -15,9 +15,6 @@
  *
  * Apply patches.
  *
- * Things that are still missing:
- *     + "No final newline" handling
- *
  * Things that we may want to support:
  *     + support indented patches?
  *     + support other kinds of patches?
@@ -57,6 +54,7 @@ struct got_patch_hunk {
 	STAILQ_ENTRY(got_patch_hunk) entries;
 	const struct got_error *err;
 	long	offset;
+	int	nonl;
 	long	old_from;
 	long	old_lines;
 	long	new_from;
@@ -200,6 +198,10 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got
 		case GOT_IMSG_PATCH_DONE:
 			goto done;
 		case GOT_IMSG_PATCH_HUNK:
+			if (h != NULL && h->nonl) {
+				err = got_error(GOT_ERR_PATCH_MALFORMED);
+				goto done;
+			}
 			datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
 			if (datalen != sizeof(hdr)) {
 				err = got_error(GOT_ERR_PRIVSEP_LEN);
@@ -223,16 +225,22 @@ recv_patch(struct imsgbuf *ibuf, int *done, struct got
 			}
 			datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
 			t = imsg.data;
-			/* at least one char plus newline */
+			/* at least one char */
 			if (datalen < 2 || t[datalen-1] != '\0') {
 				err = got_error(GOT_ERR_PRIVSEP_MSG);
 				goto done;
 			}
-			if (*t != ' ' && *t != '-' && *t != '+') {
+			if (*t != ' ' && *t != '-' && *t != '+' &&
+			    *t != '\\') {
 				err = got_error(GOT_ERR_PRIVSEP_MSG);
 				goto done;
 			}
-			err = pushline(h, t);
+			if (h->nonl)
+				err = got_error(GOT_ERR_PATCH_MALFORMED);
+			if (*t == '\\')
+				h->nonl = 1;
+			else
+				err = pushline(h, t);
 			if (err)
 				goto done;
 			break;
@@ -302,6 +310,8 @@ locate_hunk(FILE *orig, struct got_patch_hunk *h, off_
 				err = got_error(GOT_ERR_HUNK_FAILED);
 			break;
 		}
+		if (line[linelen - 1] == '\n')
+			line[linelen - 1] = '\0';
 		(*lineno)++;
 
 		if ((mode == ' ' && !strcmp(h->lines[0] + 1, line)) ||
@@ -354,6 +364,8 @@ test_hunk(FILE *orig, struct got_patch_hunk *h)
 					    GOT_ERR_HUNK_FAILED);
 				goto done;
 			}
+			if (line[linelen - 1] == '\n')
+				line[linelen - 1] = '\0';
 			if (strcmp(h->lines[i] + 1, line)) {
 				err = got_error(GOT_ERR_HUNK_FAILED);
 				goto done;
@@ -375,7 +387,7 @@ apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *
 	for (i = 0; i < h->len; ++i) {
 		switch (*h->lines[i]) {
 		case ' ':
-			if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
+			if (fprintf(tmp, "%s\n", h->lines[i] + 1) < 0)
 				return got_error_from_errno("fprintf");
 			/* fallthrough */
 		case '-':
@@ -384,6 +396,11 @@ apply_hunk(FILE *tmp, struct got_patch_hunk *h, long *
 		case '+':
 			if (fprintf(tmp, "%s", h->lines[i] + 1) < 0)
 				return got_error_from_errno("fprintf");
+			if (i != h->len - 1 || !h->nonl) {
+				if (fprintf(tmp, "\n") < 0)
+					return got_error_from_errno(
+					    "fprintf");
+			}
 			break;
 		}
 	}
blob - 26f7367c64b4782476828a3a83a22a307a19c83d
blob + 78e6d33f5861a66ec7f99cd55afc0429cbb446ae
--- libexec/got-read-patch/got-read-patch.c
+++ libexec/got-read-patch/got-read-patch.c
@@ -291,7 +291,7 @@ send_line(const char *line)
 	static const struct got_error *err = NULL;
 	char *p = NULL;
 
-	if (*line != '+' && *line != '-' && *line != ' ') {
+	if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
 		if (asprintf(&p, " %s", line) == -1)
 			return got_error_from_errno("asprintf");
 		line = p;
@@ -307,6 +307,32 @@ send_line(const char *line)
 }
 
 static const struct got_error *
+peek_special_line(FILE *fp, int send)
+{
+	const struct got_error *err;
+	char ch;
+
+	ch = fgetc(fp);
+	if (ch != EOF && ch != '\\') {
+		ungetc(ch, fp);
+		return NULL;
+	}
+
+	if (ch == '\\' && send) {
+		err = send_line("\\");
+		if (err)
+			return err;
+	}
+
+	while (ch != EOF && ch != '\n')
+		ch = fgetc(fp);
+
+	if (ch != EOF || feof(fp))
+		return NULL;
+	return got_error(GOT_ERR_IO);
+}
+
+static const struct got_error *
 parse_hunk(FILE *fp, int *ok)
 {
 	static const struct got_error *err = NULL;
@@ -351,13 +377,15 @@ parse_hunk(FILE *fp, int *ok)
 			err = got_error(GOT_ERR_PATCH_TRUNCATED);
 			goto done;
 		}
+		if (line[linelen - 1] == '\n')
+			line[linelen - 1] = '\0';
 
 		/* usr.bin/patch allows '=' as context char */
 		if (*line == '=')
 			*line = ' ';
 
 		ch = *line;
-		if (ch == '\t' || ch == '\n')
+		if (ch == '\t' || ch == '\0')
 			ch = ' ';	/* the space got eaten */
 
 		switch (ch) {
@@ -384,6 +412,13 @@ parse_hunk(FILE *fp, int *ok)
 		err = send_line(line);
 		if (err)
 			goto done;
+
+		if ((ch == '-' && leftold == 0) ||
+		    (ch == '+' && leftnew == 0)) {
+			err = peek_special_line(fp, ch == '+');
+			if (err)
+				goto done;
+		}
 	}
 
 done:
blob - 7be41fc2a24825f9f6432515b0f6f0c1f93b403b
blob + 654d85e31ae466ed47907c34cc051be70a8351e4
--- regress/cmdline/patch.sh
+++ regress/cmdline/patch.sh
@@ -1085,6 +1085,132 @@ EOF
 	test_done $testroot $ret
 }
 
+test_patch_no_newline() {
+	local testroot=`test_init patch_no_newline`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done $testroot $ret
+		return 1
+	fi
+
+	cat <<EOF > $testroot/wt/patch
+--- /dev/null
++++ eta
+@@ -0,0 +1 @@
++eta
+\ No newline at end of file
+EOF
+
+	(cd $testroot/wt && got patch patch) > $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done $testroot $ret
+		return 1
+	fi
+
+	echo "A  eta" > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done $testroot $ret
+		return 1
+	fi
+
+	echo -n eta > $testroot/wt/eta.expected
+	cmp -s $testroot/wt/eta.expected $testroot/wt/eta
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/wt/eta.expected $testroot/wt/eta
+		test_done $testroot $ret
+		return 1
+	fi
+
+	(cd $testroot/wt && got commit -m 'add eta') > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done $testroot $ret
+		return 1
+	fi
+
+	cat <<EOF > $testroot/wt/patch
+--- eta
++++ eta
+@@ -1 +1 @@
+-eta
+\ No newline at end of file
++ETA
+\ No newline at end of file
+EOF
+
+	(cd $testroot/wt && got patch patch) > $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done $testroot $ret
+		return 1
+	fi
+
+	echo "M  eta" > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done $testroot $ret
+		return 1
+	fi
+
+	echo -n ETA > $testroot/wt/eta.expected
+	cmp -s $testroot/wt/eta.expected $testroot/wt/eta
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/wt/eta.expected $testroot/wt/eta
+		test_done $testroot $ret
+		return 1
+	fi
+
+	(cd $testroot/wt && got commit -m 'edit eta') > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done $testroot $ret
+		return 1
+	fi
+
+	cat <<EOF > $testroot/wt/patch
+--- eta
++++ eta
+@@ -1 +1 @@
+-ETA
+\ No newline at end of file
++eta
+EOF
+
+	(cd $testroot/wt && got patch patch) > $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done $testroot $ret
+		return 1
+	fi
+
+	echo "M  eta" > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done $testroot $ret
+		return 1
+	fi
+
+	echo eta > $testroot/wt/eta.expected
+	cmp -s $testroot/wt/eta.expected $testroot/wt/eta
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/wt/eta.expected $testroot/wt/eta
+	fi
+	test_done $testroot $ret
+}
+
 test_parseargs "$@"
 run_test test_patch_simple_add_file
 run_test test_patch_simple_rm_file
@@ -1104,3 +1230,4 @@ run_test test_patch_preserve_perm
 run_test test_patch_create_dirs
 run_test test_patch_with_offset
 run_test test_patch_prefer_new_path
+run_test test_patch_no_newline