annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52984
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
1 use hg::{config::Config, repo::Repo, utils::files::get_path_from_bytes};
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
2 use pyo3::{
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
3 types::{PyBytes, PyBytesMethods},
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
4 Bound, PyResult,
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
5 };
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
6
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
7 use crate::utils::HgPyErrExt;
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
8
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
9 /// Get a repository from a given [`PyObject`] path, and bubble up any error
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
10 /// that comes up.
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
11 pub fn repo_from_path(repo_path: &Bound<'_, PyBytes>) -> PyResult<Repo> {
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
12 // TODO make the Config a Python class and downcast it here, otherwise we
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
13 // lose CLI args and runtime overrides done in Python.
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
14 let config = Config::load_non_repo().into_pyerr(repo_path.py())?;
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
15 let repo = Repo::find(
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
16 &config,
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
17 Some(get_path_from_bytes(repo_path.as_bytes()).to_owned()),
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
18 )
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
19 .into_pyerr(repo_path.py())?;
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
20 Ok(repo)
a39680ec3e76 pyo3: add a `repo` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
21 }