t / t4001-diff-rename.shon commit Merge branch 'js/mingw-tests-2.8' into maint (0c1a8ec)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5
   6test_description='Test rename detection in diff engine.
   7
   8'
   9. ./test-lib.sh
  10. "$TEST_DIRECTORY"/diff-lib.sh
  11
  12echo >path0 'Line 1
  13Line 2
  14Line 3
  15Line 4
  16Line 5
  17Line 6
  18Line 7
  19Line 8
  20Line 9
  21Line 10
  22line 11
  23Line 12
  24Line 13
  25Line 14
  26Line 15
  27'
  28
  29test_expect_success \
  30    'update-index --add a file.' \
  31    'git update-index --add path0'
  32
  33test_expect_success \
  34    'write that tree.' \
  35    'tree=$(git write-tree) && echo $tree'
  36
  37sed -e 's/line/Line/' <path0 >path1
  38rm -f path0
  39test_expect_success \
  40    'renamed and edited the file.' \
  41    'git update-index --add --remove path0 path1'
  42
  43test_expect_success \
  44    'git diff-index -p -M after rename and editing.' \
  45    'git diff-index -p -M $tree >current'
  46cat >expected <<\EOF
  47diff --git a/path0 b/path1
  48rename from path0
  49rename to path1
  50--- a/path0
  51+++ b/path1
  52@@ -8,7 +8,7 @@ Line 7
  53 Line 8
  54 Line 9
  55 Line 10
  56-line 11
  57+Line 11
  58 Line 12
  59 Line 13
  60 Line 14
  61EOF
  62
  63test_expect_success \
  64    'validate the output.' \
  65    'compare_diff_patch current expected'
  66
  67test_expect_success 'favour same basenames over different ones' '
  68        cp path1 another-path &&
  69        git add another-path &&
  70        git commit -m 1 &&
  71        git rm path1 &&
  72        mkdir subdir &&
  73        git mv another-path subdir/path1 &&
  74        git status | test_i18ngrep "renamed: .*path1 -> subdir/path1"'
  75
  76test_expect_success 'favour same basenames even with minor differences' '
  77        git show HEAD:path1 | sed "s/15/16/" > subdir/path1 &&
  78        git status | test_i18ngrep "renamed: .*path1 -> subdir/path1"'
  79
  80test_expect_success 'two files with same basename and same content' '
  81        git reset --hard &&
  82        mkdir -p dir/A dir/B &&
  83        cp path1 dir/A/file &&
  84        cp path1 dir/B/file &&
  85        git add dir &&
  86        git commit -m 2 &&
  87        git mv dir other-dir &&
  88        git status | test_i18ngrep "renamed: .*dir/A/file -> other-dir/A/file"
  89'
  90
  91test_expect_success 'setup for many rename source candidates' '
  92        git reset --hard &&
  93        for i in 0 1 2 3 4 5 6 7 8 9;
  94        do
  95                for j in 0 1 2 3 4 5 6 7 8 9;
  96                do
  97                        echo "$i$j" >"path$i$j"
  98                done
  99        done &&
 100        git add "path??" &&
 101        test_tick &&
 102        git commit -m "hundred" &&
 103        (cat path1; echo new) >new-path &&
 104        echo old >>path1 &&
 105        git add new-path path1 &&
 106        git diff -l 4 -C -C --cached --name-status >actual 2>actual.err &&
 107        sed -e "s/^\([CM]\)[0-9]*       /\1     /" actual >actual.munged &&
 108        cat >expect <<-EOF &&
 109        C       path1   new-path
 110        M       path1
 111        EOF
 112        test_cmp expect actual.munged &&
 113        grep warning actual.err
 114'
 115
 116test_expect_success 'rename pretty print with nothing in common' '
 117        mkdir -p a/b/ &&
 118        : >a/b/c &&
 119        git add a/b/c &&
 120        git commit -m "create a/b/c" &&
 121        mkdir -p c/b/ &&
 122        git mv a/b/c c/b/a &&
 123        git commit -m "a/b/c -> c/b/a" &&
 124        git diff -M --summary HEAD^ HEAD >output &&
 125        test_i18ngrep " a/b/c => c/b/a " output &&
 126        git diff -M --stat HEAD^ HEAD >output &&
 127        test_i18ngrep " a/b/c => c/b/a " output
 128'
 129
 130test_expect_success 'rename pretty print with common prefix' '
 131        mkdir -p c/d &&
 132        git mv c/b/a c/d/e &&
 133        git commit -m "c/b/a -> c/d/e" &&
 134        git diff -M --summary HEAD^ HEAD >output &&
 135        test_i18ngrep " c/{b/a => d/e} " output &&
 136        git diff -M --stat HEAD^ HEAD >output &&
 137        test_i18ngrep " c/{b/a => d/e} " output
 138'
 139
 140test_expect_success 'rename pretty print with common suffix' '
 141        mkdir d &&
 142        git mv c/d/e d/e &&
 143        git commit -m "c/d/e -> d/e" &&
 144        git diff -M --summary HEAD^ HEAD >output &&
 145        test_i18ngrep " {c/d => d}/e " output &&
 146        git diff -M --stat HEAD^ HEAD >output &&
 147        test_i18ngrep " {c/d => d}/e " output
 148'
 149
 150test_expect_success 'rename pretty print with common prefix and suffix' '
 151        mkdir d/f &&
 152        git mv d/e d/f/e &&
 153        git commit -m "d/e -> d/f/e" &&
 154        git diff -M --summary HEAD^ HEAD >output &&
 155        test_i18ngrep " d/{ => f}/e " output &&
 156        git diff -M --stat HEAD^ HEAD >output &&
 157        test_i18ngrep " d/{ => f}/e " output
 158'
 159
 160test_expect_success 'rename pretty print common prefix and suffix overlap' '
 161        mkdir d/f/f &&
 162        git mv d/f/e d/f/f/e &&
 163        git commit -m "d/f/e d/f/f/e" &&
 164        git diff -M --summary HEAD^ HEAD >output &&
 165        test_i18ngrep " d/f/{ => f}/e " output &&
 166        git diff -M --stat HEAD^ HEAD >output &&
 167        test_i18ngrep " d/f/{ => f}/e " output
 168'
 169
 170test_done