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