1#!/bin/sh
   2#
   3# Copyright (c) 2005 Johannes Schindelin
   4#
   5test_description='Test git config in different settings'
   7. ./test-lib.sh
   9test -f .git/config && rm .git/config
  11git config core.penguin "little blue"
  13cat > expect << EOF
  15[core]
  16        penguin = little blue
  17EOF
  18test_expect_success 'initial' 'cmp .git/config expect'
  20git config Core.Movie BadPhysics
  22cat > expect << EOF
  24[core]
  25        penguin = little blue
  26        Movie = BadPhysics
  27EOF
  28test_expect_success 'mixed case' 'cmp .git/config expect'
  30git config Cores.WhatEver Second
  32cat > expect << EOF
  34[core]
  35        penguin = little blue
  36        Movie = BadPhysics
  37[Cores]
  38        WhatEver = Second
  39EOF
  40test_expect_success 'similar section' 'cmp .git/config expect'
  42git config CORE.UPPERCASE true
  44cat > expect << EOF
  46[core]
  47        penguin = little blue
  48        Movie = BadPhysics
  49        UPPERCASE = true
  50[Cores]
  51        WhatEver = Second
  52EOF
  53test_expect_success 'similar section' 'cmp .git/config expect'
  55test_expect_success 'replace with non-match' \
  57        'git config core.penguin kingpin !blue'
  58test_expect_success 'replace with non-match (actually matching)' \
  60        'git config core.penguin "very blue" !kingpin'
  61cat > expect << EOF
  63[core]
  64        penguin = very blue
  65        Movie = BadPhysics
  66        UPPERCASE = true
  67        penguin = kingpin
  68[Cores]
  69        WhatEver = Second
  70EOF
  71test_expect_success 'non-match result' 'cmp .git/config expect'
  73cat > .git/config <<\EOF
  75[alpha]
  76bar = foo
  77[beta]
  78baz = multiple \
  79lines
  80EOF
  81test_expect_success 'unset with cont. lines' \
  83        'git config --unset beta.baz'
  84cat > expect <<\EOF
  86[alpha]
  87bar = foo
  88[beta]
  89EOF
  90test_expect_success 'unset with cont. lines is correct' 'cmp .git/config expect'
  92cat > .git/config << EOF
  94[beta] ; silly comment # another comment
  95noIndent= sillyValue ; 'nother silly comment
  96# empty line
  98                ; comment
  99                haha   ="beta" # last silly comment
 100haha = hello
 101        haha = bello
 102[nextSection] noNewline = ouch
 103EOF
 104cp .git/config .git/config2
 106test_expect_success 'multiple unset' \
 108        'git config --unset-all beta.haha'
 109cat > expect << EOF
 111[beta] ; silly comment # another comment
 112noIndent= sillyValue ; 'nother silly comment
 113# empty line
 115                ; comment
 116[nextSection] noNewline = ouch
 117EOF
 118test_expect_success 'multiple unset is correct' 'cmp .git/config expect'
 120mv .git/config2 .git/config
 122test_expect_success '--replace-all' \
 124        'git config --replace-all beta.haha gamma'
 125cat > expect << EOF
 127[beta] ; silly comment # another comment
 128noIndent= sillyValue ; 'nother silly comment
 129# empty line
 131                ; comment
 132        haha = gamma
 133[nextSection] noNewline = ouch
 134EOF
 135test_expect_success 'all replaced' 'cmp .git/config expect'
 137git config beta.haha alpha
 139cat > expect << EOF
 141[beta] ; silly comment # another comment
 142noIndent= sillyValue ; 'nother silly comment
 143# empty line
 145                ; comment
 146        haha = alpha
 147[nextSection] noNewline = ouch
 148EOF
 149test_expect_success 'really mean test' 'cmp .git/config expect'
 151git config nextsection.nonewline wow
 153cat > expect << EOF
 155[beta] ; silly comment # another comment
 156noIndent= sillyValue ; 'nother silly comment
 157# empty line
 159                ; comment
 160        haha = alpha
 161[nextSection]
 162        nonewline = wow
 163EOF
 164test_expect_success 'really really mean test' 'cmp .git/config expect'
 166test_expect_success 'get value' 'test alpha = $(git config beta.haha)'
 168git config --unset beta.haha
 169cat > expect << EOF
 171[beta] ; silly comment # another comment
 172noIndent= sillyValue ; 'nother silly comment
 173# empty line
 175                ; comment
 176[nextSection]
 177        nonewline = wow
 178EOF
 179test_expect_success 'unset' 'cmp .git/config expect'
 181git config nextsection.NoNewLine "wow2 for me" "for me$"
 183cat > expect << EOF
 185[beta] ; silly comment # another comment
 186noIndent= sillyValue ; 'nother silly comment
 187# empty line
 189                ; comment
 190[nextSection]
 191        nonewline = wow
 192        NoNewLine = wow2 for me
 193EOF
 194test_expect_success 'multivar' 'cmp .git/config expect'
 196test_expect_success 'non-match' \
 198        'git config --get nextsection.nonewline !for'
 199test_expect_success 'non-match value' \
 201        'test wow = $(git config --get nextsection.nonewline !for)'
 202test_expect_success 'ambiguous get' '
 204        ! git config --get nextsection.nonewline
 205'
 206test_expect_success 'get multivar' \
 208        'git config --get-all nextsection.nonewline'
 209git config nextsection.nonewline "wow3" "wow$"
 211cat > expect << EOF
 213[beta] ; silly comment # another comment
 214noIndent= sillyValue ; 'nother silly comment
 215# empty line
 217                ; comment
 218[nextSection]
 219        nonewline = wow3
 220        NoNewLine = wow2 for me
 221EOF
 222test_expect_success 'multivar replace' 'cmp .git/config expect'
 224test_expect_success 'ambiguous value' '
 226        ! git config nextsection.nonewline
 227'
 228test_expect_success 'ambiguous unset' '
 230        ! git config --unset nextsection.nonewline
 231'
 232test_expect_success 'invalid unset' '
 234        ! git config --unset somesection.nonewline
 235'
 236git config --unset nextsection.nonewline "wow3$"
 238cat > expect << EOF
 240[beta] ; silly comment # another comment
 241noIndent= sillyValue ; 'nother silly comment
 242# empty line
 244                ; comment
 245[nextSection]
 246        NoNewLine = wow2 for me
 247EOF
 248test_expect_success 'multivar unset' 'cmp .git/config expect'
 250test_expect_success 'invalid key' '! git config inval.2key blabla'
 252test_expect_success 'correct key' 'git config 123456.a123 987'
 254test_expect_success 'hierarchical section' \
 256        'git config Version.1.2.3eX.Alpha beta'
 257cat > expect << EOF
 259[beta] ; silly comment # another comment
 260noIndent= sillyValue ; 'nother silly comment
 261# empty line
 263                ; comment
 264[nextSection]
 265        NoNewLine = wow2 for me
 266[123456]
 267        a123 = 987
 268[Version "1.2.3eX"]
 269        Alpha = beta
 270EOF
 271test_expect_success 'hierarchical section value' 'cmp .git/config expect'
 273cat > expect << EOF
 275beta.noindent=sillyValue
 276nextsection.nonewline=wow2 for me
 277123456.a123=987
 278version.1.2.3eX.alpha=beta
 279EOF
 280test_expect_success 'working --list' \
 282        'git config --list > output && cmp output expect'
 283cat > expect << EOF
 285beta.noindent sillyValue
 286nextsection.nonewline wow2 for me
 287EOF
 288test_expect_success '--get-regexp' \
 290        'git config --get-regexp in > output && cmp output expect'
 291git config --add nextsection.nonewline "wow4 for you"
 293cat > expect << EOF
 295wow2 for me
 296wow4 for you
 297EOF
 298test_expect_success '--add' \
 300        'git config --get-all nextsection.nonewline > output && cmp output expect'
 301cat > .git/config << EOF
 303[novalue]
 304        variable
 305[emptyvalue]
 306        variable =
 307EOF
 308test_expect_success 'get variable with no value' \
 310        'git config --get novalue.variable ^$'
 311test_expect_success 'get variable with empty value' \
 313        'git config --get emptyvalue.variable ^$'
 314echo novalue.variable > expect
 316test_expect_success 'get-regexp variable with no value' \
 318        'git config --get-regexp novalue > output &&
 319         cmp output expect'
 320echo 'emptyvalue.variable ' > expect
 322test_expect_success 'get-regexp variable with empty value' \
 324        'git config --get-regexp emptyvalue > output &&
 325         cmp output expect'
 326echo true > expect
 328test_expect_success 'get bool variable with no value' \
 330        'git config --bool novalue.variable > output &&
 331         cmp output expect'
 332echo false > expect
 334test_expect_success 'get bool variable with empty value' \
 336        'git config --bool emptyvalue.variable > output &&
 337         cmp output expect'
 338git config > output 2>&1
 340test_expect_success 'no arguments, but no crash' \
 342        "test $? = 129 && grep usage output"
 343cat > .git/config << EOF
 345[a.b]
 346        c = d
 347EOF
 348git config a.x y
 350cat > expect << EOF
 352[a.b]
 353        c = d
 354[a]
 355        x = y
 356EOF
 357test_expect_success 'new section is partial match of another' 'cmp .git/config expect'
 359git config b.x y
 361git config a.b c
 362cat > expect << EOF
 364[a.b]
 365        c = d
 366[a]
 367        x = y
 368        b = c
 369[b]
 370        x = y
 371EOF
 372test_expect_success 'new variable inserts into proper section' 'cmp .git/config expect'
 374test_expect_success 'alternative GIT_CONFIG (non-existing file should fail)' \
 376        'git config --file non-existing-config -l; test $? != 0'
 377cat > other-config << EOF
 379[ein]
 380        bahn = strasse
 381EOF
 382cat > expect << EOF
 384ein.bahn=strasse
 385EOF
 386GIT_CONFIG=other-config git config -l > output
 388test_expect_success 'alternative GIT_CONFIG' 'cmp output expect'
 390test_expect_success 'alternative GIT_CONFIG (--file)' \
 392        'git config --file other-config -l > output && cmp output expect'
 393GIT_CONFIG=other-config git config anwohner.park ausweis
 395cat > expect << EOF
 397[ein]
 398        bahn = strasse
 399[anwohner]
 400        park = ausweis
 401EOF
 402test_expect_success '--set in alternative GIT_CONFIG' 'cmp other-config expect'
 404cat > .git/config << EOF
 406# Hallo
 407        #Bello
 408[branch "eins"]
 409        x = 1
 410[branch.eins]
 411        y = 1
 412        [branch "1 234 blabl/a"]
 413weird
 414EOF
 415test_expect_success "rename section" \
 417        "git config --rename-section branch.eins branch.zwei"
 418cat > expect << EOF
 420# Hallo
 421        #Bello
 422[branch "zwei"]
 423        x = 1
 424[branch "zwei"]
 425        y = 1
 426        [branch "1 234 blabl/a"]
 427weird
 428EOF
 429test_expect_success "rename succeeded" "test_cmp expect .git/config"
 431test_expect_success "rename non-existing section" '
 433        ! git config --rename-section branch."world domination" branch.drei
 434'
 435test_expect_success "rename succeeded" "test_cmp expect .git/config"
 437test_expect_success "rename another section" \
 439        'git config --rename-section branch."1 234 blabl/a" branch.drei'
 440cat > expect << EOF
 442# Hallo
 443        #Bello
 444[branch "zwei"]
 445        x = 1
 446[branch "zwei"]
 447        y = 1
 448[branch "drei"]
 449weird
 450EOF
 451test_expect_success "rename succeeded" "test_cmp expect .git/config"
 453cat >> .git/config << EOF
 455  [branch "zwei"] a = 1 [branch "vier"]
 456EOF
 457test_expect_success "remove section" "git config --remove-section branch.zwei"
 459cat > expect << EOF
 461# Hallo
 462        #Bello
 463[branch "drei"]
 464weird
 465EOF
 466test_expect_success "section was removed properly" \
 468        "test_cmp expect .git/config"
 469rm .git/config
 471cat > expect << EOF
 473[gitcvs]
 474        enabled = true
 475        dbname = %Ggitcvs2.%a.%m.sqlite
 476[gitcvs "ext"]
 477        dbname = %Ggitcvs1.%a.%m.sqlite
 478EOF
 479test_expect_success 'section ending' '
 481        git config gitcvs.enabled true &&
 483        git config gitcvs.ext.dbname %Ggitcvs1.%a.%m.sqlite &&
 484        git config gitcvs.dbname %Ggitcvs2.%a.%m.sqlite &&
 485        cmp .git/config expect
 486'
 488test_expect_success numbers '
 490        git config kilo.gram 1k &&
 492        git config mega.ton 1m &&
 493        k=$(git config --int --get kilo.gram) &&
 494        test z1024 = "z$k" &&
 495        m=$(git config --int --get mega.ton) &&
 496        test z1048576 = "z$m"
 497'
 498cat > expect <<EOF
 500fatal: bad config value for 'aninvalid.unit' in .git/config
 501EOF
 502test_expect_success 'invalid unit' '
 504        git config aninvalid.unit "1auto" &&
 506        s=$(git config aninvalid.unit) &&
 507        test "z1auto" = "z$s" &&
 508        if git config --int --get aninvalid.unit 2>actual
 509        then
 510                echo config should have failed
 511                false
 512        fi &&
 513        cmp actual expect
 514'
 515cat > expect << EOF
 517true
 518false
 519true
 520false
 521true
 522false
 523true
 524false
 525EOF
 526test_expect_success bool '
 528        git config bool.true1 01 &&
 530        git config bool.true2 -1 &&
 531        git config bool.true3 YeS &&
 532        git config bool.true4 true &&
 533        git config bool.false1 000 &&
 534        git config bool.false2 "" &&
 535        git config bool.false3 nO &&
 536        git config bool.false4 FALSE &&
 537        rm -f result &&
 538        for i in 1 2 3 4
 539        do
 540            git config --bool --get bool.true$i >>result
 541            git config --bool --get bool.false$i >>result
 542        done &&
 543        cmp expect result'
 544test_expect_success 'invalid bool (--get)' '
 546        git config bool.nobool foobar &&
 548        ! git config --bool --get bool.nobool'
 549test_expect_success 'invalid bool (set)' '
 551        ! git config --bool bool.nobool foobar'
 553rm .git/config
 555cat > expect <<\EOF
 557[bool]
 558        true1 = true
 559        true2 = true
 560        true3 = true
 561        true4 = true
 562        false1 = false
 563        false2 = false
 564        false3 = false
 565        false4 = false
 566EOF
 567test_expect_success 'set --bool' '
 569        git config --bool bool.true1 01 &&
 571        git config --bool bool.true2 -1 &&
 572        git config --bool bool.true3 YeS &&
 573        git config --bool bool.true4 true &&
 574        git config --bool bool.false1 000 &&
 575        git config --bool bool.false2 "" &&
 576        git config --bool bool.false3 nO &&
 577        git config --bool bool.false4 FALSE &&
 578        cmp expect .git/config'
 579rm .git/config
 581cat > expect <<\EOF
 583[int]
 584        val1 = 1
 585        val2 = -1
 586        val3 = 5242880
 587EOF
 588test_expect_success 'set --int' '
 590        git config --int int.val1 01 &&
 592        git config --int int.val2 -1 &&
 593        git config --int int.val3 5m &&
 594        cmp expect .git/config'
 595rm .git/config
 597cat >expect <<\EOF
 599[bool]
 600        true1 = true
 601        true2 = true
 602        false1 = false
 603        false2 = false
 604[int]
 605        int1 = 0
 606        int2 = 1
 607        int3 = -1
 608EOF
 609test_expect_success 'get --bool-or-int' '
 611        (
 612                echo "[bool]"
 613                echo true1
 614                echo true2 = true
 615                echo false = false
 616                echo "[int]"
 617                echo int1 = 0
 618                echo int2 = 1
 619                echo int3 = -1
 620        ) >>.git/config &&
 621        test $(git config --bool-or-int bool.true1) = true &&
 622        test $(git config --bool-or-int bool.true2) = true &&
 623        test $(git config --bool-or-int bool.false) = false &&
 624        test $(git config --bool-or-int int.int1) = 0 &&
 625        test $(git config --bool-or-int int.int2) = 1 &&
 626        test $(git config --bool-or-int int.int3) = -1
 627'
 629rm .git/config
 631cat >expect <<\EOF
 632[bool]
 633        true1 = true
 634        false1 = false
 635        true2 = true
 636        false2 = false
 637[int]
 638        int1 = 0
 639        int2 = 1
 640        int3 = -1
 641EOF
 642test_expect_success 'set --bool-or-int' '
 644        git config --bool-or-int bool.true1 true &&
 645        git config --bool-or-int bool.false1 false &&
 646        git config --bool-or-int bool.true2 yes &&
 647        git config --bool-or-int bool.false2 no &&
 648        git config --bool-or-int int.int1 0 &&
 649        git config --bool-or-int int.int2 1 &&
 650        git config --bool-or-int int.int3 -1 &&
 651        test_cmp expect .git/config
 652'
 653rm .git/config
 655git config quote.leading " test"
 657git config quote.ending "test "
 658git config quote.semicolon "test;test"
 659git config quote.hash "test#test"
 660cat > expect << EOF
 662[quote]
 663        leading = " test"
 664        ending = "test "
 665        semicolon = "test;test"
 666        hash = "test#test"
 667EOF
 668test_expect_success 'quoting' 'cmp .git/config expect'
 670test_expect_success 'key with newline' '
 672        ! git config "key.with
 673newline" 123'
 674test_expect_success 'value with newline' 'git config key.sub value.with\\\
 676newline'
 677cat > .git/config <<\EOF
 679[section]
 680        ; comment \
 681        continued = cont\
 682inued
 683        noncont   = not continued ; \
 684        quotecont = "cont;\
 685inued"
 686EOF
 687cat > expect <<\EOF
 689section.continued=continued
 690section.noncont=not continued
 691section.quotecont=cont;inued
 692EOF
 693git config --list > result
 695test_expect_success 'value continued on next line' 'cmp result expect'
 697cat > .git/config <<\EOF
 699[section "sub=section"]
 700        val1 = foo=bar
 701        val2 = foo\nbar
 702        val3 = \n\n
 703        val4 =
 704        val5
 705EOF
 706cat > expect <<\EOF
 708section.sub=section.val1
 709foo=barQsection.sub=section.val2
 710foo
 711barQsection.sub=section.val3
 712Qsection.sub=section.val4
 715Qsection.sub=section.val5Q
 716EOF
 717git config --null --list | perl -pe 'y/\000/Q/' > result
 719echo >>result
 720test_expect_success '--null --list' 'cmp result expect'
 722git config --null --get-regexp 'val[0-9]' | perl -pe 'y/\000/Q/' > result
 724echo >>result
 725test_expect_success '--null --get-regexp' 'cmp result expect'
 727test_expect_success 'symlinked configuration' '
 729        ln -s notyet myconfig &&
 731        GIT_CONFIG=myconfig git config test.frotz nitfol &&
 732        test -h myconfig &&
 733        test -f notyet &&
 734        test "z$(GIT_CONFIG=notyet git config test.frotz)" = znitfol &&
 735        GIT_CONFIG=myconfig git config test.xyzzy rezrov &&
 736        test -h myconfig &&
 737        test -f notyet &&
 738        test "z$(GIT_CONFIG=notyet git config test.frotz)" = znitfol &&
 739        test "z$(GIT_CONFIG=notyet git config test.xyzzy)" = zrezrov
 740'
 742test_done