rust/hg-pyo3/src/update.rs
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 11 Mar 2025 02:29:42 +0100
branchstable
changeset 53042 cdd7bf612c7b
parent 52980 a60d1eb74dc6
permissions -rw-r--r--
bundle-spec: properly format boolean parameter (issue6960) This was breaking automatic clone bundle generation. This changeset fixes it and add a test to catch it in the future.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52980
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     1
// update.rs
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     2
//
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     3
// Copyright 2025 Mercurial developers
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     4
//
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     7
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     8
//! Bindings for the `hg::update` module provided by the
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     9
//! `hg-core` package.
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    10
//!
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    11
//! From Python, this will be seen as `mercurial.pyo3_rustext.update`
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    12
use pyo3::prelude::*;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    13
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    14
use hg::progress::{HgProgressBar, Progress};
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    15
use hg::update::update_from_null as core_update_from_null;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    16
use hg::BaseRevision;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    17
use pyo3::types::PyBytes;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    18
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    19
use crate::exceptions::FallbackError;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    20
use crate::repo::repo_from_path;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    21
use crate::utils::{new_submodule, with_sigint_wrapper, HgPyErrExt};
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    22
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    23
/// See [`core_update_from_null`].
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    24
#[pyfunction]
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    25
#[pyo3(signature = (repo_path, to, num_cpus))]
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    26
pub fn update_from_null(
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    27
    repo_path: &Bound<'_, PyBytes>,
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    28
    to: BaseRevision,
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    29
    num_cpus: Option<usize>,
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    30
) -> PyResult<usize> {
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    31
    log::trace!("Using update from null fastpath");
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    32
    let repo = repo_from_path(repo_path)?;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    33
    let progress: &dyn Progress = &HgProgressBar::new("updating");
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    34
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    35
    with_sigint_wrapper(repo_path.py(), || {
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    36
        core_update_from_null(&repo, to.into(), progress, num_cpus)
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    37
    })?
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    38
    .into_pyerr(repo_path.py())
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    39
}
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    40
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    41
pub fn init_module<'py>(
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    42
    py: Python<'py>,
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    43
    package: &str,
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    44
) -> PyResult<Bound<'py, PyModule>> {
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    45
    let m = new_submodule(py, package, "update")?;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    46
    m.add("FallbackError", py.get_type::<FallbackError>())?;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    47
    m.add_function(wrap_pyfunction!(update_from_null, &m)?)?;
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    48
    Ok(m)
a60d1eb74dc6 pyo3: add `update` module
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    49
}