Commit 6d4b609718ad7ef7211974624a06564f15610a8b

Carlos Martín Nieto 2011-06-28T15:20:42

Add git_config_del to delete a variable Signed-off-by: Carlos Martín Nieto <cmn@elego.de>

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;