Skip to content

Commit fe4472a

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 685d706 commit fe4472a

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
@@ -2056,6 +2056,20 @@ int git_config_key_is_valid(const char *key)
20562056
return !git_config_parse_key_1(key, NULL, NULL, 1);
20572057
}
20582058

2059+
static int lock_config_file(const char *config_filename,
2060+
struct lock_file **result)
2061+
{
2062+
int fd;
2063+
2064+
*result = xcalloc(1, sizeof(struct lock_file));
2065+
fd = hold_lock_file_for_update(*result, config_filename, 0);
2066+
if (fd < 0)
2067+
error("could not lock config file %s: %s", config_filename,
2068+
strerror(errno));
2069+
2070+
return fd;
2071+
}
2072+
20592073
/*
20602074
* If value==NULL, unset in (remove from) config,
20612075
* if value_regex!=NULL, disregard key/value pairs where value does not match.
@@ -2107,8 +2121,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
21072121
* The lock serves a purpose in addition to locking: the new
21082122
* contents of .git/config will be written into it.
21092123
*/
2110-
lock = xcalloc(1, sizeof(struct lock_file));
2111-
fd = hold_lock_file_for_update(lock, config_filename, 0);
2124+
fd = lock_config_file(config_filename, &lock);
21122125
if (fd < 0) {
21132126
error_errno("could not lock config file %s", config_filename);
21142127
free(store.key);
@@ -2409,12 +2422,9 @@ int git_config_rename_section_in_file(const char *config_filename,
24092422
if (!config_filename)
24102423
config_filename = filename_buf = git_pathdup("config");
24112424

2412-
lock = xcalloc(1, sizeof(struct lock_file));
2413-
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
2414-
if (out_fd < 0) {
2415-
ret = error("could not lock config file %s", config_filename);
2425+
out_fd = lock_config_file(config_filename, &lock);
2426+
if (out_fd < 0)
24162427
goto out;
2417-
}
24182428

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

0 commit comments

Comments
 (0)