1#!/bin/sh
   2test_description='git blame corner cases'
   4. ./test-lib.sh
   5pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'
   7test_expect_success setup '
   9        echo A A A A A >one &&
  11        echo B B B B B >two &&
  12        echo C C C C C >tres &&
  13        echo ABC >mouse &&
  14        for i in 1 2 3 4 5 6 7 8 9
  15        do
  16                echo $i
  17        done >nine_lines &&
  18        for i in 1 2 3 4 5 6 7 8 9 a
  19        do
  20                echo $i
  21        done >ten_lines &&
  22        git add one two tres mouse nine_lines ten_lines &&
  23        test_tick &&
  24        GIT_AUTHOR_NAME=Initial git commit -m Initial &&
  25        cat one >uno &&
  27        mv two dos &&
  28        cat one >>tres &&
  29        echo DEF >>mouse
  30        git add uno dos tres mouse &&
  31        test_tick &&
  32        GIT_AUTHOR_NAME=Second git commit -a -m Second &&
  33        echo GHIJK >>mouse &&
  35        git add mouse &&
  36        test_tick &&
  37        GIT_AUTHOR_NAME=Third git commit -m Third &&
  38        cat mouse >cow &&
  40        git add cow &&
  41        test_tick &&
  42        GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
  43        {
  45                echo ABC
  46                echo DEF
  47                echo XXXX
  48                echo GHIJK
  49        } >cow &&
  50        git add cow &&
  51        test_tick &&
  52        GIT_AUTHOR_NAME=Fifth git commit -m Fifth
  53'
  54test_expect_success 'straight copy without -C' '
  56        git blame uno | grep Second
  58'
  60test_expect_success 'straight move without -C' '
  62        git blame dos | grep Initial
  64'
  66test_expect_success 'straight copy with -C' '
  68        git blame -C1 uno | grep Second
  70'
  72test_expect_success 'straight move with -C' '
  74        git blame -C1 dos | grep Initial
  76'
  78test_expect_success 'straight copy with -C -C' '
  80        git blame -C -C1 uno | grep Initial
  82'
  84test_expect_success 'straight move with -C -C' '
  86        git blame -C -C1 dos | grep Initial
  88'
  90test_expect_success 'append without -C' '
  92        git blame -L2 tres | grep Second
  94'
  96test_expect_success 'append with -C' '
  98        git blame -L2 -C1 tres | grep Second
 100'
 102test_expect_success 'append with -C -C' '
 104        git blame -L2 -C -C1 tres | grep Second
 106'
 108test_expect_success 'append with -C -C -C' '
 110        git blame -L2 -C -C -C1 tres | grep Initial
 112'
 114test_expect_success 'blame wholesale copy' '
 116        git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
 118        {
 119                echo mouse-Initial
 120                echo mouse-Second
 121                echo mouse-Third
 122        } >expected &&
 123        test_cmp expected current
 124'
 126test_expect_success 'blame wholesale copy and more' '
 128        git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
 130        {
 131                echo mouse-Initial
 132                echo mouse-Second
 133                echo cow-Fifth
 134                echo mouse-Third
 135        } >expected &&
 136        test_cmp expected current
 137'
 139test_expect_success 'blame path that used to be a directory' '
 141        mkdir path &&
 142        echo A A A A A >path/file &&
 143        echo B B B B B >path/elif &&
 144        git add path &&
 145        test_tick &&
 146        git commit -m "path was a directory" &&
 147        rm -fr path &&
 148        echo A A A A A >path &&
 149        git add path &&
 150        test_tick &&
 151        git commit -m "path is a regular file" &&
 152        git blame HEAD^.. -- path
 153'
 154test_expect_success 'blame to a commit with no author name' '
 156  TREE=`git rev-parse HEAD:`
 157  cat >badcommit <<EOF
 158tree $TREE
 159author <noname> 1234567890 +0000
 160committer David Reiss <dreiss@facebook.com> 1234567890 +0000
 161some message
 163EOF
 164  COMMIT=`git hash-object -t commit -w badcommit`
 165  git --no-pager blame $COMMIT -- uno >/dev/null
 166'
 167test_expect_success 'blame -L with invalid start' '
 169        test_must_fail git blame -L5 tres 2>errors &&
 170        grep "has only 2 lines" errors
 171'
 172test_expect_success 'blame -L with invalid end' '
 174        test_must_fail git blame -L1,5 tres 2>errors &&
 175        grep "has only 2 lines" errors
 176'
 177test_expect_success 'indent of line numbers, nine lines' '
 179        git blame nine_lines >actual &&
 180        test $(grep -c "  " actual) = 0
 181'
 182test_expect_success 'indent of line numbers, ten lines' '
 184        git blame ten_lines >actual &&
 185        test $(grep -c "  " actual) = 9
 186'
 187test_done