##// END OF EJS Templates
chg: populate CHGHG if not set...
chg: populate CHGHG if not set Normally, chg determines which `hg` executable to use by first consulting the `$CHGHG` and `$HG` environment variables, and if neither are present defaults to the `hg` found in the user's `$PATH`. If built with the `HGPATHREL` compiler flag, chg will instead assume that there exists an `hg` executable in the same directory as the `chg` binary and attempt to use that. This can cause problems in situations where there are multiple actively-used Mercurial installations on the same system. When a `chg` client connects to a running command server, the server process performs some basic validation to determine whether a new command server needs to be spawned. These checks include things like checking certain "sensitive" environment variables and config sections, as well as checking whether the mtime of the extensions, hg's `__version__.py` module, and the Python interpreter have changed. Crucially, the command server doesn't explicitly check whether the executable it is running from matches the executable that the `chg` client would have otherwise invoked had there been no existing command server process. Without `HGPATHREL`, this still gets implicitly checked during the validation step, because the only way to specify an alternate hg executable (apart from `$PATH`) is via the `$CHGHG` and `$HG` environment variables, both of which are checked. With `HGPATHREL`, however, the command server has no way of knowing which hg executable the client would have run. This means that a client located at `/version_B/bin/chg` will happily connect to a command server running `/version_A/bin/hg` instead of `/version_B/bin/hg` as expected. A simple solution is to have the client set `$CHGHG` itself, which then allows the command server's environment validation to work as intended. I have tested this manually using two locally built hg installations and it seems to work with no ill effects. That said, I'm not sure how to write an automated test for this since the `chg` available to the tests isn't even built with the `HGPATHREL` compiler flag to begin with.

File last commit:

r44398:f98f0e3d default
r51279:cf4d2f31 stable
Show More
discovery.rs
161 lines | 5.7 KiB | application/rls-services+xml | RustLexer
// discovery.rs
//
// Copyright 2018 Georges Racinet <gracinet@anybox.fr>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//! Bindings for the `hg::discovery` module provided by the
//! `hg-core` crate. From Python, this will be seen as `rustext.discovery`
//!
//! # Classes visible from Python:
//! - [`PartialDiscover`] is the Rust implementation of
//! `mercurial.setdiscovery.partialdiscovery`.
use crate::{
cindex::Index, conversion::rev_pyiter_collect, exceptions::GraphError,
};
use cpython::{
ObjectProtocol, PyDict, PyModule, PyObject, PyResult, PyTuple, Python,
PythonObject, ToPyObject,
};
use hg::discovery::PartialDiscovery as CorePartialDiscovery;
use hg::Revision;
use std::collections::HashSet;
use std::cell::RefCell;
use crate::revlog::pyindex_to_graph;
py_class!(pub class PartialDiscovery |py| {
data inner: RefCell<Box<CorePartialDiscovery<Index>>>;
// `_respectsize` is currently only here to replicate the Python API and
// will be used in future patches inside methods that are yet to be
// implemented.
def __new__(
_cls,
repo: PyObject,
targetheads: PyObject,
respectsize: bool,
randomize: bool = true
) -> PyResult<PartialDiscovery> {
let index = repo.getattr(py, "changelog")?.getattr(py, "index")?;
Self::create_instance(
py,
RefCell::new(Box::new(CorePartialDiscovery::new(
pyindex_to_graph(py, index)?,
rev_pyiter_collect(py, &targetheads)?,
respectsize,
randomize,
)))
)
}
def addcommons(&self, commons: PyObject) -> PyResult<PyObject> {
let mut inner = self.inner(py).borrow_mut();
let commons_vec: Vec<Revision> = rev_pyiter_collect(py, &commons)?;
inner.add_common_revisions(commons_vec)
.map_err(|e| GraphError::pynew(py, e))?;
Ok(py.None())
}
def addmissings(&self, missings: PyObject) -> PyResult<PyObject> {
let mut inner = self.inner(py).borrow_mut();
let missings_vec: Vec<Revision> = rev_pyiter_collect(py, &missings)?;
inner.add_missing_revisions(missings_vec)
.map_err(|e| GraphError::pynew(py, e))?;
Ok(py.None())
}
def addinfo(&self, sample: PyObject) -> PyResult<PyObject> {
let mut missing: Vec<Revision> = Vec::new();
let mut common: Vec<Revision> = Vec::new();
for info in sample.iter(py)? { // info is a pair (Revision, bool)
let mut revknown = info?.iter(py)?;
let rev: Revision = revknown.next().unwrap()?.extract(py)?;
let known: bool = revknown.next().unwrap()?.extract(py)?;
if known {
common.push(rev);
} else {
missing.push(rev);
}
}
let mut inner = self.inner(py).borrow_mut();
inner.add_common_revisions(common)
.map_err(|e| GraphError::pynew(py, e))?;
inner.add_missing_revisions(missing)
.map_err(|e| GraphError::pynew(py, e))?;
Ok(py.None())
}
def hasinfo(&self) -> PyResult<bool> {
Ok(self.inner(py).borrow().has_info())
}
def iscomplete(&self) -> PyResult<bool> {
Ok(self.inner(py).borrow().is_complete())
}
def stats(&self) -> PyResult<PyDict> {
let stats = self.inner(py).borrow().stats();
let as_dict: PyDict = PyDict::new(py);
as_dict.set_item(py, "undecided",
stats.undecided.map(
|l| l.to_py_object(py).into_object())
.unwrap_or_else(|| py.None()))?;
Ok(as_dict)
}
def commonheads(&self) -> PyResult<HashSet<Revision>> {
self.inner(py).borrow().common_heads()
.map_err(|e| GraphError::pynew(py, e))
}
def takefullsample(&self, _headrevs: PyObject,
size: usize) -> PyResult<PyObject> {
let mut inner = self.inner(py).borrow_mut();
let sample = inner.take_full_sample(size)
.map_err(|e| GraphError::pynew(py, e))?;
let as_vec: Vec<PyObject> = sample
.iter()
.map(|rev| rev.to_py_object(py).into_object())
.collect();
Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
}
def takequicksample(&self, headrevs: PyObject,
size: usize) -> PyResult<PyObject> {
let mut inner = self.inner(py).borrow_mut();
let revsvec: Vec<Revision> = rev_pyiter_collect(py, &headrevs)?;
let sample = inner.take_quick_sample(revsvec, size)
.map_err(|e| GraphError::pynew(py, e))?;
let as_vec: Vec<PyObject> = sample
.iter()
.map(|rev| rev.to_py_object(py).into_object())
.collect();
Ok(PyTuple::new(py, as_vec.as_slice()).into_object())
}
});
/// Create the module, with __package__ given from parent
pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
let dotted_name = &format!("{}.discovery", package);
let m = PyModule::new(py, dotted_name)?;
m.add(py, "__package__", package)?;
m.add(
py,
"__doc__",
"Discovery of common node sets - Rust implementation",
)?;
m.add_class::<PartialDiscovery>(py)?;
let sys = PyModule::import(py, "sys")?;
let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?;
sys_modules.set_item(py, dotted_name, &m)?;
// Example C code (see pyexpat.c and import.c) will "give away the
// reference", but we won't because it will be consumed once the
// Rust PyObject is dropped.
Ok(m)
}