color-words: change algorithm to allow for 0-character word boundaries
Up until now, the color-words code assumed that word boundaries are
identical to white space characters.
Therefore, it could get away with a very simple scheme: it copied the
hunks, substituted newlines for each white space character, called
libxdiff with the processed text, and then identified the text to
output by the offsets (which agreed since the original text had the
same length).
This code was ugly, for a number of reasons:
- it was impossible to introduce 0-character word boundaries,
- we had to print everything word by word, and
- the code needed extra special handling of newlines in the removed part.
Fix all of these issues by processing the text such that
- we build word lists, separated by newlines,
- we remember the original offsets for every word, and
- after calling libxdiff on the wordlists, we parse the hunk headers, and
find the corresponding offsets, and then
- we print the removed/added parts in one go.
The pre and post samples in the test were provided by Santi Béjar.
Note that there is some strange special handling of hunk headers where
one line range is 0 due to POSIX: in this case, the start is one too
low. In other words a hunk header '@@ -1,0 +2 @@' actually means that
the line must be added after the _second_ line of the pre text, _not_
the first.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-01-18 00:29:44 +08:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='word diff colors'
|
|
|
|
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
|
|
|
test_expect_success setup '
|
|
|
|
|
2010-10-31 09:46:54 +08:00
|
|
|
git config diff.color.old red &&
|
|
|
|
git config diff.color.new green &&
|
2009-11-27 14:55:18 +08:00
|
|
|
git config diff.color.func magenta
|
color-words: change algorithm to allow for 0-character word boundaries
Up until now, the color-words code assumed that word boundaries are
identical to white space characters.
Therefore, it could get away with a very simple scheme: it copied the
hunks, substituted newlines for each white space character, called
libxdiff with the processed text, and then identified the text to
output by the offsets (which agreed since the original text had the
same length).
This code was ugly, for a number of reasons:
- it was impossible to introduce 0-character word boundaries,
- we had to print everything word by word, and
- the code needed extra special handling of newlines in the removed part.
Fix all of these issues by processing the text such that
- we build word lists, separated by newlines,
- we remember the original offsets for every word, and
- after calling libxdiff on the wordlists, we parse the hunk headers, and
find the corresponding offsets, and then
- we print the removed/added parts in one go.
The pre and post samples in the test were provided by Santi Béjar.
Note that there is some strange special handling of hunk headers where
one line range is 0 due to POSIX: in this case, the start is one too
low. In other words a hunk header '@@ -1,0 +2 @@' actually means that
the line must be added after the _second_ line of the pre text, _not_
the first.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-01-18 00:29:44 +08:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
word_diff () {
|
|
|
|
test_must_fail git diff --no-index "$@" pre post > output &&
|
2009-12-08 18:12:02 +08:00
|
|
|
test_decode_color <output >output.decrypted &&
|
color-words: change algorithm to allow for 0-character word boundaries
Up until now, the color-words code assumed that word boundaries are
identical to white space characters.
Therefore, it could get away with a very simple scheme: it copied the
hunks, substituted newlines for each white space character, called
libxdiff with the processed text, and then identified the text to
output by the offsets (which agreed since the original text had the
same length).
This code was ugly, for a number of reasons:
- it was impossible to introduce 0-character word boundaries,
- we had to print everything word by word, and
- the code needed extra special handling of newlines in the removed part.
Fix all of these issues by processing the text such that
- we build word lists, separated by newlines,
- we remember the original offsets for every word, and
- after calling libxdiff on the wordlists, we parse the hunk headers, and
find the corresponding offsets, and then
- we print the removed/added parts in one go.
The pre and post samples in the test were provided by Santi Béjar.
Note that there is some strange special handling of hunk headers where
one line range is 0 due to POSIX: in this case, the start is one too
low. In other words a hunk header '@@ -1,0 +2 @@' actually means that
the line must be added after the _second_ line of the pre text, _not_
the first.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-01-18 00:29:44 +08:00
|
|
|
test_cmp expect output.decrypted
|
|
|
|
}
|
|
|
|
|
|
|
|
cat > pre <<\EOF
|
|
|
|
h(4)
|
|
|
|
|
|
|
|
a = b + c
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat > post <<\EOF
|
|
|
|
h(4),hh[44]
|
|
|
|
|
|
|
|
a = b + c
|
|
|
|
|
|
|
|
aa = a
|
|
|
|
|
|
|
|
aeff = aeff * ( aaa )
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat > expect <<\EOF
|
2010-10-21 06:17:25 +08:00
|
|
|
<BOLD>diff --git a/pre b/post<RESET>
|
|
|
|
<BOLD>index 330b04f..5ed8eff 100644<RESET>
|
|
|
|
<BOLD>--- a/pre<RESET>
|
|
|
|
<BOLD>+++ b/post<RESET>
|
2009-12-08 18:12:02 +08:00
|
|
|
<CYAN>@@ -1,3 +1,7 @@<RESET>
|
color-words: change algorithm to allow for 0-character word boundaries
Up until now, the color-words code assumed that word boundaries are
identical to white space characters.
Therefore, it could get away with a very simple scheme: it copied the
hunks, substituted newlines for each white space character, called
libxdiff with the processed text, and then identified the text to
output by the offsets (which agreed since the original text had the
same length).
This code was ugly, for a number of reasons:
- it was impossible to introduce 0-character word boundaries,
- we had to print everything word by word, and
- the code needed extra special handling of newlines in the removed part.
Fix all of these issues by processing the text such that
- we build word lists, separated by newlines,
- we remember the original offsets for every word, and
- after calling libxdiff on the wordlists, we parse the hunk headers, and
find the corresponding offsets, and then
- we print the removed/added parts in one go.
The pre and post samples in the test were provided by Santi Béjar.
Note that there is some strange special handling of hunk headers where
one line range is 0 due to POSIX: in this case, the start is one too
low. In other words a hunk header '@@ -1,0 +2 @@' actually means that
the line must be added after the _second_ line of the pre text, _not_
the first.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-01-18 00:29:44 +08:00
|
|
|
<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
|
2009-11-28 14:04:10 +08:00
|
|
|
|
color-words: change algorithm to allow for 0-character word boundaries
Up until now, the color-words code assumed that word boundaries are
identical to white space characters.
Therefore, it could get away with a very simple scheme: it copied the
hunks, substituted newlines for each white space character, called
libxdiff with the processed text, and then identified the text to
output by the offsets (which agreed since the original text had the
same length).
This code was ugly, for a number of reasons:
- it was impossible to introduce 0-character word boundaries,
- we had to print everything word by word, and
- the code needed extra special handling of newlines in the removed part.
Fix all of these issues by processing the text such that
- we build word lists, separated by newlines,
- we remember the original offsets for every word, and
- after calling libxdiff on the wordlists, we parse the hunk headers, and
find the corresponding offsets, and then
- we print the removed/added parts in one go.
The pre and post samples in the test were provided by Santi Béjar.
Note that there is some strange special handling of hunk headers where
one line range is 0 due to POSIX: in this case, the start is one too
low. In other words a hunk header '@@ -1,0 +2 @@' actually means that
the line must be added after the _second_ line of the pre text, _not_
the first.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-01-18 00:29:44 +08:00
|
|
|
a = b + c<RESET>
|
|
|
|
|
|
|
|
<GREEN>aa = a<RESET>
|
|
|
|
|
|
|
|
<GREEN>aeff = aeff * ( aaa )<RESET>
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success 'word diff with runs of whitespace' '
|
|
|
|
|
|
|
|
word_diff --color-words
|
|
|
|
|
|
|
|
'
|
|
|
|
|
2010-04-14 23:59:06 +08:00
|
|
|
test_expect_success '--word-diff=color' '
|
|
|
|
|
|
|
|
word_diff --word-diff=color
|
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '--color --word-diff=color' '
|
|
|
|
|
|
|
|
word_diff --color --word-diff=color
|
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
sed 's/#.*$//' > expect <<EOF
|
|
|
|
diff --git a/pre b/post
|
|
|
|
index 330b04f..5ed8eff 100644
|
|
|
|
--- a/pre
|
|
|
|
+++ b/post
|
|
|
|
@@ -1,3 +1,7 @@
|
|
|
|
-h(4)
|
|
|
|
+h(4),hh[44]
|
|
|
|
~
|
|
|
|
# significant space
|
|
|
|
~
|
|
|
|
a = b + c
|
|
|
|
~
|
|
|
|
~
|
|
|
|
+aa = a
|
|
|
|
~
|
|
|
|
~
|
|
|
|
+aeff = aeff * ( aaa )
|
|
|
|
~
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success '--word-diff=porcelain' '
|
|
|
|
|
|
|
|
word_diff --word-diff=porcelain
|
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
cat > expect <<EOF
|
|
|
|
diff --git a/pre b/post
|
|
|
|
index 330b04f..5ed8eff 100644
|
|
|
|
--- a/pre
|
|
|
|
+++ b/post
|
|
|
|
@@ -1,3 +1,7 @@
|
|
|
|
[-h(4)-]{+h(4),hh[44]+}
|
|
|
|
|
|
|
|
a = b + c
|
|
|
|
|
|
|
|
{+aa = a+}
|
|
|
|
|
|
|
|
{+aeff = aeff * ( aaa )+}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success '--word-diff=plain' '
|
|
|
|
|
|
|
|
word_diff --word-diff=plain
|
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '--word-diff=plain --no-color' '
|
|
|
|
|
|
|
|
word_diff --word-diff=plain --no-color
|
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
cat > expect <<EOF
|
2010-10-21 06:17:25 +08:00
|
|
|
<BOLD>diff --git a/pre b/post<RESET>
|
|
|
|
<BOLD>index 330b04f..5ed8eff 100644<RESET>
|
|
|
|
<BOLD>--- a/pre<RESET>
|
|
|
|
<BOLD>+++ b/post<RESET>
|
2010-04-14 23:59:06 +08:00
|
|
|
<CYAN>@@ -1,3 +1,7 @@<RESET>
|
|
|
|
<RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
|
|
|
|
|
|
|
|
a = b + c<RESET>
|
|
|
|
|
|
|
|
<GREEN>{+aa = a+}<RESET>
|
|
|
|
|
|
|
|
<GREEN>{+aeff = aeff * ( aaa )+}<RESET>
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success '--word-diff=plain --color' '
|
|
|
|
|
|
|
|
word_diff --word-diff=plain --color
|
|
|
|
|
|
|
|
'
|
|
|
|
|
2009-10-28 20:24:30 +08:00
|
|
|
cat > expect <<\EOF
|
2010-10-21 06:17:25 +08:00
|
|
|
<BOLD>diff --git a/pre b/post<RESET>
|
|
|
|
<BOLD>index 330b04f..5ed8eff 100644<RESET>
|
|
|
|
<BOLD>--- a/pre<RESET>
|
|
|
|
<BOLD>+++ b/post<RESET>
|
2009-12-28 15:01:32 +08:00
|
|
|
<CYAN>@@ -1 +1 @@<RESET>
|
2009-10-28 20:24:30 +08:00
|
|
|
<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
|
2009-12-28 15:01:32 +08:00
|
|
|
<CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
|
2009-10-28 20:24:30 +08:00
|
|
|
|
|
|
|
<GREEN>aa = a<RESET>
|
|
|
|
|
|
|
|
<GREEN>aeff = aeff * ( aaa )<RESET>
|
|
|
|
EOF
|
|
|
|
|
2009-10-29 18:45:03 +08:00
|
|
|
test_expect_success 'word diff without context' '
|
2009-10-28 20:24:30 +08:00
|
|
|
|
|
|
|
word_diff --color-words --unified=0
|
|
|
|
|
|
|
|
'
|
|
|
|
|
2009-01-18 00:29:45 +08:00
|
|
|
cat > expect <<\EOF
|
2010-10-21 06:17:25 +08:00
|
|
|
<BOLD>diff --git a/pre b/post<RESET>
|
|
|
|
<BOLD>index 330b04f..5ed8eff 100644<RESET>
|
|
|
|
<BOLD>--- a/pre<RESET>
|
|
|
|
<BOLD>+++ b/post<RESET>
|
2009-12-08 18:12:02 +08:00
|
|
|
<CYAN>@@ -1,3 +1,7 @@<RESET>
|
2009-01-18 00:29:45 +08:00
|
|
|
h(4),<GREEN>hh<RESET>[44]
|
2009-11-28 14:04:10 +08:00
|
|
|
|
2009-01-18 00:29:45 +08:00
|
|
|
a = b + c<RESET>
|
|
|
|
|
|
|
|
<GREEN>aa = a<RESET>
|
|
|
|
|
|
|
|
<GREEN>aeff = aeff * ( aaa<RESET> )
|
|
|
|
EOF
|
2009-01-21 11:46:57 +08:00
|
|
|
cp expect expect.letter-runs-are-words
|
2009-01-18 00:29:45 +08:00
|
|
|
|
|
|
|
test_expect_success 'word diff with a regular expression' '
|
|
|
|
|
|
|
|
word_diff --color-words="[a-z]+"
|
|
|
|
|
|
|
|
'
|
|
|
|
|
2009-01-18 00:29:48 +08:00
|
|
|
test_expect_success 'set a diff driver' '
|
2009-01-21 12:59:54 +08:00
|
|
|
git config diff.testdriver.wordRegex "[^[:space:]]" &&
|
2009-01-18 00:29:48 +08:00
|
|
|
cat <<EOF > .gitattributes
|
|
|
|
pre diff=testdriver
|
|
|
|
post diff=testdriver
|
|
|
|
EOF
|
|
|
|
'
|
|
|
|
|
2009-01-21 11:46:57 +08:00
|
|
|
test_expect_success 'option overrides .gitattributes' '
|
2009-01-18 00:29:48 +08:00
|
|
|
|
|
|
|
word_diff --color-words="[a-z]+"
|
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
cat > expect <<\EOF
|
2010-10-21 06:17:25 +08:00
|
|
|
<BOLD>diff --git a/pre b/post<RESET>
|
|
|
|
<BOLD>index 330b04f..5ed8eff 100644<RESET>
|
|
|
|
<BOLD>--- a/pre<RESET>
|
|
|
|
<BOLD>+++ b/post<RESET>
|
2009-12-08 18:12:02 +08:00
|
|
|
<CYAN>@@ -1,3 +1,7 @@<RESET>
|
2009-01-18 00:29:48 +08:00
|
|
|
h(4)<GREEN>,hh[44]<RESET>
|
2009-11-28 14:04:10 +08:00
|
|
|
|
2009-01-18 00:29:48 +08:00
|
|
|
a = b + c<RESET>
|
|
|
|
|
|
|
|
<GREEN>aa = a<RESET>
|
|
|
|
|
|
|
|
<GREEN>aeff = aeff * ( aaa )<RESET>
|
|
|
|
EOF
|
2009-01-21 11:46:57 +08:00
|
|
|
cp expect expect.non-whitespace-is-word
|
2009-01-18 00:29:48 +08:00
|
|
|
|
2009-01-21 11:46:57 +08:00
|
|
|
test_expect_success 'use regex supplied by driver' '
|
2009-01-18 00:29:48 +08:00
|
|
|
|
|
|
|
word_diff --color-words
|
|
|
|
|
|
|
|
'
|
|
|
|
|
2009-01-21 12:59:54 +08:00
|
|
|
test_expect_success 'set diff.wordRegex option' '
|
|
|
|
git config diff.wordRegex "[[:alnum:]]+"
|
2009-01-21 11:46:57 +08:00
|
|
|
'
|
|
|
|
|
|
|
|
cp expect.letter-runs-are-words expect
|
|
|
|
|
|
|
|
test_expect_success 'command-line overrides config' '
|
|
|
|
word_diff --color-words="[a-z]+"
|
|
|
|
'
|
|
|
|
|
2010-04-14 23:59:06 +08:00
|
|
|
cat > expect <<\EOF
|
2010-10-21 06:17:25 +08:00
|
|
|
<BOLD>diff --git a/pre b/post<RESET>
|
|
|
|
<BOLD>index 330b04f..5ed8eff 100644<RESET>
|
|
|
|
<BOLD>--- a/pre<RESET>
|
|
|
|
<BOLD>+++ b/post<RESET>
|
2010-04-14 23:59:06 +08:00
|
|
|
<CYAN>@@ -1,3 +1,7 @@<RESET>
|
|
|
|
h(4),<GREEN>{+hh+}<RESET>[44]
|
|
|
|
|
|
|
|
a = b + c<RESET>
|
|
|
|
|
|
|
|
<GREEN>{+aa = a+}<RESET>
|
|
|
|
|
|
|
|
<GREEN>{+aeff = aeff * ( aaa+}<RESET> )
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success 'command-line overrides config: --word-diff-regex' '
|
|
|
|
word_diff --color --word-diff-regex="[a-z]+"
|
|
|
|
'
|
|
|
|
|
2009-01-21 11:46:57 +08:00
|
|
|
cp expect.non-whitespace-is-word expect
|
|
|
|
|
|
|
|
test_expect_success '.gitattributes override config' '
|
|
|
|
word_diff --color-words
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'remove diff driver regex' '
|
2009-01-21 12:59:54 +08:00
|
|
|
git config --unset diff.testdriver.wordRegex
|
2009-01-21 11:46:57 +08:00
|
|
|
'
|
|
|
|
|
|
|
|
cat > expect <<\EOF
|
2010-10-21 06:17:25 +08:00
|
|
|
<BOLD>diff --git a/pre b/post<RESET>
|
|
|
|
<BOLD>index 330b04f..5ed8eff 100644<RESET>
|
|
|
|
<BOLD>--- a/pre<RESET>
|
|
|
|
<BOLD>+++ b/post<RESET>
|
2009-12-08 18:12:02 +08:00
|
|
|
<CYAN>@@ -1,3 +1,7 @@<RESET>
|
2009-01-21 11:46:57 +08:00
|
|
|
h(4),<GREEN>hh[44<RESET>]
|
2009-11-28 14:04:10 +08:00
|
|
|
|
2009-01-21 11:46:57 +08:00
|
|
|
a = b + c<RESET>
|
|
|
|
|
|
|
|
<GREEN>aa = a<RESET>
|
|
|
|
|
|
|
|
<GREEN>aeff = aeff * ( aaa<RESET> )
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success 'use configured regex' '
|
|
|
|
word_diff --color-words
|
|
|
|
'
|
|
|
|
|
2009-01-18 00:29:45 +08:00
|
|
|
echo 'aaa (aaa)' > pre
|
|
|
|
echo 'aaa (aaa) aaa' > post
|
|
|
|
|
|
|
|
cat > expect <<\EOF
|
2010-10-21 06:17:25 +08:00
|
|
|
<BOLD>diff --git a/pre b/post<RESET>
|
|
|
|
<BOLD>index c29453b..be22f37 100644<RESET>
|
|
|
|
<BOLD>--- a/pre<RESET>
|
|
|
|
<BOLD>+++ b/post<RESET>
|
2009-12-08 18:12:02 +08:00
|
|
|
<CYAN>@@ -1 +1 @@<RESET>
|
2009-01-18 00:29:45 +08:00
|
|
|
aaa (aaa) <GREEN>aaa<RESET>
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success 'test parsing words for newline' '
|
|
|
|
|
|
|
|
word_diff --color-words="a+"
|
|
|
|
|
2009-01-18 00:29:48 +08:00
|
|
|
|
2009-01-18 00:29:45 +08:00
|
|
|
'
|
|
|
|
|
|
|
|
echo '(:' > pre
|
|
|
|
echo '(' > post
|
|
|
|
|
|
|
|
cat > expect <<\EOF
|
2010-10-21 06:17:25 +08:00
|
|
|
<BOLD>diff --git a/pre b/post<RESET>
|
|
|
|
<BOLD>index 289cb9d..2d06f37 100644<RESET>
|
|
|
|
<BOLD>--- a/pre<RESET>
|
|
|
|
<BOLD>+++ b/post<RESET>
|
2009-12-08 18:12:02 +08:00
|
|
|
<CYAN>@@ -1 +1 @@<RESET>
|
2009-01-18 00:29:45 +08:00
|
|
|
(<RED>:<RESET>
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success 'test when words are only removed at the end' '
|
|
|
|
|
|
|
|
word_diff --color-words=.
|
|
|
|
|
|
|
|
'
|
|
|
|
|
2010-04-14 23:59:06 +08:00
|
|
|
cat > expect <<\EOF
|
|
|
|
diff --git a/pre b/post
|
|
|
|
index 289cb9d..2d06f37 100644
|
|
|
|
--- a/pre
|
|
|
|
+++ b/post
|
|
|
|
@@ -1 +1 @@
|
|
|
|
-(:
|
|
|
|
+(
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success '--word-diff=none' '
|
|
|
|
|
|
|
|
word_diff --word-diff=plain --word-diff=none
|
|
|
|
|
|
|
|
'
|
|
|
|
|
color-words: change algorithm to allow for 0-character word boundaries
Up until now, the color-words code assumed that word boundaries are
identical to white space characters.
Therefore, it could get away with a very simple scheme: it copied the
hunks, substituted newlines for each white space character, called
libxdiff with the processed text, and then identified the text to
output by the offsets (which agreed since the original text had the
same length).
This code was ugly, for a number of reasons:
- it was impossible to introduce 0-character word boundaries,
- we had to print everything word by word, and
- the code needed extra special handling of newlines in the removed part.
Fix all of these issues by processing the text such that
- we build word lists, separated by newlines,
- we remember the original offsets for every word, and
- after calling libxdiff on the wordlists, we parse the hunk headers, and
find the corresponding offsets, and then
- we print the removed/added parts in one go.
The pre and post samples in the test were provided by Santi Béjar.
Note that there is some strange special handling of hunk headers where
one line range is 0 due to POSIX: in this case, the start is one too
low. In other words a hunk header '@@ -1,0 +2 @@' actually means that
the line must be added after the _second_ line of the pre text, _not_
the first.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-01-18 00:29:44 +08:00
|
|
|
test_done
|