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