Auto-quote config values in config.c:store_write_pair()
authorBrian Gernhardt <benji@silverinsanity.com>
Tue, 9 Jan 2007 05:27:41 +0000 (00:27 -0500)
committerJunio C Hamano <junkio@cox.net>
Tue, 9 Jan 2007 06:00:18 +0000 (22:00 -0800)
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>
config.c
t/t1300-repo-config.sh

index 9ded954496af7698ef46fb971c69e4b01e2e9db3..2cd0263e13cab1b445498d82e73d1ca09b0b58f0 100644 (file)
--- a/config.c
+++ b/config.c
@@ -513,11 +513,23 @@ static int store_write_pair(int fd, const char* key, const char* value)
 {
        int i;
        int length = strlen(key+store.baselen+1);
+       int quote = 0;
+
+       /* Check to see if the value needs to be quoted. */
+       if (value[0] == ' ')
+               quote = 1;
+       for (i = 0; value[i]; i++)
+               if (value[i] == ';' || value[i] == '#')
+                       quote = 1;
+       if (value[i-1] == ' ')
+               quote = 1;
 
        if (write_in_full(fd, "\t", 1) != 1 ||
            write_in_full(fd, key+store.baselen+1, length) != length ||
            write_in_full(fd, " = ", 3) != 3)
                return 0;
+       if (quote && write_in_full(fd, "\"", 1) != 1)
+               return 0;
        for (i = 0; value[i]; i++)
                switch (value[i]) {
                case '\n':
@@ -537,6 +549,8 @@ static int store_write_pair(int fd, const char* key, const char* value)
                                return 0;
                        break;
                }
+       if (quote && write_in_full(fd, "\"", 1) != 1)
+               return 0;
        if (write_in_full(fd, "\n", 1) != 1)
                return 0;
        return 1;
index a29caa06dc6545b7fc23b3446a713b75f49cd146..60acdd368bfcb6ccb4698f6fd7533dd25befd9b0 100755 (executable)
@@ -401,5 +401,22 @@ test_expect_success numbers '
        test z1048576 = "z$m"
 '
 
+rm .git/config
+
+git-repo-config quote.leading " test"
+git-repo-config quote.ending "test "
+git-repo-config quote.semicolon "test;test"
+git-repo-config quote.hash "test#test"
+
+cat > expect << EOF
+[quote]
+       leading = " test"
+       ending = "test "
+       semicolon = "test;test"
+       hash = "test#test"
+EOF
+
+test_expect_success 'quoting' 'cmp .git/config expect'
+
 test_done