Skip to content

Commit 4aa2aae

Browse files
kbleesdscho
authored andcommitted
config.c: create missing parent directories when modifying config files
'git config' (--add / --unset etc.) automatically creates missing config files. However, it fails with a misleading error message "could not lock config file" if the parent directory doesn't exist. Also create missing parent directories. This is particularly important when calling git config -f /non/existing/directory/config ... We *must not* create the leading directories when locking a config file. It is the caller's responsibility to ensure that the directory exists, just like it is the caller's responsibility to call `git init` before running repository operations. Point in case: if we simply create all leading directories, calling `git config user.name me` *outside* of a Git worktree will *create* .git/! This fixes #643 and https://groups.google.com/forum/#!topic/git-for-windows/fVRdnDIKVuw [jes: prevented bogus .git/ directories from being created] Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 00b556c commit 4aa2aae

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

config.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,20 @@ int git_config_key_is_valid(const char *key)
20762076
return !git_config_parse_key_1(key, NULL, NULL, 1);
20772077
}
20782078

2079+
static int lock_config_file(const char *config_filename,
2080+
struct lock_file **result)
2081+
{
2082+
int fd;
2083+
2084+
*result = xcalloc(1, sizeof(struct lock_file));
2085+
fd = hold_lock_file_for_update(*result, config_filename, 0);
2086+
if (fd < 0)
2087+
error("could not lock config file %s: %s", config_filename,
2088+
strerror(errno));
2089+
2090+
return fd;
2091+
}
2092+
20792093
/*
20802094
* If value==NULL, unset in (remove from) config,
20812095
* if value_regex!=NULL, disregard key/value pairs where value does not match.
@@ -2127,8 +2141,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
21272141
* The lock serves a purpose in addition to locking: the new
21282142
* contents of .git/config will be written into it.
21292143
*/
2130-
lock = xcalloc(1, sizeof(struct lock_file));
2131-
fd = hold_lock_file_for_update(lock, config_filename, 0);
2144+
fd = lock_config_file(config_filename, &lock);
21322145
if (fd < 0) {
21332146
error_errno("could not lock config file %s", config_filename);
21342147
free(store.key);
@@ -2429,12 +2442,9 @@ int git_config_rename_section_in_file(const char *config_filename,
24292442
if (!config_filename)
24302443
config_filename = filename_buf = git_pathdup("config");
24312444

2432-
lock = xcalloc(1, sizeof(struct lock_file));
2433-
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
2434-
if (out_fd < 0) {
2435-
ret = error("could not lock config file %s", config_filename);
2445+
out_fd = lock_config_file(config_filename, &lock);
2446+
if (out_fd < 0)
24362447
goto out;
2437-
}
24382448

24392449
if (!(config_file = fopen(config_filename, "rb"))) {
24402450
/* no config file means nothing to rename, no error */

0 commit comments

Comments
 (0)