]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1300-config.sh
builtin/config: introduce "get" subcommand
[thirdparty/git.git] / t / t1300-config.sh
CommitLineData
942c1f53
JS
1#!/bin/sh
2#
3# Copyright (c) 2005 Johannes Schindelin
4#
5
5be60078 6test_description='Test git config in different settings'
942c1f53 7
06d53148 8GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
9export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
67606301 11TEST_PASSES_SANITIZE_LEAK=true
942c1f53
JS
12. ./test-lib.sh
13
14970509
PS
14for mode in legacy subcommands
15do
16
17case "$mode" in
18legacy)
19 mode_prefix="--"
4e513890
PS
20 mode_get=""
21 mode_get_all="--get-all"
22 mode_get_regexp="--get-regexp"
14970509
PS
23 ;;
24subcommands)
25 mode_prefix=""
4e513890
PS
26 mode_get="get"
27 mode_get_all="get --all"
28 mode_get_regexp="get --regexp --all --show-names"
14970509
PS
29 ;;
30*)
31 BUG "unknown mode $mode";;
32esac
33
d71bc1b4
DS
34test_expect_success 'setup whitespace config' '
35 sed -e "s/^|//" \
36 -e "s/[$]$//" \
37 -e "s/X/ /g" >.git/config <<-\EOF
38 [section]
39 | solid = rock
40 | sparse = big XX blue
41 | sparseAndTail = big XX blue $
42 | sparseAndTailQuoted = "big XX blue "
43 | sparseAndBiggerTail = big XX blue X X
44 | sparseAndBiggerTailQuoted = "big XX blue X X"
45 | sparseAndBiggerTailQuotedPlus = "big XX blue X X"X $
46 | headAndTail = Xbig blue $
47 | headAndTailQuoted = "Xbig blue "
48 | headAndTailQuotedPlus = "Xbig blue " $
49 | annotated = big blueX# to be discarded
50 | annotatedQuoted = "big blue"X# to be discarded
51 EOF
52'
53
54test_expect_success 'no internal whitespace' '
55 echo "rock" >expect &&
56 git config --get section.solid >actual &&
57 test_cmp expect actual
58'
59
60test_expect_success 'internal whitespace' '
61 echo "big QQ blue" | q_to_tab >expect &&
62 git config --get section.sparse >actual &&
63 test_cmp expect actual
64'
65
66test_expect_success 'internal and trailing whitespace' '
67 echo "big QQ blue" | q_to_tab >expect &&
68 git config --get section.sparseAndTail >actual &&
69 test_cmp expect actual
70'
71
72test_expect_success 'internal and trailing whitespace, all quoted' '
73 echo "big QQ blue " | q_to_tab >expect &&
74 git config --get section.sparseAndTailQuoted >actual &&
75 test_cmp expect actual
76'
77
78test_expect_success 'internal and more trailing whitespace' '
79 echo "big QQ blue" | q_to_tab >expect &&
80 git config --get section.sparseAndBiggerTail >actual &&
81 test_cmp expect actual
82'
83
84test_expect_success 'internal and more trailing whitespace, all quoted' '
85 echo "big QQ blue Q Q" | q_to_tab >expect &&
86 git config --get section.sparseAndBiggerTailQuoted >actual &&
87 test_cmp expect actual
88'
89
90test_expect_success 'internal and more trailing whitespace, not all quoted' '
91 echo "big QQ blue Q Q" | q_to_tab >expect &&
92 git config --get section.sparseAndBiggerTailQuotedPlus >actual &&
93 test_cmp expect actual
94'
95
96test_expect_success 'leading and trailing whitespace' '
97 echo "big blue" >expect &&
98 git config --get section.headAndTail >actual &&
99 test_cmp expect actual
100'
101
102test_expect_success 'leading and trailing whitespace, all quoted' '
103 echo "Qbig blue " | q_to_tab >expect &&
104 git config --get section.headAndTailQuoted >actual &&
105 test_cmp expect actual
106'
107
108test_expect_success 'leading and trailing whitespace, not all quoted' '
109 echo "Qbig blue " | q_to_tab >expect &&
110 git config --get section.headAndTailQuotedPlus >actual &&
111 test_cmp expect actual
112'
113
114test_expect_success 'inline comment' '
115 echo "big blue" >expect &&
116 git config --get section.annotated >actual &&
117 test_cmp expect actual
118'
119
120test_expect_success 'inline comment, quoted' '
121 echo "big blue" >expect &&
122 git config --get section.annotatedQuoted >actual &&
123 test_cmp expect actual
124'
125
5a953fc5
JK
126test_expect_success 'clear default config' '
127 rm -f .git/config
128'
942c1f53
JS
129
130cat > expect << EOF
04f6b0a1 131[section]
942c1f53
JS
132 penguin = little blue
133EOF
5a953fc5 134test_expect_success 'initial' '
04f6b0a1 135 git config section.penguin "little blue" &&
5a953fc5
JK
136 test_cmp expect .git/config
137'
942c1f53
JS
138
139cat > expect << EOF
04f6b0a1 140[section]
942c1f53
JS
141 penguin = little blue
142 Movie = BadPhysics
143EOF
5a953fc5 144test_expect_success 'mixed case' '
04f6b0a1 145 git config Section.Movie BadPhysics &&
5a953fc5
JK
146 test_cmp expect .git/config
147'
942c1f53
JS
148
149cat > expect << EOF
04f6b0a1 150[section]
942c1f53
JS
151 penguin = little blue
152 Movie = BadPhysics
04f6b0a1 153[Sections]
942c1f53
JS
154 WhatEver = Second
155EOF
5a953fc5 156test_expect_success 'similar section' '
04f6b0a1 157 git config Sections.WhatEver Second &&
5a953fc5
JK
158 test_cmp expect .git/config
159'
942c1f53
JS
160
161cat > expect << EOF
04f6b0a1 162[section]
942c1f53
JS
163 penguin = little blue
164 Movie = BadPhysics
165 UPPERCASE = true
04f6b0a1 166[Sections]
942c1f53
JS
167 WhatEver = Second
168EOF
5a953fc5 169test_expect_success 'uppercase section' '
04f6b0a1 170 git config SECTION.UPPERCASE true &&
5a953fc5
JK
171 test_cmp expect .git/config
172'
942c1f53 173
ed838e66 174test_expect_success 'replace with non-match' '
04f6b0a1 175 git config section.penguin kingpin !blue
ed838e66 176'
f98d863d 177
ed838e66 178test_expect_success 'replace with non-match (actually matching)' '
04f6b0a1 179 git config section.penguin "very blue" !kingpin
ed838e66 180'
f98d863d
JS
181
182cat > expect << EOF
04f6b0a1 183[section]
f98d863d
JS
184 Movie = BadPhysics
185 UPPERCASE = true
fbad334d
JH
186 penguin = gentoo # Pygoscelis papua
187 disposition = peckish # find fish
188 foo = bar #abc
31399a6b
JH
189 spsp = value # and comment
190 htsp = value # and comment
04f6b0a1 191[Sections]
f98d863d
JS
192 WhatEver = Second
193EOF
194
42d5c033
RS
195test_expect_success 'append comments' '
196 git config --replace-all --comment="Pygoscelis papua" section.penguin gentoo &&
197 git config --comment="find fish" section.disposition peckish &&
fbad334d 198 git config --comment="#abc" section.foo bar &&
31399a6b
JH
199
200 git config --comment="and comment" section.spsp value &&
201 git config --comment=" # and comment" section.htsp value &&
202
42d5c033
RS
203 test_cmp expect .git/config
204'
205
206test_expect_success 'Prohibited LF in comment' '
207 test_must_fail git config --comment="a${LF}b" section.k v
208'
f98d863d 209
5a953fc5 210test_expect_success 'non-match result' 'test_cmp expect .git/config'
f98d863d 211
88d42af8 212test_expect_success 'find mixed-case key by canonical name' '
04f6b0a1 213 test_cmp_config Second sections.whatever
88d42af8
JK
214'
215
216test_expect_success 'find mixed-case key by non-canonical name' '
04f6b0a1 217 test_cmp_config Second SeCtIoNs.WhAtEvEr
88d42af8
JK
218'
219
220test_expect_success 'subsections are not canonicalized by git-config' '
221 cat >>.git/config <<-\EOF &&
222 [section.SubSection]
223 key = one
224 [section "SubSection"]
225 key = two
226 EOF
a5db0b77
NTND
227 test_cmp_config one section.subsection.key &&
228 test_cmp_config two section.SubSection.key
88d42af8 229'
f98d863d 230
3d77fbb6
AR
231test_missing_key () {
232 local key="$1" &&
233 local title="$2" &&
234 test_expect_success "value for $title is not printed" '
235 test_must_fail git config "$key" >out 2>err &&
236 test_must_be_empty out &&
237 test_must_be_empty err
238 '
239}
240
241test_missing_key 'missingsection.missingkey' 'missing section and missing key'
242test_missing_key 'missingsection.penguin' 'missing section and existing key'
243test_missing_key 'section.missingkey' 'existing section and missing key'
244test_missing_key 'section.MissingSubSection.missingkey' 'missing subsection and missing key'
245test_missing_key 'section.SubSection.missingkey' 'existing subsection and missing key'
246test_missing_key 'section.MissingSubSection.key' 'missing subsection and existing key'
247
7a31cc0f
FL
248cat > .git/config <<\EOF
249[alpha]
250bar = foo
251[beta]
252baz = multiple \
253lines
85bf5d61 254foo = bar
7a31cc0f
FL
255EOF
256
ed838e66
JK
257test_expect_success 'unset with cont. lines' '
258 git config --unset beta.baz
259'
7a31cc0f
FL
260
261cat > expect <<\EOF
262[alpha]
263bar = foo
264[beta]
85bf5d61 265foo = bar
7a31cc0f
FL
266EOF
267
5a953fc5 268test_expect_success 'unset with cont. lines is correct' 'test_cmp expect .git/config'
7a31cc0f 269
942c1f53
JS
270cat > .git/config << EOF
271[beta] ; silly comment # another comment
272noIndent= sillyValue ; 'nother silly comment
273
274# empty line
275 ; comment
276 haha ="beta" # last silly comment
4ddba79d
JS
277haha = hello
278 haha = bello
942c1f53
JS
279[nextSection] noNewline = ouch
280EOF
281
4ddba79d
JS
282cp .git/config .git/config2
283
ed838e66
JK
284test_expect_success 'multiple unset' '
285 git config --unset-all beta.haha
286'
4ddba79d
JS
287
288cat > expect << EOF
289[beta] ; silly comment # another comment
290noIndent= sillyValue ; 'nother silly comment
291
292# empty line
293 ; comment
294[nextSection] noNewline = ouch
295EOF
296
ed838e66
JK
297test_expect_success 'multiple unset is correct' '
298 test_cmp expect .git/config
299'
4ddba79d 300
bf71b4b3
CR
301cp .git/config2 .git/config
302
303test_expect_success '--replace-all missing value' '
304 test_must_fail git config --replace-all beta.haha &&
305 test_cmp .git/config2 .git/config
306'
307
308rm .git/config2
4ddba79d 309
ed838e66
JK
310test_expect_success '--replace-all' '
311 git config --replace-all beta.haha gamma
312'
4ddba79d
JS
313
314cat > expect << EOF
315[beta] ; silly comment # another comment
316noIndent= sillyValue ; 'nother silly comment
317
318# empty line
319 ; comment
320 haha = gamma
321[nextSection] noNewline = ouch
322EOF
323
ed838e66
JK
324test_expect_success 'all replaced' '
325 test_cmp expect .git/config
326'
942c1f53
JS
327
328cat > expect << EOF
329[beta] ; silly comment # another comment
330noIndent= sillyValue ; 'nother silly comment
331
332# empty line
333 ; comment
334 haha = alpha
335[nextSection] noNewline = ouch
336EOF
5a953fc5
JK
337test_expect_success 'really mean test' '
338 git config beta.haha alpha &&
339 test_cmp expect .git/config
340'
942c1f53
JS
341
342cat > expect << EOF
343[beta] ; silly comment # another comment
344noIndent= sillyValue ; 'nother silly comment
345
346# empty line
347 ; comment
348 haha = alpha
349[nextSection]
350 nonewline = wow
351EOF
5a953fc5
JK
352test_expect_success 'really really mean test' '
353 git config nextsection.nonewline wow &&
354 test_cmp expect .git/config
355'
942c1f53 356
ed838e66 357test_expect_success 'get value' '
a5db0b77 358 test_cmp_config alpha beta.haha
ed838e66 359'
942c1f53
JS
360
361cat > expect << EOF
362[beta] ; silly comment # another comment
363noIndent= sillyValue ; 'nother silly comment
364
365# empty line
366 ; comment
367[nextSection]
368 nonewline = wow
369EOF
5a953fc5
JK
370test_expect_success 'unset' '
371 git config --unset beta.haha &&
372 test_cmp expect .git/config
373'
942c1f53
JS
374
375cat > expect << EOF
376[beta] ; silly comment # another comment
377noIndent= sillyValue ; 'nother silly comment
378
379# empty line
380 ; comment
381[nextSection]
382 nonewline = wow
383 NoNewLine = wow2 for me
384EOF
5a953fc5
JK
385test_expect_success 'multivar' '
386 git config nextsection.NoNewLine "wow2 for me" "for me$" &&
387 test_cmp expect .git/config
388'
942c1f53 389
ed838e66
JK
390test_expect_success 'non-match' '
391 git config --get nextsection.nonewline !for
392'
f98d863d 393
ed838e66 394test_expect_success 'non-match value' '
a5db0b77 395 test_cmp_config wow --get nextsection.nonewline !for
ed838e66 396'
f98d863d 397
00b347d3 398test_expect_success 'multi-valued get returns final one' '
a5db0b77 399 test_cmp_config "wow2 for me" --get nextsection.nonewline
41ac414e 400'
4ddba79d 401
cb20b691
JK
402test_expect_success 'multi-valued get-all returns all' '
403 cat >expect <<-\EOF &&
404 wow
405 wow2 for me
406 EOF
4e513890 407 git config ${mode_get_all} nextsection.nonewline >actual &&
cb20b691 408 test_cmp expect actual
ed838e66 409'
4ddba79d 410
942c1f53
JS
411cat > expect << EOF
412[beta] ; silly comment # another comment
413noIndent= sillyValue ; 'nother silly comment
414
415# empty line
416 ; comment
417[nextSection]
418 nonewline = wow3
419 NoNewLine = wow2 for me
420EOF
5a953fc5
JK
421test_expect_success 'multivar replace' '
422 git config nextsection.nonewline "wow3" "wow$" &&
423 test_cmp expect .git/config
424'
942c1f53 425
41ac414e 426test_expect_success 'ambiguous unset' '
d492b31c 427 test_must_fail git config --unset nextsection.nonewline
41ac414e 428'
942c1f53 429
41ac414e 430test_expect_success 'invalid unset' '
d492b31c 431 test_must_fail git config --unset somesection.nonewline
41ac414e 432'
942c1f53 433
942c1f53
JS
434cat > expect << EOF
435[beta] ; silly comment # another comment
436noIndent= sillyValue ; 'nother silly comment
437
438# empty line
439 ; comment
440[nextSection]
441 NoNewLine = wow2 for me
442EOF
443
5a953fc5
JK
444test_expect_success 'multivar unset' '
445 git config --unset nextsection.nonewline "wow3$" &&
446 test_cmp expect .git/config
447'
942c1f53 448
d492b31c 449test_expect_success 'invalid key' 'test_must_fail git config inval.2key blabla'
942c1f53 450
5be60078 451test_expect_success 'correct key' 'git config 123456.a123 987'
942c1f53 452
ed838e66
JK
453test_expect_success 'hierarchical section' '
454 git config Version.1.2.3eX.Alpha beta
455'
b17e659d
JS
456
457cat > expect << EOF
458[beta] ; silly comment # another comment
459noIndent= sillyValue ; 'nother silly comment
460
461# empty line
462 ; comment
463[nextSection]
464 NoNewLine = wow2 for me
465[123456]
466 a123 = 987
d14f7764
LT
467[Version "1.2.3eX"]
468 Alpha = beta
b17e659d
JS
469EOF
470
ed838e66
JK
471test_expect_success 'hierarchical section value' '
472 test_cmp expect .git/config
473'
b17e659d 474
2fa9a0fb
JS
475cat > expect << EOF
476beta.noindent=sillyValue
477nextsection.nonewline=wow2 for me
478123456.a123=987
d55aaefa 479version.1.2.3eX.alpha=beta
2fa9a0fb
JS
480EOF
481
ed838e66 482test_expect_success 'working --list' '
14970509 483 git config ${mode_prefix}list > output &&
ed838e66
JK
484 test_cmp expect output
485'
1f2baa78 486test_expect_success '--list without repo produces empty output' '
14970509 487 git --git-dir=nonexistent config ${mode_prefix}list >output &&
1c5e94f4 488 test_must_be_empty output
1f2baa78
JK
489'
490
578625fa
SG
491cat > expect << EOF
492beta.noindent
493nextsection.nonewline
494123456.a123
495version.1.2.3eX.alpha
496EOF
497
498test_expect_success '--name-only --list' '
14970509 499 git config ${mode_prefix}list --name-only >output &&
578625fa
SG
500 test_cmp expect output
501'
502
2fa9a0fb
JS
503cat > expect << EOF
504beta.noindent sillyValue
505nextsection.nonewline wow2 for me
506EOF
507
ed838e66 508test_expect_success '--get-regexp' '
4e513890 509 git config ${mode_get_regexp} in >output &&
ed838e66
JK
510 test_cmp expect output
511'
2fa9a0fb 512
578625fa
SG
513cat > expect << EOF
514beta.noindent
515nextsection.nonewline
516EOF
517
518test_expect_success '--name-only --get-regexp' '
4e513890 519 git config ${mode_get_regexp} --name-only in >output &&
578625fa
SG
520 test_cmp expect output
521'
522
89c4afe0
BG
523cat > expect << EOF
524wow2 for me
525wow4 for you
526EOF
527
5a953fc5
JK
528test_expect_success '--add' '
529 git config --add nextsection.nonewline "wow4 for you" &&
4e513890 530 git config ${mode_get_all} nextsection.nonewline > output &&
5a953fc5
JK
531 test_cmp expect output
532'
89c4afe0 533
f067a137
JF
534cat > .git/config << EOF
535[novalue]
536 variable
09bc098c
CC
537[emptyvalue]
538 variable =
f067a137
JF
539EOF
540
ed838e66
JK
541test_expect_success 'get variable with no value' '
542 git config --get novalue.variable ^$
543'
f067a137 544
ed838e66
JK
545test_expect_success 'get variable with empty value' '
546 git config --get emptyvalue.variable ^$
547'
09bc098c 548
b69ba460
FL
549echo novalue.variable > expect
550
ed838e66 551test_expect_success 'get-regexp variable with no value' '
4e513890 552 git config ${mode_get_regexp} novalue > output &&
ed838e66
JK
553 test_cmp expect output
554'
b69ba460 555
008e3cc5
MM
556echo 'novalue.variable true' > expect
557
ed838e66 558test_expect_success 'get-regexp --bool variable with no value' '
4e513890 559 git config ${mode_get_regexp} --bool novalue > output &&
ed838e66
JK
560 test_cmp expect output
561'
008e3cc5 562
09bc098c
CC
563echo 'emptyvalue.variable ' > expect
564
ed838e66 565test_expect_success 'get-regexp variable with empty value' '
4e513890 566 git config ${mode_get_regexp} emptyvalue > output &&
ed838e66
JK
567 test_cmp expect output
568'
09bc098c
CC
569
570echo true > expect
571
ed838e66
JK
572test_expect_success 'get bool variable with no value' '
573 git config --bool novalue.variable > output &&
574 test_cmp expect output
575'
09bc098c
CC
576
577echo false > expect
578
ed838e66
JK
579test_expect_success 'get bool variable with empty value' '
580 git config --bool emptyvalue.variable > output &&
581 test_cmp expect output
582'
09bc098c 583
003f69b2
JK
584test_expect_success 'no arguments, but no crash' '
585 test_must_fail git config >output 2>&1 &&
6789275d 586 test_grep usage output
003f69b2 587'
2fa9a0fb 588
bd886fd3
SE
589cat > .git/config << EOF
590[a.b]
591 c = d
592EOF
593
bd886fd3
SE
594cat > expect << EOF
595[a.b]
596 c = d
597[a]
598 x = y
599EOF
600
5a953fc5
JK
601test_expect_success 'new section is partial match of another' '
602 git config a.x y &&
603 test_cmp expect .git/config
604'
bd886fd3
SE
605
606cat > expect << EOF
607[a.b]
608 c = d
609[a]
610 x = y
611 b = c
612[b]
613 x = y
614EOF
615
5a953fc5
JK
616test_expect_success 'new variable inserts into proper section' '
617 git config b.x y &&
618 git config a.b c &&
619 test_cmp expect .git/config
620'
bd886fd3 621
f7e87141 622test_expect_success 'alternative --file (non-existing file should fail)' '
34479d71
623 test_must_fail git config --file non-existing-config -l &&
624 test_must_fail git config --file non-existing-config test.xyzzy
ed838e66 625'
773a69fb 626
9c3796fc
JS
627cat > other-config << EOF
628[ein]
629 bahn = strasse
630EOF
631
632cat > expect << EOF
633ein.bahn=strasse
634EOF
635
5a953fc5 636test_expect_success 'alternative GIT_CONFIG' '
14970509 637 GIT_CONFIG=other-config git config ${mode_prefix}list >output &&
5a953fc5
JK
638 test_cmp expect output
639'
9c3796fc 640
ed838e66 641test_expect_success 'alternative GIT_CONFIG (--file)' '
14970509 642 git config ${mode_prefix}list --file other-config >output &&
ed838e66
JK
643 test_cmp expect output
644'
773a69fb 645
3caec73b 646test_expect_success 'alternative GIT_CONFIG (--file=-)' '
14970509 647 git config ${mode_prefix}list --file - <other-config >output &&
3caec73b
KS
648 test_cmp expect output
649'
650
651test_expect_success 'setting a value in stdin is an error' '
652 test_must_fail git config --file - some.value foo
653'
654
655test_expect_success 'editing stdin is an error' '
656 test_must_fail git config --file - --edit
657'
658
65807ee6 659test_expect_success 'refer config from subdirectory' '
14970509 660 test_when_finished "rm -r x" &&
65807ee6 661 mkdir x &&
a5db0b77 662 test_cmp_config -C x strasse --file=../other-config --get ein.bahn
270a3443
JK
663'
664
9c3796fc
JS
665cat > expect << EOF
666[ein]
667 bahn = strasse
668[anwohner]
669 park = ausweis
670EOF
671
f7e87141
JK
672test_expect_success '--set in alternative file' '
673 git config --file=other-config anwohner.park ausweis &&
5a953fc5
JK
674 test_cmp expect other-config
675'
9c3796fc 676
0667fcfb
JS
677cat > .git/config << EOF
678# Hallo
679 #Bello
680[branch "eins"]
681 x = 1
682[branch.eins]
683 y = 1
684 [branch "1 234 blabl/a"]
685weird
686EOF
687
ed838e66
JK
688test_expect_success 'rename section' '
689 git config --rename-section branch.eins branch.zwei
690'
0667fcfb
JS
691
692cat > expect << EOF
693# Hallo
694 #Bello
695[branch "zwei"]
696 x = 1
697[branch "zwei"]
698 y = 1
699 [branch "1 234 blabl/a"]
700weird
701EOF
702
ed838e66
JK
703test_expect_success 'rename succeeded' '
704 test_cmp expect .git/config
705'
0667fcfb 706
ed838e66 707test_expect_success 'rename non-existing section' '
d492b31c
SB
708 test_must_fail git config --rename-section \
709 branch."world domination" branch.drei
41ac414e 710'
0667fcfb 711
ed838e66
JK
712test_expect_success 'rename succeeded' '
713 test_cmp expect .git/config
714'
0667fcfb 715
ed838e66
JK
716test_expect_success 'rename another section' '
717 git config --rename-section branch."1 234 blabl/a" branch.drei
718'
0667fcfb
JS
719
720cat > expect << EOF
721# Hallo
722 #Bello
723[branch "zwei"]
724 x = 1
725[branch "zwei"]
726 y = 1
727[branch "drei"]
728weird
729EOF
730
ed838e66
JK
731test_expect_success 'rename succeeded' '
732 test_cmp expect .git/config
733'
9a5abfc7
AV
734
735cat >> .git/config << EOF
736[branch "vier"] z = 1
737EOF
738
ed838e66
JK
739test_expect_success 'rename a section with a var on the same line' '
740 git config --rename-section branch.vier branch.zwei
741'
9a5abfc7
AV
742
743cat > expect << EOF
744# Hallo
745 #Bello
746[branch "zwei"]
747 x = 1
748[branch "zwei"]
749 y = 1
750[branch "drei"]
751weird
752[branch "zwei"]
753 z = 1
754EOF
755
ed838e66
JK
756test_expect_success 'rename succeeded' '
757 test_cmp expect .git/config
758'
0667fcfb 759
94a35b1a
JK
760test_expect_success 'renaming empty section name is rejected' '
761 test_must_fail git config --rename-section branch.zwei ""
762'
763
764test_expect_success 'renaming to bogus section is rejected' '
765 test_must_fail git config --rename-section branch.zwei "bogus name"
766'
767
a5bb10fd 768test_expect_success 'renaming a section with a long line' '
29198213
TB
769 {
770 printf "[b]\\n" &&
771 printf " c = d %1024s [a] e = f\\n" " " &&
772 printf "[a] g = h\\n"
773 } >y &&
774 git config -f y --rename-section a xyz &&
775 test_must_fail git config -f y b.e
776'
777
a5bb10fd 778test_expect_success 'renaming an embedded section with a long line' '
29198213
TB
779 {
780 printf "[b]\\n" &&
781 printf " c = d %1024s [a] [foo] e = f\\n" " " &&
782 printf "[a] g = h\\n"
783 } >y &&
784 git config -f y --rename-section a xyz &&
785 test_must_fail git config -f y foo.e
786'
787
3bb3d6ba
TB
788test_expect_success 'renaming a section with an overly-long line' '
789 {
790 printf "[b]\\n" &&
791 printf " c = d %525000s e" " " &&
792 printf "[a] g = h\\n"
793 } >y &&
794 test_must_fail git config -f y --rename-section a xyz 2>err &&
b524e896 795 grep "refusing to work with overly long line in .y. on line 2" err
3bb3d6ba
TB
796'
797
118f8b24
PB
798cat >> .git/config << EOF
799 [branch "zwei"] a = 1 [branch "vier"]
800EOF
801
ed838e66
JK
802test_expect_success 'remove section' '
803 git config --remove-section branch.zwei
804'
118f8b24
PB
805
806cat > expect << EOF
807# Hallo
808 #Bello
809[branch "drei"]
810weird
811EOF
812
ed838e66
JK
813test_expect_success 'section was removed properly' '
814 test_cmp expect .git/config
815'
118f8b24 816
b18a2be3
SP
817cat > expect << EOF
818[gitcvs]
819 enabled = true
820 dbname = %Ggitcvs2.%a.%m.sqlite
821[gitcvs "ext"]
822 dbname = %Ggitcvs1.%a.%m.sqlite
823EOF
824
825test_expect_success 'section ending' '
795290e5 826 rm -f .git/config &&
5be60078
JH
827 git config gitcvs.enabled true &&
828 git config gitcvs.ext.dbname %Ggitcvs1.%a.%m.sqlite &&
829 git config gitcvs.dbname %Ggitcvs2.%a.%m.sqlite &&
5a953fc5 830 test_cmp expect .git/config
b18a2be3
SP
831
832'
833
d77a64d3 834test_expect_success numbers '
5be60078
JH
835 git config kilo.gram 1k &&
836 git config mega.ton 1m &&
ed838e66
JK
837 echo 1024 >expect &&
838 echo 1048576 >>expect &&
839 git config --int --get kilo.gram >actual &&
840 git config --int --get mega.ton >>actual &&
841 test_cmp expect actual
d77a64d3
SP
842'
843
00160242
JK
844test_expect_success '--int is at least 64 bits' '
845 git config giga.watts 121g &&
a5db0b77
NTND
846 echo >expect &&
847 test_cmp_config 129922760704 --int --get giga.watts
00160242
JK
848'
849
c8deb5a1 850test_expect_success 'invalid unit' '
c8deb5a1 851 git config aninvalid.unit "1auto" &&
a5db0b77 852 test_cmp_config 1auto aninvalid.unit &&
ed838e66 853 test_must_fail git config --int --get aninvalid.unit 2>actual &&
6789275d 854 test_grep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual
c8deb5a1
SP
855'
856
f276e2a4
AK
857test_expect_success 'invalid unit boolean' '
858 git config commit.gpgsign "1true" &&
859 test_cmp_config 1true commit.gpgsign &&
860 test_must_fail git config --bool --get commit.gpgsign 2>actual &&
6789275d 861 test_grep "bad boolean config value .1true. for .commit.gpgsign." actual
f276e2a4
AK
862'
863
e2e14251
JS
864test_expect_success 'line number is reported correctly' '
865 printf "[bool]\n\tvar\n" >invalid &&
866 test_must_fail git config -f invalid --path bool.var 2>actual &&
6789275d 867 test_grep "line 2" actual
e2e14251
JS
868'
869
473166b9 870test_expect_success 'invalid stdin config' '
14970509 871 echo "[broken" | test_must_fail git config ${mode_prefix}list --file - >output 2>&1 &&
6789275d 872 test_grep "bad config line 1 in standard input" output
473166b9
LS
873'
874
cab333cb
FL
875cat > expect << EOF
876true
877false
878true
879false
880true
881false
882true
883false
884EOF
885
886test_expect_success bool '
887
5be60078
JH
888 git config bool.true1 01 &&
889 git config bool.true2 -1 &&
890 git config bool.true3 YeS &&
891 git config bool.true4 true &&
892 git config bool.false1 000 &&
893 git config bool.false2 "" &&
894 git config bool.false3 nO &&
895 git config bool.false4 FALSE &&
cab333cb
FL
896 rm -f result &&
897 for i in 1 2 3 4
898 do
74d2f569 899 git config --bool --get bool.true$i >>result &&
db5875aa 900 git config --bool --get bool.false$i >>result || return 1
e3e042b1 901 done &&
ed838e66 902 test_cmp expect result'
cab333cb 903
41ac414e 904test_expect_success 'invalid bool (--get)' '
cab333cb 905
5be60078 906 git config bool.nobool foobar &&
d492b31c 907 test_must_fail git config --bool --get bool.nobool'
cab333cb 908
41ac414e 909test_expect_success 'invalid bool (set)' '
db1696b8 910
d492b31c 911 test_must_fail git config --bool bool.nobool foobar'
db1696b8 912
db1696b8
FL
913cat > expect <<\EOF
914[bool]
915 true1 = true
916 true2 = true
917 true3 = true
918 true4 = true
919 false1 = false
920 false2 = false
921 false3 = false
922 false4 = false
923EOF
924
925test_expect_success 'set --bool' '
926
795290e5 927 rm -f .git/config &&
5be60078
JH
928 git config --bool bool.true1 01 &&
929 git config --bool bool.true2 -1 &&
930 git config --bool bool.true3 YeS &&
931 git config --bool bool.true4 true &&
932 git config --bool bool.false1 000 &&
933 git config --bool bool.false2 "" &&
934 git config --bool bool.false3 nO &&
935 git config --bool bool.false4 FALSE &&
ed838e66 936 test_cmp expect .git/config'
db1696b8 937
db1696b8
FL
938cat > expect <<\EOF
939[int]
940 val1 = 1
941 val2 = -1
942 val3 = 5242880
943EOF
944
945test_expect_success 'set --int' '
946
795290e5 947 rm -f .git/config &&
5be60078
JH
948 git config --int int.val1 01 &&
949 git config --int int.val2 -1 &&
950 git config --int int.val3 5m &&
ed838e66
JK
951 test_cmp expect .git/config
952'
db1696b8 953
ed838e66
JK
954test_expect_success 'get --bool-or-int' '
955 cat >.git/config <<-\EOF &&
956 [bool]
957 true1
c35b0b58 958 true2 = true
ed838e66
JK
959 false = false
960 [int]
c35b0b58
JH
961 int1 = 0
962 int2 = 1
963 int3 = -1
ed838e66
JK
964 EOF
965 cat >expect <<-\EOF &&
966 true
967 true
968 false
969 0
970 1
971 -1
972 EOF
973 {
974 git config --bool-or-int bool.true1 &&
975 git config --bool-or-int bool.true2 &&
976 git config --bool-or-int bool.false &&
977 git config --bool-or-int int.int1 &&
978 git config --bool-or-int int.int2 &&
979 git config --bool-or-int int.int3
980 } >actual &&
981 test_cmp expect actual
c35b0b58
JH
982'
983
c35b0b58
JH
984cat >expect <<\EOF
985[bool]
986 true1 = true
987 false1 = false
988 true2 = true
989 false2 = false
990[int]
991 int1 = 0
992 int2 = 1
993 int3 = -1
994EOF
995
996test_expect_success 'set --bool-or-int' '
795290e5 997 rm -f .git/config &&
c35b0b58
JH
998 git config --bool-or-int bool.true1 true &&
999 git config --bool-or-int bool.false1 false &&
1000 git config --bool-or-int bool.true2 yes &&
1001 git config --bool-or-int bool.false2 no &&
1002 git config --bool-or-int int.int1 0 &&
1003 git config --bool-or-int int.int2 1 &&
1004 git config --bool-or-int int.int3 -1 &&
1005 test_cmp expect .git/config
1006'
1007
1349484e
MM
1008cat >expect <<\EOF
1009[path]
1010 home = ~/
1011 normal = /dev/null
1012 trailingtilde = foo~
1013EOF
1014
f57a8715 1015test_expect_success !MINGW 'set --path' '
795290e5 1016 rm -f .git/config &&
1349484e
MM
1017 git config --path path.home "~/" &&
1018 git config --path path.normal "/dev/null" &&
1019 git config --path path.trailingtilde "foo~" &&
1020 test_cmp expect .git/config'
1021
f57a8715 1022if test_have_prereq !MINGW && test "${HOME+set}"
79bf1490
JN
1023then
1024 test_set_prereq HOMEVAR
1025fi
1026
1349484e
MM
1027cat >expect <<EOF
1028$HOME/
1029/dev/null
1030foo~
1031EOF
1032
79bf1490 1033test_expect_success HOMEVAR 'get --path' '
1349484e
MM
1034 git config --get --path path.home > result &&
1035 git config --get --path path.normal >> result &&
1036 git config --get --path path.trailingtilde >> result &&
1037 test_cmp expect result
1038'
1039
79bf1490
JN
1040cat >expect <<\EOF
1041/dev/null
1042foo~
1043EOF
1044
f57a8715 1045test_expect_success !MINGW 'get --path copes with unset $HOME' '
79bf1490 1046 (
ed6c994a 1047 sane_unset HOME &&
79bf1490
JN
1048 test_must_fail git config --get --path path.home \
1049 >result 2>msg &&
1050 git config --get --path path.normal >>result &&
1051 git config --get --path path.trailingtilde >>result
1052 ) &&
6789275d 1053 test_grep "[Ff]ailed to expand.*~/" msg &&
79bf1490
JN
1054 test_cmp expect result
1055'
1056
962c38ee
CMN
1057test_expect_success 'get --path barfs on boolean variable' '
1058 echo "[path]bool" >.git/config &&
1059 test_must_fail git config --get --path path.bool
1060'
1061
5f967424
HM
1062test_expect_success 'get --expiry-date' '
1063 rel="3.weeks.5.days.00:00" &&
1064 rel_out="$rel ->" &&
1065 cat >.git/config <<-\EOF &&
1066 [date]
1067 valid1 = "3.weeks.5.days 00:00"
1068 valid2 = "Fri Jun 4 15:46:55 2010"
1069 valid3 = "2017/11/11 11:11:11PM"
1070 valid4 = "2017/11/10 09:08:07 PM"
1071 valid5 = "never"
1072 invalid1 = "abc"
1073 EOF
1074 cat >expect <<-EOF &&
a801a7cf 1075 $(test-tool date timestamp $rel)
5f967424
HM
1076 1275666415
1077 1510441871
1078 1510348087
1079 0
1080 EOF
d38722eb 1081 : "work around heredoc parsing bug fixed in dash 0.5.7 (in ec2c84d)" &&
5f967424 1082 {
7abcbcb7 1083 echo "$rel_out $(git config --expiry-date date.valid1)" &&
5f967424
HM
1084 git config --expiry-date date.valid2 &&
1085 git config --expiry-date date.valid3 &&
1086 git config --expiry-date date.valid4 &&
1087 git config --expiry-date date.valid5
1088 } >actual &&
1089 test_cmp expect actual &&
1090 test_must_fail git config --expiry-date date.invalid1
1091'
1092
63e2a0f8
TB
1093test_expect_success 'get --type=color' '
1094 rm .git/config &&
1095 git config foo.color "red" &&
1096 git config --get --type=color foo.color >actual.raw &&
1097 test_decode_color <actual.raw >actual &&
1098 echo "<RED>" >expect &&
1099 test_cmp expect actual
1100'
1101
1102cat >expect << EOF
1103[foo]
1104 color = red
1105EOF
1106
1107test_expect_success 'set --type=color' '
1108 rm .git/config &&
1109 git config --type=color foo.color "red" &&
1110 test_cmp expect .git/config
1111'
1112
1113test_expect_success 'get --type=color barfs on non-color' '
1114 echo "[foo]bar=not-a-color" >.git/config &&
1115 test_must_fail git config --get --type=color foo.bar
1116'
1117
1118test_expect_success 'set --type=color barfs on non-color' '
1119 test_must_fail git config --type=color foo.color "not-a-color" 2>error &&
6789275d 1120 test_grep "cannot parse color" error
63e2a0f8
TB
1121'
1122
cdd4fb15
BG
1123cat > expect << EOF
1124[quote]
1125 leading = " test"
1126 ending = "test "
1127 semicolon = "test;test"
1128 hash = "test#test"
1129EOF
5a953fc5 1130test_expect_success 'quoting' '
795290e5 1131 rm -f .git/config &&
5a953fc5
JK
1132 git config quote.leading " test" &&
1133 git config quote.ending "test " &&
1134 git config quote.semicolon "test;test" &&
1135 git config quote.hash "test#test" &&
1136 test_cmp expect .git/config
1137'
cdd4fb15 1138
41ac414e 1139test_expect_success 'key with newline' '
4e513890 1140 test_must_fail git config ${mode_get} "key.with
41ac414e 1141newline" 123'
6f71686e 1142
e0d10e1c 1143test_expect_success 'value with newline' 'git config key.sub value.with\\\
6f71686e
JS
1144newline'
1145
83cd10a0
JN
1146cat > .git/config <<\EOF
1147[section]
1148 ; comment \
1149 continued = cont\
1150inued
1151 noncont = not continued ; \
1152 quotecont = "cont;\
1153inued"
1154EOF
1155
1156cat > expect <<\EOF
1157section.continued=continued
1158section.noncont=not continued
1159section.quotecont=cont;inued
1160EOF
1161
5a953fc5 1162test_expect_success 'value continued on next line' '
14970509 1163 git config ${mode_prefix}list > result &&
dcbaa0b3 1164 test_cmp expect result
5a953fc5 1165'
83cd10a0 1166
2275d502
FL
1167cat > .git/config <<\EOF
1168[section "sub=section"]
1169 val1 = foo=bar
1170 val2 = foo\nbar
1171 val3 = \n\n
1172 val4 =
1173 val5
1174EOF
1175
1176cat > expect <<\EOF
2b9a5020
AR
1177section.sub=section.val1
1178foo=barQsection.sub=section.val2
1179foo
1180barQsection.sub=section.val3
2275d502
FL
1181
1182
2b9a5020
AR
1183Qsection.sub=section.val4
1184Qsection.sub=section.val5Q
2275d502 1185EOF
5a953fc5 1186test_expect_success '--null --list' '
14970509 1187 git config ${mode_prefix}list --null >result.raw &&
a0578e03 1188 nul_to_q <result.raw >result &&
5a953fc5
JK
1189 echo >>result &&
1190 test_cmp expect result
1191'
2275d502 1192
5a953fc5 1193test_expect_success '--null --get-regexp' '
4e513890 1194 git config ${mode_get_regexp} --null "val[0-9]" >result.raw &&
a0578e03 1195 nul_to_q <result.raw >result &&
5a953fc5
JK
1196 echo >>result &&
1197 test_cmp expect result
1198'
2275d502 1199
d71bc1b4
DS
1200test_expect_success 'inner whitespace kept verbatim, spaces only' '
1201 echo "foo bar" >expect &&
1202 git config section.val "foo bar" &&
4e513890 1203 git config ${mode_get} section.val >actual &&
d71bc1b4
DS
1204 test_cmp expect actual
1205'
1206
1207test_expect_success 'inner whitespace kept verbatim, horizontal tabs only' '
1208 echo "fooQQbar" | q_to_tab >expect &&
1209 git config section.val "$(cat expect)" &&
4e513890 1210 git config ${mode_get} section.val >actual &&
d71bc1b4
DS
1211 test_cmp expect actual
1212'
1213
1214test_expect_success 'inner whitespace kept verbatim, horizontal tabs and spaces' '
1215 echo "foo Q bar" | q_to_tab >expect &&
1216 git config section.val "$(cat expect)" &&
4e513890 1217 git config ${mode_get} section.val >actual &&
d71bc1b4 1218 test_cmp expect actual
ebdaae37
BS
1219'
1220
704a3143 1221test_expect_success SYMLINKS 'symlinked configuration' '
14970509 1222 test_when_finished "rm myconfig" &&
65a5a21d 1223 ln -s notyet myconfig &&
f7e87141 1224 git config --file=myconfig test.frotz nitfol &&
65a5a21d
JH
1225 test -h myconfig &&
1226 test -f notyet &&
f7e87141
JK
1227 test "z$(git config --file=notyet test.frotz)" = znitfol &&
1228 git config --file=myconfig test.xyzzy rezrov &&
65a5a21d
JH
1229 test -h myconfig &&
1230 test -f notyet &&
ed838e66
JK
1231 cat >expect <<-\EOF &&
1232 nitfol
1233 rezrov
1234 EOF
1235 {
f7e87141
JK
1236 git config --file=notyet test.frotz &&
1237 git config --file=notyet test.xyzzy
ed838e66
JK
1238 } >actual &&
1239 test_cmp expect actual
65a5a21d
JH
1240'
1241
1f2baa78 1242test_expect_success SYMLINKS 'symlink to nonexistent configuration' '
14970509 1243 test_when_finished "rm linktonada linktolinktonada" &&
1f2baa78
JK
1244 ln -s doesnotexist linktonada &&
1245 ln -s linktonada linktolinktonada &&
14970509
PS
1246 test_must_fail git config ${mode_prefix}list --file=linktonada &&
1247 test_must_fail git config ${mode_prefix}list --file=linktolinktonada
1f2baa78
JK
1248'
1249
91c5a5e0
PS
1250test_expect_success 'check split_cmdline return' '
1251 test_when_finished "rm -rf repo" &&
1252 git init repo &&
1253 (
1254 cd repo &&
1255 git config alias.split-cmdline-fix "echo \"" &&
1256 test_must_fail git split-cmdline-fix &&
1257 echo foo >foo &&
1258 git add foo &&
1259 git commit -m "initial commit" &&
1260 git config branch.main.mergeoptions "echo \"" &&
1261 test_must_fail git merge main
1262 )
1263'
dc4179f9 1264
8b1fa778 1265test_expect_success 'git -c "key=value" support' '
ed838e66
JK
1266 cat >expect <<-\EOF &&
1267 value
1268 value
1269 true
1270 EOF
1271 {
04f6b0a1 1272 git -c section.name=value config section.name &&
ed838e66
JK
1273 git -c foo.CamelCase=value config foo.camelcase &&
1274 git -c foo.flag config --bool foo.flag
1275 } >actual &&
1276 test_cmp expect actual &&
04f6b0a1 1277 test_must_fail git -c name=value config section.name
b09c53a3
LP
1278'
1279
a789ca70
JH
1280# We just need a type-specifier here that cares about the
1281# distinction internally between a NULL boolean and a real
1282# string (because most of git's internal parsers do care).
1283# Using "--path" works, but we do not otherwise care about
1284# its semantics.
1285test_expect_success 'git -c can represent empty string' '
1286 echo >expect &&
1287 git -c foo.empty= config --path foo.empty >actual &&
1288 test_cmp expect actual
1289'
1290
b09c53a3 1291test_expect_success 'key sanity-checking' '
4e513890
PS
1292 test_must_fail git config ${mode_get} foo=bar &&
1293 test_must_fail git config ${mode_get} foo=.bar &&
1294 test_must_fail git config ${mode_get} foo.ba=r &&
1295 test_must_fail git config ${mode_get} foo.1bar &&
1296 test_must_fail git config ${mode_get} foo."ba
b09c53a3 1297 z".bar &&
2169ddc0
LP
1298 test_must_fail git config . false &&
1299 test_must_fail git config .foo false &&
1300 test_must_fail git config foo. false &&
1301 test_must_fail git config .foo. false &&
b09c53a3
LP
1302 git config foo.bar true &&
1303 git config foo."ba =z".bar false
8b1fa778
AR
1304'
1305
73546c08 1306test_expect_success 'git -c works with aliases of builtins' '
06eb708f
JK
1307 git config alias.checkconfig "-c foo.check=bar config foo.check" &&
1308 echo bar >expect &&
1309 git checkconfig >actual &&
1310 test_cmp expect actual
1311'
1312
643df7e2 1313test_expect_success 'aliases can be CamelCased' '
91c5a5e0
PS
1314 test_when_finished "rm -rf repo" &&
1315 git init repo &&
1316 (
1317 cd repo &&
1318 test_commit A &&
1319 git config alias.CamelCased "rev-parse HEAD" &&
1320 git CamelCased >out &&
1321 git rev-parse HEAD >expect &&
1322 test_cmp expect out
1323 )
084b0440
JS
1324'
1325
5bf6529a
JK
1326test_expect_success 'git -c does not split values on equals' '
1327 echo "value with = in it" >expect &&
04f6b0a1 1328 git -c section.foo="value with = in it" config section.foo >actual &&
5bf6529a
JK
1329 test_cmp expect actual
1330'
1331
1c2c9bee
JK
1332test_expect_success 'git -c dies on bogus config' '
1333 test_must_fail git -c core.bare=foo rev-parse
1334'
1335
1336test_expect_success 'git -c complains about empty key' '
1337 test_must_fail git -c "=foo" rev-parse
1338'
1339
c5d6350b
JK
1340test_expect_success 'git -c complains about empty key and value' '
1341 test_must_fail git -c "" rev-parse
1342'
1343
d1f88498 1344test_expect_success 'multiple git -c appends config' '
4e513890 1345 test_config alias.x "!git -c x.two=2 config ${mode_get_regexp} ^x\.*" &&
d1f88498
JK
1346 cat >expect <<-\EOF &&
1347 x.one 1
1348 x.two 2
1349 EOF
1350 git -c x.one=1 x >actual &&
1351 test_cmp expect actual
1352'
1353
1274a155
JH
1354test_expect_success 'last one wins: two level vars' '
1355
1356 # sec.var and sec.VAR are the same variable, as the first
1357 # and the last level of a configuration variable name is
1358 # case insensitive.
1359
1360 echo VAL >expect &&
1361
1362 git -c sec.var=val -c sec.VAR=VAL config --get sec.var >actual &&
1363 test_cmp expect actual &&
1364 git -c SEC.var=val -c sec.var=VAL config --get sec.var >actual &&
1365 test_cmp expect actual &&
1366
1367 git -c sec.var=val -c sec.VAR=VAL config --get SEC.var >actual &&
1368 test_cmp expect actual &&
1369 git -c SEC.var=val -c sec.var=VAL config --get sec.VAR >actual &&
1370 test_cmp expect actual
1371'
1372
1373test_expect_success 'last one wins: three level vars' '
1374
1375 # v.a.r and v.A.r are not the same variable, as the middle
1376 # level of a three-level configuration variable name is
1377 # case sensitive.
1378
1379 echo val >expect &&
1380 git -c v.a.r=val -c v.A.r=VAL config --get v.a.r >actual &&
1381 test_cmp expect actual &&
1382 git -c v.a.r=val -c v.A.r=VAL config --get V.a.R >actual &&
1383 test_cmp expect actual &&
1384
1385 # v.a.r and V.a.R are the same variable, as the first
1386 # and the last level of a configuration variable name is
1387 # case insensitive.
1388
1389 echo VAL >expect &&
1390 git -c v.a.r=val -c v.a.R=VAL config --get v.a.r >actual &&
1391 test_cmp expect actual &&
1392 git -c v.a.r=val -c V.a.r=VAL config --get v.a.r >actual &&
1393 test_cmp expect actual &&
1394 git -c v.a.r=val -c v.a.R=VAL config --get V.a.R >actual &&
1395 test_cmp expect actual &&
1396 git -c v.a.r=val -c V.a.r=VAL config --get V.a.R >actual &&
1397 test_cmp expect actual
1398'
1399
999d9026
SB
1400test_expect_success 'old-fashioned settings are case insensitive' '
1401 test_when_finished "rm -f testConfig testConfig_expect testConfig_actual" &&
1402
1403 cat >testConfig_actual <<-EOF &&
3de7ee36
MR
1404 [V.A]
1405 r = value1
999d9026
SB
1406 EOF
1407 q_to_tab >testConfig_expect <<-EOF &&
3de7ee36
MR
1408 [V.A]
1409 Qr = value2
999d9026
SB
1410 EOF
1411 git config -f testConfig_actual "v.a.r" value2 &&
1412 test_cmp testConfig_expect testConfig_actual &&
1413
1414 cat >testConfig_actual <<-EOF &&
3de7ee36
MR
1415 [V.A]
1416 r = value1
999d9026
SB
1417 EOF
1418 q_to_tab >testConfig_expect <<-EOF &&
3de7ee36
MR
1419 [V.A]
1420 QR = value2
999d9026
SB
1421 EOF
1422 git config -f testConfig_actual "V.a.R" value2 &&
1423 test_cmp testConfig_expect testConfig_actual &&
1424
1425 cat >testConfig_actual <<-EOF &&
3de7ee36
MR
1426 [V.A]
1427 r = value1
999d9026
SB
1428 EOF
1429 q_to_tab >testConfig_expect <<-EOF &&
3de7ee36
MR
1430 [V.A]
1431 r = value1
1432 Qr = value2
999d9026
SB
1433 EOF
1434 git config -f testConfig_actual "V.A.r" value2 &&
1435 test_cmp testConfig_expect testConfig_actual &&
1436
1437 cat >testConfig_actual <<-EOF &&
3de7ee36
MR
1438 [V.A]
1439 r = value1
999d9026
SB
1440 EOF
1441 q_to_tab >testConfig_expect <<-EOF &&
3de7ee36
MR
1442 [V.A]
1443 r = value1
1444 Qr = value2
999d9026
SB
1445 EOF
1446 git config -f testConfig_actual "v.A.r" value2 &&
1447 test_cmp testConfig_expect testConfig_actual
1448'
1449
1450test_expect_success 'setting different case sensitive subsections ' '
1451 test_when_finished "rm -f testConfig testConfig_expect testConfig_actual" &&
1452
1453 cat >testConfig_actual <<-EOF &&
3de7ee36
MR
1454 [V "A"]
1455 R = v1
1456 [K "E"]
1457 Y = v1
1458 [a "b"]
1459 c = v1
1460 [d "e"]
1461 f = v1
999d9026
SB
1462 EOF
1463 q_to_tab >testConfig_expect <<-EOF &&
3de7ee36
MR
1464 [V "A"]
1465 Qr = v2
1466 [K "E"]
1467 Qy = v2
1468 [a "b"]
1469 Qc = v2
1470 [d "e"]
1471 f = v1
1472 [d "E"]
1473 Qf = v2
999d9026
SB
1474 EOF
1475 # exact match
1476 git config -f testConfig_actual a.b.c v2 &&
1477 # match section and subsection, key is cased differently.
1478 git config -f testConfig_actual K.E.y v2 &&
1479 # section and key are matched case insensitive, but subsection needs
1480 # to match; When writing out new values only the key is adjusted
1481 git config -f testConfig_actual v.A.r v2 &&
1482 # subsection is not matched:
1483 git config -f testConfig_actual d.E.f v2 &&
1484 test_cmp testConfig_expect testConfig_actual
1485'
1486
1274a155
JH
1487for VAR in a .a a. a.0b a."b c". a."b c".0d
1488do
1489 test_expect_success "git -c $VAR=VAL rejects invalid '$VAR'" '
1490 test_must_fail git -c "$VAR=VAL" config -l
1491 '
1492done
1493
1494for VAR in a.b a."b c".d
1495do
1496 test_expect_success "git -c $VAR=VAL works with valid '$VAR'" '
1497 echo VAL >expect &&
1498 git -c "$VAR=VAL" config --get "$VAR" >actual &&
1499 test_cmp expect actual
1500 '
1501done
1502
d1f88498 1503test_expect_success 'git -c is not confused by empty environment' '
14970509 1504 GIT_CONFIG_PARAMETERS="" git -c x.one=1 config ${mode_prefix}list
d1f88498
JK
1505'
1506
f9dbb64f
JK
1507test_expect_success 'GIT_CONFIG_PARAMETERS handles old-style entries' '
1508 v="${SQ}key.one=foo${SQ}" &&
1509 v="$v ${SQ}key.two=bar${SQ}" &&
1510 v="$v ${SQ}key.ambiguous=section.whatever=value${SQ}" &&
4e513890 1511 GIT_CONFIG_PARAMETERS=$v git config ${mode_get_regexp} "key.*" >actual &&
f9dbb64f
JK
1512 cat >expect <<-EOF &&
1513 key.one foo
1514 key.two bar
1515 key.ambiguous section.whatever=value
1516 EOF
1517 test_cmp expect actual
1518'
1519
1520test_expect_success 'GIT_CONFIG_PARAMETERS handles new-style entries' '
1521 v="${SQ}key.one${SQ}=${SQ}foo${SQ}" &&
1522 v="$v ${SQ}key.two${SQ}=${SQ}bar${SQ}" &&
1523 v="$v ${SQ}key.ambiguous=section.whatever${SQ}=${SQ}value${SQ}" &&
4e513890 1524 GIT_CONFIG_PARAMETERS=$v git config ${mode_get_regexp} "key.*" >actual &&
f9dbb64f
JK
1525 cat >expect <<-EOF &&
1526 key.one foo
1527 key.two bar
1528 key.ambiguous=section.whatever value
1529 EOF
1530 test_cmp expect actual
1531'
1532
1533test_expect_success 'old and new-style entries can mix' '
1534 v="${SQ}key.oldone=oldfoo${SQ}" &&
1535 v="$v ${SQ}key.newone${SQ}=${SQ}newfoo${SQ}" &&
1536 v="$v ${SQ}key.oldtwo=oldbar${SQ}" &&
1537 v="$v ${SQ}key.newtwo${SQ}=${SQ}newbar${SQ}" &&
4e513890 1538 GIT_CONFIG_PARAMETERS=$v git config ${mode_get_regexp} "key.*" >actual &&
f9dbb64f
JK
1539 cat >expect <<-EOF &&
1540 key.oldone oldfoo
1541 key.newone newfoo
1542 key.oldtwo oldbar
1543 key.newtwo newbar
1544 EOF
1545 test_cmp expect actual
1546'
1547
1548test_expect_success 'old and new bools with ambiguous subsection' '
1549 v="${SQ}key.with=equals.oldbool${SQ}" &&
1550 v="$v ${SQ}key.with=equals.newbool${SQ}=" &&
4e513890 1551 GIT_CONFIG_PARAMETERS=$v git config ${mode_get_regexp} "key.*" >actual &&
f9dbb64f
JK
1552 cat >expect <<-EOF &&
1553 key.with equals.oldbool
1554 key.with=equals.newbool
1555 EOF
1556 test_cmp expect actual
1557'
1558
ddbbf8eb
JK
1559test_expect_success 'detect bogus GIT_CONFIG_PARAMETERS' '
1560 cat >expect <<-\EOF &&
1561 env.one one
1562 env.two two
1563 EOF
bd482d6e 1564 GIT_CONFIG_PARAMETERS="${SQ}env.one=one${SQ} ${SQ}env.two=two${SQ}" \
4e513890 1565 git config ${mode_get_regexp} "env.*" >actual &&
ddbbf8eb
JK
1566 test_cmp expect actual &&
1567
1568 cat >expect <<-EOF &&
bd482d6e 1569 env.one one${SQ}
ddbbf8eb
JK
1570 env.two two
1571 EOF
bd482d6e 1572 GIT_CONFIG_PARAMETERS="${SQ}env.one=one${SQ}\\$SQ$SQ$SQ ${SQ}env.two=two${SQ}" \
4e513890 1573 git config ${mode_get_regexp} "env.*" >actual &&
ddbbf8eb
JK
1574 test_cmp expect actual &&
1575
1576 test_must_fail env \
bd482d6e 1577 GIT_CONFIG_PARAMETERS="${SQ}env.one=one${SQ}\\$SQ ${SQ}env.two=two${SQ}" \
4e513890 1578 git config ${mode_get_regexp} "env.*"
ddbbf8eb
JK
1579'
1580
ce81b1da
PS
1581test_expect_success 'git --config-env=key=envvar support' '
1582 cat >expect <<-\EOF &&
1583 value
1584 value
c331551c
PS
1585 value
1586 value
1587 false
ce81b1da
PS
1588 false
1589 EOF
1590 {
1591 ENVVAR=value git --config-env=core.name=ENVVAR config core.name &&
c331551c 1592 ENVVAR=value git --config-env core.name=ENVVAR config core.name &&
ce81b1da 1593 ENVVAR=value git --config-env=foo.CamelCase=ENVVAR config foo.camelcase &&
c331551c
PS
1594 ENVVAR=value git --config-env foo.CamelCase=ENVVAR config foo.camelcase &&
1595 ENVVAR= git --config-env=foo.flag=ENVVAR config --bool foo.flag &&
1596 ENVVAR= git --config-env foo.flag=ENVVAR config --bool foo.flag
ce81b1da
PS
1597 } >actual &&
1598 test_cmp expect actual
1599'
1600
c331551c
PS
1601test_expect_success 'git --config-env with missing value' '
1602 test_must_fail env ENVVAR=value git --config-env 2>error &&
1603 grep "no config key given for --config-env" error &&
1604 test_must_fail env ENVVAR=value git --config-env config core.name 2>error &&
1605 grep "invalid config format: config" error
1606'
1607
ce81b1da
PS
1608test_expect_success 'git --config-env fails with invalid parameters' '
1609 test_must_fail git --config-env=foo.flag config --bool foo.flag 2>error &&
6789275d 1610 test_grep "invalid config format: foo.flag" error &&
ce81b1da 1611 test_must_fail git --config-env=foo.flag= config --bool foo.flag 2>error &&
6789275d 1612 test_grep "missing environment variable name for configuration ${SQ}foo.flag${SQ}" error &&
ce81b1da
PS
1613 sane_unset NONEXISTENT &&
1614 test_must_fail git --config-env=foo.flag=NONEXISTENT config --bool foo.flag 2>error &&
6789275d 1615 test_grep "missing environment variable ${SQ}NONEXISTENT${SQ} for configuration ${SQ}foo.flag${SQ}" error
ce81b1da
PS
1616'
1617
1618test_expect_success 'git -c and --config-env work together' '
1619 cat >expect <<-\EOF &&
1620 bar.cmd cmd-value
1621 bar.env env-value
1622 EOF
1623 ENVVAR=env-value git \
1624 -c bar.cmd=cmd-value \
1625 --config-env=bar.env=ENVVAR \
4e513890 1626 config ${mode_get_regexp} "^bar.*" >actual &&
ce81b1da
PS
1627 test_cmp expect actual
1628'
1629
1630test_expect_success 'git -c and --config-env override each other' '
1631 cat >expect <<-\EOF &&
1632 env
1633 cmd
1634 EOF
1635 {
1636 ENVVAR=env git -c bar.bar=cmd --config-env=bar.bar=ENVVAR config bar.bar &&
1637 ENVVAR=env git --config-env=bar.bar=ENVVAR -c bar.bar=cmd config bar.bar
1638 } >actual &&
1639 test_cmp expect actual
1640'
1641
1ff21c05
PS
1642test_expect_success '--config-env handles keys with equals' '
1643 echo value=with=equals >expect &&
1644 ENVVAR=value=with=equals git \
1645 --config-env=section.subsection=with=equals.key=ENVVAR \
1646 config section.subsection=with=equals.key >actual &&
1647 test_cmp expect actual
1648'
1649
d8d77153
PS
1650test_expect_success 'git config handles environment config pairs' '
1651 GIT_CONFIG_COUNT=2 \
1652 GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="foo" \
1653 GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="bar" \
4e513890 1654 git config ${mode_get_regexp} "pair.*" >actual &&
d8d77153
PS
1655 cat >expect <<-EOF &&
1656 pair.one foo
1657 pair.two bar
1658 EOF
1659 test_cmp expect actual
1660'
1661
1662test_expect_success 'git config ignores pairs without count' '
1663 test_must_fail env GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
4e513890 1664 git config ${mode_get} pair.one 2>error &&
d8d77153
PS
1665 test_must_be_empty error
1666'
1667
d8d77153
PS
1668test_expect_success 'git config ignores pairs exceeding count' '
1669 GIT_CONFIG_COUNT=1 \
1670 GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
1671 GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="value" \
4e513890 1672 git config ${mode_get_regexp} "pair.*" >actual 2>error &&
d8d77153
PS
1673 cat >expect <<-EOF &&
1674 pair.one value
1675 EOF
93f86046
AR
1676 test_cmp expect actual &&
1677 test_must_be_empty error
d8d77153
PS
1678'
1679
1680test_expect_success 'git config ignores pairs with zero count' '
1681 test_must_fail env \
1682 GIT_CONFIG_COUNT=0 GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
4e513890 1683 git config ${mode_get} pair.one 2>error &&
d8d77153
PS
1684 test_must_be_empty error
1685'
1686
1687test_expect_success 'git config ignores pairs with empty count' '
1688 test_must_fail env \
1689 GIT_CONFIG_COUNT= GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
4e513890 1690 git config ${mode_get} pair.one 2>error &&
d8d77153
PS
1691 test_must_be_empty error
1692'
1693
1694test_expect_success 'git config fails with invalid count' '
14970509 1695 test_must_fail env GIT_CONFIG_COUNT=10a git config ${mode_prefix}list 2>error &&
6789275d 1696 test_grep "bogus count" error &&
14970509 1697 test_must_fail env GIT_CONFIG_COUNT=9999999999999999 git config ${mode_prefix}list 2>error &&
6789275d 1698 test_grep "too many entries" error
d8d77153
PS
1699'
1700
1701test_expect_success 'git config fails with missing config key' '
1702 test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_VALUE_0="value" \
14970509 1703 git config ${mode_prefix}list 2>error &&
6789275d 1704 test_grep "missing config key" error
d8d77153
PS
1705'
1706
1707test_expect_success 'git config fails with missing config value' '
1708 test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="pair.one" \
14970509 1709 git config ${mode_prefix}list 2>error &&
6789275d 1710 test_grep "missing config value" error
d8d77153
PS
1711'
1712
1713test_expect_success 'git config fails with invalid config pair key' '
1714 test_must_fail env GIT_CONFIG_COUNT=1 \
1715 GIT_CONFIG_KEY_0= GIT_CONFIG_VALUE_0=value \
14970509 1716 git config ${mode_prefix}list &&
d8d77153
PS
1717 test_must_fail env GIT_CONFIG_COUNT=1 \
1718 GIT_CONFIG_KEY_0=missing-section GIT_CONFIG_VALUE_0=value \
14970509 1719 git config ${mode_prefix}list
d8d77153
PS
1720'
1721
1722test_expect_success 'environment overrides config file' '
1723 test_when_finished "rm -f .git/config" &&
1724 cat >.git/config <<-EOF &&
1725 [pair]
1726 one = value
1727 EOF
1728 GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=override \
4e513890 1729 git config ${mode_get} pair.one >actual &&
d8d77153
PS
1730 cat >expect <<-EOF &&
1731 override
1732 EOF
1733 test_cmp expect actual
1734'
1735
1736test_expect_success 'GIT_CONFIG_PARAMETERS overrides environment config' '
1737 GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=value \
1738 GIT_CONFIG_PARAMETERS="${SQ}pair.one=override${SQ}" \
4e513890 1739 git config ${mode_get} pair.one >actual &&
d8d77153
PS
1740 cat >expect <<-EOF &&
1741 override
1742 EOF
1743 test_cmp expect actual
1744'
1745
1746test_expect_success 'command line overrides environment config' '
1747 GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=value \
1748 git -c pair.one=override config pair.one >actual &&
1749 cat >expect <<-EOF &&
1750 override
1751 EOF
1752 test_cmp expect actual
1753'
1754
270a3443
JK
1755test_expect_success 'git config --edit works' '
1756 git config -f tmp test.value no &&
1757 echo test.value=yes >expect &&
1758 GIT_EDITOR="echo [test]value=yes >" git config -f tmp --edit &&
14970509 1759 git config ${mode_prefix}list -f tmp >actual &&
270a3443
JK
1760 test_cmp expect actual
1761'
1762
1763test_expect_success 'git config --edit respects core.editor' '
1764 git config -f tmp test.value no &&
1765 echo test.value=yes >expect &&
1766 test_config core.editor "echo [test]value=yes >" &&
1767 git config -f tmp --edit &&
14970509 1768 git config ${mode_prefix}list -f tmp >actual &&
270a3443
JK
1769 test_cmp expect actual
1770'
1771
4b340593
MS
1772# malformed configuration files
1773test_expect_success 'barf on syntax error' '
1774 cat >.git/config <<-\EOF &&
6fc68e7c 1775 # broken key=value
4b340593
MS
1776 [section]
1777 key garbage
1778 EOF
a7cae290 1779 test_must_fail git config --get section.key 2>error &&
6789275d 1780 test_grep " line 3 " error
4b340593
MS
1781'
1782
1783test_expect_success 'barf on incomplete section header' '
1784 cat >.git/config <<-\EOF &&
1785 # broken section line
1786 [section
1787 key = value
1788 EOF
a7cae290 1789 test_must_fail git config --get section.key 2>error &&
6789275d 1790 test_grep " line 2 " error
4b340593
MS
1791'
1792
1793test_expect_success 'barf on incomplete string' '
1794 cat >.git/config <<-\EOF &&
6fc68e7c 1795 # broken value string
4b340593
MS
1796 [section]
1797 key = "value string
1798 EOF
a7cae290 1799 test_must_fail git config --get section.key 2>error &&
6789275d 1800 test_grep " line 3 " error
4b340593
MS
1801'
1802
d4770964
JH
1803test_expect_success 'urlmatch' '
1804 cat >.git/config <<-\EOF &&
1805 [http]
1806 sslVerify
1807 [http "https://weak.example.com"]
1808 sslVerify = false
1809 cookieFile = /tmp/cookie.txt
1810 EOF
1811
27b30be6
JK
1812 test_expect_code 1 git config --bool --get-urlmatch doesnt.exist https://good.example.com >actual &&
1813 test_must_be_empty actual &&
4e513890
PS
1814 test_expect_code 1 git config get --url=https://good.example.com --bool doesnt.exist >actual &&
1815 test_must_be_empty actual &&
27b30be6 1816
d4770964
JH
1817 echo true >expect &&
1818 git config --bool --get-urlmatch http.SSLverify https://good.example.com >actual &&
1819 test_cmp expect actual &&
4e513890
PS
1820 git config get --bool --url=https://good.example.com http.SSLverify >actual &&
1821 test_cmp expect actual &&
d4770964
JH
1822
1823 echo false >expect &&
1824 git config --bool --get-urlmatch http.sslverify https://weak.example.com >actual &&
1825 test_cmp expect actual &&
4e513890
PS
1826 git config get --bool --url=https://weak.example.com http.sslverify >actual &&
1827 test_cmp expect actual &&
d4770964
JH
1828
1829 {
1830 echo http.cookiefile /tmp/cookie.txt &&
1831 echo http.sslverify false
1832 } >expect &&
1833 git config --get-urlmatch HTTP https://weak.example.com >actual &&
4e513890
PS
1834 test_cmp expect actual &&
1835 git config get --url=https://weak.example.com HTTP >actual &&
d4770964
JH
1836 test_cmp expect actual
1837'
1838
26b66932
GC
1839test_expect_success 'urlmatch with --show-scope' '
1840 cat >.git/config <<-\EOF &&
1841 [http "https://weak.example.com"]
1842 sslVerify = false
1843 cookieFile = /tmp/cookie.txt
1844 EOF
1845
1846 cat >expect <<-EOF &&
1847 local http.cookiefile /tmp/cookie.txt
1848 local http.sslverify false
1849 EOF
1850 git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
4e513890
PS
1851 test_cmp expect actual &&
1852 git config get --url=https://weak.example.com --show-scope HTTP >actual &&
26b66932
GC
1853 test_cmp expect actual
1854'
1855
af99049c
PS
1856test_expect_success 'urlmatch favors more specific URLs' '
1857 cat >.git/config <<-\EOF &&
1858 [http "https://example.com/"]
1859 cookieFile = /tmp/root.txt
1860 [http "https://example.com/subdirectory"]
1861 cookieFile = /tmp/subdirectory.txt
1862 [http "https://user@example.com/"]
1863 cookieFile = /tmp/user.txt
1864 [http "https://averylonguser@example.com/"]
1865 cookieFile = /tmp/averylonguser.txt
a272b9e7
PS
1866 [http "https://preceding.example.com"]
1867 cookieFile = /tmp/preceding.txt
1868 [http "https://*.example.com"]
1869 cookieFile = /tmp/wildcard.txt
1870 [http "https://*.example.com/wildcardwithsubdomain"]
1871 cookieFile = /tmp/wildcardwithsubdomain.txt
732f9344 1872 [http "https://*.example.*"]
1873 cookieFile = /tmp/multiwildcard.txt
a272b9e7
PS
1874 [http "https://trailing.example.com"]
1875 cookieFile = /tmp/trailing.txt
1876 [http "https://user@*.example.com/"]
1877 cookieFile = /tmp/wildcardwithuser.txt
1878 [http "https://sub.example.com/"]
1879 cookieFile = /tmp/sub.txt
af99049c
PS
1880 EOF
1881
1882 echo http.cookiefile /tmp/root.txt >expect &&
1883 git config --get-urlmatch HTTP https://example.com >actual &&
1884 test_cmp expect actual &&
4e513890
PS
1885 git config get --url=https://example.com HTTP >actual &&
1886 test_cmp expect actual &&
af99049c
PS
1887
1888 echo http.cookiefile /tmp/subdirectory.txt >expect &&
1889 git config --get-urlmatch HTTP https://example.com/subdirectory >actual &&
1890 test_cmp expect actual &&
4e513890
PS
1891 git config get --url=https://example.com/subdirectory HTTP >actual &&
1892 test_cmp expect actual &&
af99049c
PS
1893
1894 echo http.cookiefile /tmp/subdirectory.txt >expect &&
1895 git config --get-urlmatch HTTP https://example.com/subdirectory/nested >actual &&
1896 test_cmp expect actual &&
4e513890
PS
1897 git config get --url=https://example.com/subdirectory/nested HTTP >actual &&
1898 test_cmp expect actual &&
af99049c
PS
1899
1900 echo http.cookiefile /tmp/user.txt >expect &&
1901 git config --get-urlmatch HTTP https://user@example.com/ >actual &&
1902 test_cmp expect actual &&
4e513890
PS
1903 git config get --url=https://user@example.com/ HTTP >actual &&
1904 test_cmp expect actual &&
af99049c
PS
1905
1906 echo http.cookiefile /tmp/subdirectory.txt >expect &&
1907 git config --get-urlmatch HTTP https://averylonguser@example.com/subdirectory >actual &&
a272b9e7 1908 test_cmp expect actual &&
4e513890
PS
1909 git config get --url=https://averylonguser@example.com/subdirectory HTTP >actual &&
1910 test_cmp expect actual &&
a272b9e7
PS
1911
1912 echo http.cookiefile /tmp/preceding.txt >expect &&
1913 git config --get-urlmatch HTTP https://preceding.example.com >actual &&
1914 test_cmp expect actual &&
4e513890
PS
1915 git config get --url=https://preceding.example.com HTTP >actual &&
1916 test_cmp expect actual &&
a272b9e7
PS
1917
1918 echo http.cookiefile /tmp/wildcard.txt >expect &&
1919 git config --get-urlmatch HTTP https://wildcard.example.com >actual &&
1920 test_cmp expect actual &&
4e513890
PS
1921 git config get --url=https://wildcard.example.com HTTP >actual &&
1922 test_cmp expect actual &&
a272b9e7
PS
1923
1924 echo http.cookiefile /tmp/sub.txt >expect &&
1925 git config --get-urlmatch HTTP https://sub.example.com/wildcardwithsubdomain >actual &&
1926 test_cmp expect actual &&
4e513890
PS
1927 git config get --url=https://sub.example.com/wildcardwithsubdomain HTTP >actual &&
1928 test_cmp expect actual &&
a272b9e7
PS
1929
1930 echo http.cookiefile /tmp/trailing.txt >expect &&
1931 git config --get-urlmatch HTTP https://trailing.example.com >actual &&
1932 test_cmp expect actual &&
4e513890
PS
1933 git config get --url=https://trailing.example.com HTTP >actual &&
1934 test_cmp expect actual &&
a272b9e7
PS
1935
1936 echo http.cookiefile /tmp/sub.txt >expect &&
1937 git config --get-urlmatch HTTP https://user@sub.example.com >actual &&
732f9344 1938 test_cmp expect actual &&
4e513890
PS
1939 git config get --url=https://user@sub.example.com HTTP >actual &&
1940 test_cmp expect actual &&
732f9344 1941
1942 echo http.cookiefile /tmp/multiwildcard.txt >expect &&
1943 git config --get-urlmatch HTTP https://wildcard.example.org >actual &&
4e513890
PS
1944 test_cmp expect actual &&
1945 git config get --url=https://wildcard.example.org HTTP >actual &&
a272b9e7
PS
1946 test_cmp expect actual
1947'
1948
1949test_expect_success 'urlmatch with wildcard' '
1950 cat >.git/config <<-\EOF &&
1951 [http]
1952 sslVerify
1953 [http "https://*.example.com"]
1954 sslVerify = false
1955 cookieFile = /tmp/cookie.txt
1956 EOF
1957
1958 test_expect_code 1 git config --bool --get-urlmatch doesnt.exist https://good.example.com >actual &&
1959 test_must_be_empty actual &&
1960
1961 echo true >expect &&
1962 git config --bool --get-urlmatch http.SSLverify https://example.com >actual &&
1963 test_cmp expect actual &&
1964
1965 echo true >expect &&
1966 git config --bool --get-urlmatch http.SSLverify https://good-example.com >actual &&
1967 test_cmp expect actual &&
1968
1969 echo true >expect &&
1970 git config --bool --get-urlmatch http.sslverify https://deep.nested.example.com >actual &&
1971 test_cmp expect actual &&
1972
1973 echo false >expect &&
1974 git config --bool --get-urlmatch http.sslverify https://good.example.com >actual &&
1975 test_cmp expect actual &&
1976
1977 {
1978 echo http.cookiefile /tmp/cookie.txt &&
1979 echo http.sslverify false
1980 } >expect &&
1981 git config --get-urlmatch HTTP https://good.example.com >actual &&
1982 test_cmp expect actual &&
1983
1984 echo http.sslverify >expect &&
1985 git config --get-urlmatch HTTP https://more.example.com.au >actual &&
af99049c
PS
1986 test_cmp expect actual
1987'
1988
53ca053b 1989# good section hygiene
22aedfcc 1990test_expect_success '--unset last key removes section (except if commented)' '
53ca053b
JK
1991 cat >.git/config <<-\EOF &&
1992 # some generic comment on the configuration file itself
1993 # a comment specific to this "section" section.
1994 [section]
1995 # some intervening lines
1996 # that should also be dropped
1997
1998 key = value
1999 # please be careful when you update the above variable
2000 EOF
2001
2002 cat >expect <<-\EOF &&
2003 # some generic comment on the configuration file itself
dde154b5
JS
2004 # a comment specific to this "section" section.
2005 [section]
2006 # some intervening lines
2007 # that should also be dropped
2008
2009 # please be careful when you update the above variable
53ca053b
JK
2010 EOF
2011
2012 git config --unset section.key &&
dde154b5
JS
2013 test_cmp expect .git/config &&
2014
2015 cat >.git/config <<-\EOF &&
2016 [section]
2017 key = value
2018 [next-section]
2019 EOF
2020
2021 cat >expect <<-\EOF &&
2022 [next-section]
53ca053b
JK
2023 EOF
2024
2025 git config --unset section.key &&
422e8ef2
JS
2026 test_cmp expect .git/config &&
2027
2028 q_to_tab >.git/config <<-\EOF &&
2029 [one]
2030 Qkey = "multiline \
2031 QQ# with comment"
2032 [two]
2033 key = true
2034 EOF
2035 git config --unset two.key &&
2036 ! grep two .git/config &&
2037
2038 q_to_tab >.git/config <<-\EOF &&
2039 [one]
2040 Qkey = "multiline \
2041 QQ# with comment"
2042 [one]
2043 key = true
2044 EOF
2045 git config --unset-all one.key &&
2046 test_line_count = 0 .git/config &&
2047
2048 q_to_tab >.git/config <<-\EOF &&
2049 [one]
2050 Qkey = true
2051 Q# a comment not at the start
2052 [two]
2053 Qkey = true
2054 EOF
2055 git config --unset two.key &&
2056 grep two .git/config &&
2057
2058 q_to_tab >.git/config <<-\EOF &&
2059 [one]
2060 Qkey = not [two "subsection"]
2061 [two "subsection"]
2062 [two "subsection"]
2063 Qkey = true
2064 [TWO "subsection"]
2065 [one]
2066 EOF
2067 git config --unset two.subsection.key &&
4e513890 2068 test "not [two subsection]" = "$(git config ${mode_get} one.key)" &&
422e8ef2 2069 test_line_count = 3 .git/config
53ca053b
JK
2070'
2071
22aedfcc 2072test_expect_success '--unset-all removes section if empty & uncommented' '
b73bdc34
JS
2073 cat >.git/config <<-\EOF &&
2074 [section]
2075 key = value1
2076 key = value2
2077 EOF
2078
2079 git config --unset-all section.key &&
2080 test_line_count = 0 .git/config
53ca053b
JK
2081'
2082
c71d8bb3 2083test_expect_success 'adding a key into an empty section reuses header' '
53ca053b
JK
2084 cat >.git/config <<-\EOF &&
2085 [section]
2086 EOF
2087
2088 q_to_tab >expect <<-\EOF &&
2089 [section]
2090 Qkey = value
2091 EOF
2092
60687de5 2093 git config section.key value &&
53ca053b
JK
2094 test_cmp expect .git/config
2095'
2096
daa22c6f
EW
2097test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
2098 chmod 0600 .git/config &&
2099 git config imap.pass Hunter2 &&
2100 perl -e \
2101 "die q(badset) if ((stat(q(.git/config)))[2] & 07777) != 0600" &&
2102 git config --rename-section imap pop &&
2103 perl -e \
2104 "die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
2105'
2106
45bf3297
JS
2107! test_have_prereq MINGW ||
2108HOME="$(pwd)" # convert to Windows path
2109
70bd879a
LS
2110test_expect_success 'set up --show-origin tests' '
2111 INCLUDE_DIR="$HOME/include" &&
2112 mkdir -p "$INCLUDE_DIR" &&
2113 cat >"$INCLUDE_DIR"/absolute.include <<-\EOF &&
3de7ee36
MR
2114 [user]
2115 absolute = include
70bd879a
LS
2116 EOF
2117 cat >"$INCLUDE_DIR"/relative.include <<-\EOF &&
3de7ee36
MR
2118 [user]
2119 relative = include
70bd879a
LS
2120 EOF
2121 cat >"$HOME"/.gitconfig <<-EOF &&
3de7ee36
MR
2122 [user]
2123 global = true
2124 override = global
2125 [include]
2126 path = "$INCLUDE_DIR/absolute.include"
70bd879a
LS
2127 EOF
2128 cat >.git/config <<-\EOF
3de7ee36
MR
2129 [user]
2130 local = true
2131 override = local
2132 [include]
2133 path = ../include/relative.include
70bd879a
LS
2134 EOF
2135'
2136
2137test_expect_success '--show-origin with --list' '
2138 cat >expect <<-EOF &&
3de7ee36
MR
2139 file:$HOME/.gitconfig user.global=true
2140 file:$HOME/.gitconfig user.override=global
2141 file:$HOME/.gitconfig include.path=$INCLUDE_DIR/absolute.include
2142 file:$INCLUDE_DIR/absolute.include user.absolute=include
2143 file:.git/config user.local=true
2144 file:.git/config user.override=local
2145 file:.git/config include.path=../include/relative.include
2146 file:.git/../include/relative.include user.relative=include
d8d77153 2147 command line: user.environ=true
3de7ee36 2148 command line: user.cmdline=true
70bd879a 2149 EOF
d8d77153 2150 GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.environ GIT_CONFIG_VALUE_0=true\
14970509 2151 git -c user.cmdline=true config ${mode_prefix}list --show-origin >output &&
70bd879a
LS
2152 test_cmp expect output
2153'
2154
2155test_expect_success '--show-origin with --list --null' '
2156 cat >expect <<-EOF &&
3de7ee36
MR
2157 file:$HOME/.gitconfigQuser.global
2158 trueQfile:$HOME/.gitconfigQuser.override
2159 globalQfile:$HOME/.gitconfigQinclude.path
2160 $INCLUDE_DIR/absolute.includeQfile:$INCLUDE_DIR/absolute.includeQuser.absolute
2161 includeQfile:.git/configQuser.local
2162 trueQfile:.git/configQuser.override
2163 localQfile:.git/configQinclude.path
2164 ../include/relative.includeQfile:.git/../include/relative.includeQuser.relative
2165 includeQcommand line:Quser.cmdline
2166 trueQ
70bd879a 2167 EOF
14970509 2168 git -c user.cmdline=true config ${mode_prefix}list --null --show-origin >output.raw &&
70bd879a
LS
2169 nul_to_q <output.raw >output &&
2170 # The here-doc above adds a newline that the --null output would not
2171 # include. Add it here to make the two comparable.
2172 echo >>output &&
2173 test_cmp expect output
2174'
2175
2176test_expect_success '--show-origin with single file' '
2177 cat >expect <<-\EOF &&
3de7ee36
MR
2178 file:.git/config user.local=true
2179 file:.git/config user.override=local
2180 file:.git/config include.path=../include/relative.include
70bd879a 2181 EOF
14970509 2182 git config ${mode_prefix}list --local --show-origin >output &&
70bd879a
LS
2183 test_cmp expect output
2184'
2185
2186test_expect_success '--show-origin with --get-regexp' '
2187 cat >expect <<-EOF &&
3de7ee36
MR
2188 file:$HOME/.gitconfig user.global true
2189 file:.git/config user.local true
70bd879a 2190 EOF
4e513890 2191 git config ${mode_get_regexp} --show-origin "user\.[g|l].*" >output &&
70bd879a
LS
2192 test_cmp expect output
2193'
2194
2195test_expect_success '--show-origin getting a single key' '
2196 cat >expect <<-\EOF &&
3de7ee36 2197 file:.git/config local
70bd879a 2198 EOF
4e513890 2199 git config ${mode_get} --show-origin user.override >output &&
70bd879a
LS
2200 test_cmp expect output
2201'
2202
2203test_expect_success 'set up custom config file' '
91c5a5e0 2204 cat >"custom.conf" <<-\EOF &&
3de7ee36
MR
2205 [user]
2206 custom = true
70bd879a 2207 EOF
91c5a5e0 2208 CUSTOM_CONFIG_FILE="$(test-tool path-utils real_path custom.conf)"
70bd879a
LS
2209'
2210
417be08d
MR
2211test_expect_success !MINGW 'set up custom config file with special name characters' '
2212 WEIRDLY_NAMED_FILE="file\" (dq) and spaces.conf" &&
2213 cp "$CUSTOM_CONFIG_FILE" "$WEIRDLY_NAMED_FILE"
2214'
2215
45bf3297 2216test_expect_success !MINGW '--show-origin escape special file name characters' '
70bd879a 2217 cat >expect <<-\EOF &&
3de7ee36 2218 file:"file\" (dq) and spaces.conf" user.custom=true
70bd879a 2219 EOF
14970509 2220 git config ${mode_prefix}list --file "$WEIRDLY_NAMED_FILE" --show-origin >output &&
70bd879a
LS
2221 test_cmp expect output
2222'
2223
2224test_expect_success '--show-origin stdin' '
2225 cat >expect <<-\EOF &&
3de7ee36 2226 standard input: user.custom=true
70bd879a 2227 EOF
14970509 2228 git config ${mode_prefix}list --file - --show-origin <"$CUSTOM_CONFIG_FILE" >output &&
70bd879a
LS
2229 test_cmp expect output
2230'
2231
2232test_expect_success '--show-origin stdin with file include' '
2233 cat >"$INCLUDE_DIR"/stdin.include <<-EOF &&
3de7ee36
MR
2234 [user]
2235 stdin = include
70bd879a
LS
2236 EOF
2237 cat >expect <<-EOF &&
3de7ee36 2238 file:$INCLUDE_DIR/stdin.include include
70bd879a 2239 EOF
bdbc17e8
MD
2240 echo "[include]path=\"$INCLUDE_DIR\"/stdin.include" |
2241 git config --show-origin --includes --file - user.stdin >output &&
2242
70bd879a
LS
2243 test_cmp expect output
2244'
2245
417be08d 2246test_expect_success '--show-origin blob' '
91c5a5e0
PS
2247 test_when_finished "rm -rf repo" &&
2248 git init repo &&
2249 (
2250 cd repo &&
2251 blob=$(git hash-object -w "$CUSTOM_CONFIG_FILE") &&
2252 cat >expect <<-EOF &&
2253 blob:$blob user.custom=true
2254 EOF
14970509 2255 git config ${mode_prefix}list --blob=$blob --show-origin >output &&
91c5a5e0
PS
2256 test_cmp expect output
2257 )
70bd879a
LS
2258'
2259
417be08d 2260test_expect_success '--show-origin blob ref' '
91c5a5e0
PS
2261 test_when_finished "rm -rf repo" &&
2262 git init repo &&
2263 (
2264 cd repo &&
2265 cat >expect <<-\EOF &&
2266 blob:main:custom.conf user.custom=true
2267 EOF
2268 cp "$CUSTOM_CONFIG_FILE" custom.conf &&
2269 git add custom.conf &&
2270 git commit -m "new config file" &&
14970509 2271 git config ${mode_prefix}list --blob=main:custom.conf --show-origin >output &&
91c5a5e0
PS
2272 test_cmp expect output
2273 )
70bd879a
LS
2274'
2275
26b66932
GC
2276test_expect_success '--show-origin with --default' '
2277 git config --show-origin --default foo some.key >actual &&
2278 echo "command line: foo" >expect &&
2279 test_cmp expect actual
2280'
2281
145d59f4
MR
2282test_expect_success '--show-scope with --list' '
2283 cat >expect <<-EOF &&
2284 global user.global=true
2285 global user.override=global
2286 global include.path=$INCLUDE_DIR/absolute.include
2287 global user.absolute=include
2288 local user.local=true
2289 local user.override=local
2290 local include.path=../include/relative.include
2291 local user.relative=include
db7961e6
GC
2292 local core.repositoryformatversion=1
2293 local extensions.worktreeconfig=true
2294 worktree user.worktree=true
145d59f4
MR
2295 command user.cmdline=true
2296 EOF
14970509 2297 test_when_finished "git worktree remove wt1" &&
db7961e6
GC
2298 git worktree add wt1 &&
2299 # We need these to test for worktree scope, but outside of this
2300 # test, this is just noise
2301 test_config core.repositoryformatversion 1 &&
2302 test_config extensions.worktreeConfig true &&
2303 git config --worktree user.worktree true &&
14970509 2304 git -c user.cmdline=true config ${mode_prefix}list --show-scope >output &&
145d59f4
MR
2305 test_cmp expect output
2306'
2307
2308test_expect_success !MINGW '--show-scope with --blob' '
2309 blob=$(git hash-object -w "$CUSTOM_CONFIG_FILE") &&
2310 cat >expect <<-EOF &&
2311 command user.custom=true
2312 EOF
14970509 2313 git config ${mode_prefix}list --blob=$blob --show-scope >output &&
145d59f4
MR
2314 test_cmp expect output
2315'
2316
2317test_expect_success '--show-scope with --local' '
2318 cat >expect <<-\EOF &&
2319 local user.local=true
2320 local user.override=local
2321 local include.path=../include/relative.include
2322 EOF
14970509 2323 git config ${mode_prefix}list --local --show-scope >output &&
145d59f4
MR
2324 test_cmp expect output
2325'
2326
2327test_expect_success '--show-scope getting a single value' '
2328 cat >expect <<-\EOF &&
2329 local true
2330 EOF
4e513890 2331 git config ${mode_get} --show-scope user.local >output &&
145d59f4
MR
2332 test_cmp expect output
2333'
2334
2335test_expect_success '--show-scope with --show-origin' '
2336 cat >expect <<-EOF &&
2337 global file:$HOME/.gitconfig user.global=true
2338 global file:$HOME/.gitconfig user.override=global
2339 global file:$HOME/.gitconfig include.path=$INCLUDE_DIR/absolute.include
2340 global file:$INCLUDE_DIR/absolute.include user.absolute=include
2341 local file:.git/config user.local=true
2342 local file:.git/config user.override=local
2343 local file:.git/config include.path=../include/relative.include
2344 local file:.git/../include/relative.include user.relative=include
2345 command command line: user.cmdline=true
2346 EOF
14970509 2347 git -c user.cmdline=true config ${mode_prefix}list --show-origin --show-scope >output &&
145d59f4
MR
2348 test_cmp expect output
2349'
2350
26b66932
GC
2351test_expect_success '--show-scope with --default' '
2352 git config --show-scope --default foo some.key >actual &&
2353 echo "command foo" >expect &&
2354 test_cmp expect actual
2355'
2356
4179b489 2357test_expect_success 'override global and system config' '
eb1cd602 2358 test_when_finished rm -f \"\$HOME\"/.gitconfig &&
4179b489
PS
2359 cat >"$HOME"/.gitconfig <<-EOF &&
2360 [home]
2361 config = true
2362 EOF
eb1cd602
ÆAB
2363
2364 test_when_finished rm -rf \"\$HOME\"/.config/git &&
4179b489
PS
2365 mkdir -p "$HOME"/.config/git &&
2366 cat >"$HOME"/.config/git/config <<-EOF &&
2367 [xdg]
2368 config = true
2369 EOF
2370 cat >.git/config <<-EOF &&
2371 [local]
2372 config = true
2373 EOF
2374 cat >custom-global-config <<-EOF &&
2375 [global]
2376 config = true
2377 EOF
2378 cat >custom-system-config <<-EOF &&
2379 [system]
2380 config = true
2381 EOF
2382
2383 cat >expect <<-EOF &&
2384 global xdg.config=true
2385 global home.config=true
2386 local local.config=true
2387 EOF
14970509 2388 git config ${mode_prefix}list --show-scope >output &&
4179b489
PS
2389 test_cmp expect output &&
2390
4179b489
PS
2391 cat >expect <<-EOF &&
2392 system system.config=true
2393 global global.config=true
2394 local local.config=true
2395 EOF
482d5499 2396 GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=custom-system-config GIT_CONFIG_GLOBAL=custom-global-config \
14970509 2397 git config ${mode_prefix}list --show-scope >output &&
4179b489
PS
2398 test_cmp expect output &&
2399
2400 cat >expect <<-EOF &&
2401 local local.config=true
2402 EOF
482d5499 2403 GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=/dev/null GIT_CONFIG_GLOBAL=/dev/null \
14970509 2404 git config ${mode_prefix}list --show-scope >output &&
4179b489
PS
2405 test_cmp expect output
2406'
2407
2408test_expect_success 'override global and system config with missing file' '
14970509
PS
2409 test_must_fail env GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=/dev/null git config ${mode_prefix}list --global &&
2410 test_must_fail env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=does-not-exist git config ${mode_prefix}list --system &&
4179b489
PS
2411 GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=does-not-exist git version
2412'
2413
2414test_expect_success 'system override has no effect with GIT_CONFIG_NOSYSTEM' '
2415 # `git config --system` has different semantics compared to other
2416 # commands as it ignores GIT_CONFIG_NOSYSTEM. We thus test whether the
2417 # variable has an effect via a different proxy.
2418 cat >alias-config <<-EOF &&
2419 [alias]
2420 hello-world = !echo "hello world"
2421 EOF
2422 test_must_fail env GIT_CONFIG_NOSYSTEM=true GIT_CONFIG_SYSTEM=alias-config \
2423 git hello-world &&
2424 GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=alias-config \
2425 git hello-world >actual &&
2426 echo "hello world" >expect &&
2427 test_cmp expect actual
2428'
2429
2430test_expect_success 'write to overridden global and system config' '
2431 cat >expect <<EOF &&
2432[config]
2433 key = value
2434EOF
2435
2436 GIT_CONFIG_GLOBAL=write-to-global git config --global config.key value &&
2437 test_cmp expect write-to-global &&
2438
2439 GIT_CONFIG_SYSTEM=write-to-system git config --system config.key value &&
2440 test_cmp expect write-to-system
2441'
2442
378fe5fc
MT
2443for opt in --local --worktree
2444do
2445 test_expect_success "$opt requires a repo" '
2446 # we expect 128 to ensure that we do not simply
2447 # fail to find anything and return code "1"
2448 test_expect_code 128 nongit git config $opt foo.bar
2449 '
2450done
588a538a 2451
0a8950be 2452cat >.git/config <<-\EOF &&
04f6b0a1 2453[section]
fb0dc3ba 2454foo = true
0a8950be 2455number = 10
fb0dc3ba 2456big = 1M
0a8950be
TB
2457EOF
2458
fb0dc3ba 2459test_expect_success 'identical modern --type specifiers are allowed' '
04f6b0a1 2460 test_cmp_config 1048576 --type=int --type=int section.big
0a8950be
TB
2461'
2462
fb0dc3ba 2463test_expect_success 'identical legacy --type specifiers are allowed' '
04f6b0a1 2464 test_cmp_config 1048576 --int --int section.big
fb0dc3ba
TB
2465'
2466
2467test_expect_success 'identical mixed --type specifiers are allowed' '
04f6b0a1 2468 test_cmp_config 1048576 --int --type=int section.big
fb0dc3ba
TB
2469'
2470
2471test_expect_success 'non-identical modern --type specifiers are not allowed' '
04f6b0a1 2472 test_must_fail git config --type=int --type=bool section.big 2>error &&
6789275d 2473 test_grep "only one type at a time" error
fb0dc3ba
TB
2474'
2475
2476test_expect_success 'non-identical legacy --type specifiers are not allowed' '
04f6b0a1 2477 test_must_fail git config --int --bool section.big 2>error &&
6789275d 2478 test_grep "only one type at a time" error
fb0dc3ba
TB
2479'
2480
2481test_expect_success 'non-identical mixed --type specifiers are not allowed' '
04f6b0a1 2482 test_must_fail git config --type=int --bool section.big 2>error &&
6789275d 2483 test_grep "only one type at a time" error
fb0dc3ba
TB
2484'
2485
2486test_expect_success '--type allows valid type specifiers' '
04f6b0a1 2487 test_cmp_config true --type=bool section.foo
fb0dc3ba
TB
2488'
2489
2490test_expect_success '--no-type unsets type specifiers' '
04f6b0a1 2491 test_cmp_config 10 --type=bool --no-type section.number
fb0dc3ba
TB
2492'
2493
2494test_expect_success 'unset type specifiers may be reset to conflicting ones' '
04f6b0a1 2495 test_cmp_config 1048576 --type=bool --no-type --type=int section.big
fb0dc3ba
TB
2496'
2497
2498test_expect_success '--type rejects unknown specifiers' '
04f6b0a1 2499 test_must_fail git config --type=nonsense section.foo 2>error &&
6789275d 2500 test_grep "unrecognized --type argument" error
fb0dc3ba
TB
2501'
2502
7595c0ec
PW
2503test_expect_success '--type=int requires at least one digit' '
2504 test_must_fail git config --type int --default m some.key >out 2>error &&
2505 grep "bad numeric config value" error &&
2506 test_must_be_empty out
2507'
2508
46fc89ce 2509test_expect_success '--replace-all does not invent newlines' '
e9313952
JS
2510 q_to_tab >.git/config <<-\EOF &&
2511 [abc]key
2512 QkeepSection
2513 [xyz]
2514 Qkey = 1
2515 [abc]
2516 Qkey = a
2517 EOF
2518 q_to_tab >expect <<-\EOF &&
2519 [abc]
2520 QkeepSection
2521 [xyz]
2522 Qkey = 1
2523 [abc]
2524 Qkey = b
2525 EOF
2526 git config --replace-all abc.key b &&
dcbaa0b3 2527 test_cmp expect .git/config
e9313952
JS
2528'
2529
2076dba2
DS
2530test_expect_success 'set all config with value-pattern' '
2531 test_when_finished rm -f config initial &&
2532 git config --file=initial abc.key one &&
2533
2534 # no match => add new entry
2535 cp initial config &&
2536 git config --file=config abc.key two a+ &&
14970509 2537 git config ${mode_prefix}list --file=config >actual &&
2076dba2
DS
2538 cat >expect <<-\EOF &&
2539 abc.key=one
2540 abc.key=two
2541 EOF
2542 test_cmp expect actual &&
2543
2544 # multiple matches => failure
2545 test_must_fail git config --file=config abc.key three o+ 2>err &&
6789275d 2546 test_grep "has multiple values" err &&
2076dba2
DS
2547
2548 # multiple values, no match => add
2549 git config --file=config abc.key three a+ &&
14970509 2550 git config ${mode_prefix}list --file=config >actual &&
2076dba2
DS
2551 cat >expect <<-\EOF &&
2552 abc.key=one
2553 abc.key=two
2554 abc.key=three
2555 EOF
2556 test_cmp expect actual &&
2557
2558 # single match => replace
2559 git config --file=config abc.key four h+ &&
14970509 2560 git config ${mode_prefix}list --file=config >actual &&
2076dba2
DS
2561 cat >expect <<-\EOF &&
2562 abc.key=one
2563 abc.key=two
2564 abc.key=four
2565 EOF
2566 test_cmp expect actual
2567'
2568
d1567194
DS
2569test_expect_success '--replace-all and value-pattern' '
2570 test_when_finished rm -f config &&
2571 git config --file=config --add abc.key one &&
2572 git config --file=config --add abc.key two &&
2573 git config --file=config --add abc.key three &&
2574 git config --file=config --replace-all abc.key four "o+" &&
14970509 2575 git config ${mode_prefix}list --file=config >actual &&
d1567194
DS
2576 cat >expect <<-\EOF &&
2577 abc.key=four
2578 abc.key=three
2579 EOF
2580 test_cmp expect actual
2581'
2582
fda43942
DS
2583test_expect_success 'refuse --fixed-value for incompatible actions' '
2584 test_when_finished rm -f config &&
2585 git config --file=config dev.null bogus &&
2586
2587 # These modes do not allow --fixed-value at all
2588 test_must_fail git config --file=config --fixed-value --add dev.null bogus &&
2589 test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
2590 test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
2591 test_must_fail git config --file=config --fixed-value --rename-section dev null &&
2592 test_must_fail git config --file=config --fixed-value --remove-section dev &&
14970509 2593 test_must_fail git config ${mode_prefix}list --file=config --fixed-value &&
fda43942
DS
2594 test_must_fail git config --file=config --fixed-value --get-color dev.null &&
2595 test_must_fail git config --file=config --fixed-value --get-colorbool dev.null &&
2596
2597 # These modes complain when --fixed-value has no value-pattern
2598 test_must_fail git config --file=config --fixed-value dev.null bogus &&
2599 test_must_fail git config --file=config --fixed-value --replace-all dev.null bogus &&
4e513890
PS
2600 test_must_fail git config ${mode_prefix}get --file=config --fixed-value dev.null &&
2601 test_must_fail git config ${mode_get_all} --file=config --fixed-value dev.null &&
2602 test_must_fail git config ${mode_get_regexp} --file=config --fixed-value "dev.*" &&
fda43942
DS
2603 test_must_fail git config --file=config --fixed-value --unset dev.null &&
2604 test_must_fail git config --file=config --fixed-value --unset-all dev.null
2605'
2606
c90702a1
DS
2607test_expect_success '--fixed-value uses exact string matching' '
2608 test_when_finished rm -f config initial &&
2609 META="a+b*c?d[e]f.g" &&
2610 git config --file=initial fixed.test "$META" &&
2611
2612 cp initial config &&
2613 git config --file=config fixed.test bogus "$META" &&
14970509 2614 git config ${mode_prefix}list --file=config >actual &&
c90702a1
DS
2615 cat >expect <<-EOF &&
2616 fixed.test=$META
2617 fixed.test=bogus
2618 EOF
2619 test_cmp expect actual &&
2620
2621 cp initial config &&
2622 git config --file=config --fixed-value fixed.test bogus "$META" &&
14970509 2623 git config ${mode_prefix}list --file=config >actual &&
c90702a1
DS
2624 cat >expect <<-\EOF &&
2625 fixed.test=bogus
2626 EOF
2627 test_cmp expect actual &&
2628
2629 cp initial config &&
2630 test_must_fail git config --file=config --unset fixed.test "$META" &&
2631 git config --file=config --fixed-value --unset fixed.test "$META" &&
4e513890 2632 test_must_fail git config ${mode_get} --file=config fixed.test &&
c90702a1
DS
2633
2634 cp initial config &&
2635 test_must_fail git config --file=config --unset-all fixed.test "$META" &&
2636 git config --file=config --fixed-value --unset-all fixed.test "$META" &&
4e513890 2637 test_must_fail git config ${mode_get} --file=config fixed.test &&
c90702a1
DS
2638
2639 cp initial config &&
2640 git config --file=config --replace-all fixed.test bogus "$META" &&
14970509 2641 git config ${mode_prefix}list --file=config >actual &&
c90702a1
DS
2642 cat >expect <<-EOF &&
2643 fixed.test=$META
2644 fixed.test=bogus
2645 EOF
2646 test_cmp expect actual &&
2647
2648 git config --file=config --fixed-value --replace-all fixed.test bogus "$META" &&
14970509 2649 git config ${mode_prefix}list --file=config >actual &&
c90702a1
DS
2650 cat >expect <<-EOF &&
2651 fixed.test=bogus
2652 fixed.test=bogus
2653 EOF
2654 test_cmp expect actual
2655'
2656
3f1bae1d
DS
2657test_expect_success '--get and --get-all with --fixed-value' '
2658 test_when_finished rm -f config &&
2659 META="a+b*c?d[e]f.g" &&
2660 git config --file=config fixed.test bogus &&
2661 git config --file=config --add fixed.test "$META" &&
2662
2663 git config --file=config --get fixed.test bogus &&
4e513890 2664 git config get --file=config --value=bogus fixed.test &&
3f1bae1d 2665 test_must_fail git config --file=config --get fixed.test "$META" &&
4e513890 2666 test_must_fail git config get --file=config --value="$META" fixed.test &&
3f1bae1d 2667 git config --file=config --get --fixed-value fixed.test "$META" &&
4e513890 2668 git config get --file=config --fixed-value --value="$META" fixed.test &&
3f1bae1d
DS
2669 test_must_fail git config --file=config --get --fixed-value fixed.test non-existent &&
2670
2671 git config --file=config --get-all fixed.test bogus &&
4e513890 2672 git config get --all --file=config --value=bogus fixed.test &&
3f1bae1d 2673 test_must_fail git config --file=config --get-all fixed.test "$META" &&
4e513890 2674 test_must_fail git config get --all --file=config --value="$META" fixed.test &&
3f1bae1d 2675 git config --file=config --get-all --fixed-value fixed.test "$META" &&
4e513890 2676 git config get --all --file=config --value="$META" --fixed-value fixed.test &&
3f1bae1d
DS
2677 test_must_fail git config --file=config --get-all --fixed-value fixed.test non-existent &&
2678
2679 git config --file=config --get-regexp fixed+ bogus &&
4e513890 2680 git config get --regexp --file=config --value=bogus fixed+ &&
3f1bae1d 2681 test_must_fail git config --file=config --get-regexp fixed+ "$META" &&
4e513890 2682 test_must_fail git config get --regexp --file=config --value="$META" fixed+ &&
3f1bae1d 2683 git config --file=config --get-regexp --fixed-value fixed+ "$META" &&
4e513890 2684 git config get --regexp --file=config --fixed-value --value="$META" fixed+ &&
3f1bae1d
DS
2685 test_must_fail git config --file=config --get-regexp --fixed-value fixed+ non-existent
2686'
2687
399b1984
JT
2688test_expect_success 'includeIf.hasconfig:remote.*.url' '
2689 git init hasremoteurlTest &&
2690 test_when_finished "rm -rf hasremoteurlTest" &&
2691
2692 cat >include-this <<-\EOF &&
2693 [user]
2694 this = this-is-included
2695 EOF
2696 cat >dont-include-that <<-\EOF &&
2697 [user]
2698 that = that-is-not-included
2699 EOF
2700 cat >>hasremoteurlTest/.git/config <<-EOF &&
2701 [includeIf "hasconfig:remote.*.url:foourl"]
2702 path = "$(pwd)/include-this"
2703 [includeIf "hasconfig:remote.*.url:barurl"]
2704 path = "$(pwd)/dont-include-that"
2705 [remote "foo"]
2706 url = foourl
2707 EOF
2708
2709 echo this-is-included >expect-this &&
2710 git -C hasremoteurlTest config --get user.this >actual-this &&
2711 test_cmp expect-this actual-this &&
2712
2713 test_must_fail git -C hasremoteurlTest config --get user.that
2714'
2715
2716test_expect_success 'includeIf.hasconfig:remote.*.url respects last-config-wins' '
2717 git init hasremoteurlTest &&
2718 test_when_finished "rm -rf hasremoteurlTest" &&
2719
2720 cat >include-two-three <<-\EOF &&
2721 [user]
2722 two = included-config
2723 three = included-config
2724 EOF
2725 cat >>hasremoteurlTest/.git/config <<-EOF &&
2726 [remote "foo"]
2727 url = foourl
2728 [user]
2729 one = main-config
2730 two = main-config
2731 [includeIf "hasconfig:remote.*.url:foourl"]
2732 path = "$(pwd)/include-two-three"
2733 [user]
2734 three = main-config
2735 EOF
2736
2737 echo main-config >expect-main-config &&
2738 echo included-config >expect-included-config &&
2739
2740 git -C hasremoteurlTest config --get user.one >actual &&
2741 test_cmp expect-main-config actual &&
2742
2743 git -C hasremoteurlTest config --get user.two >actual &&
2744 test_cmp expect-included-config actual &&
2745
2746 git -C hasremoteurlTest config --get user.three >actual &&
2747 test_cmp expect-main-config actual
2748'
2749
2750test_expect_success 'includeIf.hasconfig:remote.*.url globs' '
2751 git init hasremoteurlTest &&
2752 test_when_finished "rm -rf hasremoteurlTest" &&
2753
2754 printf "[user]\ndss = yes\n" >double-star-start &&
2755 printf "[user]\ndse = yes\n" >double-star-end &&
2756 printf "[user]\ndsm = yes\n" >double-star-middle &&
2757 printf "[user]\nssm = yes\n" >single-star-middle &&
2758 printf "[user]\nno = no\n" >no &&
2759
2760 cat >>hasremoteurlTest/.git/config <<-EOF &&
2761 [remote "foo"]
2762 url = https://foo/bar/baz
2763 [includeIf "hasconfig:remote.*.url:**/baz"]
2764 path = "$(pwd)/double-star-start"
2765 [includeIf "hasconfig:remote.*.url:**/nomatch"]
2766 path = "$(pwd)/no"
2767 [includeIf "hasconfig:remote.*.url:https:/**"]
2768 path = "$(pwd)/double-star-end"
2769 [includeIf "hasconfig:remote.*.url:nomatch:/**"]
2770 path = "$(pwd)/no"
2771 [includeIf "hasconfig:remote.*.url:https:/**/baz"]
2772 path = "$(pwd)/double-star-middle"
2773 [includeIf "hasconfig:remote.*.url:https:/**/nomatch"]
2774 path = "$(pwd)/no"
2775 [includeIf "hasconfig:remote.*.url:https://*/bar/baz"]
2776 path = "$(pwd)/single-star-middle"
2777 [includeIf "hasconfig:remote.*.url:https://*/baz"]
2778 path = "$(pwd)/no"
2779 EOF
2780
2781 git -C hasremoteurlTest config --get user.dss &&
2782 git -C hasremoteurlTest config --get user.dse &&
2783 git -C hasremoteurlTest config --get user.dsm &&
2784 git -C hasremoteurlTest config --get user.ssm &&
2785 test_must_fail git -C hasremoteurlTest config --get user.no
2786'
2787
2788test_expect_success 'includeIf.hasconfig:remote.*.url forbids remote url in such included files' '
2789 git init hasremoteurlTest &&
2790 test_when_finished "rm -rf hasremoteurlTest" &&
2791
2792 cat >include-with-url <<-\EOF &&
2793 [remote "bar"]
2794 url = barurl
2795 EOF
2796 cat >>hasremoteurlTest/.git/config <<-EOF &&
2797 [includeIf "hasconfig:remote.*.url:foourl"]
2798 path = "$(pwd)/include-with-url"
2799 EOF
2800
2801 # test with any Git command
2802 test_must_fail git -C hasremoteurlTest status 2>err &&
2803 grep "fatal: remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url" err
2804'
2805
daa33250
PS
2806test_expect_success 'negated mode causes failure' '
2807 test_must_fail git config --no-get 2>err &&
2808 grep "unknown option \`no-get${SQ}" err
2809'
2810
2811test_expect_success 'specifying multiple modes causes failure' '
2812 cat >expect <<-EOF &&
2813 error: options ${SQ}--get-all${SQ} and ${SQ}--get${SQ} cannot be used together
2814 EOF
2815 test_must_fail git config --get --get-all 2>err &&
2816 test_cmp expect err
2817'
2818
14970509
PS
2819done
2820
942c1f53 2821test_done