config: refresh on delete When we delete an entry, we also want to refresh the configuration to catch any changes that happened externally. This allows us to simplify the logic, as we no longer need to delete these variables internally. The whole state will be refreshed and the deleted entries won't be there.
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
diff --git a/src/config_file.c b/src/config_file.c
index 227ec82..e5b5655 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -26,7 +26,7 @@ GIT__USE_STRMAP;
typedef struct cvar_t {
struct cvar_t *next;
git_config_entry *entry;
- int included; /* whether this is part of [include] */
+ bool included; /* whether this is part of [include] */
} cvar_t;
typedef struct git_config_file_iter {
@@ -546,19 +546,14 @@ static int config_delete(git_config_backend *cfg, const char *name)
return -1;
}
- git_strmap_delete_at(values, pos);
-
- result = config_write(b, var->entry->name, NULL, NULL);
+ if ((result = config_write(b, var->entry->name, NULL, NULL)) < 0)
+ return result;
- cvar_free(var);
- return result;
+ return config_refresh(cfg);
}
static int config_delete_multivar(git_config_backend *cfg, const char *name, const char *regexp)
{
- cvar_t *var, *prev = NULL, *new_head = NULL;
- cvar_t **to_delete;
- int to_delete_idx;
diskfile_backend *b = (diskfile_backend *)cfg;
git_strmap *values = b->header.values;
char *key;
@@ -572,59 +567,25 @@ static int config_delete_multivar(git_config_backend *cfg, const char *name, con
pos = git_strmap_lookup_index(values, key);
if (!git_strmap_valid_index(values, pos)) {
- giterr_set(GITERR_CONFIG, "Could not find key '%s' to delete", name);
git__free(key);
+ giterr_set(GITERR_CONFIG, "Could not find key '%s' to delete", name);
return GIT_ENOTFOUND;
}
- var = git_strmap_value_at(values, pos);
-
result = regcomp(&preg, regexp, REG_EXTENDED);
if (result < 0) {
- git__free(key);
giterr_set_regex(&preg, result);
- regfree(&preg);
- return -1;
- }
-
- to_delete = git__calloc(cvar_length(var), sizeof(cvar_t *));
- GITERR_CHECK_ALLOC(to_delete);
- to_delete_idx = 0;
-
- while (var != NULL) {
- cvar_t *next = var->next;
-
- if (regexec(&preg, var->entry->value, 0, NULL, 0) == 0) {
- // If we are past the head, reattach previous node to next one,
- // otherwise set the new head for the strmap.
- if (prev != NULL) {
- prev->next = next;
- } else {
- new_head = next;
- }
-
- to_delete[to_delete_idx++] = var;
- } else {
- prev = var;
- }
-
- var = next;
- }
-
- if (new_head != NULL) {
- git_strmap_set_value_at(values, pos, new_head);
- } else {
- git_strmap_delete_at(values, pos);
+ result = -1;
+ goto out;
}
- if (to_delete_idx > 0)
- result = config_write(b, key, &preg, NULL);
+ if ((result = config_write(b, key, &preg, NULL)) < 0)
+ goto out;
- while (to_delete_idx-- > 0)
- cvar_free(to_delete[to_delete_idx]);
+ result = config_refresh(cfg);
+out:
git__free(key);
- git__free(to_delete);
regfree(&preg);
return result;
}
@@ -1532,7 +1493,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
* this, but instead we'll handle it gracefully with an error. */
if (value == NULL) {
giterr_set(GITERR_CONFIG,
- "Race condition when writing a config file (a cvar has been removed)");
+ "race condition when writing a config file (a cvar has been removed)");
goto rewrite_fail;
}
diff --git a/tests/config/multivar.c b/tests/config/multivar.c
index afdb1e5..0150089 100644
--- a/tests/config/multivar.c
+++ b/tests/config/multivar.c
@@ -231,13 +231,13 @@ void test_config_multivar__delete(void)
n = 0;
cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
- cl_assert(n == 2);
+ cl_assert_equal_i(2, n);
cl_git_pass(git_config_delete_multivar(cfg, _name, "github"));
n = 0;
cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
- cl_assert(n == 1);
+ cl_assert_equal_i(1, n);
git_config_free(cfg);
@@ -245,7 +245,7 @@ void test_config_multivar__delete(void)
n = 0;
cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n));
- cl_assert(n == 1);
+ cl_assert_equal_i(1, n);
git_config_free(cfg);
}