Skip to content

Commit 3e1c137

Browse files
committed
config_file: move refresh into write function
We are quite lazy in how we refresh our config file backend when updating any of its keys: instead of just updating our in-memory representation of the keys, we just discard the old set of keys and then re-read the config file contents from disk. This refresh currently happens separately at every callsite of `config_write`, but it is clear that we _always_ want to refresh if we have written the config file to disk. If we didn't, then we'd run around with an outdated config file backend that does not represent what we have on disk. By moving the refresh into `config_write`, we are also able to optimize the case where the config file is currently locked. Before, we would've tried to re-read the file even if we have only updated its cached contents without touching the on-disk file. Thus we'd have unnecessarily stat'd the file, even though we know that it shouldn't have been modified in the meantime due to its lock.
1 parent d7f58ea commit 3e1c137

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/config_file.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,6 @@ static int config_set(git_config_backend *cfg, const char *name, const char *val
284284
if ((error = config_write(b, name, key, NULL, esc_value)) < 0)
285285
goto out;
286286

287-
error = config_refresh(cfg);
288-
289287
out:
290288
git_config_entries_free(entries);
291289
git__free(esc_value);
@@ -352,8 +350,6 @@ static int config_set_multivar(
352350
if ((result = config_write(b, name, key, &preg, value)) < 0)
353351
goto out;
354352

355-
result = config_refresh(cfg);
356-
357353
out:
358354
git__free(key);
359355
p_regfree(&preg);
@@ -385,9 +381,6 @@ static int config_delete(git_config_backend *cfg, const char *name)
385381
if ((error = config_write(b, name, entry->name, NULL, NULL)) < 0)
386382
goto out;
387383

388-
if ((error = config_refresh(cfg)) < 0)
389-
goto out;
390-
391384
out:
392385
git_config_entries_free(entries);
393386
git__free(key);
@@ -426,9 +419,6 @@ static int config_delete_multivar(git_config_backend *cfg, const char *name, con
426419
if ((result = config_write(b, name, key, &preg, NULL)) < 0)
427420
goto out;
428421

429-
if ((result = config_refresh(cfg)) < 0)
430-
goto out;
431-
432422
out:
433423
git_config_entries_free(entries);
434424
git__free(key);
@@ -1208,7 +1198,12 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
12081198
git_buf_attach(&cfg->locked_content, git_buf_detach(&buf), len);
12091199
} else {
12101200
git_filebuf_write(&file, git_buf_cstr(&buf), git_buf_len(&buf));
1211-
result = git_filebuf_commit(&file);
1201+
1202+
if ((result = git_filebuf_commit(&file)) < 0)
1203+
goto done;
1204+
1205+
if ((result = config_refresh(&cfg->header.parent)) < 0)
1206+
goto done;
12121207
}
12131208

12141209
done:

0 commit comments

Comments
 (0)