Skip to content

Commit 5811e3b

Browse files
committed
config_file: use wildmatch to evaluate conditionals
We currently use `p_fnmatch` to compute whether a given "gitdir:" or "gitdir/i:" conditional matches the current configuration file path. As git.git has moved to use `wildmatch` instead of `p_fnmatch` throughout its complete codebase, we evaluate conditionals inconsistently with git.git in some special cases. Convert `p_fnmatch` to use `wildmatch`. The `FNM_LEADINGDIR` flag cannot be translated to `wildmatch`, but in fact git.git doesn't use it here either. And in fact, dropping it while we go increases compatibility with git.git.
1 parent cf1a114 commit 5811e3b

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/config_file.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "array.h"
1919
#include "config_parse.h"
2020
#include "config_entries.h"
21-
#include "fnmatch.h"
21+
#include "wildmatch.h"
2222

2323
#include <ctype.h>
2424
#include <sys/types.h>
@@ -702,7 +702,7 @@ static int do_match_gitdir(
702702
bool case_insensitive)
703703
{
704704
git_buf pattern = GIT_BUF_INIT, gitdir = GIT_BUF_INIT;
705-
int error, fnmatch_flags;
705+
int error;
706706

707707
if (condition[0] == '.' && git_path_is_dirsep(condition[1])) {
708708
git_path_dirname_r(&pattern, cfg_file);
@@ -728,15 +728,8 @@ static int do_match_gitdir(
728728
if (git_path_is_dirsep(gitdir.ptr[gitdir.size - 1]))
729729
git_buf_truncate(&gitdir, gitdir.size - 1);
730730

731-
fnmatch_flags = FNM_PATHNAME|FNM_LEADING_DIR;
732-
if (case_insensitive)
733-
fnmatch_flags |= FNM_IGNORECASE;
734-
735-
if ((error = p_fnmatch(pattern.ptr, gitdir.ptr, fnmatch_flags)) < 0)
736-
goto out;
737-
738-
*matches = (error == 0);
739-
731+
*matches = wildmatch(pattern.ptr, gitdir.ptr,
732+
WM_PATHNAME | (case_insensitive ? WM_CASEFOLD : 0)) == WM_MATCH;
740733
out:
741734
git_buf_dispose(&pattern);
742735
git_buf_dispose(&gitdir);

tests/config/conditionals.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ void test_config_conditionals__gitdir(void)
6868
assert_condition_includes("gitdir", "empty_stand*/", true);
6969
assert_condition_includes("gitdir", "empty_stand*/.git", true);
7070
assert_condition_includes("gitdir", "empty_stand*/.git/", false);
71+
assert_condition_includes("gitdir", "empty_standard_repo", false);
7172
assert_condition_includes("gitdir", "empty_standard_repo/", true);
7273
assert_condition_includes("gitdir", "empty_standard_repo/.git", true);
7374
assert_condition_includes("gitdir", "empty_standard_repo/.git/", false);
@@ -79,8 +80,10 @@ void test_config_conditionals__gitdir(void)
7980
assert_condition_includes("gitdir", "~/empty_standard_repo", false);
8081

8182
assert_condition_includes("gitdir", sandbox_path(&path, "/"), true);
83+
assert_condition_includes("gitdir", sandbox_path(&path, "/*"), false);
8284
assert_condition_includes("gitdir", sandbox_path(&path, "/**"), true);
8385

86+
assert_condition_includes("gitdir", sandbox_path(&path, "empty_standard_repo"), false);
8487
assert_condition_includes("gitdir", sandbox_path(&path, "empty_standard_repo/"), true);
8588
assert_condition_includes("gitdir", sandbox_path(&path, "empty_standard_repo/"), true);
8689
assert_condition_includes("gitdir", sandbox_path(&path, "Empty_Standard_Repo"), false);

0 commit comments

Comments
 (0)