Skip to main content

Module paths

Module paths 

Source
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_bytes taking 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_path expressed relative to the directory from_dir, both expected to be absolute, returning a PathBuf.
relative_path_bytes
Render input relative to prefix, a faithful byte-level port of git’s relative_path() (path.c) for the POSIX, both-relative case (no DOS drive). prefix is the cwd prefix and must end with / when non-empty, matching git’s cmd_prefix. Emits ../ for each prefix component not shared with input, then the unshared tail of input.
relative_path_from_absolute
Compute target expressed relative to the directory cwd, both expected to be absolute. Like git’s relative_path() common-ancestor case: emits ../ per unshared cwd component, then the unshared tail of target, with a trailing / when the result names cwd itself.
relative_path_from_absolute_components
Component-math core of relative_path_from_absolute with no filesystem access: callers that already hold canonicalized/real paths use this directly.
relative_path_lexical
Compute target expressed relative to base as a slash-separated string, purely lexically (both sides normalized with normalize_lexical, no filesystem access). Returns "." when the two paths coincide.