1#!/bin/sh
   2test_description='.mailmap configurations'
   4. ./test-lib.sh
   6fuzz_blame () {
   8        sed "
   9                s/$_x05[0-9a-f][0-9a-f][0-9a-f]/OBJID/g
  10                s/$_x05[0-9a-f][0-9a-f]/OBJI/g
  11                s/[-0-9]\{10\} [:0-9]\{8\} [-+][0-9]\{4\}/DATE/g
  12        " "$@"
  13}
  14test_expect_success setup '
  16        echo one >one &&
  17        git add one &&
  18        test_tick &&
  19        git commit -m initial &&
  20        echo two >>one &&
  21        git add one &&
  22        test_tick &&
  23        git commit --author "nick1 <bugs@company.xx>" -m second
  24'
  25cat >expect <<\EOF
  27A U Thor (1):
  28      initial
  29nick1 (1):
  31      second
  32EOF
  34test_expect_success 'No mailmap' '
  36        git shortlog HEAD >actual &&
  37        test_cmp expect actual
  38'
  39cat >expect <<\EOF
  41Repo Guy (1):
  42      initial
  43nick1 (1):
  45      second
  46EOF
  48test_expect_success 'default .mailmap' '
  50        echo "Repo Guy <author@example.com>" > .mailmap &&
  51        git shortlog HEAD >actual &&
  52        test_cmp expect actual
  53'
  54# Using a mailmap file in a subdirectory of the repo here, but
  56# could just as well have been a file outside of the repository
  57cat >expect <<\EOF
  58Internal Guy (1):
  59      second
  60Repo Guy (1):
  62      initial
  63EOF
  65test_expect_success 'mailmap.file set' '
  66        mkdir -p internal_mailmap &&
  67        echo "Internal Guy <bugs@company.xx>" > internal_mailmap/.mailmap &&
  68        git config mailmap.file internal_mailmap/.mailmap &&
  69        git shortlog HEAD >actual &&
  70        test_cmp expect actual
  71'
  72cat >expect <<\EOF
  74External Guy (1):
  75      initial
  76Internal Guy (1):
  78      second
  79EOF
  81test_expect_success 'mailmap.file override' '
  82        echo "External Guy <author@example.com>" >> internal_mailmap/.mailmap &&
  83        git config mailmap.file internal_mailmap/.mailmap &&
  84        git shortlog HEAD >actual &&
  85        test_cmp expect actual
  86'
  87cat >expect <<\EOF
  89Repo Guy (1):
  90      initial
  91nick1 (1):
  93      second
  94EOF
  96test_expect_success 'mailmap.file non-existent' '
  98        rm internal_mailmap/.mailmap &&
  99        rmdir internal_mailmap &&
 100        git shortlog HEAD >actual &&
 101        test_cmp expect actual
 102'
 103cat >expect <<\EOF
 105Internal Guy (1):
 106      second
 107Repo Guy (1):
 109      initial
 110EOF
 112test_expect_success 'name entry after email entry' '
 114        mkdir -p internal_mailmap &&
 115        echo "<bugs@company.xy> <bugs@company.xx>" >internal_mailmap/.mailmap &&
 116        echo "Internal Guy <bugs@company.xx>" >>internal_mailmap/.mailmap &&
 117        git shortlog HEAD >actual &&
 118        test_cmp expect actual
 119'
 120cat >expect <<\EOF
 122Internal Guy (1):
 123      second
 124Repo Guy (1):
 126      initial
 127EOF
 129test_expect_success 'name entry after email entry, case-insensitive' '
 131        mkdir -p internal_mailmap &&
 132        echo "<bugs@company.xy> <bugs@company.xx>" >internal_mailmap/.mailmap &&
 133        echo "Internal Guy <BUGS@Company.xx>" >>internal_mailmap/.mailmap &&
 134        git shortlog HEAD >actual &&
 135        test_cmp expect actual
 136'
 137cat >expect <<\EOF
 139A U Thor (1):
 140      initial
 141nick1 (1):
 143      second
 144EOF
 146test_expect_success 'No mailmap files, but configured' '
 147        rm -f .mailmap internal_mailmap/.mailmap &&
 148        git shortlog HEAD >actual &&
 149        test_cmp expect actual
 150'
 151test_expect_success 'setup mailmap blob tests' '
 153        git checkout -b map &&
 154        test_when_finished "git checkout master" &&
 155        cat >just-bugs <<-\EOF &&
 156        Blob Guy <bugs@company.xx>
 157        EOF
 158        cat >both <<-\EOF &&
 159        Blob Guy <author@example.com>
 160        Blob Guy <bugs@company.xx>
 161        EOF
 162        git add just-bugs both &&
 163        git commit -m "my mailmaps" &&
 164        echo "Repo Guy <author@example.com>" >.mailmap &&
 165        echo "Internal Guy <author@example.com>" >internal.map
 166'
 167test_expect_success 'mailmap.blob set' '
 169        cat >expect <<-\EOF &&
 170        Blob Guy (1):
 171              second
 172        Repo Guy (1):
 174              initial
 175        EOF
 177        git -c mailmap.blob=map:just-bugs shortlog HEAD >actual &&
 178        test_cmp expect actual
 179'
 180test_expect_success 'mailmap.blob overrides .mailmap' '
 182        cat >expect <<-\EOF &&
 183        Blob Guy (2):
 184              initial
 185              second
 186        EOF
 188        git -c mailmap.blob=map:both shortlog HEAD >actual &&
 189        test_cmp expect actual
 190'
 191test_expect_success 'mailmap.file overrides mailmap.blob' '
 193        cat >expect <<-\EOF &&
 194        Blob Guy (1):
 195              second
 196        Internal Guy (1):
 198              initial
 199        EOF
 201        git \
 202          -c mailmap.blob=map:both \
 203          -c mailmap.file=internal.map \
 204          shortlog HEAD >actual &&
 205        test_cmp expect actual
 206'
 207test_expect_success 'mailmap.blob can be missing' '
 209        cat >expect <<-\EOF &&
 210        Repo Guy (1):
 211              initial
 212        nick1 (1):
 214              second
 215        EOF
 217        git -c mailmap.blob=map:nonexistent shortlog HEAD >actual &&
 218        test_cmp expect actual
 219'
 220test_expect_success 'mailmap.blob defaults to off in non-bare repo' '
 222        git init non-bare &&
 223        (
 224                cd non-bare &&
 225                test_commit one .mailmap "Fake Name <author@example.com>" &&
 226                echo "     1    Fake Name" >expect &&
 227                git shortlog -ns HEAD >actual &&
 228                test_cmp expect actual &&
 229                rm .mailmap &&
 230                echo "     1    A U Thor" >expect &&
 231                git shortlog -ns HEAD >actual &&
 232                test_cmp expect actual
 233        )
 234'
 235test_expect_success 'mailmap.blob defaults to HEAD:.mailmap in bare repo' '
 237        git clone --bare non-bare bare &&
 238        (
 239                cd bare &&
 240                echo "     1    Fake Name" >expect &&
 241                git shortlog -ns HEAD >actual &&
 242                test_cmp expect actual
 243        )
 244'
 245test_expect_success 'cleanup after mailmap.blob tests' '
 247        rm -f .mailmap
 248'
 249# Extended mailmap configurations should give us the following output for shortlog
 251cat >expect <<\EOF
 252A U Thor <author@example.com> (1):
 253      initial
 254CTO <cto@company.xx> (1):
 256      seventh
 257Other Author <other@author.xx> (2):
 259      third
 260      fourth
 261Santa Claus <santa.claus@northpole.xx> (2):
 263      fifth
 264      sixth
 265Some Dude <some@dude.xx> (1):
 267      second
 268EOF
 270test_expect_success 'Shortlog output (complex mapping)' '
 272        echo three >>one &&
 273        git add one &&
 274        test_tick &&
 275        git commit --author "nick2 <bugs@company.xx>" -m third &&
 276        echo four >>one &&
 278        git add one &&
 279        test_tick &&
 280        git commit --author "nick2 <nick2@company.xx>" -m fourth &&
 281        echo five >>one &&
 283        git add one &&
 284        test_tick &&
 285        git commit --author "santa <me@company.xx>" -m fifth &&
 286        echo six >>one &&
 288        git add one &&
 289        test_tick &&
 290        git commit --author "claus <me@company.xx>" -m sixth &&
 291        echo seven >>one &&
 293        git add one &&
 294        test_tick &&
 295        git commit --author "CTO <cto@coompany.xx>" -m seventh &&
 296        mkdir -p internal_mailmap &&
 298        echo "Committed <committer@example.com>" > internal_mailmap/.mailmap &&
 299        echo "<cto@company.xx>                       <cto@coompany.xx>" >> internal_mailmap/.mailmap &&
 300        echo "Some Dude <some@dude.xx>         nick1 <bugs@company.xx>" >> internal_mailmap/.mailmap &&
 301        echo "Other Author <other@author.xx>   nick2 <bugs@company.xx>" >> internal_mailmap/.mailmap &&
 302        echo "Other Author <other@author.xx>         <nick2@company.xx>" >> internal_mailmap/.mailmap &&
 303        echo "Santa Claus <santa.claus@northpole.xx> <me@company.xx>" >> internal_mailmap/.mailmap &&
 304        echo "Santa Claus <santa.claus@northpole.xx> <me@company.xx>" >> internal_mailmap/.mailmap &&
 305        git shortlog -e HEAD >actual &&
 307        test_cmp expect actual
 308'
 310# git log with --pretty format which uses the name and email mailmap placemarkers
 312cat >expect <<\EOF
 313Author CTO <cto@coompany.xx> maps to CTO <cto@company.xx>
 314Committer C O Mitter <committer@example.com> maps to Committed <committer@example.com>
 315Author claus <me@company.xx> maps to Santa Claus <santa.claus@northpole.xx>
 317Committer C O Mitter <committer@example.com> maps to Committed <committer@example.com>
 318Author santa <me@company.xx> maps to Santa Claus <santa.claus@northpole.xx>
 320Committer C O Mitter <committer@example.com> maps to Committed <committer@example.com>
 321Author nick2 <nick2@company.xx> maps to Other Author <other@author.xx>
 323Committer C O Mitter <committer@example.com> maps to Committed <committer@example.com>
 324Author nick2 <bugs@company.xx> maps to Other Author <other@author.xx>
 326Committer C O Mitter <committer@example.com> maps to Committed <committer@example.com>
 327Author nick1 <bugs@company.xx> maps to Some Dude <some@dude.xx>
 329Committer C O Mitter <committer@example.com> maps to Committed <committer@example.com>
 330Author A U Thor <author@example.com> maps to A U Thor <author@example.com>
 332Committer C O Mitter <committer@example.com> maps to Committed <committer@example.com>
 333EOF
 334test_expect_success 'Log output (complex mapping)' '
 336        git log --pretty=format:"Author %an <%ae> maps to %aN <%aE>%nCommitter %cn <%ce> maps to %cN <%cE>%n" >actual &&
 337        test_cmp expect actual
 338'
 339cat >expect <<\EOF
 341Author: CTO <cto@company.xx>
 342Author: Santa Claus <santa.claus@northpole.xx>
 343Author: Santa Claus <santa.claus@northpole.xx>
 344Author: Other Author <other@author.xx>
 345Author: Other Author <other@author.xx>
 346Author: Some Dude <some@dude.xx>
 347Author: A U Thor <author@example.com>
 348EOF
 349test_expect_success 'Log output with --use-mailmap' '
 351        git log --use-mailmap | grep Author >actual &&
 352        test_cmp expect actual
 353'
 354cat >expect <<\EOF
 356Author: CTO <cto@company.xx>
 357Author: Santa Claus <santa.claus@northpole.xx>
 358Author: Santa Claus <santa.claus@northpole.xx>
 359Author: Other Author <other@author.xx>
 360Author: Other Author <other@author.xx>
 361Author: Some Dude <some@dude.xx>
 362Author: A U Thor <author@example.com>
 363EOF
 364test_expect_success 'Log output with log.mailmap' '
 366        git -c log.mailmap=True log | grep Author >actual &&
 367        test_cmp expect actual
 368'
 369cat >expect <<\EOF
 371Author: Santa Claus <santa.claus@northpole.xx>
 372Author: Santa Claus <santa.claus@northpole.xx>
 373EOF
 374test_expect_success 'Grep author with --use-mailmap' '
 376        git log --use-mailmap --author Santa | grep Author >actual &&
 377        test_cmp expect actual
 378'
 379cat >expect <<\EOF
 380Author: Santa Claus <santa.claus@northpole.xx>
 381Author: Santa Claus <santa.claus@northpole.xx>
 382EOF
 383test_expect_success 'Grep author with log.mailmap' '
 385        git -c log.mailmap=True log --author Santa | grep Author >actual &&
 386        test_cmp expect actual
 387'
 388>expect
 390test_expect_success 'Only grep replaced author with --use-mailmap' '
 392        git log --use-mailmap --author "<cto@coompany.xx>" >actual &&
 393        test_cmp expect actual
 394'
 395# git blame
 397cat >expect <<\EOF
 398^OBJI (A U Thor     DATE 1) one
 399OBJID (Some Dude    DATE 2) two
 400OBJID (Other Author DATE 3) three
 401OBJID (Other Author DATE 4) four
 402OBJID (Santa Claus  DATE 5) five
 403OBJID (Santa Claus  DATE 6) six
 404OBJID (CTO          DATE 7) seven
 405EOF
 406test_expect_success 'Blame output (complex mapping)' '
 407        git blame one >actual &&
 408        fuzz_blame actual >actual.fuzz &&
 409        test_cmp expect actual.fuzz
 410'
 411test_done