Expand description
Canonical shared path helpers (phase-2 consolidation).
Single home for:
- the faithful port of git’s
relative_path()(path.c) over raw bytes, - lexical (no-filesystem) normalization and absolute↔relative computation,
- bytes↔path conversions matching git’s byte-oriented path handling.
Everything here is pure string/component math unless documented otherwise;
only relative_path_from_absolute touches the filesystem.
Functions§
- bytes_
to_ os_ path - Interpret raw path bytes as a (relative)
PathBuf. On Unix the bytes are the OS-native path encoding; off Unix they are decoded lossily as UTF-8. - bytes_
to_ path_ string - Interpret raw path bytes as a UTF-8
String, failing rather than guessing: git stores paths as opaque bytes, and callers that must echo them back into string-shaped data structures refuse non-UTF-8 input explicitly. - normalize_
lexical - Lexically normalize a path (collapse
./.., no filesystem access). - os_
str_ to_ bytes - A path/
OsStr’s byte encoding. On Unix these are the OS-native bytes; off Unix they are decoded lossily as UTF-8 with\normalized to/, matching git’s forward-slash path convention. - path_
to_ bytes - Convenience wrapper over
os_str_to_bytestaking a whole path. - path_
to_ slash - Render a path with forward slashes (git uses
/in trace prefixes and on-wire path fields): the join of its normal components, dropping any root. - relative_
path_ between - Compute
to_pathexpressed relative to the directoryfrom_dir, both expected to be absolute, returning aPathBuf. - relative_
path_ bytes - Render
inputrelative toprefix, a faithful byte-level port of git’srelative_path()(path.c) for the POSIX, both-relative case (no DOS drive).prefixis the cwd prefix and must end with/when non-empty, matching git’scmd_prefix. Emits../for eachprefixcomponent not shared withinput, then the unshared tail ofinput. - relative_
path_ from_ absolute - Compute
targetexpressed relative to the directorycwd, both expected to be absolute. Like git’srelative_path()common-ancestor case: emits../per unsharedcwdcomponent, then the unshared tail oftarget, with a trailing/when the result namescwditself. - relative_
path_ from_ absolute_ components - Component-math core of
relative_path_from_absolutewith no filesystem access: callers that already hold canonicalized/real paths use this directly. - relative_
path_ lexical - Compute
targetexpressed relative tobaseas a slash-separated string, purely lexically (both sides normalized withnormalize_lexical, no filesystem access). Returns"."when the two paths coincide.