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

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