Commit f7d837c835a55a98a85d10f2e600a7a5ae2281c6

Patrick Steinhardt 2017-05-24T12:12:29

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.

diff --git a/src/config_file.c b/src/config_file.c
index ba520fc..ffcdca7 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1612,11 +1612,12 @@ static int parse_include(struct reader *reader,
 	return result;
 }
 
-static int conditional_match_gitdir(
+static int do_match_gitdir(
 	int *matches,
 	const git_repository *repo,
 	const char *cfg_file,
-	const char *value)
+	const char *value,
+	bool case_insensitive)
 {
 	git_buf path = GIT_BUF_INIT;
 	int error, fnmatch_flags;
@@ -1640,9 +1641,10 @@ static int conditional_match_gitdir(
 		git_buf_puts(&path, "**");
 
 	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)
-
 		goto out;
 
 	*matches = (error == 0);
@@ -1652,11 +1654,30 @@ out:
 	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 {
 	const char *prefix;
 	int (*matches)(int *matches, const git_repository *repo, const char *cfg, const char *value);
 } conditions[] = {
-	{ "gitdir:", conditional_match_gitdir }
+	{ "gitdir:", conditional_match_gitdir },
+	{ "gitdir/i:", conditional_match_gitdir_i }
 };
 
 static int parse_conditional_include(struct reader *reader,
diff --git a/tests/config/conditionals.c b/tests/config/conditionals.c
index 323bbae..83dd45c 100644
--- a/tests/config/conditionals.c
+++ b/tests/config/conditionals.c
@@ -76,6 +76,19 @@ void test_config_conditionals__gitdir(void)
 	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)
 {
 	assert_condition_includes("foobar", ".git", false);