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