#version-control #cli #path #single #bstr


A crate of the gitoxide project dealing paths and their conversions

10 unstable releases (3 breaking)

0.10.0 Sep 8, 2023
0.9.0 Aug 22, 2023
0.8.4 Jul 22, 2023
0.8.3 Jun 29, 2023
0.7.2 Feb 20, 2023

#825 in Configuration

Download history 79542/week @ 2023-08-04 75597/week @ 2023-08-11 84732/week @ 2023-08-18 91656/week @ 2023-08-25 109270/week @ 2023-09-01 123700/week @ 2023-09-08 122328/week @ 2023-09-15 130992/week @ 2023-09-22 128619/week @ 2023-09-29 138986/week @ 2023-10-06 133149/week @ 2023-10-13 125715/week @ 2023-10-20 129921/week @ 2023-10-27 130150/week @ 2023-11-03 132470/week @ 2023-11-10 93430/week @ 2023-11-17

507,330 downloads per month
Used in 169 crates (21 directly)


1.5K SLoC

This crate contains an assortment of utilities to deal with paths and their conversions.

Generally git treats paths as bytes, but inherently assumes non-illformed UTF-8 as encoding on windows. Internally, it expects slashes to be used as path separators and paths in files must have slashes, with conversions being performed on windows accordingly.


  • windows
  • dirent.c contains all implementation (seemingly) of opening directories and reading their entries, along with all path conversions (UTF-16 for windows). This is done on the fly so git can work with in UTF-8.
  • mingw is used for the conversion and it appears they handle surrogates during the conversion, maybe some sort of non-strict UTF-8 converter? Actually it uses WideCharToMultiByte under the hood which by now does fail if the UTF-8 would be invalid unicode, i.e. unicode pairs.
  • OsString on windows already stores strings as WTF-8, which supports surrogate pairs, something that UTF-8 isn't allowed do it for security reasons, after all it's UTF-16 specific and exists only to extend the encodable code-points.
  • informative reading on WTF-8 which is the encoding used by Rust internally that deals with surrogates and non-wellformed surrogates (those that aren't in pairs).
  • unix
  • It uses opendir and readdir respectively. There is no encoding specified, except that these paths are null-terminated.


Surrogate pairs are a way to extend the encodable value range in UTF-16 encodings, used primarily on windows and in Javascript. For a long time these codepoints used for surrogates, always to be used in pairs, were not assigned, until…they were for rare emojies and the likes. The unicode standard does not require surrogates to happen in pairs, even though by now unpaired surrogates in UTF-16 are considered ill-formed, which aren't supposed to be converted to UTF-8 for example.

This is the reason we have to deal with to_string_lossy(), it's just for that quirk.

This also means the only platform ever eligible to see conversion errors is windows, and there it's only older pre-vista windows versions which incorrectly allow ill-formed UTF-16 strings. Newer versions don't perform such conversions anymore, for example when going from UTF-16 to UTF-8, they will trigger an error.


Since WideCharToMultiByte by now is fixed (Vista onward) to produce valid UTF-8, lone surrogate codepoints will cause failure, which git doesn't care about.

We will, though, which means from now on we can just convert to UTF-8 on windows and bubble up errors where necessary, preventing potential mismatched surrogate pairs to ever be saved on disk by gitoxide.

Even though the error only exists on older windows versions, we will represent it in the type system through fallible function calls. Callers may .expect() on the result to indicate they don't wish to handle this special and rare case. Note that servers should not ever get into a code-path which does panic though.


~76K SLoC