647218b4ef9700dc383a75976319d2de2fee18f3
   1#!/bin/sh
   2
   3# Copyright (c) 2009 Jens Lehmann
   4# Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
   5
   6test_description='git rev-list --pretty=format test'
   7
   8. ./test-lib.sh
   9. "$TEST_DIRECTORY"/lib-terminal.sh
  10
  11test_tick
  12# Tested non-UTF-8 encoding
  13test_encoding="ISO8859-1"
  14
  15# String "added" in German
  16# (translated with Google Translate),
  17# encoded in UTF-8, used as a commit log message below.
  18added_utf8_part=$(printf "\303\274")
  19added_utf8_part_iso88591=$(echo "$added_utf8_part" | iconv -f utf-8 -t $test_encoding)
  20added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
  21added_iso88591=$(echo "$added" | iconv -f utf-8 -t $test_encoding)
  22# same but "changed"
  23changed_utf8_part=$(printf "\303\244")
  24changed_utf8_part_iso88591=$(echo "$changed_utf8_part" | iconv -f utf-8 -t $test_encoding)
  25changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
  26changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t $test_encoding)
  27
  28# Count of char to truncate
  29# Number is chosen so, that non-ACSII characters
  30# (see $added_utf8_part and $changed_utf8_part)
  31# fall into truncated parts of appropriate words both from left and right
  32truncate_count=20
  33
  34test_expect_success 'setup' '
  35        : >foo &&
  36        git add foo &&
  37        git config i18n.commitEncoding $test_encoding &&
  38        echo "$added_iso88591" | git commit -F - &&
  39        head1=$(git rev-parse --verify HEAD) &&
  40        head1_short=$(git rev-parse --verify --short $head1) &&
  41        tree1=$(git rev-parse --verify HEAD:) &&
  42        tree1_short=$(git rev-parse --verify --short $tree1) &&
  43        echo "$changed" > foo &&
  44        echo "$changed_iso88591" | git commit -a -F - &&
  45        head2=$(git rev-parse --verify HEAD) &&
  46        head2_short=$(git rev-parse --verify --short $head2) &&
  47        tree2=$(git rev-parse --verify HEAD:) &&
  48        tree2_short=$(git rev-parse --verify --short $tree2) &&
  49        git config --unset i18n.commitEncoding
  50'
  51
  52# usage: test_format name format_string [failure] <expected_output
  53test_format () {
  54        cat >expect.$1
  55        test_expect_${3:-success} "format $1" "
  56                git rev-list --pretty=format:'$2' master >output.$1 &&
  57                test_cmp expect.$1 output.$1
  58        "
  59}
  60
  61# Feed to --format to provide predictable colored sequences.
  62AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
  63has_color () {
  64        test_decode_color <"$1" >decoded &&
  65        echo "<RED>foo<RESET>" >expect &&
  66        test_cmp expect decoded
  67}
  68
  69has_no_color () {
  70        echo foo >expect &&
  71        test_cmp expect "$1"
  72}
  73
  74test_format percent %%h <<EOF
  75commit $head2
  76%h
  77commit $head1
  78%h
  79EOF
  80
  81test_format hash %H%n%h <<EOF
  82commit $head2
  83$head2
  84$head2_short
  85commit $head1
  86$head1
  87$head1_short
  88EOF
  89
  90test_format tree %T%n%t <<EOF
  91commit $head2
  92$tree2
  93$tree2_short
  94commit $head1
  95$tree1
  96$tree1_short
  97EOF
  98
  99test_format parents %P%n%p <<EOF
 100commit $head2
 101$head1
 102$head1_short
 103commit $head1
 104
 105
 106EOF
 107
 108# we don't test relative here
 109test_format author %an%n%ae%n%ad%n%aD%n%at <<EOF
 110commit $head2
 111A U Thor
 112author@example.com
 113Thu Apr 7 15:13:13 2005 -0700
 114Thu, 7 Apr 2005 15:13:13 -0700
 1151112911993
 116commit $head1
 117A U Thor
 118author@example.com
 119Thu Apr 7 15:13:13 2005 -0700
 120Thu, 7 Apr 2005 15:13:13 -0700
 1211112911993
 122EOF
 123
 124test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<EOF
 125commit $head2
 126C O Mitter
 127committer@example.com
 128Thu Apr 7 15:13:13 2005 -0700
 129Thu, 7 Apr 2005 15:13:13 -0700
 1301112911993
 131commit $head1
 132C O Mitter
 133committer@example.com
 134Thu Apr 7 15:13:13 2005 -0700
 135Thu, 7 Apr 2005 15:13:13 -0700
 1361112911993
 137EOF
 138
 139test_format encoding %e <<EOF
 140commit $head2
 141$test_encoding
 142commit $head1
 143$test_encoding
 144EOF
 145
 146test_format subject %s <<EOF
 147commit $head2
 148$changed
 149commit $head1
 150$added
 151EOF
 152
 153test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
 154commit $head2
 155changed (ge${changed_utf8_part}ndert)..
 156commit $head1
 157added (hinzugef${added_utf8_part}gt..
 158EOF
 159
 160test_format body %b <<EOF
 161commit $head2
 162commit $head1
 163EOF
 164
 165test_format raw-body %B <<EOF
 166commit $head2
 167$changed
 168
 169commit $head1
 170$added
 171
 172EOF
 173
 174test_expect_success 'basic colors' '
 175        cat >expect <<-EOF &&
 176        commit $head2
 177        <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
 178        EOF
 179        format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
 180        git rev-list --format="$format" -1 master >actual.raw &&
 181        test_decode_color <actual.raw >actual &&
 182        test_cmp expect actual
 183'
 184
 185test_expect_success 'advanced colors' '
 186        cat >expect <<-EOF &&
 187        commit $head2
 188        <BOLD;RED;BYELLOW>foo<RESET>
 189        EOF
 190        format="%C(red yellow bold)foo%C(reset)" &&
 191        git rev-list --format="$format" -1 master >actual.raw &&
 192        test_decode_color <actual.raw >actual &&
 193        test_cmp expect actual
 194'
 195
 196test_expect_success '%C(auto,...) does not enable color by default' '
 197        git log --format=$AUTO_COLOR -1 >actual &&
 198        has_no_color actual
 199'
 200
 201test_expect_success '%C(auto,...) enables colors for color.diff' '
 202        git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
 203        has_color actual
 204'
 205
 206test_expect_success '%C(auto,...) enables colors for color.ui' '
 207        git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
 208        has_color actual
 209'
 210
 211test_expect_success '%C(auto,...) respects --color' '
 212        git log --format=$AUTO_COLOR -1 --color >actual &&
 213        has_color actual
 214'
 215
 216test_expect_success '%C(auto,...) respects --no-color' '
 217        git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
 218        has_no_color actual
 219'
 220
 221test_expect_success TTY '%C(auto,...) respects --color=auto (stdout is tty)' '
 222        test_terminal env TERM=vt100 \
 223                git log --format=$AUTO_COLOR -1 --color=auto >actual &&
 224        has_color actual
 225'
 226
 227test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
 228        (
 229                TERM=vt100 && export TERM &&
 230                git log --format=$AUTO_COLOR -1 --color=auto >actual &&
 231                has_no_color actual
 232        )
 233'
 234
 235test_expect_success '%C(auto) respects --color' '
 236        git log --color --format="%C(auto)%H" -1 >actual.raw &&
 237        test_decode_color <actual.raw >actual &&
 238        echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
 239        test_cmp expect actual
 240'
 241
 242test_expect_success '%C(auto) respects --no-color' '
 243        git log --no-color --format="%C(auto)%H" -1 >actual &&
 244        git rev-parse HEAD >expect &&
 245        test_cmp expect actual
 246'
 247
 248iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
 249Test printing of complex bodies
 250
 251This commit message is much longer than the others,
 252and it will be encoded in $test_encoding. We should therefore
 253include an ISO8859 character: ¡bueno!
 254EOF
 255
 256test_expect_success 'setup complex body' '
 257        git config i18n.commitencoding $test_encoding &&
 258        echo change2 >foo && git commit -a -F commit-msg &&
 259        head3=$(git rev-parse --verify HEAD) &&
 260        head3_short=$(git rev-parse --short $head3)
 261'
 262
 263test_format complex-encoding %e <<EOF
 264commit $head3
 265$test_encoding
 266commit $head2
 267$test_encoding
 268commit $head1
 269$test_encoding
 270EOF
 271
 272test_format complex-subject %s <<EOF
 273commit $head3
 274Test printing of complex bodies
 275commit $head2
 276$changed_iso88591
 277commit $head1
 278$added_iso88591
 279EOF
 280
 281test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
 282commit $head3
 283Test printing of c..
 284commit $head2
 285changed (ge${changed_utf8_part_iso88591}ndert)..
 286commit $head1
 287added (hinzugef${added_utf8_part_iso88591}gt..
 288EOF
 289
 290test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
 291commit $head3
 292Test prin..ex bodies
 293commit $head2
 294changed (..dert) foo
 295commit $head1
 296added (hi..f${added_utf8_part_iso88591}gt) foo
 297EOF
 298
 299test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
 300commit $head3
 301.. of complex bodies
 302commit $head2
 303..ged (ge${changed_utf8_part_iso88591}ndert) foo
 304commit $head1
 305.. (hinzugef${added_utf8_part_iso88591}gt) foo
 306EOF
 307
 308test_expect_success 'prepare expected messages (for test %b)' '
 309        cat <<-EOF >expected.utf-8 &&
 310        commit $head3
 311        This commit message is much longer than the others,
 312        and it will be encoded in $test_encoding. We should therefore
 313        include an ISO8859 character: ¡bueno!
 314
 315        commit $head2
 316        commit $head1
 317        EOF
 318        iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
 319'
 320
 321test_format complex-body %b <expected.ISO8859-1
 322
 323# Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
 324# so unset i18n.commitEncoding to test encoding conversion
 325git config --unset i18n.commitEncoding
 326
 327test_format complex-subject-commitencoding-unset %s <<EOF
 328commit $head3
 329Test printing of complex bodies
 330commit $head2
 331$changed
 332commit $head1
 333$added
 334EOF
 335
 336test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
 337commit $head3
 338Test printing of c..
 339commit $head2
 340changed (ge${changed_utf8_part}ndert)..
 341commit $head1
 342added (hinzugef${added_utf8_part}gt..
 343EOF
 344
 345test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
 346commit $head3
 347Test prin..ex bodies
 348commit $head2
 349changed (..dert) foo
 350commit $head1
 351added (hi..f${added_utf8_part}gt) foo
 352EOF
 353
 354test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
 355commit $head3
 356.. of complex bodies
 357commit $head2
 358..ged (ge${changed_utf8_part}ndert) foo
 359commit $head1
 360.. (hinzugef${added_utf8_part}gt) foo
 361EOF
 362
 363test_format complex-body-commitencoding-unset %b <expected.utf-8
 364
 365test_expect_success '%x00 shows NUL' '
 366        echo  >expect commit $head3 &&
 367        echo >>expect fooQbar &&
 368        git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
 369        nul_to_q <actual.nul >actual &&
 370        test_cmp expect actual
 371'
 372
 373test_expect_success '%ad respects --date=' '
 374        echo 2005-04-07 >expect.ad-short &&
 375        git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
 376        test_cmp expect.ad-short output.ad-short
 377'
 378
 379test_expect_success 'empty email' '
 380        test_tick &&
 381        C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
 382        A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
 383        verbose test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700"
 384'
 385
 386test_expect_success 'del LF before empty (1)' '
 387        git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
 388        test_line_count = 2 actual
 389'
 390
 391test_expect_success 'del LF before empty (2)' '
 392        git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
 393        test_line_count = 6 actual &&
 394        grep "^$" actual
 395'
 396
 397test_expect_success 'add LF before non-empty (1)' '
 398        git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
 399        test_line_count = 2 actual
 400'
 401
 402test_expect_success 'add LF before non-empty (2)' '
 403        git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
 404        test_line_count = 6 actual &&
 405        grep "^$" actual
 406'
 407
 408test_expect_success 'add SP before non-empty (1)' '
 409        git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
 410        test $(wc -w <actual) = 3
 411'
 412
 413test_expect_success 'add SP before non-empty (2)' '
 414        git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
 415        test $(wc -w <actual) = 6
 416'
 417
 418test_expect_success '--abbrev' '
 419        echo SHORT SHORT SHORT >expect2 &&
 420        echo LONG LONG LONG >expect3 &&
 421        git log -1 --format="%h %h %h" HEAD >actual1 &&
 422        git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
 423        git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
 424        sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
 425        sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
 426        test_cmp expect2 fuzzy2 &&
 427        test_cmp expect3 fuzzy3 &&
 428        ! test_cmp actual1 actual2
 429'
 430
 431test_expect_success '%H is not affected by --abbrev-commit' '
 432        git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
 433        len=$(wc -c <actual) &&
 434        test $len = 41
 435'
 436
 437test_expect_success '%h is not affected by --abbrev-commit' '
 438        git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
 439        len=$(wc -c <actual) &&
 440        test $len = 21
 441'
 442
 443test_expect_success '"%h %gD: %gs" is same as git-reflog' '
 444        git reflog >expect &&
 445        git log -g --format="%h %gD: %gs" >actual &&
 446        test_cmp expect actual
 447'
 448
 449test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
 450        git reflog --date=raw >expect &&
 451        git log -g --format="%h %gD: %gs" --date=raw >actual &&
 452        test_cmp expect actual
 453'
 454
 455test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
 456        git reflog --abbrev=13 --date=raw >expect &&
 457        git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
 458        test_cmp expect actual
 459'
 460
 461test_expect_success '%gd shortens ref name' '
 462        echo "master@{0}" >expect.gd-short &&
 463        git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
 464        test_cmp expect.gd-short actual.gd-short
 465'
 466
 467test_expect_success 'reflog identity' '
 468        echo "C O Mitter:committer@example.com" >expect &&
 469        git log -g -1 --format="%gn:%ge" >actual &&
 470        test_cmp expect actual
 471'
 472
 473test_expect_success 'oneline with empty message' '
 474        git commit -m "dummy" --allow-empty &&
 475        git commit -m "dummy" --allow-empty &&
 476        git filter-branch --msg-filter "sed -e s/dummy//" HEAD^^.. &&
 477        git rev-list --oneline HEAD >test.txt &&
 478        test_line_count = 5 test.txt &&
 479        git rev-list --oneline --graph HEAD >testg.txt &&
 480        test_line_count = 5 testg.txt
 481'
 482
 483test_expect_success 'single-character name is parsed correctly' '
 484        git commit --author="a <a@example.com>" --allow-empty -m foo &&
 485        echo "a <a@example.com>" >expect &&
 486        git log -1 --format="%an <%ae>" >actual &&
 487        test_cmp expect actual
 488'
 489
 490test_expect_success 'unused %G placeholders are passed through' '
 491        echo "%GX %G" >expect &&
 492        git log -1 --format="%GX %G" >actual &&
 493        test_cmp expect actual
 494'
 495
 496test_done