0a65b585af9e346901096a891e1b6bc0abfc1da2
   1#!/bin/sh
   2
   3test_description='Test diff indent heuristic.
   4
   5'
   6. ./test-lib.sh
   7. "$TEST_DIRECTORY"/diff-lib.sh
   8
   9# Compare two diff outputs. Ignore "index" lines, because we don't
  10# care about SHA-1s or file modes.
  11compare_diff () {
  12        sed -e "/^index /d" <"$1" >.tmp-1
  13        sed -e "/^index /d" <"$2" >.tmp-2
  14        test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
  15}
  16
  17test_expect_success 'prepare' '
  18        cat <<-\EOF >spaces.txt &&
  19        1
  20        2
  21        a
  22
  23        b
  24        3
  25        4
  26        EOF
  27
  28        cat <<-\EOF >functions.c &&
  29        1
  30        2
  31        /* function */
  32        foo() {
  33            foo
  34        }
  35
  36        3
  37        4
  38        EOF
  39
  40        git add spaces.txt functions.c &&
  41        test_tick &&
  42        git commit -m initial &&
  43        git branch old &&
  44
  45        cat <<-\EOF >spaces.txt &&
  46        1
  47        2
  48        a
  49
  50        b
  51        a
  52
  53        b
  54        3
  55        4
  56        EOF
  57
  58        cat <<-\EOF >functions.c &&
  59        1
  60        2
  61        /* function */
  62        bar() {
  63            foo
  64        }
  65
  66        /* function */
  67        foo() {
  68            foo
  69        }
  70
  71        3
  72        4
  73        EOF
  74
  75        git add spaces.txt functions.c &&
  76        test_tick &&
  77        git commit -m initial &&
  78        git branch new &&
  79
  80        tr "_" " " <<-\EOF >spaces-expect &&
  81        diff --git a/spaces.txt b/spaces.txt
  82        --- a/spaces.txt
  83        +++ b/spaces.txt
  84        @@ -3,5 +3,8 @@
  85         a
  86        _
  87         b
  88        +a
  89        +
  90        +b
  91         3
  92         4
  93        EOF
  94
  95        tr "_" " " <<-\EOF >spaces-compacted-expect &&
  96        diff --git a/spaces.txt b/spaces.txt
  97        --- a/spaces.txt
  98        +++ b/spaces.txt
  99        @@ -2,6 +2,9 @@
 100         2
 101         a
 102        _
 103        +b
 104        +a
 105        +
 106         b
 107         3
 108         4
 109        EOF
 110
 111        tr "_" " " <<-\EOF >functions-expect &&
 112        diff --git a/functions.c b/functions.c
 113        --- a/functions.c
 114        +++ b/functions.c
 115        @@ -1,6 +1,11 @@
 116         1
 117         2
 118         /* function */
 119        +bar() {
 120        +    foo
 121        +}
 122        +
 123        +/* function */
 124         foo() {
 125             foo
 126         }
 127        EOF
 128
 129        tr "_" " " <<-\EOF >functions-compacted-expect
 130        diff --git a/functions.c b/functions.c
 131        --- a/functions.c
 132        +++ b/functions.c
 133        @@ -1,5 +1,10 @@
 134         1
 135         2
 136        +/* function */
 137        +bar() {
 138        +    foo
 139        +}
 140        +
 141         /* function */
 142         foo() {
 143             foo
 144        EOF
 145'
 146
 147test_expect_success 'diff: ugly spaces' '
 148        git diff old new -- spaces.txt >out &&
 149        compare_diff spaces-expect out
 150'
 151
 152test_expect_success 'diff: nice spaces with --indent-heuristic' '
 153        git diff --indent-heuristic old new -- spaces.txt >out-compacted &&
 154        compare_diff spaces-compacted-expect out-compacted
 155'
 156
 157test_expect_success 'diff: nice spaces with diff.indentHeuristic' '
 158        git -c diff.indentHeuristic=true diff old new -- spaces.txt >out-compacted2 &&
 159        compare_diff spaces-compacted-expect out-compacted2
 160'
 161
 162test_expect_success 'diff: --no-indent-heuristic overrides config' '
 163        git -c diff.indentHeuristic=true diff --no-indent-heuristic old new -- spaces.txt >out2 &&
 164        compare_diff spaces-expect out2
 165'
 166
 167test_expect_success 'diff: --indent-heuristic with --patience' '
 168        git diff --indent-heuristic --patience old new -- spaces.txt >out-compacted3 &&
 169        compare_diff spaces-compacted-expect out-compacted3
 170'
 171
 172test_expect_success 'diff: --indent-heuristic with --histogram' '
 173        git diff --indent-heuristic --histogram old new -- spaces.txt >out-compacted4 &&
 174        compare_diff spaces-compacted-expect out-compacted4
 175'
 176
 177test_expect_success 'diff: ugly functions' '
 178        git diff old new -- functions.c >out &&
 179        compare_diff functions-expect out
 180'
 181
 182test_expect_success 'diff: nice functions with --indent-heuristic' '
 183        git diff --indent-heuristic old new -- functions.c >out-compacted &&
 184        compare_diff functions-compacted-expect out-compacted
 185'
 186
 187test_done