1#!/bin/sh
2#
3# Copyright (c) 2005 Johannes Schindelin
4#
5
6test_description='Test git config in different settings'
7
8. ./test-lib.sh
9
10test_expect_success 'clear default config' '
11 rm -f .git/config
12'
13
14cat > expect << EOF
15[core]
16 penguin = little blue
17EOF
18test_expect_success 'initial' '
19 git config core.penguin "little blue" &&
20 test_cmp expect .git/config
21'
22
23cat > expect << EOF
24[core]
25 penguin = little blue
26 Movie = BadPhysics
27EOF
28test_expect_success 'mixed case' '
29 git config Core.Movie BadPhysics &&
30 test_cmp expect .git/config
31'
32
33cat > expect << EOF
34[core]
35 penguin = little blue
36 Movie = BadPhysics
37[Cores]
38 WhatEver = Second
39EOF
40test_expect_success 'similar section' '
41 git config Cores.WhatEver Second &&
42 test_cmp expect .git/config
43'
44
45cat > expect << EOF
46[core]
47 penguin = little blue
48 Movie = BadPhysics
49 UPPERCASE = true
50[Cores]
51 WhatEver = Second
52EOF
53test_expect_success 'uppercase section' '
54 git config CORE.UPPERCASE true &&
55 test_cmp expect .git/config
56'
57
58test_expect_success 'replace with non-match' '
59 git config core.penguin kingpin !blue
60'
61
62test_expect_success 'replace with non-match (actually matching)' '
63 git config core.penguin "very blue" !kingpin
64'
65
66cat > expect << EOF
67[core]
68 penguin = very blue
69 Movie = BadPhysics
70 UPPERCASE = true
71 penguin = kingpin
72[Cores]
73 WhatEver = Second
74EOF
75
76test_expect_success 'non-match result' 'test_cmp expect .git/config'
77
78test_expect_success 'find mixed-case key by canonical name' '
79 echo Second >expect &&
80 git config cores.whatever >actual &&
81 test_cmp expect actual
82'
83
84test_expect_success 'find mixed-case key by non-canonical name' '
85 echo Second >expect &&
86 git config CoReS.WhAtEvEr >actual &&
87 test_cmp expect actual
88'
89
90test_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 echo one >expect &&
98 git config section.subsection.key >actual &&
99 test_cmp expect actual &&
100 echo two >expect &&
101 git config section.SubSection.key >actual &&
102 test_cmp expect actual
103'
104
105cat > .git/config <<\EOF
106[alpha]
107bar = foo
108[beta]
109baz = multiple \
110lines
111EOF
112
113test_expect_success 'unset with cont. lines' '
114 git config --unset beta.baz
115'
116
117cat > expect <<\EOF
118[alpha]
119bar = foo
120[beta]
121EOF
122
123test_expect_success 'unset with cont. lines is correct' 'test_cmp expect .git/config'
124
125cat > .git/config << EOF
126[beta] ; silly comment # another comment
127noIndent= sillyValue ; 'nother silly comment
128
129# empty line
130 ; comment
131 haha ="beta" # last silly comment
132haha = hello
133 haha = bello
134[nextSection] noNewline = ouch
135EOF
136
137cp .git/config .git/config2
138
139test_expect_success 'multiple unset' '
140 git config --unset-all beta.haha
141'
142
143cat > expect << EOF
144[beta] ; silly comment # another comment
145noIndent= sillyValue ; 'nother silly comment
146
147# empty line
148 ; comment
149[nextSection] noNewline = ouch
150EOF
151
152test_expect_success 'multiple unset is correct' '
153 test_cmp expect .git/config
154'
155
156cp .git/config2 .git/config
157
158test_expect_success '--replace-all missing value' '
159 test_must_fail git config --replace-all beta.haha &&
160 test_cmp .git/config2 .git/config
161'
162
163rm .git/config2
164
165test_expect_success '--replace-all' '
166 git config --replace-all beta.haha gamma
167'
168
169cat > expect << EOF
170[beta] ; silly comment # another comment
171noIndent= sillyValue ; 'nother silly comment
172
173# empty line
174 ; comment
175 haha = gamma
176[nextSection] noNewline = ouch
177EOF
178
179test_expect_success 'all replaced' '
180 test_cmp expect .git/config
181'
182
183cat > expect << EOF
184[beta] ; silly comment # another comment
185noIndent= sillyValue ; 'nother silly comment
186
187# empty line
188 ; comment
189 haha = alpha
190[nextSection] noNewline = ouch
191EOF
192test_expect_success 'really mean test' '
193 git config beta.haha alpha &&
194 test_cmp expect .git/config
195'
196
197cat > expect << EOF
198[beta] ; silly comment # another comment
199noIndent= sillyValue ; 'nother silly comment
200
201# empty line
202 ; comment
203 haha = alpha
204[nextSection]
205 nonewline = wow
206EOF
207test_expect_success 'really really mean test' '
208 git config nextsection.nonewline wow &&
209 test_cmp expect .git/config
210'
211
212test_expect_success 'get value' '
213 echo alpha >expect &&
214 git config beta.haha >actual &&
215 test_cmp expect actual
216'
217
218cat > expect << EOF
219[beta] ; silly comment # another comment
220noIndent= sillyValue ; 'nother silly comment
221
222# empty line
223 ; comment
224[nextSection]
225 nonewline = wow
226EOF
227test_expect_success 'unset' '
228 git config --unset beta.haha &&
229 test_cmp expect .git/config
230'
231
232cat > expect << EOF
233[beta] ; silly comment # another comment
234noIndent= sillyValue ; 'nother silly comment
235
236# empty line
237 ; comment
238[nextSection]
239 nonewline = wow
240 NoNewLine = wow2 for me
241EOF
242test_expect_success 'multivar' '
243 git config nextsection.NoNewLine "wow2 for me" "for me$" &&
244 test_cmp expect .git/config
245'
246
247test_expect_success 'non-match' '
248 git config --get nextsection.nonewline !for
249'
250
251test_expect_success 'non-match value' '
252 echo wow >expect &&
253 git config --get nextsection.nonewline !for >actual &&
254 test_cmp expect actual
255'
256
257test_expect_success 'multi-valued get returns final one' '
258 echo "wow2 for me" >expect &&
259 git config --get nextsection.nonewline >actual &&
260 test_cmp expect actual
261'
262
263test_expect_success 'multi-valued get-all returns all' '
264 cat >expect <<-\EOF &&
265 wow
266 wow2 for me
267 EOF
268 git config --get-all nextsection.nonewline >actual &&
269 test_cmp expect actual
270'
271
272cat > expect << EOF
273[beta] ; silly comment # another comment
274noIndent= sillyValue ; 'nother silly comment
275
276# empty line
277 ; comment
278[nextSection]
279 nonewline = wow3
280 NoNewLine = wow2 for me
281EOF
282test_expect_success 'multivar replace' '
283 git config nextsection.nonewline "wow3" "wow$" &&
284 test_cmp expect .git/config
285'
286
287test_expect_success 'ambiguous unset' '
288 test_must_fail git config --unset nextsection.nonewline
289'
290
291test_expect_success 'invalid unset' '
292 test_must_fail git config --unset somesection.nonewline
293'
294
295cat > expect << EOF
296[beta] ; silly comment # another comment
297noIndent= sillyValue ; 'nother silly comment
298
299# empty line
300 ; comment
301[nextSection]
302 NoNewLine = wow2 for me
303EOF
304
305test_expect_success 'multivar unset' '
306 git config --unset nextsection.nonewline "wow3$" &&
307 test_cmp expect .git/config
308'
309
310test_expect_success 'invalid key' 'test_must_fail git config inval.2key blabla'
311
312test_expect_success 'correct key' 'git config 123456.a123 987'
313
314test_expect_success 'hierarchical section' '
315 git config Version.1.2.3eX.Alpha beta
316'
317
318cat > expect << EOF
319[beta] ; silly comment # another comment
320noIndent= sillyValue ; 'nother silly comment
321
322# empty line
323 ; comment
324[nextSection]
325 NoNewLine = wow2 for me
326[123456]
327 a123 = 987
328[Version "1.2.3eX"]
329 Alpha = beta
330EOF
331
332test_expect_success 'hierarchical section value' '
333 test_cmp expect .git/config
334'
335
336cat > expect << EOF
337beta.noindent=sillyValue
338nextsection.nonewline=wow2 for me
339123456.a123=987
340version.1.2.3eX.alpha=beta
341EOF
342
343test_expect_success 'working --list' '
344 git config --list > output &&
345 test_cmp expect output
346'
347cat > expect << EOF
348EOF
349
350test_expect_success '--list without repo produces empty output' '
351 git --git-dir=nonexistent config --list >output &&
352 test_cmp expect output
353'
354
355cat > expect << EOF
356beta.noindent
357nextsection.nonewline
358123456.a123
359version.1.2.3eX.alpha
360EOF
361
362test_expect_success '--name-only --list' '
363 git config --name-only --list >output &&
364 test_cmp expect output
365'
366
367cat > expect << EOF
368beta.noindent sillyValue
369nextsection.nonewline wow2 for me
370EOF
371
372test_expect_success '--get-regexp' '
373 git config --get-regexp in >output &&
374 test_cmp expect output
375'
376
377cat > expect << EOF
378beta.noindent
379nextsection.nonewline
380EOF
381
382test_expect_success '--name-only --get-regexp' '
383 git config --name-only --get-regexp in >output &&
384 test_cmp expect output
385'
386
387cat > expect << EOF
388wow2 for me
389wow4 for you
390EOF
391
392test_expect_success '--add' '
393 git config --add nextsection.nonewline "wow4 for you" &&
394 git config --get-all nextsection.nonewline > output &&
395 test_cmp expect output
396'
397
398cat > .git/config << EOF
399[novalue]
400 variable
401[emptyvalue]
402 variable =
403EOF
404
405test_expect_success 'get variable with no value' '
406 git config --get novalue.variable ^$
407'
408
409test_expect_success 'get variable with empty value' '
410 git config --get emptyvalue.variable ^$
411'
412
413echo novalue.variable > expect
414
415test_expect_success 'get-regexp variable with no value' '
416 git config --get-regexp novalue > output &&
417 test_cmp expect output
418'
419
420echo 'novalue.variable true' > expect
421
422test_expect_success 'get-regexp --bool variable with no value' '
423 git config --bool --get-regexp novalue > output &&
424 test_cmp expect output
425'
426
427echo 'emptyvalue.variable ' > expect
428
429test_expect_success 'get-regexp variable with empty value' '
430 git config --get-regexp emptyvalue > output &&
431 test_cmp expect output
432'
433
434echo true > expect
435
436test_expect_success 'get bool variable with no value' '
437 git config --bool novalue.variable > output &&
438 test_cmp expect output
439'
440
441echo false > expect
442
443test_expect_success 'get bool variable with empty value' '
444 git config --bool emptyvalue.variable > output &&
445 test_cmp expect output
446'
447
448test_expect_success 'no arguments, but no crash' '
449 test_must_fail git config >output 2>&1 &&
450 test_i18ngrep usage output
451'
452
453cat > .git/config << EOF
454[a.b]
455 c = d
456EOF
457
458cat > expect << EOF
459[a.b]
460 c = d
461[a]
462 x = y
463EOF
464
465test_expect_success 'new section is partial match of another' '
466 git config a.x y &&
467 test_cmp expect .git/config
468'
469
470cat > expect << EOF
471[a.b]
472 c = d
473[a]
474 x = y
475 b = c
476[b]
477 x = y
478EOF
479
480test_expect_success 'new variable inserts into proper section' '
481 git config b.x y &&
482 git config a.b c &&
483 test_cmp expect .git/config
484'
485
486test_expect_success 'alternative --file (non-existing file should fail)' '
487 test_must_fail git config --file non-existing-config -l
488'
489
490cat > other-config << EOF
491[ein]
492 bahn = strasse
493EOF
494
495cat > expect << EOF
496ein.bahn=strasse
497EOF
498
499test_expect_success 'alternative GIT_CONFIG' '
500 GIT_CONFIG=other-config git config --list >output &&
501 test_cmp expect output
502'
503
504test_expect_success 'alternative GIT_CONFIG (--file)' '
505 git config --file other-config --list >output &&
506 test_cmp expect output
507'
508
509test_expect_success 'alternative GIT_CONFIG (--file=-)' '
510 git config --file - --list <other-config >output &&
511 test_cmp expect output
512'
513
514test_expect_success 'setting a value in stdin is an error' '
515 test_must_fail git config --file - some.value foo
516'
517
518test_expect_success 'editing stdin is an error' '
519 test_must_fail git config --file - --edit
520'
521
522test_expect_success 'refer config from subdirectory' '
523 mkdir x &&
524 (
525 cd x &&
526 echo strasse >expect &&
527 git config --get --file ../other-config ein.bahn >actual &&
528 test_cmp expect actual
529 )
530
531'
532
533test_expect_success 'refer config from subdirectory via --file' '
534 (
535 cd x &&
536 git config --file=../other-config --get ein.bahn >actual &&
537 test_cmp expect actual
538 )
539'
540
541cat > expect << EOF
542[ein]
543 bahn = strasse
544[anwohner]
545 park = ausweis
546EOF
547
548test_expect_success '--set in alternative file' '
549 git config --file=other-config anwohner.park ausweis &&
550 test_cmp expect other-config
551'
552
553cat > .git/config << EOF
554# Hallo
555 #Bello
556[branch "eins"]
557 x = 1
558[branch.eins]
559 y = 1
560 [branch "1 234 blabl/a"]
561weird
562EOF
563
564test_expect_success 'rename section' '
565 git config --rename-section branch.eins branch.zwei
566'
567
568cat > expect << EOF
569# Hallo
570 #Bello
571[branch "zwei"]
572 x = 1
573[branch "zwei"]
574 y = 1
575 [branch "1 234 blabl/a"]
576weird
577EOF
578
579test_expect_success 'rename succeeded' '
580 test_cmp expect .git/config
581'
582
583test_expect_success 'rename non-existing section' '
584 test_must_fail git config --rename-section \
585 branch."world domination" branch.drei
586'
587
588test_expect_success 'rename succeeded' '
589 test_cmp expect .git/config
590'
591
592test_expect_success 'rename another section' '
593 git config --rename-section branch."1 234 blabl/a" branch.drei
594'
595
596cat > expect << EOF
597# Hallo
598 #Bello
599[branch "zwei"]
600 x = 1
601[branch "zwei"]
602 y = 1
603[branch "drei"]
604weird
605EOF
606
607test_expect_success 'rename succeeded' '
608 test_cmp expect .git/config
609'
610
611cat >> .git/config << EOF
612[branch "vier"] z = 1
613EOF
614
615test_expect_success 'rename a section with a var on the same line' '
616 git config --rename-section branch.vier branch.zwei
617'
618
619cat > expect << EOF
620# Hallo
621 #Bello
622[branch "zwei"]
623 x = 1
624[branch "zwei"]
625 y = 1
626[branch "drei"]
627weird
628[branch "zwei"]
629 z = 1
630EOF
631
632test_expect_success 'rename succeeded' '
633 test_cmp expect .git/config
634'
635
636test_expect_success 'renaming empty section name is rejected' '
637 test_must_fail git config --rename-section branch.zwei ""
638'
639
640test_expect_success 'renaming to bogus section is rejected' '
641 test_must_fail git config --rename-section branch.zwei "bogus name"
642'
643
644cat >> .git/config << EOF
645 [branch "zwei"] a = 1 [branch "vier"]
646EOF
647
648test_expect_success 'remove section' '
649 git config --remove-section branch.zwei
650'
651
652cat > expect << EOF
653# Hallo
654 #Bello
655[branch "drei"]
656weird
657EOF
658
659test_expect_success 'section was removed properly' '
660 test_cmp expect .git/config
661'
662
663cat > expect << EOF
664[gitcvs]
665 enabled = true
666 dbname = %Ggitcvs2.%a.%m.sqlite
667[gitcvs "ext"]
668 dbname = %Ggitcvs1.%a.%m.sqlite
669EOF
670
671test_expect_success 'section ending' '
672 rm -f .git/config &&
673 git config gitcvs.enabled true &&
674 git config gitcvs.ext.dbname %Ggitcvs1.%a.%m.sqlite &&
675 git config gitcvs.dbname %Ggitcvs2.%a.%m.sqlite &&
676 test_cmp expect .git/config
677
678'
679
680test_expect_success numbers '
681 git config kilo.gram 1k &&
682 git config mega.ton 1m &&
683 echo 1024 >expect &&
684 echo 1048576 >>expect &&
685 git config --int --get kilo.gram >actual &&
686 git config --int --get mega.ton >>actual &&
687 test_cmp expect actual
688'
689
690test_expect_success '--int is at least 64 bits' '
691 git config giga.watts 121g &&
692 echo 129922760704 >expect &&
693 git config --int --get giga.watts >actual &&
694 test_cmp expect actual
695'
696
697test_expect_success 'invalid unit' '
698 git config aninvalid.unit "1auto" &&
699 echo 1auto >expect &&
700 git config aninvalid.unit >actual &&
701 test_cmp expect actual &&
702 test_must_fail git config --int --get aninvalid.unit 2>actual &&
703 test_i18ngrep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual
704'
705
706test_expect_success 'invalid stdin config' '
707 echo "[broken" | test_must_fail git config --list --file - >output 2>&1 &&
708 test_i18ngrep "bad config line 1 in standard input" output
709'
710
711cat > expect << EOF
712true
713false
714true
715false
716true
717false
718true
719false
720EOF
721
722test_expect_success bool '
723
724 git config bool.true1 01 &&
725 git config bool.true2 -1 &&
726 git config bool.true3 YeS &&
727 git config bool.true4 true &&
728 git config bool.false1 000 &&
729 git config bool.false2 "" &&
730 git config bool.false3 nO &&
731 git config bool.false4 FALSE &&
732 rm -f result &&
733 for i in 1 2 3 4
734 do
735 git config --bool --get bool.true$i >>result
736 git config --bool --get bool.false$i >>result
737 done &&
738 test_cmp expect result'
739
740test_expect_success 'invalid bool (--get)' '
741
742 git config bool.nobool foobar &&
743 test_must_fail git config --bool --get bool.nobool'
744
745test_expect_success 'invalid bool (set)' '
746
747 test_must_fail git config --bool bool.nobool foobar'
748
749cat > expect <<\EOF
750[bool]
751 true1 = true
752 true2 = true
753 true3 = true
754 true4 = true
755 false1 = false
756 false2 = false
757 false3 = false
758 false4 = false
759EOF
760
761test_expect_success 'set --bool' '
762
763 rm -f .git/config &&
764 git config --bool bool.true1 01 &&
765 git config --bool bool.true2 -1 &&
766 git config --bool bool.true3 YeS &&
767 git config --bool bool.true4 true &&
768 git config --bool bool.false1 000 &&
769 git config --bool bool.false2 "" &&
770 git config --bool bool.false3 nO &&
771 git config --bool bool.false4 FALSE &&
772 test_cmp expect .git/config'
773
774cat > expect <<\EOF
775[int]
776 val1 = 1
777 val2 = -1
778 val3 = 5242880
779EOF
780
781test_expect_success 'set --int' '
782
783 rm -f .git/config &&
784 git config --int int.val1 01 &&
785 git config --int int.val2 -1 &&
786 git config --int int.val3 5m &&
787 test_cmp expect .git/config
788'
789
790test_expect_success 'get --bool-or-int' '
791 cat >.git/config <<-\EOF &&
792 [bool]
793 true1
794 true2 = true
795 false = false
796 [int]
797 int1 = 0
798 int2 = 1
799 int3 = -1
800 EOF
801 cat >expect <<-\EOF &&
802 true
803 true
804 false
805 0
806 1
807 -1
808 EOF
809 {
810 git config --bool-or-int bool.true1 &&
811 git config --bool-or-int bool.true2 &&
812 git config --bool-or-int bool.false &&
813 git config --bool-or-int int.int1 &&
814 git config --bool-or-int int.int2 &&
815 git config --bool-or-int int.int3
816 } >actual &&
817 test_cmp expect actual
818'
819
820cat >expect <<\EOF
821[bool]
822 true1 = true
823 false1 = false
824 true2 = true
825 false2 = false
826[int]
827 int1 = 0
828 int2 = 1
829 int3 = -1
830EOF
831
832test_expect_success 'set --bool-or-int' '
833 rm -f .git/config &&
834 git config --bool-or-int bool.true1 true &&
835 git config --bool-or-int bool.false1 false &&
836 git config --bool-or-int bool.true2 yes &&
837 git config --bool-or-int bool.false2 no &&
838 git config --bool-or-int int.int1 0 &&
839 git config --bool-or-int int.int2 1 &&
840 git config --bool-or-int int.int3 -1 &&
841 test_cmp expect .git/config
842'
843
844cat >expect <<\EOF
845[path]
846 home = ~/
847 normal = /dev/null
848 trailingtilde = foo~
849EOF
850
851test_expect_success !MINGW 'set --path' '
852 rm -f .git/config &&
853 git config --path path.home "~/" &&
854 git config --path path.normal "/dev/null" &&
855 git config --path path.trailingtilde "foo~" &&
856 test_cmp expect .git/config'
857
858if test_have_prereq !MINGW && test "${HOME+set}"
859then
860 test_set_prereq HOMEVAR
861fi
862
863cat >expect <<EOF
864$HOME/
865/dev/null
866foo~
867EOF
868
869test_expect_success HOMEVAR 'get --path' '
870 git config --get --path path.home > result &&
871 git config --get --path path.normal >> result &&
872 git config --get --path path.trailingtilde >> result &&
873 test_cmp expect result
874'
875
876cat >expect <<\EOF
877/dev/null
878foo~
879EOF
880
881test_expect_success !MINGW 'get --path copes with unset $HOME' '
882 (
883 unset HOME;
884 test_must_fail git config --get --path path.home \
885 >result 2>msg &&
886 git config --get --path path.normal >>result &&
887 git config --get --path path.trailingtilde >>result
888 ) &&
889 test_i18ngrep "[Ff]ailed to expand.*~/" msg &&
890 test_cmp expect result
891'
892
893test_expect_success 'get --path barfs on boolean variable' '
894 echo "[path]bool" >.git/config &&
895 test_must_fail git config --get --path path.bool
896'
897
898cat > expect << EOF
899[quote]
900 leading = " test"
901 ending = "test "
902 semicolon = "test;test"
903 hash = "test#test"
904EOF
905test_expect_success 'quoting' '
906 rm -f .git/config &&
907 git config quote.leading " test" &&
908 git config quote.ending "test " &&
909 git config quote.semicolon "test;test" &&
910 git config quote.hash "test#test" &&
911 test_cmp expect .git/config
912'
913
914test_expect_success 'key with newline' '
915 test_must_fail git config "key.with
916newline" 123'
917
918test_expect_success 'value with newline' 'git config key.sub value.with\\\
919newline'
920
921cat > .git/config <<\EOF
922[section]
923 ; comment \
924 continued = cont\
925inued
926 noncont = not continued ; \
927 quotecont = "cont;\
928inued"
929EOF
930
931cat > expect <<\EOF
932section.continued=continued
933section.noncont=not continued
934section.quotecont=cont;inued
935EOF
936
937test_expect_success 'value continued on next line' '
938 git config --list > result &&
939 test_cmp result expect
940'
941
942cat > .git/config <<\EOF
943[section "sub=section"]
944 val1 = foo=bar
945 val2 = foo\nbar
946 val3 = \n\n
947 val4 =
948 val5
949EOF
950
951cat > expect <<\EOF
952section.sub=section.val1
953foo=barQsection.sub=section.val2
954foo
955barQsection.sub=section.val3
956
957
958Qsection.sub=section.val4
959Qsection.sub=section.val5Q
960EOF
961test_expect_success '--null --list' '
962 git config --null --list >result.raw &&
963 nul_to_q <result.raw >result &&
964 echo >>result &&
965 test_cmp expect result
966'
967
968test_expect_success '--null --get-regexp' '
969 git config --null --get-regexp "val[0-9]" >result.raw &&
970 nul_to_q <result.raw >result &&
971 echo >>result &&
972 test_cmp expect result
973'
974
975test_expect_success 'inner whitespace kept verbatim' '
976 git config section.val "foo bar" &&
977 echo "foo bar" >expect &&
978 git config section.val >actual &&
979 test_cmp expect actual
980'
981
982test_expect_success SYMLINKS 'symlinked configuration' '
983 ln -s notyet myconfig &&
984 git config --file=myconfig test.frotz nitfol &&
985 test -h myconfig &&
986 test -f notyet &&
987 test "z$(git config --file=notyet test.frotz)" = znitfol &&
988 git config --file=myconfig test.xyzzy rezrov &&
989 test -h myconfig &&
990 test -f notyet &&
991 cat >expect <<-\EOF &&
992 nitfol
993 rezrov
994 EOF
995 {
996 git config --file=notyet test.frotz &&
997 git config --file=notyet test.xyzzy
998 } >actual &&
999 test_cmp expect actual
1000'
1001
1002test_expect_success 'nonexistent configuration' '
1003 test_must_fail git config --file=doesnotexist --list &&
1004 test_must_fail git config --file=doesnotexist test.xyzzy
1005'
1006
1007test_expect_success SYMLINKS 'symlink to nonexistent configuration' '
1008 ln -s doesnotexist linktonada &&
1009 ln -s linktonada linktolinktonada &&
1010 test_must_fail git config --file=linktonada --list &&
1011 test_must_fail git config --file=linktolinktonada --list
1012'
1013
1014test_expect_success 'check split_cmdline return' "
1015 git config alias.split-cmdline-fix 'echo \"' &&
1016 test_must_fail git split-cmdline-fix &&
1017 echo foo > foo &&
1018 git add foo &&
1019 git commit -m 'initial commit' &&
1020 git config branch.master.mergeoptions 'echo \"' &&
1021 test_must_fail git merge master
1022"
1023
1024test_expect_success 'git -c "key=value" support' '
1025 cat >expect <<-\EOF &&
1026 value
1027 value
1028 true
1029 EOF
1030 {
1031 git -c core.name=value config core.name &&
1032 git -c foo.CamelCase=value config foo.camelcase &&
1033 git -c foo.flag config --bool foo.flag
1034 } >actual &&
1035 test_cmp expect actual &&
1036 test_must_fail git -c name=value config core.name
1037'
1038
1039# We just need a type-specifier here that cares about the
1040# distinction internally between a NULL boolean and a real
1041# string (because most of git's internal parsers do care).
1042# Using "--path" works, but we do not otherwise care about
1043# its semantics.
1044test_expect_success 'git -c can represent empty string' '
1045 echo >expect &&
1046 git -c foo.empty= config --path foo.empty >actual &&
1047 test_cmp expect actual
1048'
1049
1050test_expect_success 'key sanity-checking' '
1051 test_must_fail git config foo=bar &&
1052 test_must_fail git config foo=.bar &&
1053 test_must_fail git config foo.ba=r &&
1054 test_must_fail git config foo.1bar &&
1055 test_must_fail git config foo."ba
1056 z".bar &&
1057 test_must_fail git config . false &&
1058 test_must_fail git config .foo false &&
1059 test_must_fail git config foo. false &&
1060 test_must_fail git config .foo. false &&
1061 git config foo.bar true &&
1062 git config foo."ba =z".bar false
1063'
1064
1065test_expect_success 'git -c works with aliases of builtins' '
1066 git config alias.checkconfig "-c foo.check=bar config foo.check" &&
1067 echo bar >expect &&
1068 git checkconfig >actual &&
1069 test_cmp expect actual
1070'
1071
1072test_expect_success 'git -c does not split values on equals' '
1073 echo "value with = in it" >expect &&
1074 git -c core.foo="value with = in it" config core.foo >actual &&
1075 test_cmp expect actual
1076'
1077
1078test_expect_success 'git -c dies on bogus config' '
1079 test_must_fail git -c core.bare=foo rev-parse
1080'
1081
1082test_expect_success 'git -c complains about empty key' '
1083 test_must_fail git -c "=foo" rev-parse
1084'
1085
1086test_expect_success 'git -c complains about empty key and value' '
1087 test_must_fail git -c "" rev-parse
1088'
1089
1090test_expect_success 'multiple git -c appends config' '
1091 test_config alias.x "!git -c x.two=2 config --get-regexp ^x\.*" &&
1092 cat >expect <<-\EOF &&
1093 x.one 1
1094 x.two 2
1095 EOF
1096 git -c x.one=1 x >actual &&
1097 test_cmp expect actual
1098'
1099
1100test_expect_success 'last one wins: two level vars' '
1101
1102 # sec.var and sec.VAR are the same variable, as the first
1103 # and the last level of a configuration variable name is
1104 # case insensitive.
1105
1106 echo VAL >expect &&
1107
1108 git -c sec.var=val -c sec.VAR=VAL config --get sec.var >actual &&
1109 test_cmp expect actual &&
1110 git -c SEC.var=val -c sec.var=VAL config --get sec.var >actual &&
1111 test_cmp expect actual &&
1112
1113 git -c sec.var=val -c sec.VAR=VAL config --get SEC.var >actual &&
1114 test_cmp expect actual &&
1115 git -c SEC.var=val -c sec.var=VAL config --get sec.VAR >actual &&
1116 test_cmp expect actual
1117'
1118
1119test_expect_success 'last one wins: three level vars' '
1120
1121 # v.a.r and v.A.r are not the same variable, as the middle
1122 # level of a three-level configuration variable name is
1123 # case sensitive.
1124
1125 echo val >expect &&
1126 git -c v.a.r=val -c v.A.r=VAL config --get v.a.r >actual &&
1127 test_cmp expect actual &&
1128 git -c v.a.r=val -c v.A.r=VAL config --get V.a.R >actual &&
1129 test_cmp expect actual &&
1130
1131 # v.a.r and V.a.R are the same variable, as the first
1132 # and the last level of a configuration variable name is
1133 # case insensitive.
1134
1135 echo VAL >expect &&
1136 git -c v.a.r=val -c v.a.R=VAL config --get v.a.r >actual &&
1137 test_cmp expect actual &&
1138 git -c v.a.r=val -c V.a.r=VAL config --get v.a.r >actual &&
1139 test_cmp expect actual &&
1140 git -c v.a.r=val -c v.a.R=VAL config --get V.a.R >actual &&
1141 test_cmp expect actual &&
1142 git -c v.a.r=val -c V.a.r=VAL config --get V.a.R >actual &&
1143 test_cmp expect actual
1144'
1145
1146for VAR in a .a a. a.0b a."b c". a."b c".0d
1147do
1148 test_expect_success "git -c $VAR=VAL rejects invalid '$VAR'" '
1149 test_must_fail git -c "$VAR=VAL" config -l
1150 '
1151done
1152
1153for VAR in a.b a."b c".d
1154do
1155 test_expect_success "git -c $VAR=VAL works with valid '$VAR'" '
1156 echo VAL >expect &&
1157 git -c "$VAR=VAL" config --get "$VAR" >actual &&
1158 test_cmp expect actual
1159 '
1160done
1161
1162test_expect_success 'git -c is not confused by empty environment' '
1163 GIT_CONFIG_PARAMETERS="" git -c x.one=1 config --list
1164'
1165
1166test_expect_success 'git config --edit works' '
1167 git config -f tmp test.value no &&
1168 echo test.value=yes >expect &&
1169 GIT_EDITOR="echo [test]value=yes >" git config -f tmp --edit &&
1170 git config -f tmp --list >actual &&
1171 test_cmp expect actual
1172'
1173
1174test_expect_success 'git config --edit respects core.editor' '
1175 git config -f tmp test.value no &&
1176 echo test.value=yes >expect &&
1177 test_config core.editor "echo [test]value=yes >" &&
1178 git config -f tmp --edit &&
1179 git config -f tmp --list >actual &&
1180 test_cmp expect actual
1181'
1182
1183# malformed configuration files
1184test_expect_success 'barf on syntax error' '
1185 cat >.git/config <<-\EOF &&
1186 # broken section line
1187 [section]
1188 key garbage
1189 EOF
1190 test_must_fail git config --get section.key >actual 2>error &&
1191 test_i18ngrep " line 3 " error
1192'
1193
1194test_expect_success 'barf on incomplete section header' '
1195 cat >.git/config <<-\EOF &&
1196 # broken section line
1197 [section
1198 key = value
1199 EOF
1200 test_must_fail git config --get section.key >actual 2>error &&
1201 test_i18ngrep " line 2 " error
1202'
1203
1204test_expect_success 'barf on incomplete string' '
1205 cat >.git/config <<-\EOF &&
1206 # broken section line
1207 [section]
1208 key = "value string
1209 EOF
1210 test_must_fail git config --get section.key >actual 2>error &&
1211 test_i18ngrep " line 3 " error
1212'
1213
1214test_expect_success 'urlmatch' '
1215 cat >.git/config <<-\EOF &&
1216 [http]
1217 sslVerify
1218 [http "https://weak.example.com"]
1219 sslVerify = false
1220 cookieFile = /tmp/cookie.txt
1221 EOF
1222
1223 test_expect_code 1 git config --bool --get-urlmatch doesnt.exist https://good.example.com >actual &&
1224 test_must_be_empty actual &&
1225
1226 echo true >expect &&
1227 git config --bool --get-urlmatch http.SSLverify https://good.example.com >actual &&
1228 test_cmp expect actual &&
1229
1230 echo false >expect &&
1231 git config --bool --get-urlmatch http.sslverify https://weak.example.com >actual &&
1232 test_cmp expect actual &&
1233
1234 {
1235 echo http.cookiefile /tmp/cookie.txt &&
1236 echo http.sslverify false
1237 } >expect &&
1238 git config --get-urlmatch HTTP https://weak.example.com >actual &&
1239 test_cmp expect actual
1240'
1241
1242test_expect_success 'urlmatch favors more specific URLs' '
1243 cat >.git/config <<-\EOF &&
1244 [http "https://example.com/"]
1245 cookieFile = /tmp/root.txt
1246 [http "https://example.com/subdirectory"]
1247 cookieFile = /tmp/subdirectory.txt
1248 [http "https://user@example.com/"]
1249 cookieFile = /tmp/user.txt
1250 [http "https://averylonguser@example.com/"]
1251 cookieFile = /tmp/averylonguser.txt
1252 [http "https://preceding.example.com"]
1253 cookieFile = /tmp/preceding.txt
1254 [http "https://*.example.com"]
1255 cookieFile = /tmp/wildcard.txt
1256 [http "https://*.example.com/wildcardwithsubdomain"]
1257 cookieFile = /tmp/wildcardwithsubdomain.txt
1258 [http "https://trailing.example.com"]
1259 cookieFile = /tmp/trailing.txt
1260 [http "https://user@*.example.com/"]
1261 cookieFile = /tmp/wildcardwithuser.txt
1262 [http "https://sub.example.com/"]
1263 cookieFile = /tmp/sub.txt
1264 EOF
1265
1266 echo http.cookiefile /tmp/root.txt >expect &&
1267 git config --get-urlmatch HTTP https://example.com >actual &&
1268 test_cmp expect actual &&
1269
1270 echo http.cookiefile /tmp/subdirectory.txt >expect &&
1271 git config --get-urlmatch HTTP https://example.com/subdirectory >actual &&
1272 test_cmp expect actual &&
1273
1274 echo http.cookiefile /tmp/subdirectory.txt >expect &&
1275 git config --get-urlmatch HTTP https://example.com/subdirectory/nested >actual &&
1276 test_cmp expect actual &&
1277
1278 echo http.cookiefile /tmp/user.txt >expect &&
1279 git config --get-urlmatch HTTP https://user@example.com/ >actual &&
1280 test_cmp expect actual &&
1281
1282 echo http.cookiefile /tmp/subdirectory.txt >expect &&
1283 git config --get-urlmatch HTTP https://averylonguser@example.com/subdirectory >actual &&
1284 test_cmp expect actual &&
1285
1286 echo http.cookiefile /tmp/preceding.txt >expect &&
1287 git config --get-urlmatch HTTP https://preceding.example.com >actual &&
1288 test_cmp expect actual &&
1289
1290 echo http.cookiefile /tmp/wildcard.txt >expect &&
1291 git config --get-urlmatch HTTP https://wildcard.example.com >actual &&
1292 test_cmp expect actual &&
1293
1294 echo http.cookiefile /tmp/sub.txt >expect &&
1295 git config --get-urlmatch HTTP https://sub.example.com/wildcardwithsubdomain >actual &&
1296 test_cmp expect actual &&
1297
1298 echo http.cookiefile /tmp/trailing.txt >expect &&
1299 git config --get-urlmatch HTTP https://trailing.example.com >actual &&
1300 test_cmp expect actual &&
1301
1302 echo http.cookiefile /tmp/sub.txt >expect &&
1303 git config --get-urlmatch HTTP https://user@sub.example.com >actual &&
1304 test_cmp expect actual
1305'
1306
1307test_expect_success 'urlmatch with wildcard' '
1308 cat >.git/config <<-\EOF &&
1309 [http]
1310 sslVerify
1311 [http "https://*.example.com"]
1312 sslVerify = false
1313 cookieFile = /tmp/cookie.txt
1314 EOF
1315
1316 test_expect_code 1 git config --bool --get-urlmatch doesnt.exist https://good.example.com >actual &&
1317 test_must_be_empty actual &&
1318
1319 echo true >expect &&
1320 git config --bool --get-urlmatch http.SSLverify https://example.com >actual &&
1321 test_cmp expect actual &&
1322
1323 echo true >expect &&
1324 git config --bool --get-urlmatch http.SSLverify https://good-example.com >actual &&
1325 test_cmp expect actual &&
1326
1327 echo true >expect &&
1328 git config --bool --get-urlmatch http.sslverify https://deep.nested.example.com >actual &&
1329 test_cmp expect actual &&
1330
1331 echo false >expect &&
1332 git config --bool --get-urlmatch http.sslverify https://good.example.com >actual &&
1333 test_cmp expect actual &&
1334
1335 {
1336 echo http.cookiefile /tmp/cookie.txt &&
1337 echo http.sslverify false
1338 } >expect &&
1339 git config --get-urlmatch HTTP https://good.example.com >actual &&
1340 test_cmp expect actual &&
1341
1342 echo http.sslverify >expect &&
1343 git config --get-urlmatch HTTP https://more.example.com.au >actual &&
1344 test_cmp expect actual
1345'
1346
1347# good section hygiene
1348test_expect_failure 'unsetting the last key in a section removes header' '
1349 cat >.git/config <<-\EOF &&
1350 # some generic comment on the configuration file itself
1351 # a comment specific to this "section" section.
1352 [section]
1353 # some intervening lines
1354 # that should also be dropped
1355
1356 key = value
1357 # please be careful when you update the above variable
1358 EOF
1359
1360 cat >expect <<-\EOF &&
1361 # some generic comment on the configuration file itself
1362 EOF
1363
1364 git config --unset section.key &&
1365 test_cmp expect .git/config
1366'
1367
1368test_expect_failure 'adding a key into an empty section reuses header' '
1369 cat >.git/config <<-\EOF &&
1370 [section]
1371 EOF
1372
1373 q_to_tab >expect <<-\EOF &&
1374 [section]
1375 Qkey = value
1376 EOF
1377
1378 git config section.key value &&
1379 test_cmp expect .git/config
1380'
1381
1382test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
1383 chmod 0600 .git/config &&
1384 git config imap.pass Hunter2 &&
1385 perl -e \
1386 "die q(badset) if ((stat(q(.git/config)))[2] & 07777) != 0600" &&
1387 git config --rename-section imap pop &&
1388 perl -e \
1389 "die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
1390'
1391
1392! test_have_prereq MINGW ||
1393HOME="$(pwd)" # convert to Windows path
1394
1395test_expect_success 'set up --show-origin tests' '
1396 INCLUDE_DIR="$HOME/include" &&
1397 mkdir -p "$INCLUDE_DIR" &&
1398 cat >"$INCLUDE_DIR"/absolute.include <<-\EOF &&
1399 [user]
1400 absolute = include
1401 EOF
1402 cat >"$INCLUDE_DIR"/relative.include <<-\EOF &&
1403 [user]
1404 relative = include
1405 EOF
1406 cat >"$HOME"/.gitconfig <<-EOF &&
1407 [user]
1408 global = true
1409 override = global
1410 [include]
1411 path = "$INCLUDE_DIR/absolute.include"
1412 EOF
1413 cat >.git/config <<-\EOF
1414 [user]
1415 local = true
1416 override = local
1417 [include]
1418 path = ../include/relative.include
1419 EOF
1420'
1421
1422test_expect_success '--show-origin with --list' '
1423 cat >expect <<-EOF &&
1424 file:$HOME/.gitconfig user.global=true
1425 file:$HOME/.gitconfig user.override=global
1426 file:$HOME/.gitconfig include.path=$INCLUDE_DIR/absolute.include
1427 file:$INCLUDE_DIR/absolute.include user.absolute=include
1428 file:.git/config user.local=true
1429 file:.git/config user.override=local
1430 file:.git/config include.path=../include/relative.include
1431 file:.git/../include/relative.include user.relative=include
1432 command line: user.cmdline=true
1433 EOF
1434 git -c user.cmdline=true config --list --show-origin >output &&
1435 test_cmp expect output
1436'
1437
1438test_expect_success '--show-origin with --list --null' '
1439 cat >expect <<-EOF &&
1440 file:$HOME/.gitconfigQuser.global
1441 trueQfile:$HOME/.gitconfigQuser.override
1442 globalQfile:$HOME/.gitconfigQinclude.path
1443 $INCLUDE_DIR/absolute.includeQfile:$INCLUDE_DIR/absolute.includeQuser.absolute
1444 includeQfile:.git/configQuser.local
1445 trueQfile:.git/configQuser.override
1446 localQfile:.git/configQinclude.path
1447 ../include/relative.includeQfile:.git/../include/relative.includeQuser.relative
1448 includeQcommand line:Quser.cmdline
1449 trueQ
1450 EOF
1451 git -c user.cmdline=true config --null --list --show-origin >output.raw &&
1452 nul_to_q <output.raw >output &&
1453 # The here-doc above adds a newline that the --null output would not
1454 # include. Add it here to make the two comparable.
1455 echo >>output &&
1456 test_cmp expect output
1457'
1458
1459test_expect_success '--show-origin with single file' '
1460 cat >expect <<-\EOF &&
1461 file:.git/config user.local=true
1462 file:.git/config user.override=local
1463 file:.git/config include.path=../include/relative.include
1464 EOF
1465 git config --local --list --show-origin >output &&
1466 test_cmp expect output
1467'
1468
1469test_expect_success '--show-origin with --get-regexp' '
1470 cat >expect <<-EOF &&
1471 file:$HOME/.gitconfig user.global true
1472 file:.git/config user.local true
1473 EOF
1474 git config --show-origin --get-regexp "user\.[g|l].*" >output &&
1475 test_cmp expect output
1476'
1477
1478test_expect_success '--show-origin getting a single key' '
1479 cat >expect <<-\EOF &&
1480 file:.git/config local
1481 EOF
1482 git config --show-origin user.override >output &&
1483 test_cmp expect output
1484'
1485
1486test_expect_success 'set up custom config file' '
1487 CUSTOM_CONFIG_FILE="file\" (dq) and spaces.conf" &&
1488 cat >"$CUSTOM_CONFIG_FILE" <<-\EOF
1489 [user]
1490 custom = true
1491 EOF
1492'
1493
1494test_expect_success !MINGW '--show-origin escape special file name characters' '
1495 cat >expect <<-\EOF &&
1496 file:"file\" (dq) and spaces.conf" user.custom=true
1497 EOF
1498 git config --file "$CUSTOM_CONFIG_FILE" --show-origin --list >output &&
1499 test_cmp expect output
1500'
1501
1502test_expect_success '--show-origin stdin' '
1503 cat >expect <<-\EOF &&
1504 standard input: user.custom=true
1505 EOF
1506 git config --file - --show-origin --list <"$CUSTOM_CONFIG_FILE" >output &&
1507 test_cmp expect output
1508'
1509
1510test_expect_success '--show-origin stdin with file include' '
1511 cat >"$INCLUDE_DIR"/stdin.include <<-EOF &&
1512 [user]
1513 stdin = include
1514 EOF
1515 cat >expect <<-EOF &&
1516 file:$INCLUDE_DIR/stdin.include include
1517 EOF
1518 echo "[include]path=\"$INCLUDE_DIR\"/stdin.include" \
1519 | git config --show-origin --includes --file - user.stdin >output &&
1520 test_cmp expect output
1521'
1522
1523test_expect_success !MINGW '--show-origin blob' '
1524 cat >expect <<-\EOF &&
1525 blob:a9d9f9e555b5c6f07cbe09d3f06fe3df11e09c08 user.custom=true
1526 EOF
1527 blob=$(git hash-object -w "$CUSTOM_CONFIG_FILE") &&
1528 git config --blob=$blob --show-origin --list >output &&
1529 test_cmp expect output
1530'
1531
1532test_expect_success !MINGW '--show-origin blob ref' '
1533 cat >expect <<-\EOF &&
1534 blob:"master:file\" (dq) and spaces.conf" user.custom=true
1535 EOF
1536 git add "$CUSTOM_CONFIG_FILE" &&
1537 git commit -m "new config file" &&
1538 git config --blob=master:"$CUSTOM_CONFIG_FILE" --show-origin --list >output &&
1539 test_cmp expect output
1540'
1541
1542test_done