view rust/hg-pyo3/src/repo.rs @ 52984:a39680ec3e76

pyo3: add a `repo` module This will host all repo-related operations. For now, this only contains the transliteration of the `repo_from_path` util from `hg-cpython`, but will later probably grow.
author Rapha?l Gom?s <rgomes@octobus.net>
date Tue, 18 Feb 2025 11:45:50 +0100
parents
children
line wrap: on
line source

use hg::{config::Config, repo::Repo, utils::files::get_path_from_bytes};
use pyo3::{
    types::{PyBytes, PyBytesMethods},
    Bound, PyResult,
};

use crate::utils::HgPyErrExt;

/// Get a repository from a given [`PyObject`] path, and bubble up any error
/// that comes up.
pub fn repo_from_path(repo_path: &Bound<'_, PyBytes>) -> PyResult<Repo> {
    // TODO make the Config a Python class and downcast it here, otherwise we
    // lose CLI args and runtime overrides done in Python.
    let config = Config::load_non_repo().into_pyerr(repo_path.py())?;
    let repo = Repo::find(
        &config,
        Some(get_path_from_bytes(repo_path.as_bytes()).to_owned()),
    )
    .into_pyerr(repo_path.py())?;
    Ok(repo)
}