t / t4008-diff-break-rewrite.shon commit Add "commit" helper script (a3e870f)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5
   6test_description='Break and then rename
   7
   8We have two very different files, file0 and file1, registered in a tree.
   9
  10We update file1 so drastically that it is more similar to file0, and
  11then remove file0.  With -B, changes to file1 should be broken into
  12separate delete and create, resulting in removal of file0, removal of
  13original file1 and creation of completely rewritten file1.
  14
  15Further, with -B and -M together, these three modifications should
  16turn into rename-edit of file0 into file1.
  17
  18Starting from the same two files in the tree, we swap file0 and file1.
  19With -B, this should be detected as two complete rewrites, resulting in
  20four changes in total.
  21
  22Further, with -B and -M together, these should turn into two renames.
  23'
  24. ./test-lib.sh
  25
  26_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
  27_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
  28sanitize_diff_raw='s/ '"$_x40"' '"$_x40"' \([CDNR]\)[0-9]*      / X X \1#       /'
  29compare_diff_raw () {
  30    # When heuristics are improved, the score numbers would change.
  31    # Ignore them while comparing.
  32    # Also we do not check SHA1 hash generation in this test, which
  33    # is a job for t0000-basic.sh
  34
  35    sed -e "$sanitize_diff_raw" <"$1" >.tmp-1
  36    sed -e "$sanitize_diff_raw" <"$2" >.tmp-2
  37    diff -u .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
  38}
  39
  40test_expect_success \
  41    setup \
  42    'cat ../../README >file0 &&
  43     cat ../../COPYING >file1 &&
  44    git-update-cache --add file0 file1 &&
  45    tree=$(git-write-tree) &&
  46    echo "$tree"'
  47
  48test_expect_success \
  49    'change file1 with copy-edit of file0 and remove file0' \
  50    'sed -e "s/git/GIT/" file0 >file1 &&
  51     rm -f file0 &&
  52    git-update-cache --remove file0 file1'
  53
  54test_expect_success \
  55    'run diff with -B' \
  56    'git-diff-cache -B --cached "$tree" >current'
  57
  58cat >expected <<\EOF
  59:100644 000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 0000000000000000000000000000000000000000 D      file0
  60:100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
  61:000000 100644 0000000000000000000000000000000000000000 11e331465a89c394dc25c780de230043750c1ec8 N100   file1
  62EOF
  63
  64test_expect_success \
  65    'validate result of -B (#1)' \
  66    'compare_diff_raw current expected'
  67
  68test_expect_success \
  69    'run diff with -B and -M' \
  70    'git-diff-cache -B -M "$tree" >current'
  71
  72cat >expected <<\EOF
  73:100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 08bb2fb671deff4c03a4d4a0a1315dff98d5732c R100   file0   file1
  74EOF
  75
  76test_expect_success \
  77    'validate result of -B -M (#2)' \
  78    'compare_diff_raw current expected'
  79
  80test_expect_success \
  81    'swap file0 and file1' \
  82    'rm -f file0 file1 &&
  83     git-read-tree -m $tree &&
  84     git-checkout-cache -f -u -a &&
  85     mv file0 tmp &&
  86     mv file1 file0 &&
  87     mv tmp file1 &&
  88     git-update-cache file0 file1'
  89
  90test_expect_success \
  91    'run diff with -B' \
  92    'git-diff-cache -B "$tree" >current'
  93
  94cat >expected <<\EOF
  95:100644 000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 0000000000000000000000000000000000000000 D100   file0
  96:000000 100644 0000000000000000000000000000000000000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 N100   file0
  97:100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
  98:000000 100644 0000000000000000000000000000000000000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 N100   file1
  99EOF
 100
 101test_expect_success \
 102    'validate result of -B (#3)' \
 103    'compare_diff_raw current expected'
 104
 105test_expect_success \
 106    'run diff with -B and -M' \
 107    'git-diff-cache -B -M "$tree" >current'
 108
 109cat >expected <<\EOF
 110:100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 6ff87c4664981e4397625791c8ea3bbb5f2279a3 R100   file1   file0
 111:100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 R100   file0   file1
 112EOF
 113
 114test_expect_success \
 115    'validate result of -B -M (#4)' \
 116    'compare_diff_raw current expected'
 117
 118test_expect_success \
 119    'make file0 into something completely different' \
 120    'rm -f file0 &&
 121     ln -s frotz file0 &&
 122     git-update-cache file0 file1'
 123
 124test_expect_success \
 125    'run diff with -B' \
 126    'git-diff-cache -B "$tree" >current'
 127
 128cat >expected <<\EOF
 129:100644 120000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 67be421f88824578857624f7b3dc75e99a8a1481 T      file0
 130:100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
 131:000000 100644 0000000000000000000000000000000000000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 N100   file1
 132EOF
 133
 134test_expect_success \
 135    'validate result of -B (#5)' \
 136    'compare_diff_raw current expected'
 137
 138test_expect_success \
 139    'run diff with -B' \
 140    'git-diff-cache -B -M "$tree" >current'
 141
 142# This should not mistake file0 as the copy source of new file1
 143# due to type differences.
 144cat >expected <<\EOF
 145:100644 120000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 67be421f88824578857624f7b3dc75e99a8a1481 T      file0
 146:100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
 147:000000 100644 0000000000000000000000000000000000000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 N100   file1
 148EOF
 149
 150test_expect_success \
 151    'validate result of -B -M (#6)' \
 152    'compare_diff_raw current expected'
 153
 154test_expect_success \
 155    'run diff with -M' \
 156    'git-diff-cache -M "$tree" >current'
 157
 158# This should not mistake file0 as the copy source of new file1
 159# due to type differences.
 160cat >expected <<\EOF
 161:100644 120000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 67be421f88824578857624f7b3dc75e99a8a1481 T      file0
 162:100644 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 M      file1
 163EOF
 164
 165test_expect_success \
 166    'validate result of -M (#7)' \
 167    'compare_diff_raw current expected'
 168
 169test_expect_success \
 170    'file1 edited to look like file0 and file0 rename-edited to file2' \
 171    'rm -f file0 file1 &&
 172     git-read-tree -m $tree &&
 173     git-checkout-cache -f -u -a &&
 174     sed -e "s/git/GIT/" file0 >file1 &&
 175     sed -e "s/git/GET/" file0 >file2 &&
 176     rm -f file0
 177     git-update-cache --add --remove file0 file1 file2'
 178
 179test_expect_success \
 180    'run diff with -B' \
 181    'git-diff-cache -B "$tree" >current'
 182
 183cat >expected <<\EOF
 184:100644 000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 0000000000000000000000000000000000000000 D      file0
 185:100644 000000 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0000000000000000000000000000000000000000 D100   file1
 186:000000 100644 0000000000000000000000000000000000000000 08bb2fb671deff4c03a4d4a0a1315dff98d5732c N100   file1
 187:000000 100644 0000000000000000000000000000000000000000 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 N      file2
 188EOF
 189
 190test_expect_success \
 191    'validate result of -B (#8)' \
 192    'compare_diff_raw current expected'
 193
 194test_expect_success \
 195    'run diff with -B -M' \
 196    'git-diff-cache -B -M "$tree" >current'
 197
 198cat >expected <<\EOF
 199:100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 08bb2fb671deff4c03a4d4a0a1315dff98d5732c C095   file0   file1
 200:100644 100644 f5deac7be59e7eeab8657fd9ae706fd6a57daed2 59f832e5c8b3f7e486be15ad0cd3e95ba9af8998 R095   file0   file2
 201EOF
 202
 203test_expect_success \
 204    'validate result of -B -M (#9)' \
 205    'compare_diff_raw current expected'
 206
 207test_done