| 1 |
// update.rs |
| 2 |
// |
| 3 |
// Copyright 2025 Mercurial developers |
| 4 |
// |
| 5 |
// This software may be used and distributed according to the terms of the |
| 6 |
// GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
//! Bindings for the `hg::update` module provided by the |
| 9 |
//! `hg-core` package. |
| 10 |
//! |
| 11 |
//! From Python, this will be seen as `mercurial.pyo3_rustext.update` |
| 12 |
use hg::progress::HgProgressBar; |
| 13 |
use hg::progress::Progress; |
| 14 |
use hg::update::update_from_clean as core_update_from_clean; |
| 15 |
use hg::update::update_from_null as core_update_from_null; |
| 16 |
use hg::update::FileConflictConfig; |
| 17 |
use hg::update::UpdateConfig; |
| 18 |
use hg::warnings::HgWarningContext; |
| 19 |
use hg::BaseRevision; |
| 20 |
use hg::Revision; |
| 21 |
use pyo3::prelude::*; |
| 22 |
use pyo3::types::PyBytes; |
| 23 |
|
| 24 |
use crate::dirstate::DirstateMap; |
| 25 |
use crate::exceptions::FallbackError; |
| 26 |
use crate::repo::repo_from_path; |
| 27 |
use crate::utils::handle_warnings; |
| 28 |
use crate::utils::new_submodule; |
| 29 |
use crate::utils::with_sigint_wrapper; |
| 30 |
use crate::utils::HgPyErrExt; |
| 31 |
use crate::utils::PyBytesDeref; |
| 32 |
|
| 33 |
/// See [`core_update_from_null`]. |
| 34 |
#[pyfunction] |
| 35 |
#[pyo3(signature = ( |
| 36 |
repo_path, |
| 37 |
to, |
| 38 |
dirstate, |
| 39 |
num_cpus, |
| 40 |
on_warnings, |
| 41 |
devel_abort_dirstate, |
| 42 |
ignored_conflict, |
| 43 |
unknown_conflict, |
| 44 |
))] |
| 45 |
#[allow(clippy::too_many_arguments)] |
| 46 |
pub fn update_from_null( |
| 47 |
repo_path: &Bound<'_, PyBytes>, |
| 48 |
to: BaseRevision, |
| 49 |
dirstate: &Bound<'_, DirstateMap>, |
| 50 |
num_cpus: Option<usize>, |
| 51 |
on_warnings: Py<PyAny>, |
| 52 |
devel_abort_dirstate: bool, |
| 53 |
ignored_conflict: &Bound<'_, PyBytes>, |
| 54 |
unknown_conflict: &Bound<'_, PyBytes>, |
| 55 |
) -> PyResult<usize> { |
| 56 |
tracing::debug!("Using update from null fastpath"); |
| 57 |
let repo = repo_from_path(repo_path)?; |
| 58 |
let progress: &dyn Progress = &HgProgressBar::new("updating"); |
| 59 |
|
| 60 |
let py = repo_path.py(); |
| 61 |
let ignored_conflict = FileConflictConfig::new( |
| 62 |
"merge.checkignored", |
| 63 |
Some(&String::from_utf8_lossy(ignored_conflict.as_bytes())), |
| 64 |
) |
| 65 |
.into_pyerr(py)?; |
| 66 |
let unknown_conflict = FileConflictConfig::new( |
| 67 |
"merge.checkunknown", |
| 68 |
Some(&String::from_utf8_lossy(unknown_conflict.as_bytes())), |
| 69 |
) |
| 70 |
.into_pyerr(py)?; |
| 71 |
let update_config = UpdateConfig { |
| 72 |
workers: num_cpus, |
| 73 |
remove_empty_dirs: false, |
| 74 |
devel_abort_dirstate, |
| 75 |
orig_backup_path: None, |
| 76 |
atomic_file: false, |
| 77 |
ignored_conflict, |
| 78 |
unknown_conflict, |
| 79 |
}; |
| 80 |
|
| 81 |
let warning_context = HgWarningContext::new(); |
| 82 |
DirstateMap::with_inner_write(dirstate, |_inner, mut dirstate| { |
| 83 |
let res = with_sigint_wrapper(py, || { |
| 84 |
core_update_from_null( |
| 85 |
&repo, |
| 86 |
to.into(), |
| 87 |
&mut dirstate, |
| 88 |
progress, |
| 89 |
&update_config, |
| 90 |
warning_context.sender(), |
| 91 |
) |
| 92 |
.map(|stats| stats.updated) |
| 93 |
}); |
| 94 |
// Handle warnings even in case of an error |
| 95 |
handle_warnings( |
| 96 |
py, |
| 97 |
warning_context, |
| 98 |
repo.working_directory_path(), |
| 99 |
on_warnings, |
| 100 |
)?; |
| 101 |
|
| 102 |
let updated = res?.into_pyerr(py)?; |
| 103 |
|
| 104 |
Ok(updated) |
| 105 |
}) |
| 106 |
} |
| 107 |
|
| 108 |
#[pyfunction] |
| 109 |
#[allow(clippy::too_many_arguments)] |
| 110 |
#[pyo3(signature = ( |
| 111 |
repo_path, |
| 112 |
dirstate, |
| 113 |
wc_manifest, |
| 114 |
target_rev, |
| 115 |
target_manifest, |
| 116 |
num_cpus, |
| 117 |
remove_empty_dirs, |
| 118 |
devel_abort_dirstate, |
| 119 |
orig_backup_path, |
| 120 |
atomic_file, |
| 121 |
on_warnings, |
| 122 |
ignored_conflict, |
| 123 |
unknown_conflict, |
| 124 |
))] |
| 125 |
pub fn update_from_clean( |
| 126 |
repo_path: &Bound<'_, PyBytes>, |
| 127 |
dirstate: &Bound<'_, DirstateMap>, |
| 128 |
wc_manifest: &Bound<'_, PyAny>, |
| 129 |
target_rev: BaseRevision, |
| 130 |
target_manifest: &Bound<'_, PyAny>, |
| 131 |
num_cpus: Option<usize>, |
| 132 |
remove_empty_dirs: bool, |
| 133 |
devel_abort_dirstate: bool, |
| 134 |
orig_backup_path: Option<&[u8]>, |
| 135 |
atomic_file: bool, |
| 136 |
on_warnings: Py<PyAny>, |
| 137 |
ignored_conflict: &Bound<'_, PyBytes>, |
| 138 |
unknown_conflict: &Bound<'_, PyBytes>, |
| 139 |
) -> PyResult<(usize, usize, usize, usize, usize)> { |
| 140 |
tracing::debug!("Using update from clean fastpath"); |
| 141 |
let repo = repo_from_path(repo_path)?; |
| 142 |
let progress: &dyn Progress = &HgProgressBar::new("updating"); |
| 143 |
let py = repo_path.py(); |
| 144 |
let ignored_conflict = FileConflictConfig::new( |
| 145 |
"merge.checkignored", |
| 146 |
Some(&String::from_utf8_lossy(ignored_conflict.as_bytes())), |
| 147 |
) |
| 148 |
.into_pyerr(py)?; |
| 149 |
let unknown_conflict = FileConflictConfig::new( |
| 150 |
"merge.checkunknown", |
| 151 |
Some(&String::from_utf8_lossy(unknown_conflict.as_bytes())), |
| 152 |
) |
| 153 |
.into_pyerr(py)?; |
| 154 |
let update_config = UpdateConfig { |
| 155 |
workers: num_cpus, |
| 156 |
remove_empty_dirs, |
| 157 |
devel_abort_dirstate, |
| 158 |
orig_backup_path: orig_backup_path.map(ToOwned::to_owned), |
| 159 |
atomic_file, |
| 160 |
ignored_conflict, |
| 161 |
unknown_conflict, |
| 162 |
}; |
| 163 |
let warning_context = HgWarningContext::new(); |
| 164 |
|
| 165 |
let wc_manifest_bytes = |
| 166 |
wc_manifest.call_method0("text")?.extract::<Py<PyBytes>>()?; |
| 167 |
|
| 168 |
let target_manifest_bytes = |
| 169 |
target_manifest.call_method0("text")?.extract::<Py<PyBytes>>()?; |
| 170 |
|
| 171 |
DirstateMap::with_inner_write(dirstate, |_inner, mut dirstate| { |
| 172 |
let res = with_sigint_wrapper(py, || { |
| 173 |
core_update_from_clean( |
| 174 |
&repo, |
| 175 |
&mut dirstate, |
| 176 |
Box::new(PyBytesDeref::new(py, wc_manifest_bytes)), |
| 177 |
// We assume that Python doesn't give the wrong revision |
| 178 |
Revision(target_rev), |
| 179 |
Box::new(PyBytesDeref::new(py, target_manifest_bytes)), |
| 180 |
progress, |
| 181 |
&update_config, |
| 182 |
warning_context.sender(), |
| 183 |
) |
| 184 |
}); |
| 185 |
// Handle warnings even in case of an error |
| 186 |
handle_warnings( |
| 187 |
py, |
| 188 |
warning_context, |
| 189 |
repo.working_directory_path(), |
| 190 |
on_warnings, |
| 191 |
)?; |
| 192 |
|
| 193 |
let merge_stats = res?.into_pyerr(repo_path.py())?; |
| 194 |
|
| 195 |
Ok(( |
| 196 |
merge_stats.added, |
| 197 |
merge_stats.updated, |
| 198 |
merge_stats.merged, |
| 199 |
merge_stats.removed, |
| 200 |
merge_stats.unresolved, |
| 201 |
)) |
| 202 |
}) |
| 203 |
} |
| 204 |
|
| 205 |
pub fn init_module<'py>( |
| 206 |
py: Python<'py>, |
| 207 |
package: &str, |
| 208 |
) -> PyResult<Bound<'py, PyModule>> { |
| 209 |
let m = new_submodule(py, package, "update")?; |
| 210 |
m.add("FallbackError", py.get_type::<FallbackError>())?; |
| 211 |
m.add_function(wrap_pyfunction!(update_from_null, &m)?)?; |
| 212 |
m.add_function(wrap_pyfunction!(update_from_clean, &m)?)?; |
| 213 |
Ok(m) |
| 214 |
} |