# [BUG] diff --word-diff inconsistently places removed words

2 messages from 2015-10-27 to 2015-10-27. Participants: Lucian Poston.
Thread: https://gitlist.dev/t/40653

## Lucian Poston, 2015-10-27 01:40

Subject: [BUG] diff --word-diff inconsistently places removed words
Message-ID: <1445910013-13382-1-git-send-email-lucian.poston@gmail.com>
URL: https://gitlist.dev/e/1445910013-13382-1-git-send-email-lucian.poston%40gmail.com

```
In the output generated by diff --word-diff, if the first word of a line was
removed, it may appear at the end of the previous line. For example, compare
the following two diffs on the same files, the first diff without --word-diff
and the second with it.


$ git diff

diff --git a/smallest-test b/smallest-test
index bc5f07a..d9116b1 100644
--- a/smallest-test
+++ b/smallest-test
@@ -1,7 +1,7 @@
 
-111 aaa
-111 aaa
+ aaa
+ aaa
 
 111 aaa
-111 aaa
+ aaa
 


$ git diff --word-diff=plain

diff --git a/smallest-test b/smallest-test
index bc5f07a..d9116b1 100644
--- a/smallest-test
+++ b/smallest-test
@@ -1,7 +1,7 @@

[-111-] aaa[-111-]
 aaa

111 aaa
[-111-] aaa


I would expect every [-111-] to be on the line where it was removed. Instead
it appears on the previous line in the case where the previous line also has a
removal.

Is this expected behavior?

There is also a patch in this email thread that contains a test to further
illustrate the issue. Running the test fails with the following diff between
the expected result and actual result.

--- expect	2015-10-27 01:08:05.415252713 +0000
+++ output.decrypted	2015-10-27 01:08:05.418252766 +0000
@@ -6,5 +6,5 @@
 11aa<RESET>
 <RED>11<RESET>aa
 
-<RED>11<RESET>aa
-<RED>11<RESET>aa
+<RED>11<RESET>aa<RED>11<RESET>
+aa


Lucian Poston (1):
  t4034: Test parsing words following newline

 t/t4034-diff-words.sh | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

-- 
2.4.10

```

## Lucian Poston, 2015-10-27 01:40

Subject: [PATCH] t4034: Test parsing words following newline
Message-ID: <1445910013-13382-2-git-send-email-lucian.poston@gmail.com>
URL: https://gitlist.dev/e/1445910013-13382-2-git-send-email-lucian.poston%40gmail.com
In-Reply-To: <1445910013-13382-1-git-send-email-lucian.poston@gmail.com>

```
Add test that shows diff --word-diff's inconsistent placement of removed
words. This occurs when both the removed word is first on its line and the
previous line has a removed word.

Signed-off-by: Lucian Poston <lucian.poston@gmail.com>
---
 t/t4034-diff-words.sh | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
index f2f55fc..8d9f1b7 100755
--- a/t/t4034-diff-words.sh
+++ b/t/t4034-diff-words.sh
@@ -265,6 +265,36 @@ test_expect_success 'test parsing words for newline' '
 	word_diff --color-words="a+"
 '
 
+test_expect_success 'test parsing words following newline' '
+	cat >pre <<-\EOF &&
+		11aa
+		11aa
+		
+		11aa
+		11aa
+	EOF
+	cat >post <<-\EOF &&
+		11aa
+		aa
+		
+		aa
+		aa
+	EOF
+	cat >expect <<-\EOF &&
+		<BOLD>diff --git a/pre b/post<RESET>
+		<BOLD>index 2e0acd4..f6e6721 100644<RESET>
+		<BOLD>--- a/pre<RESET>
+		<BOLD>+++ b/post<RESET>
+		<CYAN>@@ -1,5 +1,5 @@<RESET>
+		11aa<RESET>
+		<RED>11<RESET>aa
+		
+		<RED>11<RESET>aa
+		<RED>11<RESET>aa
+	EOF
+	word_diff --color-words="a+|1+"
+'
+
 test_expect_success 'test when words are only removed at the end' '
 	echo "(:" >pre &&
 	echo "(" >post &&
-- 
2.4.10

```
