Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVicent Martí <vicent@github.com>2013-11-10 19:33:11 +0400
committerVicent Martí <vicent@github.com>2013-11-10 19:33:11 +0400
commitb9cb72c28a937a887676a553fef8e21bbc7be3f0 (patch)
treee1f69ec17a1f64e0fecf7164fda4c2b9a083db42 /src/config_file.c
parent0df96f2b05f45e62047fc592ded37c0ef18ec27b (diff)
parent590c5efb3bab80b3e52cf68b705ef461d7388874 (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.c26
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);
}
}