threads / discuss / 36608

unexpected behaviour for "git diff --word-diff"

Subject: unexpected behaviour for "git diff --word-diff"

## tl;dr

One message between May 8, 2014 and May 8, 2014.

replies: 0people: 1as markdown or json

ovhrb6ho1pnnv7lo@mailinator.com· May 8, 2014, 16:39 UTC · lore
Hi,

I get an unexpected behaviour for "git diff --word-diff" if 2 adjacent lines start and end with the same letter. I tried versions 1.7.9 and 1.9.0.

Attached is a script with 7 examples. For each example, a file with 2 lines is compared to a file with the same lines, only 1 or 2 letters appended in each line. Each example is executed 4 times: with and without --word-diff-regex=., and with and without -R. The script prints the result of the diff of the 2 files. Just below it also prints the result of a counterexample: the same 2 lines with 2 lines (1 was sometimes not enough) of "xxx" between them.

Only for tests 5c,d and 6c,d the result matches the result for the counterexample.

Kind regards, Thomas

#!/bin/bash

########################################## # execute 6 tests with counterexamples for "git diff --word-diff" # # (optional) argument: (sub-)test to execute, e.g. "1" or "3b" # if none/empty: all tests are executed # # common header lines of the diff output are removed ##########################################

# init global variables: # command to execute GITDIFF="git diff --word-diff=plain --no-index --no-color" # names of test files file1=$0.1 file2=$0.2 file1x=$0.1x file2x=$0.2x # wanted test case, info to print want= info=

##########################################
# do_sub_test()
#   call "git diff" for a sub-test and its counterexample
#   common header lines of the diff output are removed
do_sub_test() {
    curr_sub_test=$1
    # set additional arguments for git diff
    case $curr_sub_test in
        *a) add_args=;;
        *b) add_args=-R;;
        *c) add_args=--word-diff-regex=.;;
        *d) add_args="-R --word-diff-regex=.";;
    esac
    # execute original test
    echo "  -- test" $curr_sub_test: DIFF $add_args: $info
    $GITDIFF $add_args $file1 $file2 | sed '/^[di+-]\|^@@ -1,2 +1,2 /d'
    # execute counterexample
    echo "    -- expected:"
    $GITDIFF $add_args $file1x $file2x | sed '/^[di+-]\|^xxx$\|^@@ -1,4 +1,4 /d'
}
##########################################
# do_test()
#   create files for a test, call do_sub_test for wanted sub-test (or all)
do_test() {
    curr_test=$1
    line1=$2
    line2=$3
    append=$4
    # create files for test
    echo -e "$line1\n$line2" > $file1
    echo -e "$line1$append\n$line2$append" > $file2
    # create files for counterexample
    echo -e "$line1\nxxx\nxxx\n$line2" > $file1x
    echo -e "$line1$append\nxxx\nxxx\n$line2$append" > $file2x
    info="append '$append' to '$line1' <nl> '$line2'"
    # call do_sub_test for wanted sub-test (or all)
    case $want in ?a|?|'') do_sub_test ${curr_test}a;; esac
    case $want in ?b|?|'') do_sub_test ${curr_test}b;; esac
    case $want in ?c|?|'') do_sub_test ${curr_test}c;; esac
    case $want in ?d|?|'') do_sub_test ${curr_test}d;; esac
}
##########################################
# do_all_tests()
#   call do_test for wanted test (or all)
do_all_tests() {
    want=$1
    case $want in 1*|'') do_test 1 '' .a .   ;; esac
    case $want in 2*|'') do_test 2 .  .a .   ;; esac
    case $want in 3*|'') do_test 3 '' a  a   ;; esac
    case $want in 4*|'') do_test 4 a  aa a   ;; esac
    case $want in 5*|'') do_test 5 '' a  b   ;; esac
    case $want in 6*|'') do_test 6 '' a  'b ';; esac
}

########################################## # main() # prepare cleanup, do tests

# clean up test files on exit and some signals trap "rm -f $file1 $file2 $file1x $file2x; exit" 0 1 2 15

usage="Usage: $0 [<testcase>]\n  <testcase> can be [1-6][a-d] or empty\n  e.g. '$0 1' or '$0 3b'"
case $1 in
    [1-6]|[1-6][a-d]|'')
    ;;
    -h|--help)
        echo -e $usage
        exit
    ;;
    *)
        echo -e "Error: invalid argument '$1'\n$usage"
        exit
    ;;
esac

# do tests do_all_tests $1

← back to recent threads