From c9007797b11ff8347ea47e3e2e71b7a1f6d7e278 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Wed, 17 Oct 2012 22:00:17 +0200 Subject: [PATCH] gitconfig: Add diff.wordregex for better git --color-words. Also add t/ which some tests and examples. --- gitconfig.m4 | 10 +++ t/expected-empty.txt | 26 ++++++ t/expected-nonspaces.txt | 27 ++++++ t/expected-word-nonword.txt | 26 ++++++ t/expected-words-nonword.txt | 26 ++++++ t/expected-words-nonwords-spaces.txt | 26 ++++++ t/expected-words-nonwords.txt | 26 ++++++ t/expected-words-nonwordspaces-spaces.txt | 26 ++++++ t/expected-words-nonwordspaces.txt | 26 ++++++ t/test.sh | 103 ++++++++++++++++++++++ 10 files changed, 322 insertions(+) create mode 100644 t/expected-empty.txt create mode 100644 t/expected-nonspaces.txt create mode 100644 t/expected-word-nonword.txt create mode 100644 t/expected-words-nonword.txt create mode 100644 t/expected-words-nonwords-spaces.txt create mode 100644 t/expected-words-nonwords.txt create mode 100644 t/expected-words-nonwordspaces-spaces.txt create mode 100644 t/expected-words-nonwordspaces.txt create mode 100755 t/test.sh diff --git a/gitconfig.m4 b/gitconfig.m4 index 439f780..f7bfd4e 100644 --- a/gitconfig.m4 +++ b/gitconfig.m4 @@ -118,6 +118,16 @@ include(../lib.m4) # Detect copies and renames. renames = copy + # Change the definition of a word as used by diff --color-words to be + # shorter (not only spaces) and thus simplify the generated diffs. + # Words ([a-zA-Z0-9_]+) are matched, or a single non-word character + # ([^a-zA-Z0-9_]), therefore changes to words are shown in complete + # (e.g. from "word" to "newword" as "[-word-]{+newword+}"), but + # changes to non-word characters are shown character wise (e.g. from + # "==" to "!=" as "[-=-]{+!+}="); [-..-] is removal, {+..+} is + # addition. See t/ for some tests and examples. + wordregex = [a-zA-Z0-9_]+|[^a-zA-Z0-9_] + # Allow diffing of some binary files. # `pdftotext-` is a wrapper around pdftotext which writes to stdout. # `sqlite3dump` is a wrapper calling `sqlite3 database-file .dump`. diff --git a/t/expected-empty.txt b/t/expected-empty.txt new file mode 100644 index 0000000..3440328 --- /dev/null +++ b/t/expected-empty.txt @@ -0,0 +1,26 @@ +diff --git a/test.txt b/test.txt +--- a/test.txt ++++ b/test.txt +@@ -1,21 +1,22 @@ +Tests +==== + +Short sentence with words! + +Longer sentence even more words, ain't that nice! + +And other one, just testing. + +option: new value +option-two: new-value-two +option-three: "value-three" + +function testme() { + firstCall(); + while (a != b) { + if (!second_var) { + third_call() && fourth_call() \ + || fifth_call(); + } + } + return 42; diff --git a/t/expected-nonspaces.txt b/t/expected-nonspaces.txt new file mode 100644 index 0000000..4a86a16 --- /dev/null +++ b/t/expected-nonspaces.txt @@ -0,0 +1,27 @@ +diff --git a/test.txt b/test.txt +--- a/test.txt ++++ b/test.txt +@@ -1,21 +1,22 @@ +Tests +[-=====-]{+====+} + +[-Simple-]{+Short+} sentence with [-a few words.-]{+words!+} + +Longer sentence[-with-] even more [-words ..-]{+words,+} ain't that [-nice?-]{+nice!+} + +And [-another-]{+other+} one, just testing. + +option: {+new+} value +option-two: [-value-two-]{+new-value-two+} +option-three: [-value-three-]{+"value-three"+} + +function testme() { + [-first_call();-] +[- if-]{+firstCall();+} +{+ while+} (a [-==-]{+!=+} b) { + if [-(!second_call())-]{+(!second_var)+} { + [-third_call();-]{+third_call() && fourth_call() \+} +{+ || fifth_call();+} + } + } + return 42; diff --git a/t/expected-word-nonword.txt b/t/expected-word-nonword.txt new file mode 100644 index 0000000..5c60650 --- /dev/null +++ b/t/expected-word-nonword.txt @@ -0,0 +1,26 @@ +diff --git a/test.txt b/test.txt +--- a/test.txt ++++ b/test.txt +@@ -1,21 +1,22 @@ +Tests +====[-=-] + +S[-imple-]{+hort+} sentence with [-a few -]words[-.-]{+!+} + +Longer sentence [-with -]even more words[- ..-]{+,+} ain't that nice[-?-]{+!+} + +And [-an-]other one, just testing. + +option: {+new +}value +option-two: {+new-+}value-two +option-three: {+"+}value-three{+"+} + +function testme() { + first[-_c-]{+C+}all(); + {+wh+}i[-f-]{+le+} (a [-=-]{+!+}= b) { + if (!second_{+var) {+} +{+ third_+}call(){+ && fourth_call(+}) [-{-]{+\+} +{+ +} {+|| fif+}th[-ird-]_call(); + } + } + return 42; diff --git a/t/expected-words-nonword.txt b/t/expected-words-nonword.txt new file mode 100644 index 0000000..7bae7ed --- /dev/null +++ b/t/expected-words-nonword.txt @@ -0,0 +1,26 @@ +diff --git a/test.txt b/test.txt +--- a/test.txt ++++ b/test.txt +@@ -1,21 +1,22 @@ +Tests +====[-=-] + +[-Simple-]{+Short+} sentence with [-a few -]words[-.-]{+!+} + +Longer sentence [-with -]even more words[- ..-]{+,+} ain't that nice[-?-]{+!+} + +And [-another-]{+other+} one, just testing. + +option: {+new +}value +option-two: {+new-+}value-two +option-three: {+"+}value-three{+"+} + +function testme() { + [-first_call-]{+firstCall+}(); + [-if-]{+while+} (a [-=-]{+!+}= b) { + if (![-second_call()-]{+second_var+}) { + third_call(){+ && fourth_call() \+} +{+ || fifth_call()+}; + } + } + return 42; diff --git a/t/expected-words-nonwords-spaces.txt b/t/expected-words-nonwords-spaces.txt new file mode 100644 index 0000000..4ac431c --- /dev/null +++ b/t/expected-words-nonwords-spaces.txt @@ -0,0 +1,26 @@ +diff --git a/test.txt b/test.txt +--- a/test.txt ++++ b/test.txt +@@ -1,21 +1,22 @@ +Tests +[-=====-]{+====+} + +[-Simple-]{+Short+} sentence with [-a few -]words[-.-]{+!+} + +Longer sentence [-with -]even more words[- .. -]{+, +}ain't that nice[-?-]{+!+} + +And [-another-]{+other+} one, just testing. + +option: {+new +}value +option-two: {+new-+}value-two +option-three[-: -]{+: "+}value-three{+"+} + +function testme() { + [-first_call-]{+firstCall+}(); + [-if-]{+while+} (a[- == -]{+ != +}b) { + if (![-second_call()) {-]{+second_var) {+} + third_call{+() && fourth_call() \+} +{+ || fifth_call+}(); + } + } + return 42; diff --git a/t/expected-words-nonwords.txt b/t/expected-words-nonwords.txt new file mode 100644 index 0000000..4ac431c --- /dev/null +++ b/t/expected-words-nonwords.txt @@ -0,0 +1,26 @@ +diff --git a/test.txt b/test.txt +--- a/test.txt ++++ b/test.txt +@@ -1,21 +1,22 @@ +Tests +[-=====-]{+====+} + +[-Simple-]{+Short+} sentence with [-a few -]words[-.-]{+!+} + +Longer sentence [-with -]even more words[- .. -]{+, +}ain't that nice[-?-]{+!+} + +And [-another-]{+other+} one, just testing. + +option: {+new +}value +option-two: {+new-+}value-two +option-three[-: -]{+: "+}value-three{+"+} + +function testme() { + [-first_call-]{+firstCall+}(); + [-if-]{+while+} (a[- == -]{+ != +}b) { + if (![-second_call()) {-]{+second_var) {+} + third_call{+() && fourth_call() \+} +{+ || fifth_call+}(); + } + } + return 42; diff --git a/t/expected-words-nonwordspaces-spaces.txt b/t/expected-words-nonwordspaces-spaces.txt new file mode 100644 index 0000000..0606ad5 --- /dev/null +++ b/t/expected-words-nonwordspaces-spaces.txt @@ -0,0 +1,26 @@ +diff --git a/test.txt b/test.txt +--- a/test.txt ++++ b/test.txt +@@ -1,21 +1,22 @@ +Tests +[-=====-]{+====+} + +[-Simple-]{+Short+} sentence with [-a few -]words[-.-]{+!+} + +Longer sentence [-with -]even more words[- ..-]{+,+} ain't that nice[-?-]{+!+} + +And [-another-]{+other+} one, just testing. + +option: {+new +}value +option-two: {+new-+}value-two +option-three: {+"+}value-three{+"+} + +function testme() { + [-first_call-]{+firstCall+}(); + [-if-]{+while+} (a [-==-]{+!=+} b) { + if (![-second_call())-]{+second_var)+} { + third_call{+() && fourth_call() \+} +{+ || fifth_call+}(); + } + } + return 42; diff --git a/t/expected-words-nonwordspaces.txt b/t/expected-words-nonwordspaces.txt new file mode 100644 index 0000000..6d80450 --- /dev/null +++ b/t/expected-words-nonwordspaces.txt @@ -0,0 +1,26 @@ +diff --git a/test.txt b/test.txt +--- a/test.txt ++++ b/test.txt +@@ -1,21 +1,22 @@ +Tests +[-=====-]{+====+} + +[-Simple-]{+Short+} sentence with[-a few-] words[-.-]{+!+} + +Longer sentence[-with-] even more words[-..-]{+,+} ain't that nice[-?-]{+!+} + +And [-another-]{+other+} one, just testing. + +option: {+new+} value +option-two: {+new-+}value-two +option-three: {+"+}value-three{+"+} + +function testme() { + [-first_call-]{+firstCall+}(); + [-if-]{+while+} (a [-==-]{+!=+} b) { + if (![-second_call())-]{+second_var)+} { + third_call{+() && fourth_call() \+} +{+ || fifth_call+}(); + } + } + return 42; diff --git a/t/test.sh b/t/test.sh new file mode 100755 index 0000000..aedbff0 --- /dev/null +++ b/t/test.sh @@ -0,0 +1,103 @@ +#!/bin/sh + +# Small test file to check different --word-diff-regex settings in Git. + +# Copyright (C) 2012 Simon Ruderich +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +set -e + +git_diff() { + git diff --color=never --word-diff=plain --word-diff-regex="$1" \ + | grep -v '^index ' > "result-$2.txt" + diff -u "../expected-$2.txt" "result-$2.txt" +} + + +# Setup test directory/files. +mkdir test +cd test + +git init > /dev/null +cat > test.txt < /dev/null + +cat > test.txt <