2016-08-29 19:33:45 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='Test diff-highlight'
|
|
|
|
|
|
|
|
CURR_DIR=$(pwd)
|
|
|
|
TEST_OUTPUT_DIRECTORY=$(pwd)
|
|
|
|
TEST_DIRECTORY="$CURR_DIR"/../../../t
|
|
|
|
DIFF_HIGHLIGHT="$CURR_DIR"/../diff-highlight
|
|
|
|
|
|
|
|
CW="$(printf "\033[7m")" # white
|
|
|
|
CR="$(printf "\033[27m")" # reset
|
|
|
|
|
|
|
|
. "$TEST_DIRECTORY"/test-lib.sh
|
|
|
|
|
|
|
|
if ! test_have_prereq PERL
|
|
|
|
then
|
|
|
|
skip_all='skipping diff-highlight tests; perl not available'
|
|
|
|
test_done
|
|
|
|
fi
|
|
|
|
|
|
|
|
# dh_test is a test helper function which takes 3 file names as parameters. The
|
|
|
|
# first 2 files are used to generate diff and commit output, which is then
|
|
|
|
# piped through diff-highlight. The 3rd file should contain the expected output
|
|
|
|
# of diff-highlight (minus the diff/commit header, ie. everything after and
|
|
|
|
# including the first @@ line).
|
|
|
|
dh_test () {
|
|
|
|
a="$1" b="$2" &&
|
|
|
|
|
|
|
|
cat >patch.exp &&
|
|
|
|
|
|
|
|
{
|
|
|
|
cat "$a" >file &&
|
|
|
|
git add file &&
|
|
|
|
git commit -m "Add a file" &&
|
|
|
|
|
|
|
|
cat "$b" >file &&
|
|
|
|
git diff file >diff.raw &&
|
|
|
|
git commit -a -m "Update a file" &&
|
|
|
|
git show >commit.raw
|
|
|
|
} >/dev/null &&
|
|
|
|
|
|
|
|
"$DIFF_HIGHLIGHT" <diff.raw | test_strip_patch_header >diff.act &&
|
|
|
|
"$DIFF_HIGHLIGHT" <commit.raw | test_strip_patch_header >commit.act &&
|
|
|
|
test_cmp patch.exp diff.act &&
|
|
|
|
test_cmp patch.exp commit.act
|
|
|
|
}
|
|
|
|
|
|
|
|
test_strip_patch_header () {
|
|
|
|
sed -n '/^@@/,$p' $*
|
|
|
|
}
|
|
|
|
|
2016-08-29 19:33:46 +02:00
|
|
|
# dh_test_setup_history generates a contrived graph such that we have at least
|
|
|
|
# 1 nesting (E) and 2 nestings (F).
|
|
|
|
#
|
2018-03-21 06:49:26 +01:00
|
|
|
# A---B master
|
2018-03-21 06:47:45 +01:00
|
|
|
# /
|
2018-03-21 06:48:40 +01:00
|
|
|
# D---E---F branch
|
2016-08-29 19:33:46 +02:00
|
|
|
#
|
|
|
|
# git log --all --graph
|
|
|
|
# * commit
|
2018-03-21 06:49:26 +01:00
|
|
|
# | B
|
2016-08-29 19:33:46 +02:00
|
|
|
# | * commit
|
|
|
|
# | | F
|
2018-03-21 06:49:26 +01:00
|
|
|
# * | commit
|
|
|
|
# | | A
|
2016-08-29 19:33:46 +02:00
|
|
|
# | * commit
|
|
|
|
# |/
|
|
|
|
# | E
|
|
|
|
# * commit
|
|
|
|
# D
|
|
|
|
#
|
|
|
|
dh_test_setup_history () {
|
2018-03-21 06:48:53 +01:00
|
|
|
echo file1 >file &&
|
2016-08-29 19:33:46 +02:00
|
|
|
git add file &&
|
2018-03-21 06:48:40 +01:00
|
|
|
test_tick &&
|
2016-08-29 19:33:46 +02:00
|
|
|
git commit -m "D" &&
|
|
|
|
|
|
|
|
git checkout -b branch &&
|
2018-03-21 06:48:53 +01:00
|
|
|
echo file2 >file &&
|
2018-03-21 06:48:40 +01:00
|
|
|
test_tick &&
|
2016-08-29 19:33:46 +02:00
|
|
|
git commit -a -m "E" &&
|
|
|
|
|
2018-03-21 06:49:26 +01:00
|
|
|
git checkout master &&
|
|
|
|
echo file2 >file &&
|
|
|
|
test_tick &&
|
|
|
|
git commit -a -m "A" &&
|
|
|
|
|
|
|
|
git checkout branch &&
|
2018-03-21 06:48:53 +01:00
|
|
|
echo file3 >file &&
|
2018-03-21 06:48:40 +01:00
|
|
|
test_tick &&
|
|
|
|
git commit -a -m "F" &&
|
|
|
|
|
|
|
|
git checkout master &&
|
2018-03-21 06:49:26 +01:00
|
|
|
echo file3 >file &&
|
2018-03-21 06:48:40 +01:00
|
|
|
test_tick &&
|
2018-03-21 06:49:26 +01:00
|
|
|
git commit -a -m "B"
|
2016-08-29 19:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
left_trim () {
|
|
|
|
"$PERL_PATH" -pe 's/^\s+//'
|
|
|
|
}
|
|
|
|
|
|
|
|
trim_graph () {
|
|
|
|
# graphs start with * or |
|
|
|
|
# followed by a space or / or \
|
|
|
|
"$PERL_PATH" -pe 's@^((\*|\|)( |/|\\))+@@'
|
|
|
|
}
|
|
|
|
|
2016-08-29 19:33:45 +02:00
|
|
|
test_expect_success 'diff-highlight highlights the beginning of a line' '
|
|
|
|
cat >a <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
bbb
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat >b <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
0bb
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
|
|
|
|
dh_test a b <<-EOF
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
aaa
|
|
|
|
-${CW}b${CR}bb
|
|
|
|
+${CW}0${CR}bb
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'diff-highlight highlights the end of a line' '
|
|
|
|
cat >a <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
bbb
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat >b <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
bb0
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
|
|
|
|
dh_test a b <<-EOF
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
aaa
|
|
|
|
-bb${CW}b${CR}
|
|
|
|
+bb${CW}0${CR}
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'diff-highlight highlights the middle of a line' '
|
|
|
|
cat >a <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
bbb
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat >b <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
b0b
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
|
|
|
|
dh_test a b <<-EOF
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
aaa
|
|
|
|
-b${CW}b${CR}b
|
|
|
|
+b${CW}0${CR}b
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'diff-highlight does not highlight whole line' '
|
|
|
|
cat >a <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
bbb
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat >b <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
000
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
|
|
|
|
dh_test a b <<-EOF
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
aaa
|
|
|
|
-bbb
|
|
|
|
+000
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_failure 'diff-highlight highlights mismatched hunk size' '
|
|
|
|
cat >a <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
bbb
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat >b <<-\EOF &&
|
|
|
|
aaa
|
|
|
|
b0b
|
|
|
|
ccc
|
|
|
|
EOF
|
|
|
|
|
|
|
|
dh_test a b <<-EOF
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
aaa
|
|
|
|
-b${CW}b${CR}b
|
|
|
|
+b${CW}0${CR}b
|
|
|
|
+ccc
|
|
|
|
EOF
|
|
|
|
'
|
|
|
|
|
2016-08-31 07:03:10 +02:00
|
|
|
# These two code points share the same leading byte in UTF-8 representation;
|
|
|
|
# a naive byte-wise diff would highlight only the second byte.
|
|
|
|
#
|
|
|
|
# - U+00f3 ("o" with acute)
|
|
|
|
o_accent=$(printf '\303\263')
|
|
|
|
# - U+00f8 ("o" with stroke)
|
|
|
|
o_stroke=$(printf '\303\270')
|
|
|
|
|
|
|
|
test_expect_success 'diff-highlight treats multibyte utf-8 as a unit' '
|
|
|
|
echo "unic${o_accent}de" >a &&
|
|
|
|
echo "unic${o_stroke}de" >b &&
|
|
|
|
dh_test a b <<-EOF
|
|
|
|
@@ -1 +1 @@
|
|
|
|
-unic${CW}${o_accent}${CR}de
|
|
|
|
+unic${CW}${o_stroke}${CR}de
|
|
|
|
EOF
|
|
|
|
'
|
|
|
|
|
|
|
|
# Unlike the UTF-8 above, these are combining code points which are meant
|
|
|
|
# to modify the character preceding them:
|
|
|
|
#
|
|
|
|
# - U+0301 (combining acute accent)
|
|
|
|
combine_accent=$(printf '\314\201')
|
|
|
|
# - U+0302 (combining circumflex)
|
|
|
|
combine_circum=$(printf '\314\202')
|
|
|
|
|
|
|
|
test_expect_failure 'diff-highlight treats combining code points as a unit' '
|
|
|
|
echo "unico${combine_accent}de" >a &&
|
|
|
|
echo "unico${combine_circum}de" >b &&
|
|
|
|
dh_test a b <<-EOF
|
|
|
|
@@ -1 +1 @@
|
|
|
|
-unic${CW}o${combine_accent}${CR}de
|
|
|
|
+unic${CW}o${combine_circum}${CR}de
|
|
|
|
EOF
|
|
|
|
'
|
2016-08-29 19:33:45 +02:00
|
|
|
|
2016-08-29 19:33:47 +02:00
|
|
|
test_expect_success 'diff-highlight works with the --graph option' '
|
2016-08-29 19:33:46 +02:00
|
|
|
dh_test_setup_history &&
|
|
|
|
|
2018-03-21 06:49:26 +01:00
|
|
|
# date-order so that the commits are interleaved for both
|
2016-08-29 19:33:46 +02:00
|
|
|
# trim graph elements so we can do a diff
|
|
|
|
# trim leading space because our trim_graph is not perfect
|
2018-03-21 06:49:26 +01:00
|
|
|
git log --branches -p --date-order |
|
2016-08-29 19:33:46 +02:00
|
|
|
"$DIFF_HIGHLIGHT" | left_trim >graph.exp &&
|
2018-03-21 06:49:26 +01:00
|
|
|
git log --branches -p --date-order --graph |
|
2016-08-29 19:33:46 +02:00
|
|
|
"$DIFF_HIGHLIGHT" | trim_graph | left_trim >graph.act &&
|
|
|
|
test_cmp graph.exp graph.act
|
|
|
|
'
|
|
|
|
|
2018-03-21 06:49:57 +01:00
|
|
|
# Just reuse the previous graph test, but with --color. Our trimming
|
|
|
|
# doesn't know about color, so just sanity check that something got
|
|
|
|
# highlighted.
|
|
|
|
test_expect_success 'diff-highlight works with color graph' '
|
|
|
|
git log --branches -p --date-order --graph --color |
|
|
|
|
"$DIFF_HIGHLIGHT" | trim_graph | left_trim >graph &&
|
|
|
|
grep "\[7m" graph
|
|
|
|
'
|
|
|
|
|
2016-08-31 07:05:38 +02:00
|
|
|
# Most combined diffs won't meet diff-highlight's line-number filter. So we
|
|
|
|
# create one here where one side drops a line and the other modifies it. That
|
|
|
|
# should result in a diff like:
|
|
|
|
#
|
|
|
|
# - modified content
|
|
|
|
# ++resolved content
|
|
|
|
#
|
|
|
|
# which naively looks like one side added "+resolved".
|
|
|
|
test_expect_success 'diff-highlight ignores combined diffs' '
|
|
|
|
echo "content" >file &&
|
|
|
|
git add file &&
|
|
|
|
git commit -m base &&
|
|
|
|
|
|
|
|
>file &&
|
|
|
|
git commit -am master &&
|
|
|
|
|
|
|
|
git checkout -b other HEAD^ &&
|
|
|
|
echo "modified content" >file &&
|
|
|
|
git commit -am other &&
|
|
|
|
|
|
|
|
test_must_fail git merge master &&
|
|
|
|
echo "resolved content" >file &&
|
|
|
|
git commit -am resolved &&
|
|
|
|
|
|
|
|
cat >expect <<-\EOF &&
|
|
|
|
--- a/file
|
|
|
|
+++ b/file
|
|
|
|
@@@ -1,1 -1,0 +1,1 @@@
|
|
|
|
- modified content
|
|
|
|
++resolved content
|
|
|
|
EOF
|
|
|
|
|
|
|
|
git show -c | "$DIFF_HIGHLIGHT" >actual.raw &&
|
|
|
|
sed -n "/^---/,\$p" <actual.raw >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
diff-highlight: detect --graph by indent
This patch fixes a corner case where diff-highlight may
scramble some diffs when combined with --graph.
Commit 7e4ffb4c17 (diff-highlight: add support for --graph
output, 2016-08-29) taught diff-highlight to skip past the
graph characters at the start of each line with this regex:
($COLOR?\|$COLOR?\s+)*
I.e., any series of pipes separated by and followed by
arbitrary whitespace. We need to match more than just a
single space because the commit in question may be indented
to accommodate other parts of the graph drawing. E.g.:
* commit 1234abcd
| ...
| diff --git ...
has only a single space, but for the last commit before a
fork:
| | |
| * | commit 1234abcd
| |/ ...
| | diff --git
the diff lines have more spaces between the pipes and the
start of the diff.
However, when we soak up all of those spaces with the
$GRAPH regex, we may accidentally include the leading space
for a context line. That means we may consider the actual
contents of a context line as part of the diff syntax. In
other words, something like this:
normal context line
-old line
+new line
-this is a context line with a leading dash
would cause us to see that final context line as a removal
line, and we'd end up showing the hunk in the wrong order:
normal context line
-old line
-this is a context line with a leading dash
+new line
Instead, let's a be a little more clever about parsing the
graph. We'll look for the actual "*" line that marks the
start of a commit, and record the indentation we see there.
Then we can skip past that indentation when checking whether
the line is a hunk header, removal, addition, etc.
There is one tricky thing: the indentation in bytes may be
different for various lines of the graph due to coloring.
E.g., the "*" on a commit line is generally shown without
color, but on the actual diff lines, it will be replaced
with a colorized "|" character, adding several bytes. We
work around this here by counting "visible" bytes. This is
unfortunately a bit more expensive, making us about twice as
slow to handle --graph output. But since this is meant to be
used interactively anyway, it's tolerably fast (and the
non-graph case is unaffected).
One alternative would be to search for hunk header lines and
use their indentation (since they'd have the same colors as
the diff lines which follow). But that just opens up
different corner cases. If we see:
| | @@ 1,2 1,3 @@
we cannot know if this is a real diff that has been
indented due to the graph, or if it's a context line that
happens to look like a diff header. We can only be sure of
the indent on the "*" lines, since we know those don't
contain arbitrary data (technically the user could include a
bunch of extra indentation via --format, but that's rare
enough to disregard).
Reported-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-03-21 06:59:01 +01:00
|
|
|
test_expect_success 'diff-highlight handles --graph with leading dash' '
|
|
|
|
cat >file <<-\EOF &&
|
|
|
|
before
|
|
|
|
the old line
|
|
|
|
-leading dash
|
|
|
|
EOF
|
|
|
|
git add file &&
|
|
|
|
git commit -m before &&
|
|
|
|
|
|
|
|
sed s/old/new/ <file >file.tmp &&
|
|
|
|
mv file.tmp file &&
|
|
|
|
git add file &&
|
|
|
|
git commit -m after &&
|
|
|
|
|
|
|
|
cat >expect <<-EOF &&
|
|
|
|
--- a/file
|
|
|
|
+++ b/file
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
before
|
|
|
|
-the ${CW}old${CR} line
|
|
|
|
+the ${CW}new${CR} line
|
|
|
|
-leading dash
|
|
|
|
EOF
|
|
|
|
git log --graph -p -1 | "$DIFF_HIGHLIGHT" >actual.raw &&
|
|
|
|
trim_graph <actual.raw | sed -n "/^---/,\$p" >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2016-08-29 19:33:45 +02:00
|
|
|
test_done
|