diff options
author | Vicent Martà <vicent@github.com> | 2013-11-10 19:33:11 +0400 |
---|---|---|
committer | Vicent Martà <vicent@github.com> | 2013-11-10 19:33:11 +0400 |
commit | b9cb72c28a937a887676a553fef8e21bbc7be3f0 (patch) | |
tree | e1f69ec17a1f64e0fecf7164fda4c2b9a083db42 /src/config_file.c | |
parent | 0df96f2b05f45e62047fc592ded37c0ef18ec27b (diff) | |
parent | 590c5efb3bab80b3e52cf68b705ef461d7388874 (diff) |
Merge pull request #1950 from csware/quote-config-values
Correctly quote config values while saving
Diffstat (limited to 'src/config_file.c')
-rw-r--r-- | src/config_file.c | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/config_file.c b/src/config_file.c index 0bd4e4ece..25ebd1c97 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -1178,6 +1178,22 @@ static int write_section(git_filebuf *file, const char *key) return result; } +static int value_needs_surrounding_quote(const char *value) +{ + const char *ptr = value; + if (*value == ' ') + return 1; + while (*ptr) { + if (*ptr == ';' || *ptr == '#') + return 1; + ++ptr; + } + if (ptr != value && *(--ptr) == ' ') + return 1; + + return 0; +} + /* * This is pretty much the parsing, except we write out anything we don't have */ @@ -1302,7 +1318,10 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p /* Then replace the variable. If the value is NULL, it * means we want to delete it, so don't write anything. */ if (value != NULL) { - git_filebuf_printf(&file, "\t%s = %s\n", name, value); + if (value_needs_surrounding_quote(value)) + git_filebuf_printf(&file, "\t%s = \"%s\"\n", name, value); + else + git_filebuf_printf(&file, "\t%s = %s\n", name, value); } /* @@ -1362,7 +1381,10 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p if (reader->buffer.size > 0 && *(reader->buffer.ptr + reader->buffer.size - 1) != '\n') git_filebuf_write(&file, "\n", 1); - git_filebuf_printf(&file, "\t%s = %s\n", name, value); + if (value_needs_surrounding_quote(value)) + git_filebuf_printf(&file, "\t%s = \"%s\"\n", name, value); + else + git_filebuf_printf(&file, "\t%s = %s\n", name, value); } } |