Commit f7d837c8 by Patrick Steinhardt

config_file: implement "gitdir/i" conditional

Next to the "gitdir" conditional for including other configuration
files, there's also a "gitdir/i" conditional. In contrast to the former
one, path matching with "gitdir/i" is done case-insensitively. This
commit implements the case-insensitive condition.
parent 071b6c06
...@@ -1612,11 +1612,12 @@ static int parse_include(struct reader *reader, ...@@ -1612,11 +1612,12 @@ static int parse_include(struct reader *reader,
return result; return result;
} }
static int conditional_match_gitdir( 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 *value,
bool case_insensitive)
{ {
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
int error, fnmatch_flags; int error, fnmatch_flags;
...@@ -1640,9 +1641,10 @@ static int conditional_match_gitdir( ...@@ -1640,9 +1641,10 @@ static int conditional_match_gitdir(
git_buf_puts(&path, "**"); git_buf_puts(&path, "**");
fnmatch_flags = FNM_PATHNAME|FNM_LEADING_DIR; fnmatch_flags = FNM_PATHNAME|FNM_LEADING_DIR;
if (case_insensitive)
fnmatch_flags |= FNM_IGNORECASE;
if ((error = p_fnmatch(path.ptr, git_repository_path(repo), fnmatch_flags)) < 0) if ((error = p_fnmatch(path.ptr, git_repository_path(repo), fnmatch_flags)) < 0)
goto out; goto out;
*matches = (error == 0); *matches = (error == 0);
...@@ -1652,11 +1654,30 @@ out: ...@@ -1652,11 +1654,30 @@ out:
return error; return error;
} }
static int conditional_match_gitdir(
int *matches,
const git_repository *repo,
const char *cfg_file,
const char *value)
{
return do_match_gitdir(matches, repo, cfg_file, value, false);
}
static int conditional_match_gitdir_i(
int *matches,
const git_repository *repo,
const char *cfg_file,
const char *value)
{
return do_match_gitdir(matches, repo, cfg_file, value, true);
}
static const struct { static const struct {
const char *prefix; const char *prefix;
int (*matches)(int *matches, const git_repository *repo, const char *cfg, const char *value); int (*matches)(int *matches, const git_repository *repo, const char *cfg, const char *value);
} conditions[] = { } conditions[] = {
{ "gitdir:", conditional_match_gitdir } { "gitdir:", conditional_match_gitdir },
{ "gitdir/i:", conditional_match_gitdir_i }
}; };
static int parse_conditional_include(struct reader *reader, static int parse_conditional_include(struct reader *reader,
......
...@@ -76,6 +76,19 @@ void test_config_conditionals__gitdir(void) ...@@ -76,6 +76,19 @@ void test_config_conditionals__gitdir(void)
git_buf_free(&path); git_buf_free(&path);
} }
void test_config_conditionals__gitdir_i(void)
{
git_buf path = GIT_BUF_INIT;
git_buf_joinpath(&path, clar_sandbox_path(), "empty_standard_repo");
assert_condition_includes("gitdir/i", path.ptr, true);
git_buf_joinpath(&path, clar_sandbox_path(), "EMPTY_STANDARD_REPO");
assert_condition_includes("gitdir/i", path.ptr, true);
git_buf_free(&path);
}
void test_config_conditionals__invalid_conditional_fails(void) void test_config_conditionals__invalid_conditional_fails(void)
{ {
assert_condition_includes("foobar", ".git", false); assert_condition_includes("foobar", ".git", false);
......
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