d75cdbbf9cce8390d8d98df6e544f40371b25f88
   1#!/bin/sh
   2#
   3# Copyright (c) 2010, Will Palmer
   4# Copyright (c) 2011, Alexey Shumkin (+ non-UTF-8 commit encoding tests)
   5#
   6
   7test_description='Test pretty formats'
   8. ./test-lib.sh
   9
  10# Tested non-UTF-8 encoding
  11test_encoding="ISO8859-1"
  12
  13sample_utf8_part=$(printf "f\303\244ng")
  14
  15commit_msg () {
  16        # String "initial. initial" partly in German
  17        # (translated with Google Translate),
  18        # encoded in UTF-8, used as a commit log message below.
  19        msg="initial. an${sample_utf8_part}lich\n"
  20        if test -n "$1"
  21        then
  22                printf "$msg" | iconv -f utf-8 -t "$1"
  23        else
  24                printf "$msg"
  25        fi
  26}
  27
  28test_expect_success 'set up basic repos' '
  29        >foo &&
  30        >bar &&
  31        git add foo &&
  32        test_tick &&
  33        git config i18n.commitEncoding $test_encoding &&
  34        commit_msg $test_encoding | git commit -F - &&
  35        git add bar &&
  36        test_tick &&
  37        git commit -m "add bar" &&
  38        git config --unset i18n.commitEncoding
  39'
  40
  41test_expect_success 'alias builtin format' '
  42        git log --pretty=oneline >expected &&
  43        git config pretty.test-alias oneline &&
  44        git log --pretty=test-alias >actual &&
  45        test_cmp expected actual
  46'
  47
  48test_expect_success 'alias masking builtin format' '
  49        git log --pretty=oneline >expected &&
  50        git config pretty.oneline "%H" &&
  51        git log --pretty=oneline >actual &&
  52        test_cmp expected actual
  53'
  54
  55test_expect_success 'alias user-defined format' '
  56        git log --pretty="format:%h" >expected &&
  57        git config pretty.test-alias "format:%h" &&
  58        git log --pretty=test-alias >actual &&
  59        test_cmp expected actual
  60'
  61
  62test_expect_success 'alias user-defined tformat with %s (ISO8859-1 encoding)' '
  63        git config i18n.logOutputEncoding $test_encoding &&
  64        git log --oneline >expected-s &&
  65        git log --pretty="tformat:%h %s" >actual-s &&
  66        git config --unset i18n.logOutputEncoding &&
  67        test_cmp expected-s actual-s
  68'
  69
  70test_expect_success 'alias user-defined tformat with %s (utf-8 encoding)' '
  71        git log --oneline >expected-s &&
  72        git log --pretty="tformat:%h %s" >actual-s &&
  73        test_cmp expected-s actual-s
  74'
  75
  76test_expect_success 'alias user-defined tformat' '
  77        git log --pretty="tformat:%h" >expected &&
  78        git config pretty.test-alias "tformat:%h" &&
  79        git log --pretty=test-alias >actual &&
  80        test_cmp expected actual
  81'
  82
  83test_expect_success 'alias non-existent format' '
  84        git config pretty.test-alias format-that-will-never-exist &&
  85        test_must_fail git log --pretty=test-alias
  86'
  87
  88test_expect_success 'alias of an alias' '
  89        git log --pretty="tformat:%h" >expected &&
  90        git config pretty.test-foo "tformat:%h" &&
  91        git config pretty.test-bar test-foo &&
  92        git log --pretty=test-bar >actual && test_cmp expected actual
  93'
  94
  95test_expect_success 'alias masking an alias' '
  96        git log --pretty=format:"Two %H" >expected &&
  97        git config pretty.duplicate "format:One %H" &&
  98        git config --add pretty.duplicate "format:Two %H" &&
  99        git log --pretty=duplicate >actual &&
 100        test_cmp expected actual
 101'
 102
 103test_expect_success 'alias loop' '
 104        git config pretty.test-foo test-bar &&
 105        git config pretty.test-bar test-foo &&
 106        test_must_fail git log --pretty=test-foo
 107'
 108
 109test_expect_success 'NUL separation' '
 110        printf "add bar\0$(commit_msg)" >expected &&
 111        git log -z --pretty="format:%s" >actual &&
 112        test_cmp expected actual
 113'
 114
 115test_expect_success 'NUL termination' '
 116        printf "add bar\0$(commit_msg)\0" >expected &&
 117        git log -z --pretty="tformat:%s" >actual &&
 118        test_cmp expected actual
 119'
 120
 121test_expect_success 'NUL separation with --stat' '
 122        stat0_part=$(git diff --stat HEAD^ HEAD) &&
 123        stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
 124        printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n" >expected &&
 125        git log -z --stat --pretty="format:%s" >actual &&
 126        test_i18ncmp expected actual
 127'
 128
 129test_expect_failure 'NUL termination with --stat' '
 130        stat0_part=$(git diff --stat HEAD^ HEAD) &&
 131        stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
 132        printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n0" >expected &&
 133        git log -z --stat --pretty="tformat:%s" >actual &&
 134        test_i18ncmp expected actual
 135'
 136
 137test_expect_success 'setup more commits' '
 138        test_commit "message one" one one message-one &&
 139        test_commit "message two" two two message-two &&
 140        head1=$(git rev-parse --verify --short HEAD~0) &&
 141        head2=$(git rev-parse --verify --short HEAD~1) &&
 142        head3=$(git rev-parse --verify --short HEAD~2) &&
 143        head4=$(git rev-parse --verify --short HEAD~3)
 144'
 145
 146test_expect_success 'left alignment formatting' '
 147        git log --pretty="tformat:%<(40)%s" >actual &&
 148        qz_to_tab_space <<EOF >expected &&
 149message two                            Z
 150message one                            Z
 151add bar                                Z
 152$(commit_msg)                    Z
 153EOF
 154        test_cmp expected actual
 155'
 156
 157test_expect_success 'left alignment formatting. i18n.logOutputEncoding' '
 158        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(40)%s" >actual &&
 159        qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 160message two                            Z
 161message one                            Z
 162add bar                                Z
 163$(commit_msg)                    Z
 164EOF
 165        test_cmp expected actual
 166'
 167
 168test_expect_success 'left alignment formatting at the nth column' '
 169        git log --pretty="tformat:%h %<|(40)%s" >actual &&
 170        qz_to_tab_space <<EOF >expected &&
 171$head1 message two                    Z
 172$head2 message one                    Z
 173$head3 add bar                        Z
 174$head4 $(commit_msg)            Z
 175EOF
 176        test_cmp expected actual
 177'
 178
 179test_expect_success 'left alignment formatting at the nth column. i18n.logOutputEncoding' '
 180        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %<|(40)%s" >actual &&
 181        qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 182$head1 message two                    Z
 183$head2 message one                    Z
 184$head3 add bar                        Z
 185$head4 $(commit_msg)            Z
 186EOF
 187        test_cmp expected actual
 188'
 189
 190test_expect_success 'left alignment formatting with no padding' '
 191        git log --pretty="tformat:%<(1)%s" >actual &&
 192        cat <<EOF >expected &&
 193message two
 194message one
 195add bar
 196$(commit_msg)
 197EOF
 198        test_cmp expected actual
 199'
 200
 201test_expect_success 'left alignment formatting with no padding. i18n.logOutputEncoding' '
 202        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(1)%s" >actual &&
 203        cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 204message two
 205message one
 206add bar
 207$(commit_msg)
 208EOF
 209        test_cmp expected actual
 210'
 211
 212test_expect_success 'left alignment formatting with trunc' '
 213        git log --pretty="tformat:%<(10,trunc)%s" >actual &&
 214        qz_to_tab_space <<EOF >expected &&
 215message ..
 216message ..
 217add bar  Z
 218initial...
 219EOF
 220        test_cmp expected actual
 221'
 222
 223test_expect_success 'left alignment formatting with trunc. i18n.logOutputEncoding' '
 224        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s" >actual &&
 225        qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 226message ..
 227message ..
 228add bar  Z
 229initial...
 230EOF
 231        test_cmp expected actual
 232'
 233
 234test_expect_success 'left alignment formatting with ltrunc' '
 235        git log --pretty="tformat:%<(10,ltrunc)%s" >actual &&
 236        qz_to_tab_space <<EOF >expected &&
 237..sage two
 238..sage one
 239add bar  Z
 240..${sample_utf8_part}lich
 241EOF
 242        test_cmp expected actual
 243'
 244
 245test_expect_success 'left alignment formatting with ltrunc. i18n.logOutputEncoding' '
 246        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,ltrunc)%s" >actual &&
 247        qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 248..sage two
 249..sage one
 250add bar  Z
 251..${sample_utf8_part}lich
 252EOF
 253        test_cmp expected actual
 254'
 255
 256test_expect_success 'left alignment formatting with mtrunc' '
 257        git log --pretty="tformat:%<(10,mtrunc)%s" >actual &&
 258        qz_to_tab_space <<EOF >expected &&
 259mess.. two
 260mess.. one
 261add bar  Z
 262init..lich
 263EOF
 264        test_cmp expected actual
 265'
 266
 267test_expect_success 'left alignment formatting with mtrunc. i18n.logOutputEncoding' '
 268        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,mtrunc)%s" >actual &&
 269        qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 270mess.. two
 271mess.. one
 272add bar  Z
 273init..lich
 274EOF
 275        test_cmp expected actual
 276'
 277
 278test_expect_success 'right alignment formatting' '
 279        git log --pretty="tformat:%>(40)%s" >actual &&
 280        qz_to_tab_space <<EOF >expected &&
 281Z                            message two
 282Z                            message one
 283Z                                add bar
 284Z                    $(commit_msg)
 285EOF
 286        test_cmp expected actual
 287'
 288
 289test_expect_success 'right alignment formatting. i18n.logOutputEncoding' '
 290        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(40)%s" >actual &&
 291        qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 292Z                            message two
 293Z                            message one
 294Z                                add bar
 295Z                    $(commit_msg)
 296EOF
 297        test_cmp expected actual
 298'
 299
 300test_expect_success 'right alignment formatting at the nth column' '
 301        git log --pretty="tformat:%h %>|(40)%s" >actual &&
 302        qz_to_tab_space <<EOF >expected &&
 303$head1                      message two
 304$head2                      message one
 305$head3                          add bar
 306$head4              $(commit_msg)
 307EOF
 308        test_cmp expected actual
 309'
 310
 311test_expect_success 'right alignment formatting at the nth column. i18n.logOutputEncoding' '
 312        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %>|(40)%s" >actual &&
 313        qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 314$head1                      message two
 315$head2                      message one
 316$head3                          add bar
 317$head4              $(commit_msg)
 318EOF
 319        test_cmp expected actual
 320'
 321
 322# Note: Space between 'message' and 'two' should be in the same column
 323# as in previous test.
 324test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' '
 325        git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual &&
 326        iconv -f utf-8 -t $test_encoding >expected <<EOF&&
 327* $head1                    message two
 328* $head2                    message one
 329* $head3                        add bar
 330* $head4            $(commit_msg)
 331EOF
 332        test_cmp expected actual
 333'
 334
 335test_expect_success 'right alignment formatting with no padding' '
 336        git log --pretty="tformat:%>(1)%s" >actual &&
 337        cat <<EOF >expected &&
 338message two
 339message one
 340add bar
 341$(commit_msg)
 342EOF
 343        test_cmp expected actual
 344'
 345
 346test_expect_success 'right alignment formatting with no padding and with --graph' '
 347        git log --graph --pretty="tformat:%>(1)%s" >actual &&
 348        cat <<EOF >expected &&
 349* message two
 350* message one
 351* add bar
 352* $(commit_msg)
 353EOF
 354        test_cmp expected actual
 355'
 356
 357test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' '
 358        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual &&
 359        cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 360message two
 361message one
 362add bar
 363$(commit_msg)
 364EOF
 365        test_cmp expected actual
 366'
 367
 368test_expect_success 'center alignment formatting' '
 369        git log --pretty="tformat:%><(40)%s" >actual &&
 370        qz_to_tab_space <<EOF >expected &&
 371Z             message two              Z
 372Z             message one              Z
 373Z               add bar                Z
 374Z         $(commit_msg)          Z
 375EOF
 376        test_cmp expected actual
 377'
 378
 379test_expect_success 'center alignment formatting. i18n.logOutputEncoding' '
 380        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%><(40)%s" >actual &&
 381        qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 382Z             message two              Z
 383Z             message one              Z
 384Z               add bar                Z
 385Z         $(commit_msg)          Z
 386EOF
 387        test_cmp expected actual
 388'
 389test_expect_success 'center alignment formatting at the nth column' '
 390        git log --pretty="tformat:%h %><|(40)%s" >actual &&
 391        qz_to_tab_space <<EOF >expected &&
 392$head1           message two          Z
 393$head2           message one          Z
 394$head3             add bar            Z
 395$head4       $(commit_msg)      Z
 396EOF
 397        test_cmp expected actual
 398'
 399
 400test_expect_success 'center alignment formatting at the nth column. i18n.logOutputEncoding' '
 401        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %><|(40)%s" >actual &&
 402        qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 403$head1           message two          Z
 404$head2           message one          Z
 405$head3             add bar            Z
 406$head4       $(commit_msg)      Z
 407EOF
 408        test_cmp expected actual
 409'
 410
 411test_expect_success 'center alignment formatting with no padding' '
 412        git log --pretty="tformat:%><(1)%s" >actual &&
 413        cat <<EOF >expected &&
 414message two
 415message one
 416add bar
 417$(commit_msg)
 418EOF
 419        test_cmp expected actual
 420'
 421
 422# save HEAD's SHA-1 digest (with no abbreviations) to use it below
 423# as far as the next test amends HEAD
 424old_head1=$(git rev-parse --verify HEAD~0)
 425test_expect_success 'center alignment formatting with no padding. i18n.logOutputEncoding' '
 426        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%><(1)%s" >actual &&
 427        cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 428message two
 429message one
 430add bar
 431$(commit_msg)
 432EOF
 433        test_cmp expected actual
 434'
 435
 436test_expect_success 'left/right alignment formatting with stealing' '
 437        git commit --amend -m short --author "long long long <long@me.com>" &&
 438        git log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
 439        cat <<EOF >expected &&
 440short long  long long
 441message ..   A U Thor
 442add bar      A U Thor
 443initial...   A U Thor
 444EOF
 445        test_cmp expected actual
 446'
 447test_expect_success 'left/right alignment formatting with stealing. i18n.logOutputEncoding' '
 448        git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
 449        cat <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
 450short long  long long
 451message ..   A U Thor
 452add bar      A U Thor
 453initial...   A U Thor
 454EOF
 455        test_cmp expected actual
 456'
 457
 458test_expect_success 'strbuf_utf8_replace() not producing NUL' '
 459        git log --color --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)%C(auto)%d" |
 460                test_decode_color |
 461                nul_to_q >actual &&
 462        ! grep Q actual
 463'
 464
 465# ISO strict date format
 466test_expect_success 'ISO and ISO-strict date formats display the same values' '
 467        git log --format=%ai%n%ci |
 468        sed -e "s/ /T/; s/ //; s/..\$/:&/" >expected &&
 469        git log --format=%aI%n%cI >actual &&
 470        test_cmp expected actual
 471'
 472
 473# get new digests (with no abbreviations)
 474head1=$(git rev-parse --verify HEAD~0) &&
 475head2=$(git rev-parse --verify HEAD~1) &&
 476
 477test_expect_success 'log decoration properly follows tag chain' '
 478        git tag -a tag1 -m tag1 &&
 479        git tag -a tag2 -m tag2 tag1 &&
 480        git tag -d tag1 &&
 481        git commit --amend -m shorter &&
 482        git log --no-walk --tags --pretty="%H %d" --decorate=full >actual &&
 483        cat <<EOF >expected &&
 484$head1  (tag: refs/tags/tag2)
 485$head2  (tag: refs/tags/message-one)
 486$old_head1  (tag: refs/tags/message-two)
 487EOF
 488        sort actual >actual1 &&
 489        test_cmp expected actual1
 490'
 491
 492test_expect_success 'clean log decoration' '
 493        git log --no-walk --tags --pretty="%H %D" --decorate=full >actual &&
 494        cat >expected <<EOF &&
 495$head1 tag: refs/tags/tag2
 496$head2 tag: refs/tags/message-one
 497$old_head1 tag: refs/tags/message-two
 498EOF
 499        sort actual >actual1 &&
 500        test_cmp expected actual1
 501'
 502
 503test_done