Skip to content

Commit e44110d

Browse files
committed
Correctly write to missing locked global config
Opening a default config when ~/.gitconfig doesn't exist, locking it, and attempting to write to it causes an assertion failure. Treat non-existent global config file content as an empty string.
1 parent bc5b19e commit e44110d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/config_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
11331133
reader.file = &cfg->file;
11341134

11351135
if (cfg->locked) {
1136-
result = git_buf_puts(&contents, git_buf_cstr(&cfg->locked_content));
1136+
result = git_buf_puts(&contents, git_buf_cstr(&cfg->locked_content) == NULL ? "" : git_buf_cstr(&cfg->locked_content));
11371137
} else {
11381138
/* Lock the file */
11391139
if ((result = git_filebuf_open(

tests/config/global.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,36 @@ void test_config_global__open_symlinked_global(void)
7575
#endif
7676
}
7777

78+
void test_config_global__lock_missing_global_config(void)
79+
{
80+
git_config *cfg;
81+
git_config_entry *entry;
82+
git_transaction *transaction;
83+
84+
p_unlink("home/.gitconfig"); /* No global config */
85+
86+
cl_git_pass(git_config_open_default(&cfg));
87+
cl_git_pass(git_config_lock(&transaction, cfg));
88+
cl_git_pass(git_config_set_string(cfg, "assertion.fail", "boom"));
89+
cl_git_pass(git_transaction_commit(transaction));
90+
git_transaction_free(transaction);
91+
92+
/* cfg is updated */
93+
cl_git_pass(git_config_get_entry(&entry, cfg, "assertion.fail"));
94+
cl_assert_equal_s("boom", entry->value);
95+
96+
git_config_entry_free(entry);
97+
git_config_free(cfg);
98+
99+
/* We can reread the new value from the global config */
100+
cl_git_pass(git_config_open_default(&cfg));
101+
cl_git_pass(git_config_get_entry(&entry, cfg, "assertion.fail"));
102+
cl_assert_equal_s("boom", entry->value);
103+
104+
git_config_entry_free(entry);
105+
git_config_free(cfg);
106+
}
107+
78108
void test_config_global__open_xdg(void)
79109
{
80110
git_config *cfg, *xdg, *selected;

0 commit comments

Comments
 (0)