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