Commit Diff


commit - 4027dbc25c21bad271f5406e382054c6095523bc
commit + b3c57ab2fb7857f055b840d29c425370effc8f9c
blob - 59e2c3f9430e3ca55e7c81bba6faf5e7222d2d4f
blob + 7a531892158d2e38781806351827aa51d3c4970a
--- 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?
@@ -58,6 +55,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;
@@ -201,6 +199,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);
@@ -224,16 +226,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;
@@ -303,6 +311,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)) ||
@@ -355,6 +365,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;
@@ -376,7 +388,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 '-':
@@ -385,6 +397,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 - 6e96334c3fc78f9bf784764803cf0d4a20adfe0f
blob + c55af97b36ddc6a225937a9d176a6954f0ac66aa
--- libexec/got-read-patch/got-read-patch.c
+++ libexec/got-read-patch/got-read-patch.c
@@ -292,7 +292,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;
@@ -308,6 +308,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;
@@ -352,13 +378,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) {
@@ -385,6 +413,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