Skip to content

repository: compare safe.directory case-insensitively on Windows - #7353

Open
tyrielv wants to merge 1 commit into
libgit2:mainfrom
tyrielv:tyrielv/safe-directory-drive-case
Open

tyrielv wants to merge 1 commit into
libgit2:mainfrom
tyrielv:tyrielv/safe-directory-drive-case

Conversation

@tyrielv

@tyrielv tyrielv commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7037

Problem

On Windows, opening a repository whose ownership check relies on a safe.directory allowlist entry fails with GIT_EOWNER when the entry differs in case from the repository path -- in practice, the drive letter (c: vs C:). git.exe accepts the same entry.

Root cause

validate_ownership_cb compares the configured safe.directory entry against the repository path with a case-sensitive strcmp/prefix compare.

Git does not have a case rule here at all: ensure_valid_ownership (setup.c) canonicalizes both the repository path and each entry to their real on-disk form via real_pathdup (which, on Windows, resolves through GetFinalPathNameByHandle and so normalizes drive-letter case), then compares. safe.directory is read from protected (system/global) config only, so the repo's core.ignorecase never enters the decision -- the match comes from canonicalization, not case-folding.

libgit2 already resolves the repository path with git_fs_path_prettify (p_realpath), but on Windows that uses GetLongPathName, which corrects path-component case but not the drive letter -- and the entry is not canonicalized at all. Hence the drive-letter-only mismatch.

Fix

Compare the configured entry against the repository path case-insensitively on Windows; POSIX filesystems stay case-sensitive. This is a lightweight approximation of git's real_pathdup canonicalization -- it resolves the reported drive-letter symptom (and tolerates component-case variants, as git does), without re-implementing full real-path resolution of the entry.

Known divergence (low risk): on a case-sensitive Windows volume/dir, git would compare canonicalized paths case-sensitively, whereas this compare always folds case. It can only ever make an explicitly allowlisted path match a case variant of itself.

Tests

Adds two Windows-only tests in repo::open:

  • allowlist entry with the opposite drive-letter case
  • allowlist entry with the entire path in a different case

The full test suite passes.

On Windows, Git canonicalizes both the repository path and each
`safe.directory` allowlist entry to their real on-disk form before
comparing them, so the comparison is effectively case-insensitive over
the whole path, including the drive letter. libgit2 compared the paths
with a case-sensitive `strcmp`, so an entry like `c:/repo` did not
match a repository recorded as `C:/repo` (or `C:/Repo`), and the
repository failed to open with a spurious ownership error.

Compare the configured entry against the repository path
case-insensitively on Windows, matching Git. POSIX filesystems are
case-sensitive, so the comparison is unchanged there.

Add Windows-only tests that open a repository allowlisted with the
opposite drive-letter case, and with the entire path in a different
case.

Assisted-by: Claude Opus 4.8
Signed-off-by: Tyrie Vella <[email protected]>
@tyrielv tyrielv changed the title Crepository: compare safe.directory case-insensitively on Windows repository: compare safe.directory case-insensitively on Windows Aug 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

safe.directory interpretation is case-sensitive on Windows for the drive letter, when it is not for Git for Windows.

1 participant