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