maint: check return of split_cmdline to avoid bad config strings
[git/git.git] / t / t1300-repo-config.sh
CommitLineData
942c1f53
JS
1#!/bin/sh
2#
3# Copyright (c) 2005 Johannes Schindelin
4#
5
5be60078 6test_description='Test git config in different settings'
942c1f53
JS
7
8. ./test-lib.sh
9
10test -f .git/config && rm .git/config
11
5be60078 12git config core.penguin "little blue"
942c1f53
JS
13
14cat > expect << EOF
942c1f53
JS
15[core]
16 penguin = little blue
17EOF
18
19test_expect_success 'initial' 'cmp .git/config expect'
20
5be60078 21git config Core.Movie BadPhysics
942c1f53
JS
22
23cat > expect << EOF
942c1f53
JS
24[core]
25 penguin = little blue
26 Movie = BadPhysics
27EOF
28
29test_expect_success 'mixed case' 'cmp .git/config expect'
30
5be60078 31git config Cores.WhatEver Second
942c1f53
JS
32
33cat > expect << EOF
942c1f53
JS
34[core]
35 penguin = little blue
36 Movie = BadPhysics
37[Cores]
38 WhatEver = Second
39EOF
40
41test_expect_success 'similar section' 'cmp .git/config expect'
42
5be60078 43git config CORE.UPPERCASE true
942c1f53
JS
44
45cat > expect << EOF
942c1f53
JS
46[core]
47 penguin = little blue
48 Movie = BadPhysics
49 UPPERCASE = true
50[Cores]
51 WhatEver = Second
52EOF
53
54test_expect_success 'similar section' 'cmp .git/config expect'
55
f98d863d 56test_expect_success 'replace with non-match' \
5be60078 57 'git config core.penguin kingpin !blue'
f98d863d
JS
58
59test_expect_success 'replace with non-match (actually matching)' \
5be60078 60 'git config core.penguin "very blue" !kingpin'
f98d863d
JS
61
62cat > expect << EOF
f98d863d
JS
63[core]
64 penguin = very blue
65 Movie = BadPhysics
66 UPPERCASE = true
67 penguin = kingpin
68[Cores]
69 WhatEver = Second
70EOF
71
72test_expect_success 'non-match result' 'cmp .git/config expect'
73
7a31cc0f
FL
74cat > .git/config <<\EOF
75[alpha]
76bar = foo
77[beta]
78baz = multiple \
79lines
80EOF
81
82test_expect_success 'unset with cont. lines' \
83 'git config --unset beta.baz'
84
85cat > expect <<\EOF
86[alpha]
87bar = foo
88[beta]
89EOF
90
91test_expect_success 'unset with cont. lines is correct' 'cmp .git/config expect'
92
942c1f53
JS
93cat > .git/config << EOF
94[beta] ; silly comment # another comment
95noIndent= sillyValue ; 'nother silly comment
96
97# empty line
98 ; comment
99 haha ="beta" # last silly comment
4ddba79d
JS
100haha = hello
101 haha = bello
942c1f53
JS
102[nextSection] noNewline = ouch
103EOF
104
4ddba79d
JS
105cp .git/config .git/config2
106
107test_expect_success 'multiple unset' \
5be60078 108 'git config --unset-all beta.haha'
4ddba79d
JS
109
110cat > expect << EOF
111[beta] ; silly comment # another comment
112noIndent= sillyValue ; 'nother silly comment
113
114# empty line
115 ; comment
116[nextSection] noNewline = ouch
117EOF
118
119test_expect_success 'multiple unset is correct' 'cmp .git/config expect'
120
121mv .git/config2 .git/config
122
123test_expect_success '--replace-all' \
5be60078 124 'git config --replace-all beta.haha gamma'
4ddba79d
JS
125
126cat > expect << EOF
127[beta] ; silly comment # another comment
128noIndent= sillyValue ; 'nother silly comment
129
130# empty line
131 ; comment
132 haha = gamma
133[nextSection] noNewline = ouch
134EOF
135
136test_expect_success 'all replaced' 'cmp .git/config expect'
137
5be60078 138git config beta.haha alpha
942c1f53
JS
139
140cat > expect << EOF
141[beta] ; silly comment # another comment
142noIndent= sillyValue ; 'nother silly comment
143
144# empty line
145 ; comment
146 haha = alpha
147[nextSection] noNewline = ouch
148EOF
149
150test_expect_success 'really mean test' 'cmp .git/config expect'
151
5be60078 152git config nextsection.nonewline wow
942c1f53
JS
153
154cat > expect << EOF
155[beta] ; silly comment # another comment
156noIndent= sillyValue ; 'nother silly comment
157
158# empty line
159 ; comment
160 haha = alpha
161[nextSection]
162 nonewline = wow
163EOF
164
165test_expect_success 'really really mean test' 'cmp .git/config expect'
166
5be60078
JH
167test_expect_success 'get value' 'test alpha = $(git config beta.haha)'
168git config --unset beta.haha
942c1f53
JS
169
170cat > expect << EOF
171[beta] ; silly comment # another comment
172noIndent= sillyValue ; 'nother silly comment
173
174# empty line
175 ; comment
176[nextSection]
177 nonewline = wow
178EOF
179
180test_expect_success 'unset' 'cmp .git/config expect'
181
5be60078 182git config nextsection.NoNewLine "wow2 for me" "for me$"
942c1f53
JS
183
184cat > expect << EOF
185[beta] ; silly comment # another comment
186noIndent= sillyValue ; 'nother silly comment
187
188# empty line
189 ; comment
190[nextSection]
191 nonewline = wow
192 NoNewLine = wow2 for me
193EOF
194
195test_expect_success 'multivar' 'cmp .git/config expect'
196
f98d863d 197test_expect_success 'non-match' \
5be60078 198 'git config --get nextsection.nonewline !for'
f98d863d
JS
199
200test_expect_success 'non-match value' \
5be60078 201 'test wow = $(git config --get nextsection.nonewline !for)'
f98d863d 202
41ac414e 203test_expect_success 'ambiguous get' '
d492b31c 204 test_must_fail git config --get nextsection.nonewline
41ac414e 205'
4ddba79d
JS
206
207test_expect_success 'get multivar' \
5be60078 208 'git config --get-all nextsection.nonewline'
4ddba79d 209
5be60078 210git config nextsection.nonewline "wow3" "wow$"
942c1f53
JS
211
212cat > expect << EOF
213[beta] ; silly comment # another comment
214noIndent= sillyValue ; 'nother silly comment
215
216# empty line
217 ; comment
218[nextSection]
219 nonewline = wow3
220 NoNewLine = wow2 for me
221EOF
222
223test_expect_success 'multivar replace' 'cmp .git/config expect'
224
41ac414e 225test_expect_success 'ambiguous value' '
d492b31c 226 test_must_fail git config nextsection.nonewline
41ac414e 227'
4ddba79d 228
41ac414e 229test_expect_success 'ambiguous unset' '
d492b31c 230 test_must_fail git config --unset nextsection.nonewline
41ac414e 231'
942c1f53 232
41ac414e 233test_expect_success 'invalid unset' '
d492b31c 234 test_must_fail git config --unset somesection.nonewline
41ac414e 235'
942c1f53 236
5be60078 237git config --unset nextsection.nonewline "wow3$"
942c1f53
JS
238
239cat > expect << EOF
240[beta] ; silly comment # another comment
241noIndent= sillyValue ; 'nother silly comment
242
243# empty line
244 ; comment
245[nextSection]
246 NoNewLine = wow2 for me
247EOF
248
249test_expect_success 'multivar unset' 'cmp .git/config expect'
250
d492b31c 251test_expect_success 'invalid key' 'test_must_fail git config inval.2key blabla'
942c1f53 252
5be60078 253test_expect_success 'correct key' 'git config 123456.a123 987'
942c1f53 254
b17e659d 255test_expect_success 'hierarchical section' \
5be60078 256 'git config Version.1.2.3eX.Alpha beta'
b17e659d
JS
257
258cat > expect << EOF
259[beta] ; silly comment # another comment
260noIndent= sillyValue ; 'nother silly comment
261
262# empty line
263 ; comment
264[nextSection]
265 NoNewLine = wow2 for me
266[123456]
267 a123 = 987
d14f7764
LT
268[Version "1.2.3eX"]
269 Alpha = beta
b17e659d
JS
270EOF
271
272test_expect_success 'hierarchical section value' 'cmp .git/config expect'
273
2fa9a0fb
JS
274cat > expect << EOF
275beta.noindent=sillyValue
276nextsection.nonewline=wow2 for me
277123456.a123=987
d55aaefa 278version.1.2.3eX.alpha=beta
2fa9a0fb
JS
279EOF
280
281test_expect_success 'working --list' \
5be60078 282 'git config --list > output && cmp output expect'
2fa9a0fb
JS
283
284cat > expect << EOF
285beta.noindent sillyValue
286nextsection.nonewline wow2 for me
287EOF
288
289test_expect_success '--get-regexp' \
5be60078 290 'git config --get-regexp in > output && cmp output expect'
2fa9a0fb 291
5be60078 292git config --add nextsection.nonewline "wow4 for you"
89c4afe0
BG
293
294cat > expect << EOF
295wow2 for me
296wow4 for you
297EOF
298
299test_expect_success '--add' \
5be60078 300 'git config --get-all nextsection.nonewline > output && cmp output expect'
89c4afe0 301
f067a137
JF
302cat > .git/config << EOF
303[novalue]
304 variable
09bc098c
CC
305[emptyvalue]
306 variable =
f067a137
JF
307EOF
308
309test_expect_success 'get variable with no value' \
5be60078 310 'git config --get novalue.variable ^$'
f067a137 311
09bc098c
CC
312test_expect_success 'get variable with empty value' \
313 'git config --get emptyvalue.variable ^$'
314
b69ba460
FL
315echo novalue.variable > expect
316
317test_expect_success 'get-regexp variable with no value' \
5be60078 318 'git config --get-regexp novalue > output &&
b69ba460
FL
319 cmp output expect'
320
09bc098c
CC
321echo 'emptyvalue.variable ' > expect
322
323test_expect_success 'get-regexp variable with empty value' \
324 'git config --get-regexp emptyvalue > output &&
325 cmp output expect'
326
327echo true > expect
328
329test_expect_success 'get bool variable with no value' \
330 'git config --bool novalue.variable > output &&
331 cmp output expect'
332
333echo false > expect
334
335test_expect_success 'get bool variable with empty value' \
336 'git config --bool emptyvalue.variable > output &&
337 cmp output expect'
338
5be60078 339git config > output 2>&1
2fa9a0fb
JS
340
341test_expect_success 'no arguments, but no crash' \
342 "test $? = 129 && grep usage output"
343
bd886fd3
SE
344cat > .git/config << EOF
345[a.b]
346 c = d
347EOF
348
5be60078 349git config a.x y
bd886fd3
SE
350
351cat > expect << EOF
352[a.b]
353 c = d
354[a]
355 x = y
356EOF
357
358test_expect_success 'new section is partial match of another' 'cmp .git/config expect'
359
5be60078
JH
360git config b.x y
361git config a.b c
bd886fd3
SE
362
363cat > expect << EOF
364[a.b]
365 c = d
366[a]
367 x = y
368 b = c
369[b]
370 x = y
371EOF
372
373test_expect_success 'new variable inserts into proper section' 'cmp .git/config expect'
374
773a69fb
AR
375test_expect_success 'alternative GIT_CONFIG (non-existing file should fail)' \
376 'git config --file non-existing-config -l; test $? != 0'
377
9c3796fc
JS
378cat > other-config << EOF
379[ein]
380 bahn = strasse
381EOF
382
383cat > expect << EOF
384ein.bahn=strasse
385EOF
386
5be60078 387GIT_CONFIG=other-config git config -l > output
9c3796fc
JS
388
389test_expect_success 'alternative GIT_CONFIG' 'cmp output expect'
390
773a69fb
AR
391test_expect_success 'alternative GIT_CONFIG (--file)' \
392 'git config --file other-config -l > output && cmp output expect'
393
5be60078 394GIT_CONFIG=other-config git config anwohner.park ausweis
9c3796fc
JS
395
396cat > expect << EOF
397[ein]
398 bahn = strasse
399[anwohner]
400 park = ausweis
401EOF
402
403test_expect_success '--set in alternative GIT_CONFIG' 'cmp other-config expect'
404
0667fcfb
JS
405cat > .git/config << EOF
406# Hallo
407 #Bello
408[branch "eins"]
409 x = 1
410[branch.eins]
411 y = 1
412 [branch "1 234 blabl/a"]
413weird
414EOF
415
416test_expect_success "rename section" \
5be60078 417 "git config --rename-section branch.eins branch.zwei"
0667fcfb
JS
418
419cat > expect << EOF
420# Hallo
421 #Bello
422[branch "zwei"]
423 x = 1
424[branch "zwei"]
425 y = 1
426 [branch "1 234 blabl/a"]
427weird
428EOF
429
3af82863 430test_expect_success "rename succeeded" "test_cmp expect .git/config"
0667fcfb 431
41ac414e 432test_expect_success "rename non-existing section" '
d492b31c
SB
433 test_must_fail git config --rename-section \
434 branch."world domination" branch.drei
41ac414e 435'
0667fcfb 436
3af82863 437test_expect_success "rename succeeded" "test_cmp expect .git/config"
0667fcfb
JS
438
439test_expect_success "rename another section" \
5be60078 440 'git config --rename-section branch."1 234 blabl/a" branch.drei'
0667fcfb
JS
441
442cat > expect << EOF
443# Hallo
444 #Bello
445[branch "zwei"]
446 x = 1
447[branch "zwei"]
448 y = 1
449[branch "drei"]
450weird
451EOF
452
3af82863 453test_expect_success "rename succeeded" "test_cmp expect .git/config"
0667fcfb 454
118f8b24
PB
455cat >> .git/config << EOF
456 [branch "zwei"] a = 1 [branch "vier"]
457EOF
458
459test_expect_success "remove section" "git config --remove-section branch.zwei"
460
461cat > expect << EOF
462# Hallo
463 #Bello
464[branch "drei"]
465weird
466EOF
467
468test_expect_success "section was removed properly" \
3af82863 469 "test_cmp expect .git/config"
118f8b24 470
b18a2be3
SP
471rm .git/config
472
473cat > expect << EOF
474[gitcvs]
475 enabled = true
476 dbname = %Ggitcvs2.%a.%m.sqlite
477[gitcvs "ext"]
478 dbname = %Ggitcvs1.%a.%m.sqlite
479EOF
480
481test_expect_success 'section ending' '
482
5be60078
JH
483 git config gitcvs.enabled true &&
484 git config gitcvs.ext.dbname %Ggitcvs1.%a.%m.sqlite &&
485 git config gitcvs.dbname %Ggitcvs2.%a.%m.sqlite &&
b18a2be3
SP
486 cmp .git/config expect
487
488'
489
d77a64d3
SP
490test_expect_success numbers '
491
5be60078
JH
492 git config kilo.gram 1k &&
493 git config mega.ton 1m &&
494 k=$(git config --int --get kilo.gram) &&
d77a64d3 495 test z1024 = "z$k" &&
5be60078 496 m=$(git config --int --get mega.ton) &&
d77a64d3
SP
497 test z1048576 = "z$m"
498'
499
c8deb5a1
SP
500cat > expect <<EOF
501fatal: bad config value for 'aninvalid.unit' in .git/config
502EOF
503
504test_expect_success 'invalid unit' '
505
506 git config aninvalid.unit "1auto" &&
507 s=$(git config aninvalid.unit) &&
508 test "z1auto" = "z$s" &&
509 if git config --int --get aninvalid.unit 2>actual
510 then
511 echo config should have failed
512 false
513 fi &&
514 cmp actual expect
515'
516
cab333cb
FL
517cat > expect << EOF
518true
519false
520true
521false
522true
523false
524true
525false
526EOF
527
528test_expect_success bool '
529
5be60078
JH
530 git config bool.true1 01 &&
531 git config bool.true2 -1 &&
532 git config bool.true3 YeS &&
533 git config bool.true4 true &&
534 git config bool.false1 000 &&
535 git config bool.false2 "" &&
536 git config bool.false3 nO &&
537 git config bool.false4 FALSE &&
cab333cb
FL
538 rm -f result &&
539 for i in 1 2 3 4
540 do
5be60078
JH
541 git config --bool --get bool.true$i >>result
542 git config --bool --get bool.false$i >>result
cab333cb
FL
543 done &&
544 cmp expect result'
545
41ac414e 546test_expect_success 'invalid bool (--get)' '
cab333cb 547
5be60078 548 git config bool.nobool foobar &&
d492b31c 549 test_must_fail git config --bool --get bool.nobool'
cab333cb 550
41ac414e 551test_expect_success 'invalid bool (set)' '
db1696b8 552
d492b31c 553 test_must_fail git config --bool bool.nobool foobar'
db1696b8
FL
554
555rm .git/config
556
557cat > expect <<\EOF
558[bool]
559 true1 = true
560 true2 = true
561 true3 = true
562 true4 = true
563 false1 = false
564 false2 = false
565 false3 = false
566 false4 = false
567EOF
568
569test_expect_success 'set --bool' '
570
5be60078
JH
571 git config --bool bool.true1 01 &&
572 git config --bool bool.true2 -1 &&
573 git config --bool bool.true3 YeS &&
574 git config --bool bool.true4 true &&
575 git config --bool bool.false1 000 &&
576 git config --bool bool.false2 "" &&
577 git config --bool bool.false3 nO &&
578 git config --bool bool.false4 FALSE &&
db1696b8
FL
579 cmp expect .git/config'
580
581rm .git/config
582
583cat > expect <<\EOF
584[int]
585 val1 = 1
586 val2 = -1
587 val3 = 5242880
588EOF
589
590test_expect_success 'set --int' '
591
5be60078
JH
592 git config --int int.val1 01 &&
593 git config --int int.val2 -1 &&
594 git config --int int.val3 5m &&
db1696b8
FL
595 cmp expect .git/config'
596
cdd4fb15
BG
597rm .git/config
598
c35b0b58
JH
599cat >expect <<\EOF
600[bool]
601 true1 = true
602 true2 = true
603 false1 = false
604 false2 = false
605[int]
606 int1 = 0
607 int2 = 1
608 int3 = -1
609EOF
610
611test_expect_success 'get --bool-or-int' '
612 (
613 echo "[bool]"
614 echo true1
615 echo true2 = true
616 echo false = false
617 echo "[int]"
618 echo int1 = 0
619 echo int2 = 1
620 echo int3 = -1
621 ) >>.git/config &&
622 test $(git config --bool-or-int bool.true1) = true &&
623 test $(git config --bool-or-int bool.true2) = true &&
624 test $(git config --bool-or-int bool.false) = false &&
625 test $(git config --bool-or-int int.int1) = 0 &&
626 test $(git config --bool-or-int int.int2) = 1 &&
627 test $(git config --bool-or-int int.int3) = -1
628
629'
630
631rm .git/config
632cat >expect <<\EOF
633[bool]
634 true1 = true
635 false1 = false
636 true2 = true
637 false2 = false
638[int]
639 int1 = 0
640 int2 = 1
641 int3 = -1
642EOF
643
644test_expect_success 'set --bool-or-int' '
645 git config --bool-or-int bool.true1 true &&
646 git config --bool-or-int bool.false1 false &&
647 git config --bool-or-int bool.true2 yes &&
648 git config --bool-or-int bool.false2 no &&
649 git config --bool-or-int int.int1 0 &&
650 git config --bool-or-int int.int2 1 &&
651 git config --bool-or-int int.int3 -1 &&
652 test_cmp expect .git/config
653'
654
655rm .git/config
656
5be60078
JH
657git config quote.leading " test"
658git config quote.ending "test "
659git config quote.semicolon "test;test"
660git config quote.hash "test#test"
cdd4fb15
BG
661
662cat > expect << EOF
663[quote]
664 leading = " test"
665 ending = "test "
666 semicolon = "test;test"
667 hash = "test#test"
668EOF
669
670test_expect_success 'quoting' 'cmp .git/config expect'
671
41ac414e 672test_expect_success 'key with newline' '
d492b31c 673 test_must_fail git config "key.with
41ac414e 674newline" 123'
6f71686e 675
e0d10e1c 676test_expect_success 'value with newline' 'git config key.sub value.with\\\
6f71686e
JS
677newline'
678
83cd10a0
JN
679cat > .git/config <<\EOF
680[section]
681 ; comment \
682 continued = cont\
683inued
684 noncont = not continued ; \
685 quotecont = "cont;\
686inued"
687EOF
688
689cat > expect <<\EOF
690section.continued=continued
691section.noncont=not continued
692section.quotecont=cont;inued
693EOF
694
e0d10e1c 695git config --list > result
83cd10a0
JN
696
697test_expect_success 'value continued on next line' 'cmp result expect'
698
2275d502
FL
699cat > .git/config <<\EOF
700[section "sub=section"]
701 val1 = foo=bar
702 val2 = foo\nbar
703 val3 = \n\n
704 val4 =
705 val5
706EOF
707
708cat > expect <<\EOF
2b9a5020
AR
709section.sub=section.val1
710foo=barQsection.sub=section.val2
711foo
712barQsection.sub=section.val3
2275d502
FL
713
714
2b9a5020
AR
715Qsection.sub=section.val4
716Qsection.sub=section.val5Q
2275d502
FL
717EOF
718
e85fe4d8 719git config --null --list | perl -pe 'y/\000/Q/' > result
2b9a5020 720echo >>result
2275d502
FL
721
722test_expect_success '--null --list' 'cmp result expect'
723
e85fe4d8 724git config --null --get-regexp 'val[0-9]' | perl -pe 'y/\000/Q/' > result
2b9a5020 725echo >>result
2275d502
FL
726
727test_expect_success '--null --get-regexp' 'cmp result expect'
728
65a5a21d
JH
729test_expect_success 'symlinked configuration' '
730
731 ln -s notyet myconfig &&
732 GIT_CONFIG=myconfig git config test.frotz nitfol &&
733 test -h myconfig &&
734 test -f notyet &&
735 test "z$(GIT_CONFIG=notyet git config test.frotz)" = znitfol &&
736 GIT_CONFIG=myconfig git config test.xyzzy rezrov &&
737 test -h myconfig &&
738 test -f notyet &&
739 test "z$(GIT_CONFIG=notyet git config test.frotz)" = znitfol &&
740 test "z$(GIT_CONFIG=notyet git config test.xyzzy)" = zrezrov
741
742'
743
dc4179f9
DM
744test_expect_success 'check split_cmdline return' "
745 git config alias.split-cmdline-fix 'echo \"' &&
746 test_must_fail git split-cmdline-fix &&
747 echo foo > foo &&
748 git add foo &&
749 git commit -m 'initial commit' &&
750 git config branch.master.mergeoptions 'echo \"' &&
751 test_must_fail git merge master
752 "
753
942c1f53 754test_done