Add git_config_del to delete a variable Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
diff --git a/src/config.c b/src/config.c
index 12fa8f2..a5e27dd 100644
--- a/src/config.c
+++ b/src/config.c
@@ -167,15 +167,15 @@ int git_config_foreach(git_config *cfg, int (*fn)(const char *, const char *, vo
return ret;
}
+int git_config_del(git_config *cfg, const char *name)
+{
+ return git_config_set_string(cfg, name, NULL);
+}
/**************
* Setters
**************/
-/*
- * Internal function to actually set the string value of a variable
- */
-
int git_config_set_long(git_config *cfg, const char *name, long int value)
{
char str_value[32]; /* All numbers should fit in here */
diff --git a/src/config_file.c b/src/config_file.c
index 5f8cffa..147e850 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -356,9 +356,14 @@ static int config_set(git_config_file *cfg, const char *name, const char *value)
}
/*
- * Otherwise, create it and stick it at the end of the queue.
+ * Otherwise, create it and stick it at the end of the queue. If
+ * value is NULL, we return an error, because you can't delete a
+ * variable that doesn't exist.
*/
+ if (value == NULL)
+ return git__throw(GIT_ENOTFOUND, "Can't delete non-exitent variable");
+
last_dot = strrchr(name, '.');
if (last_dot == NULL) {
return git__throw(GIT_EINVALIDTYPE, "Variables without section aren't allowed");
@@ -1011,8 +1016,15 @@ static int config_write(diskfile_backend *cfg, cvar_t *var)
break;
}
- /* Then replace the variable */
- error = git_filebuf_printf(&file, "\t%s = %s\n", var->name, var->value);
+ /*
+ * Then replace the variable. If the value is NULL, it
+ * means we want to delete it, so pretend everything went
+ * fine
+ */
+ if (var->value == NULL)
+ error = GIT_SUCCESS;
+ else
+ error = git_filebuf_printf(&file, "\t%s = %s\n", var->name, var->value);
if (error < GIT_SUCCESS) {
git__rethrow(error, "Failed to overwrite the variable");
break;