]> ruderich.org/simon Gitweb - config/dotfiles.git/commitdiff
gitconfig: Add diff.wordregex for better git --color-words.
authorSimon Ruderich <simon@ruderich.org>
Wed, 17 Oct 2012 20:00:17 +0000 (22:00 +0200)
committerSimon Ruderich <simon@ruderich.org>
Wed, 17 Oct 2012 20:00:17 +0000 (22:00 +0200)
Also add t/ which some tests and examples.

gitconfig.m4
t/expected-empty.txt [new file with mode: 0644]
t/expected-nonspaces.txt [new file with mode: 0644]
t/expected-word-nonword.txt [new file with mode: 0644]
t/expected-words-nonword.txt [new file with mode: 0644]
t/expected-words-nonwords-spaces.txt [new file with mode: 0644]
t/expected-words-nonwords.txt [new file with mode: 0644]
t/expected-words-nonwordspaces-spaces.txt [new file with mode: 0644]
t/expected-words-nonwordspaces.txt [new file with mode: 0644]
t/test.sh [new file with mode: 0755]

index 439f7808977f5864c0beb026fa27115b6faf0bd4..f7bfd4eaba94dc79e565e62c7d635fdec26c422c 100644 (file)
@@ -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 (file)
index 0000000..3440328
--- /dev/null
@@ -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 (file)
index 0000000..4a86a16
--- /dev/null
@@ -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 (file)
index 0000000..5c60650
--- /dev/null
@@ -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 (file)
index 0000000..7bae7ed
--- /dev/null
@@ -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 (file)
index 0000000..4ac431c
--- /dev/null
@@ -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 (file)
index 0000000..4ac431c
--- /dev/null
@@ -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 (file)
index 0000000..0606ad5
--- /dev/null
@@ -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 (file)
index 0000000..6d80450
--- /dev/null
@@ -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 (executable)
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 <http://www.gnu.org/licenses/>.
+
+
+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 <<EOF
+Tests
+=====
+
+Simple sentence with a few words.
+
+Longer sentence with even more words .. ain't that nice?
+
+And another one, just testing.
+
+option: value
+option-two: value-two
+option-three: value-three
+
+function testme() {
+    first_call();
+    if (a == b) {
+        if (!second_call()) {
+            third_call();
+        }
+    }
+    return 42;
+}
+EOF
+git add test.txt
+git commit -m 'Initial commit.' > /dev/null
+
+cat > test.txt <<EOF
+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;
+}
+EOF
+
+
+git_diff '' empty
+
+git_diff '[^[:space:]]+' nonspaces
+
+git_diff '[a-zA-Z0-9_]|[^a-zA-Z0-9_]'    word-nonword
+git_diff '[a-zA-Z0-9_]+|[^a-zA-Z0-9_]'   words-nonword
+git_diff '[a-zA-Z0-9_]+|[^a-zA-Z0-9_]+'  words-nonwords
+git_diff '[a-zA-Z0-9_]+|[^a-zA-Z0-9_ ]+' words-nonwordspaces
+
+git_diff '[a-zA-Z0-9_]+|[^a-zA-Z0-9_]+|[ ]+'  words-nonwords-spaces
+git_diff '[a-zA-Z0-9_]+|[^a-zA-Z0-9_ ]+|[ ]+' words-nonwordspaces-spaces
+
+
+# Remove test directory after successful testing.
+rm -rf ../test