Skip to content

fix(wsl): enumerate concrete repo paths for git safe.directory - #249

Closed
szymonos wants to merge 1 commit into
mainfrom
fix/safe-directory-enumeration
Closed

fix(wsl): enumerate concrete repo paths for git safe.directory#249
szymonos wants to merge 1 commit into
mainfrom
fix/safe-directory-enumeration

Conversation

@szymonos

@szymonos szymonos commented May 2, 2026

Copy link
Copy Markdown
Owner

Summary

Follow-up on review feedback on #246. The `safe.directory` block added there used shell-style globs (`/mnt/c/.../source/repos/` and `/* `), but git's `safe.directory` does not honor shell globs — it matches literal paths only. The single special value `*` means "trust everything," which is too broad. Result: the entries appeared in `~/.gitconfig` but did NOT actually suppress the "dubious ownership" warning on Windows-mount repos.

Approach (per @szymonos's suggestion)

Enumerate concrete repo directories on the Windows host rather than calling into WSL — the host-side enumeration is fast (no per-directory WSL/Windows filesystem roundtrips), and `safe.directory` needs literal paths anyway:

  1. `Get-ChildItem -Directory` two levels deep under `$env:USERPROFILE\source\repos` (covers flat `repos/` and org-scoped `repos//` layouts)
  2. Filter to dirs that contain a `.git` subdir — keeps the entry list bounded to actual repos
  3. Translate each Windows path to its `/mnt/c/...` form (drive letter detected from the actual path, not hard-coded `c`)
  4. Pass all paths in a single `wsl.exe --exec bash -c` invocation that adds any missing entries

Other improvements bundled

  • `$env:USERPROFILE` instead of `/mnt/c/Users/$env:USERNAME` so the home dir resolves correctly when the Windows username contains `.` or non-ASCII (common in corporate AD setups where `$env:USERNAME` is `firstname.lastname.N` but the home dir might be a truncated/munged variant)
  • Drive letter detected from the path — works for source trees on `D:\` etc.
  • Single bash invocation instead of one per glob entry

Test plan

  • `pwsh` parse-check
  • `make lint` clean
  • Manual on Windows host: run `wsl/wsl_setup.ps1 `, verify `git config --global --get-all safe.directory` inside the distro lists the concrete repo paths, and `git -C /mnt/c/Users//source/repos// status` no longer prints the "dubious ownership" warning

🤖 Generated with Claude Code

git's safe.directory does NOT honor shell globs - it matches literal
paths only (the single special value `*` means "trust everything",
which is too broad). The previous code added `/mnt/c/.../source/repos/*`
and `/mnt/c/.../source/repos/*/*` as entries, which git treats as the
literal strings "<...>/*" and never matches against real repo paths.
Result: the "dubious ownership" warning was NOT actually suppressed
on Windows-mount repos, despite the entries appearing in ~/.gitconfig.

Fix: enumerate concrete repo directories on the Windows host using
Get-ChildItem (fast - no WSL/host filesystem roundtrips per dir),
filter to dirs that contain a .git subdir, translate each to its
/mnt/c/... form, and pass them as separate safe.directory entries
in a single bash invocation.

Other improvements:
- $env:USERPROFILE instead of /mnt/c/Users/$env:USERNAME so the
  Windows home dir is resolved correctly (Windows usernames don't
  always match the home dir name when they contain special chars
  like '.' or non-ASCII).
- Drive letter detected from the actual repo path rather than
  hard-coded as 'c' - works if you keep your source tree on D:\.
- Single bash invocation for all paths, idempotency check via
  grep -qFx against the existing list.

Reported in code review of #246.
@szymonos szymonos closed this Jun 14, 2026
@szymonos
szymonos deleted the fix/safe-directory-enumeration branch June 14, 2026 13:24
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.

1 participant