Auto-quote config values in config.c:store_write_pair()
Suggested by Jakub Narebski <jnareb@gmail.com> on the list. When we send a value to store_write_pair(), make sure that the value that gets read out matches the one passed in. This means that for any value that contains leading or trailing whitespace or any comment character (# and ;), we need to surround it in quotes. Signed-off-by: Brian Gernhardt <benji@silverinsanity.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Brian Gernhardt committed
Jan 9, 2007 at 00:27 UTC
cdd4fb15cf06ec1de588bee4576509857d8e2cb4
2 files changed
+31
config.c
+14
@@ -513,11 +513,23 @@ static int store_write_pair(int fd, const char* key, const char* value)
513
{
514
int i;
515
int length = strlen(key+store.baselen+1);
516
+ int quote = 0;
517
+
518
+ /* Check to see if the value needs to be quoted. */
519
+ if (value[0] == ' ')
520
+ quote = 1;
521
+ for (i = 0; value[i]; i++)
522
+ if (value[i] == ';' || value[i] == '#')
523
+ quote = 1;
524
+ if (value[i-1] == ' ')
525
+ quote = 1;
526
527
if (write_in_full(fd, "\t", 1) != 1 ||
528
write_in_full(fd, key+store.baselen+1, length) != length ||
529
write_in_full(fd, " = ", 3) != 3)
530
return 0;
531
+ if (quote && write_in_full(fd, "\"", 1) != 1)
532
+ return 0;
533
for (i = 0; value[i]; i++)
534
switch (value[i]) {
535
case '\n':
@@ -537,6 +549,8 @@ static int store_write_pair(int fd, const char* key, const char* value)
549
return 0;
550
break;
551
}
552
+ if (quote && write_in_full(fd, "\"", 1) != 1)
553
+ return 0;
554
if (write_in_full(fd, "\n", 1) != 1)
555
return 0;
556
return 1;
t/t1300-repo-config.sh
+17
@@ -401,5 +401,22 @@ test_expect_success numbers '
401
test z1048576 = "z$m"
402
'
403
404
+rm .git/config
405
+
406
+git-repo-config quote.leading " test"
407
+git-repo-config quote.ending "test "
408
+git-repo-config quote.semicolon "test;test"
409
+git-repo-config quote.hash "test#test"
410
+
411
+cat > expect << EOF
412
+[quote]
413
+ leading = " test"
414
+ ending = "test "
415
+ semicolon = "test;test"
416
+ hash = "test#test"
417
+EOF
418
+
419
+test_expect_success 'quoting' 'cmp .git/config expect'
420
+
421
test_done
422