Skip to content

Commit 29aef94

Browse files
committed
config, attrcache: don't fallback to dirs literally named ~
The config and attrcache file reading code would attempt to load a file in a home directory by expanding the `~` and looking for the file, using `git_sysdir_find_global_file`. If the file was not found, the error handling would look for the literal path, eg `~/filename.txt`. Use the new `git_config_expand_global_file` instead, which allows us to get the path to the file separately, when the path is prefixed with `~/`, and fail with a not found error without falling back to looking for the literal path.
1 parent 5135dda commit 29aef94

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

src/attrcache.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,16 @@ static int attr_cache__lookup_path(
290290
const char *cfgval = entry->value;
291291

292292
/* expand leading ~/ as needed */
293-
if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
294-
!git_sysdir_find_global_file(&buf, &cfgval[2]))
295-
*out = git_buf_detach(&buf);
296-
else if (cfgval)
293+
if (cfgval && cfgval[0] == '~' && cfgval[1] == '/') {
294+
if (! (error = git_sysdir_expand_global_file(&buf, &cfgval[2])))
295+
*out = git_buf_detach(&buf);
296+
} else if (cfgval) {
297297
*out = git__strdup(cfgval);
298+
}
298299
}
299-
else if (!git_sysdir_find_xdg_file(&buf, fallback))
300+
else if (!git_sysdir_find_xdg_file(&buf, fallback)) {
300301
*out = git_buf_detach(&buf);
302+
}
301303

302304
git_config_entry_free(entry);
303305
git_buf_free(&buf);

src/config_file.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,16 +1254,8 @@ static int strip_comments(char *line, int in_quotes)
12541254
static int included_path(git_buf *out, const char *dir, const char *path)
12551255
{
12561256
/* From the user's home */
1257-
int result;
1258-
if (path[0] == '~' && path[1] == '/') {
1259-
result = git_sysdir_find_global_file(out, &path[1]);
1260-
if (result == GIT_ENOTFOUND) {
1261-
git_buf_sets(out, &path[1]);
1262-
return 0;
1263-
}
1264-
1265-
return result;
1266-
}
1257+
if (path[0] == '~' && path[1] == '/')
1258+
return git_sysdir_expand_global_file(out, &path[1]);
12671259

12681260
return git_path_join_unrooted(out, path, dir, NULL);
12691261
}

0 commit comments

Comments
 (0)