##// END OF EJS Templates
hooks: introduce a `:run-with-plain` option for hooks...
hooks: introduce a `:run-with-plain` option for hooks This option control if HGPLAIN should be set or not for the hooks. This is the first step to give user some control of the HGPLAIN setting for they hooks. Some hooks (eg: consistency checking) deserve to be run with HGPLAIN, some other (eg: user set visual helper) might need to respect the user config and setting. So both usage are valid and we need to restore the ability to run -without- HGPLAIN that got lost in Mercurial 5.7. This does not offer a way to restore the pre-5.7 behavior yet (respect whatever HGPLAIN setting from the shell), this will be dealt with in the next changeset. The option name is a bit verbose because implementing this highlighs the need for another option: `:run-if-plain`. That would make it possible for some hooks to be easily disabled if HG PLAIN is set. However such option would be a new feature, not something introduced to mitigate a behavior change introduced in 5.7, so the `:run-if-plain` option belong to the default branch and is not part of this series. Differential Revision: https://phab.mercurial-scm.org/D9981

File last commit:

r47163:3e2d539d default
r47242:7289eac7 stable
Show More
find_root.rs
100 lines | 2.7 KiB | application/rls-services+xml | RustLexer
Antoine Cezar
hg-core: add FindRoot operation to find repository root path...
r45502 use std::fmt;
use std::path::{Path, PathBuf};
/// Kind of error encoutered by FindRoot
#[derive(Debug)]
pub enum FindRootErrorKind {
/// Root of the repository has not been found
/// Contains the current directory used by FindRoot
RootNotFound(PathBuf),
/// The current directory does not exists or permissions are insufficient
/// to get access to it
GetCurrentDirError(std::io::Error),
}
/// A FindRoot error
#[derive(Debug)]
pub struct FindRootError {
/// Kind of error encoutered by FindRoot
pub kind: FindRootErrorKind,
}
impl std::error::Error for FindRootError {}
impl fmt::Display for FindRootError {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
unimplemented!()
}
}
/// Find the root of the repository
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 /// by searching for a .hg directory in the process’ current directory and its
Antoine Cezar
hg-core: add FindRoot operation to find repository root path...
r45502 /// ancestors
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 pub fn find_root() -> Result<PathBuf, FindRootError> {
let current_dir = std::env::current_dir().map_err(|e| FindRootError {
kind: FindRootErrorKind::GetCurrentDirError(e),
})?;
Ok(find_root_from_path(&current_dir)?.into())
Antoine Cezar
hg-core: add FindRoot operation to find repository root path...
r45502 }
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 /// Find the root of the repository
/// by searching for a .hg directory in the given directory and its ancestors
pub fn find_root_from_path(start: &Path) -> Result<&Path, FindRootError> {
if start.join(".hg").exists() {
return Ok(start);
Antoine Cezar
hg-core: add FindRoot operation to find repository root path...
r45502 }
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 for ancestor in start.ancestors() {
if ancestor.join(".hg").exists() {
return Ok(ancestor);
Antoine Cezar
hg-core: add FindRoot operation to find repository root path...
r45502 }
}
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 Err(FindRootError {
kind: FindRootErrorKind::RootNotFound(start.into()),
})
Antoine Cezar
hg-core: add FindRoot operation to find repository root path...
r45502 }
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile;
#[test]
fn dot_hg_not_found() {
let tmp_dir = tempfile::tempdir().unwrap();
let path = tmp_dir.path();
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 let err = find_root_from_path(&path).unwrap_err();
Antoine Cezar
hg-core: add FindRoot operation to find repository root path...
r45502
// TODO do something better
assert!(match err {
FindRootError { kind } => match kind {
FindRootErrorKind::RootNotFound(p) => p == path.to_path_buf(),
_ => false,
},
})
}
#[test]
fn dot_hg_in_current_path() {
let tmp_dir = tempfile::tempdir().unwrap();
let root = tmp_dir.path();
fs::create_dir_all(root.join(".hg")).unwrap();
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 let result = find_root_from_path(&root).unwrap();
Antoine Cezar
hg-core: add FindRoot operation to find repository root path...
r45502
assert_eq!(result, root)
}
#[test]
fn dot_hg_in_parent() {
let tmp_dir = tempfile::tempdir().unwrap();
let root = tmp_dir.path();
fs::create_dir_all(root.join(".hg")).unwrap();
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 let directory = root.join("some/nested/directory");
let result = find_root_from_path(&directory).unwrap();
Antoine Cezar
hg-core: add FindRoot operation to find repository root path...
r45502
assert_eq!(result, root)
}
} /* tests */