t / t6006-rev-list-format.shon commit Merge branch 'jn/config-ignore-inaccessible' into maint (b1c418e)
   1#!/bin/sh
   2
   3test_description='git rev-list --pretty=format test'
   4
   5. ./test-lib.sh
   6. "$TEST_DIRECTORY"/lib-terminal.sh
   7
   8test_tick
   9test_expect_success 'setup' '
  10touch foo && git add foo && git commit -m "added foo" &&
  11  echo changed >foo && git commit -a -m "changed foo"
  12'
  13
  14# usage: test_format name format_string <expected_output
  15test_format () {
  16        cat >expect.$1
  17        test_expect_success "format $1" "
  18                git rev-list --pretty=format:'$2' master >output.$1 &&
  19                test_cmp expect.$1 output.$1
  20        "
  21}
  22
  23# Feed to --format to provide predictable colored sequences.
  24AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
  25has_color () {
  26        printf '\033[31mfoo\033[m\n' >expect &&
  27        test_cmp expect "$1"
  28}
  29
  30has_no_color () {
  31        echo foo >expect &&
  32        test_cmp expect "$1"
  33}
  34
  35test_format percent %%h <<'EOF'
  36commit 131a310eb913d107dd3c09a65d1651175898735d
  37%h
  38commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
  39%h
  40EOF
  41
  42test_format hash %H%n%h <<'EOF'
  43commit 131a310eb913d107dd3c09a65d1651175898735d
  44131a310eb913d107dd3c09a65d1651175898735d
  45131a310
  46commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
  4786c75cfd708a0e5868dc876ed5b8bb66c80b4873
  4886c75cf
  49EOF
  50
  51test_format tree %T%n%t <<'EOF'
  52commit 131a310eb913d107dd3c09a65d1651175898735d
  53fe722612f26da5064c32ca3843aa154bdb0b08a0
  54fe72261
  55commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
  564d5fcadc293a348e88f777dc0920f11e7d71441c
  574d5fcad
  58EOF
  59
  60test_format parents %P%n%p <<'EOF'
  61commit 131a310eb913d107dd3c09a65d1651175898735d
  6286c75cfd708a0e5868dc876ed5b8bb66c80b4873
  6386c75cf
  64commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
  65
  66
  67EOF
  68
  69# we don't test relative here
  70test_format author %an%n%ae%n%ad%n%aD%n%at <<'EOF'
  71commit 131a310eb913d107dd3c09a65d1651175898735d
  72A U Thor
  73author@example.com
  74Thu Apr 7 15:13:13 2005 -0700
  75Thu, 7 Apr 2005 15:13:13 -0700
  761112911993
  77commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
  78A U Thor
  79author@example.com
  80Thu Apr 7 15:13:13 2005 -0700
  81Thu, 7 Apr 2005 15:13:13 -0700
  821112911993
  83EOF
  84
  85test_format committer %cn%n%ce%n%cd%n%cD%n%ct <<'EOF'
  86commit 131a310eb913d107dd3c09a65d1651175898735d
  87C O Mitter
  88committer@example.com
  89Thu Apr 7 15:13:13 2005 -0700
  90Thu, 7 Apr 2005 15:13:13 -0700
  911112911993
  92commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
  93C O Mitter
  94committer@example.com
  95Thu Apr 7 15:13:13 2005 -0700
  96Thu, 7 Apr 2005 15:13:13 -0700
  971112911993
  98EOF
  99
 100test_format encoding %e <<'EOF'
 101commit 131a310eb913d107dd3c09a65d1651175898735d
 102commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 103EOF
 104
 105test_format subject %s <<'EOF'
 106commit 131a310eb913d107dd3c09a65d1651175898735d
 107changed foo
 108commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 109added foo
 110EOF
 111
 112test_format body %b <<'EOF'
 113commit 131a310eb913d107dd3c09a65d1651175898735d
 114commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 115EOF
 116
 117test_format raw-body %B <<'EOF'
 118commit 131a310eb913d107dd3c09a65d1651175898735d
 119changed foo
 120
 121commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 122added foo
 123
 124EOF
 125
 126test_format colors %Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy <<'EOF'
 127commit 131a310eb913d107dd3c09a65d1651175898735d
 128\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
 129commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 130\e[31mfoo\e[32mbar\e[34mbaz\e[mxyzzy
 131EOF
 132
 133test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<'EOF'
 134commit 131a310eb913d107dd3c09a65d1651175898735d
 135\e[1;31;43mfoo\e[m
 136commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 137\e[1;31;43mfoo\e[m
 138EOF
 139
 140test_expect_success '%C(auto) does not enable color by default' '
 141        git log --format=$AUTO_COLOR -1 >actual &&
 142        has_no_color actual
 143'
 144
 145test_expect_success '%C(auto) enables colors for color.diff' '
 146        git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
 147        has_color actual
 148'
 149
 150test_expect_success '%C(auto) enables colors for color.ui' '
 151        git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
 152        has_color actual
 153'
 154
 155test_expect_success '%C(auto) respects --color' '
 156        git log --format=$AUTO_COLOR -1 --color >actual &&
 157        has_color actual
 158'
 159
 160test_expect_success '%C(auto) respects --no-color' '
 161        git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
 162        has_no_color actual
 163'
 164
 165test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
 166        (
 167                TERM=vt100 && export TERM &&
 168                test_terminal \
 169                        git log --format=$AUTO_COLOR -1 --color=auto >actual &&
 170                has_color actual
 171        )
 172'
 173
 174test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
 175        (
 176                TERM=vt100 && export TERM &&
 177                git log --format=$AUTO_COLOR -1 --color=auto >actual &&
 178                has_no_color actual
 179        )
 180'
 181
 182cat >commit-msg <<'EOF'
 183Test printing of complex bodies
 184
 185This commit message is much longer than the others,
 186and it will be encoded in iso8859-1. We should therefore
 187include an iso8859 character: ¡bueno!
 188EOF
 189test_expect_success 'setup complex body' '
 190git config i18n.commitencoding iso8859-1 &&
 191  echo change2 >foo && git commit -a -F commit-msg
 192'
 193
 194test_format complex-encoding %e <<'EOF'
 195commit 1ed88da4a5b5ed8c449114ac131efc62178734c3
 196iso8859-1
 197commit 131a310eb913d107dd3c09a65d1651175898735d
 198commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 199EOF
 200
 201test_format complex-subject %s <<'EOF'
 202commit 1ed88da4a5b5ed8c449114ac131efc62178734c3
 203Test printing of complex bodies
 204commit 131a310eb913d107dd3c09a65d1651175898735d
 205changed foo
 206commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 207added foo
 208EOF
 209
 210test_format complex-body %b <<'EOF'
 211commit 1ed88da4a5b5ed8c449114ac131efc62178734c3
 212This commit message is much longer than the others,
 213and it will be encoded in iso8859-1. We should therefore
 214include an iso8859 character: ¡bueno!
 215
 216commit 131a310eb913d107dd3c09a65d1651175898735d
 217commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 218EOF
 219
 220test_expect_success '%x00 shows NUL' '
 221        echo  >expect commit 1ed88da4a5b5ed8c449114ac131efc62178734c3 &&
 222        echo >>expect fooQbar &&
 223        git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
 224        nul_to_q <actual.nul >actual &&
 225        test_cmp expect actual
 226'
 227
 228test_expect_success '%ad respects --date=' '
 229        echo 2005-04-07 >expect.ad-short &&
 230        git log -1 --date=short --pretty=tformat:%ad >output.ad-short master &&
 231        test_cmp expect.ad-short output.ad-short
 232'
 233
 234test_expect_success 'empty email' '
 235        test_tick &&
 236        C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
 237        A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
 238        test "$A" = "A U Thor,,Thu Apr 7 15:14:13 2005 -0700" || {
 239                echo "Eh? $A" >failure
 240                false
 241        }
 242'
 243
 244test_expect_success 'del LF before empty (1)' '
 245        git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
 246        test_line_count = 2 actual
 247'
 248
 249test_expect_success 'del LF before empty (2)' '
 250        git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
 251        test_line_count = 6 actual &&
 252        grep "^$" actual
 253'
 254
 255test_expect_success 'add LF before non-empty (1)' '
 256        git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
 257        test_line_count = 2 actual
 258'
 259
 260test_expect_success 'add LF before non-empty (2)' '
 261        git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
 262        test_line_count = 6 actual &&
 263        grep "^$" actual
 264'
 265
 266test_expect_success 'add SP before non-empty (1)' '
 267        git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
 268        test $(wc -w <actual) = 2
 269'
 270
 271test_expect_success 'add SP before non-empty (2)' '
 272        git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
 273        test $(wc -w <actual) = 4
 274'
 275
 276test_expect_success '--abbrev' '
 277        echo SHORT SHORT SHORT >expect2 &&
 278        echo LONG LONG LONG >expect3 &&
 279        git log -1 --format="%h %h %h" HEAD >actual1 &&
 280        git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
 281        git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
 282        sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
 283        sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
 284        test_cmp expect2 fuzzy2 &&
 285        test_cmp expect3 fuzzy3 &&
 286        ! test_cmp actual1 actual2
 287'
 288
 289test_expect_success '%H is not affected by --abbrev-commit' '
 290        git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
 291        len=$(wc -c <actual) &&
 292        test $len = 41
 293'
 294
 295test_expect_success '%h is not affected by --abbrev-commit' '
 296        git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
 297        len=$(wc -c <actual) &&
 298        test $len = 21
 299'
 300
 301test_expect_success '"%h %gD: %gs" is same as git-reflog' '
 302        git reflog >expect &&
 303        git log -g --format="%h %gD: %gs" >actual &&
 304        test_cmp expect actual
 305'
 306
 307test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
 308        git reflog --date=raw >expect &&
 309        git log -g --format="%h %gD: %gs" --date=raw >actual &&
 310        test_cmp expect actual
 311'
 312
 313test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
 314        git reflog --abbrev=13 --date=raw >expect &&
 315        git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
 316        test_cmp expect actual
 317'
 318
 319test_expect_success '%gd shortens ref name' '
 320        echo "master@{0}" >expect.gd-short &&
 321        git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&
 322        test_cmp expect.gd-short actual.gd-short
 323'
 324
 325test_expect_success 'reflog identity' '
 326        echo "C O Mitter:committer@example.com" >expect &&
 327        git log -g -1 --format="%gn:%ge" >actual &&
 328        test_cmp expect actual
 329'
 330
 331test_expect_success 'oneline with empty message' '
 332        git commit -m "dummy" --allow-empty &&
 333        git commit -m "dummy" --allow-empty &&
 334        git filter-branch --msg-filter "sed -e s/dummy//" HEAD^^.. &&
 335        git rev-list --oneline HEAD >test.txt &&
 336        test_line_count = 5 test.txt &&
 337        git rev-list --oneline --graph HEAD >testg.txt &&
 338        test_line_count = 5 testg.txt
 339'
 340
 341test_expect_success 'single-character name is parsed correctly' '
 342        git commit --author="a <a@example.com>" --allow-empty -m foo &&
 343        echo "a <a@example.com>" >expect &&
 344        git log -1 --format="%an <%ae>" >actual &&
 345        test_cmp expect actual
 346'
 347
 348test_done