Commit 5d987f7d by Patrick Steinhardt

config_file: refactor `do_match_gitdir` to improve readability

The function `do_match_gitdir` has some horribly named parameters
and variables. Rename them to improve readability. Furthermore,
fix a potentially undetected out-of-memory condition when
appending "**" to the pattern.
parent de70bb46
...@@ -698,41 +698,41 @@ static int do_match_gitdir( ...@@ -698,41 +698,41 @@ static int do_match_gitdir(
int *matches, int *matches,
const git_repository *repo, const git_repository *repo,
const char *cfg_file, const char *cfg_file,
const char *value, const char *condition,
bool case_insensitive) bool case_insensitive)
{ {
git_buf path = GIT_BUF_INIT; git_buf pattern = GIT_BUF_INIT;
int error, fnmatch_flags; int error, fnmatch_flags;
if (value[0] == '.' && git_path_is_dirsep(value[1])) { if (condition[0] == '.' && git_path_is_dirsep(condition[1])) {
git_path_dirname_r(&path, cfg_file); git_path_dirname_r(&pattern, cfg_file);
git_buf_joinpath(&path, path.ptr, value + 2); git_buf_joinpath(&pattern, pattern.ptr, condition + 2);
} else if (value[0] == '~' && git_path_is_dirsep(value[1])) } else if (condition[0] == '~' && git_path_is_dirsep(condition[1]))
git_sysdir_expand_global_file(&path, value + 1); git_sysdir_expand_global_file(&pattern, condition + 1);
else if (!git_path_is_absolute(value)) else if (!git_path_is_absolute(condition))
git_buf_joinpath(&path, "**", value); git_buf_joinpath(&pattern, "**", condition);
else else
git_buf_sets(&path, value); git_buf_sets(&pattern, condition);
if (git_path_is_dirsep(condition[strlen(condition) - 1]))
git_buf_puts(&pattern, "**");
if (git_buf_oom(&path)) { if (git_buf_oom(&pattern)) {
error = -1; error = -1;
goto out; goto out;
} }
if (git_path_is_dirsep(value[strlen(value) - 1]))
git_buf_puts(&path, "**");
fnmatch_flags = FNM_PATHNAME|FNM_LEADING_DIR; fnmatch_flags = FNM_PATHNAME|FNM_LEADING_DIR;
if (case_insensitive) if (case_insensitive)
fnmatch_flags |= FNM_IGNORECASE; fnmatch_flags |= FNM_IGNORECASE;
if ((error = p_fnmatch(path.ptr, git_repository_path(repo), fnmatch_flags)) < 0) if ((error = p_fnmatch(pattern.ptr, git_repository_path(repo), fnmatch_flags)) < 0)
goto out; goto out;
*matches = (error == 0); *matches = (error == 0);
out: out:
git_buf_dispose(&path); git_buf_dispose(&pattern);
return error; return error;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment