60c2f6c36f942c75cd1c9a4f775cc2d37355b13e
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Junio C Hamano
   4#
   5
   6test_description='Binary diff and apply
   7'
   8
   9. ./test-lib.sh
  10
  11cat >expect.binary-numstat <<\EOF
  121       1       a
  13-       -       b
  141       1       c
  15-       -       d
  16EOF
  17
  18test_expect_success 'prepare repository' '
  19         echo AIT >a && echo BIT >b && echo CIT >c && echo DIT >d &&
  20         git update-index --add a b c d &&
  21         echo git >a &&
  22         cat "$TEST_DIRECTORY"/test-binary-1.png >b &&
  23         echo git >c &&
  24         cat b b >d
  25'
  26
  27cat > expected <<\EOF
  28 a |    2 +-
  29 b |  Bin
  30 c |    2 +-
  31 d |  Bin
  32 4 files changed, 2 insertions(+), 2 deletions(-)
  33EOF
  34test_expect_success 'apply --stat output for binary file change' '
  35        git diff >diff &&
  36        git apply --stat --summary <diff >current &&
  37        test_i18ncmp expected current
  38'
  39
  40test_expect_success 'diff --shortstat output for binary file change' '
  41        echo " 4 files changed, 2 insertions(+), 2 deletions(-)" >expected &&
  42        git diff --shortstat >current &&
  43        test_i18ncmp expected current
  44'
  45
  46test_expect_success 'diff --shortstat output for binary file change only' '
  47        echo " 1 file changed, 0 insertions(+), 0 deletions(-)" >expected &&
  48        git diff --shortstat -- b >current &&
  49        test_i18ncmp expected current
  50'
  51
  52test_expect_success 'apply --numstat notices binary file change' '
  53        git diff >diff &&
  54        git apply --numstat <diff >current &&
  55        test_cmp expect.binary-numstat current
  56'
  57
  58test_expect_success 'apply --numstat understands diff --binary format' '
  59        git diff --binary >diff &&
  60        git apply --numstat <diff >current &&
  61        test_cmp expect.binary-numstat current
  62'
  63
  64# apply needs to be able to skip the binary material correctly
  65# in order to report the line number of a corrupt patch.
  66test_expect_success 'apply detecting corrupt patch correctly' '
  67         git diff | sed -e "s/-CIT/xCIT/" >broken &&
  68         if git apply --stat --summary broken 2>detected
  69         then
  70                echo unhappy - should have detected an error
  71                (exit 1)
  72         else
  73                echo happy
  74         fi &&
  75         detected=`cat detected` &&
  76         detected=`expr "$detected" : "fatal.*at line \\([0-9]*\\)\$"` &&
  77         detected=`sed -ne "${detected}p" broken` &&
  78         test "$detected" = xCIT
  79'
  80
  81test_expect_success 'apply detecting corrupt patch correctly' '
  82         git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
  83         if git apply --stat --summary broken 2>detected
  84         then
  85                echo unhappy - should have detected an error
  86                (exit 1)
  87         else
  88                echo happy
  89         fi &&
  90         detected=`cat detected` &&
  91         detected=`expr "$detected" : "fatal.*at line \\([0-9]*\\)\$"` &&
  92         detected=`sed -ne "${detected}p" broken` &&
  93         test "$detected" = xCIT
  94'
  95
  96test_expect_success 'initial commit' 'git commit -a -m initial'
  97
  98# Try removal (b), modification (d), and creation (e).
  99test_expect_success 'diff-index with --binary' '
 100         echo AIT >a && mv b e && echo CIT >c && cat e >d &&
 101         git update-index --add --remove a b c d e &&
 102         tree0=`git write-tree` &&
 103         git diff --cached --binary >current &&
 104         git apply --stat --summary current
 105'
 106
 107test_expect_success 'apply binary patch' '
 108         git reset --hard &&
 109         git apply --binary --index <current &&
 110         tree1=`git write-tree` &&
 111         test "$tree1" = "$tree0"
 112'
 113
 114test_expect_success 'diff --no-index with binary creation' '
 115        echo Q | q_to_nul >binary &&
 116        (: hide error code from diff, which just indicates differences
 117         git diff --binary --no-index /dev/null binary >current ||
 118         true
 119        ) &&
 120        rm binary &&
 121        git apply --binary <current &&
 122        echo Q >expected &&
 123        nul_to_q <binary >actual &&
 124        test_cmp expected actual
 125'
 126
 127cat >expect <<EOF
 128 binfile  |   Bin 0 -> 1026 bytes
 129 textfile | 10000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 130EOF
 131
 132test_expect_success 'diff --stat with binary files and big change count' '
 133        echo X | dd of=binfile bs=1k seek=1 &&
 134        git add binfile &&
 135        i=0 &&
 136        while test $i -lt 10000; do
 137                echo $i &&
 138                i=$(($i + 1))
 139        done >textfile &&
 140        git add textfile &&
 141        git diff --cached --stat binfile textfile >output &&
 142        grep " | " output >actual &&
 143        test_cmp expect actual
 144'
 145
 146test_done