Skip to main content

repolith_actions/
paths.rs

1//! Path utilities shared by every action and by the manifest factory.
2//!
3//! Currently exposes a single helper, `expand_tilde`, because the rest
4//! of the codebase relies on `std::path::PathBuf` directly.
5
6use std::path::{Path, PathBuf};
7
8/// Expand a leading `~/` or bare `~` in `p` to [`dirs::home_dir`].
9///
10/// Other paths (absolute without `~`, relative, anything starting with
11/// `~user/`) are returned unchanged.
12///
13/// Neither `cargo` nor `git` performs this expansion themselves, so a
14/// manifest that writes `install_to = "~/.local/bin"` or
15/// `path = "~/code/foo"` would otherwise create a literal `~` directory
16/// next to the orchestrator's cwd. Apply this helper before passing
17/// user-supplied paths to subprocesses.
18#[must_use]
19pub fn expand_tilde(p: &Path) -> PathBuf {
20    let s = p.to_string_lossy();
21    if let Some(rest) = s.strip_prefix("~/") {
22        if let Some(home) = dirs::home_dir() {
23            return home.join(rest);
24        }
25    } else if s == "~"
26        && let Some(home) = dirs::home_dir()
27    {
28        return home;
29    }
30    p.to_path_buf()
31}