Commit dbc68eedb1de26bf5a444ecad5150b0a243d422b

Omar Polo 2022-06-21T16:52:34

got patch: ignore blobs not found since diffs are often enclosed in other formats (e.g. emails) we might parse something and think it's a blob id when it's not. This should already happens, but apply_patch is looking for the wrong error due to a leftover from previous attempts. Reported by stsp@ while here tweak the test_patch_merge_unknown_blob to also try with a dummy commit id, as now got-read-patch requires it in order to consider a blob id. ok stsp@

diff --git a/lib/patch.c b/lib/patch.c
index 9209e54..1fc82c6 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -643,7 +643,11 @@ apply_patch(int *overlapcnt, struct got_worktree *worktree,
 	/* don't run the diff3 merge on creations/deletions */
 	if (*p->blob != '\0' && p->old != NULL && p->new != NULL) {
 		err = open_blob(&apath, &afile, p->blob, repo);
-		if (err && err->code != GOT_ERR_NOT_REF)
+		/*
+		 * ignore failures to open this blob, we might have
+		 * parsed gibberish.
+		 */
+		if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
 			return err;
 		else if (err == NULL)
 			do_merge = 1;
diff --git a/regress/cmdline/patch.sh b/regress/cmdline/patch.sh
index 20e899e..5d79b58 100755
--- a/regress/cmdline/patch.sh
+++ b/regress/cmdline/patch.sh
@@ -1588,9 +1588,11 @@ test_patch_merge_unknown_blob() {
 
 	cat <<EOF > $testroot/wt/patch
 I've got a
+diff aaaabbbbccccddddeeeeffff0000111122223333 foo/bar
+with a
 blob - aaaabbbbccccddddeeeeffff0000111122223333
 and also a
-blob + 0000111122223333444455556666888899990000
+blob + 0000111122223333444455556666777788889999
 for this dummy diff
 --- alpha
 +++ alpha
@@ -1612,6 +1614,39 @@ EOF
 	ret=$?
 	if [ $ret -ne 0 ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done $testroot $ret
+		return 1
+	fi
+
+	# try again without a `diff' header
+
+	cat <<EOF > $testroot/wt/patch
+I've got a
+blob - aaaabbbbccccddddeeeeffff0000111122223333
+and also a
+blob + 0000111122223333444455556666777788889999
+for this dummy diff
+--- alpha
++++ alpha
+@@ -1 +1 @@
+-alpha
++ALPHA
+will it work?
+EOF
+
+	(cd $testroot/wt && got revert alpha > /dev/null && got patch patch) \
+		> $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done $testroot $ret
+		return 1
+	fi
+
+	echo 'M  alpha' > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
 	fi
 	test_done $testroot $ret
 }