Blame SOURCES/0001-add-p-fix-counting-empty-context-lines-in-edited-pat.patch

6743a1
From 5be233541a4fc2e395087fe51a30a3664165e8bc Mon Sep 17 00:00:00 2001
6743a1
From: Phillip Wood <phillip.wood@dunelm.org.uk>
6743a1
Date: Fri, 1 Jun 2018 18:46:44 +0100
6743a1
Subject: [PATCH] add -p: fix counting empty context lines in edited patches
6743a1
6743a1
recount_edited_hunk() introduced in commit 2b8ea7f3c7 ("add -p:
6743a1
calculate offset delta for edited patches", 2018-03-05) required all
6743a1
context lines to start with a space, empty lines are not counted. This
6743a1
was intended to avoid any recounting problems if the user had
6743a1
introduced empty lines at the end when editing the patch. However this
6743a1
introduced a regression into 'git add -p' as it seems it is common for
6743a1
editors to strip the trailing whitespace from empty context lines when
6743a1
patches are edited thereby introducing empty lines that should be
6743a1
counted. 'git apply' knows how to deal with such empty lines and POSIX
6743a1
states that whether or not there is an space on an empty context line
6743a1
is implementation defined [1].
6743a1
6743a1
Fix the regression by counting lines consist solely of a newline as
6743a1
well as lines starting with a space as context lines and add a test to
6743a1
prevent future regressions.
6743a1
6743a1
[1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/diff.html
6743a1
6743a1
Reported-by: Mahmoud Al-Qudsi <mqudsi@neosmart.net>
6743a1
Reported-by: Oliver Joseph Ash <oliverjash@gmail.com>
6743a1
Reported-by: Jeff Felchner <jfelchner1@gmail.com>
6743a1
Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
6743a1
---
6743a1
 git-add--interactive.perl  |  2 +-
6743a1
 t/t3701-add-interactive.sh | 43 ++++++++++++++++++++++++++++++++++++++
6743a1
 2 files changed, 44 insertions(+), 1 deletion(-)
6743a1
6743a1
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
6743a1
index c1f52e457f..befbe8c749 100755
6743a1
--- a/git-add--interactive.perl
6743a1
+++ b/git-add--interactive.perl
6743a1
@@ -1055,7 +1055,7 @@ sub recount_edited_hunk {
6743a1
 			$o_cnt++;
6743a1
 		} elsif ($mode eq '+') {
6743a1
 			$n_cnt++;
6743a1
-		} elsif ($mode eq ' ') {
6743a1
+		} elsif ($mode eq ' ' or $_ eq "\n") {
6743a1
 			$o_cnt++;
6743a1
 			$n_cnt++;
6743a1
 		}
6743a1
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
6743a1
index b170fb02b8..3e9139dca8 100755
6743a1
--- a/t/t3701-add-interactive.sh
6743a1
+++ b/t/t3701-add-interactive.sh
6743a1
@@ -175,6 +175,49 @@ test_expect_success 'real edit works' '
6743a1
 	diff_cmp expected output
6743a1
 '
6743a1
 
6743a1
+test_expect_success 'setup file' '
6743a1
+	test_write_lines a "" b "" c >file &&
6743a1
+	git add file &&
6743a1
+	test_write_lines a "" d "" c >file
6743a1
+'
6743a1
+
6743a1
+test_expect_success 'setup patch' '
6743a1
+	SP=" " &&
6743a1
+	NULL="" &&
6743a1
+	cat >patch <<-EOF
6743a1
+	@@ -1,4 +1,4 @@
6743a1
+	 a
6743a1
+	$NULL
6743a1
+	-b
6743a1
+	+f
6743a1
+	$SP
6743a1
+	c
6743a1
+	EOF
6743a1
+'
6743a1
+
6743a1
+test_expect_success 'setup expected' '
6743a1
+	cat >expected <<-EOF
6743a1
+	diff --git a/file b/file
6743a1
+	index b5dd6c9..f910ae9 100644
6743a1
+	--- a/file
6743a1
+	+++ b/file
6743a1
+	@@ -1,5 +1,5 @@
6743a1
+	 a
6743a1
+	$SP
6743a1
+	-f
6743a1
+	+d
6743a1
+	$SP
6743a1
+	 c
6743a1
+	EOF
6743a1
+'
6743a1
+
6743a1
+test_expect_success 'edit can strip spaces from empty context lines' '
6743a1
+	test_write_lines e n q | git add -p 2>error &&
6743a1
+	test_must_be_empty error &&
6743a1
+	git diff >output &&
6743a1
+	diff_cmp expected output
6743a1
+'
6743a1
+
6743a1
 test_expect_success 'skip files similarly as commit -a' '
6743a1
 	git reset &&
6743a1
 	echo file >.gitignore &&