view rust/rhg/src/commands/root.rs @ 48495:e293ff808a05

rhg: Use binary search in manifest lookup … instead of linear scan, when looking for a single entry based on its path. Manifest entries are sorted by path, but are variable-size so we can’t use the standard library’s `[T]::binary_search`. We can still jump to a byte index and then look around for entry boundaries. Differential Revision: https://phab.mercurial-scm.org/D11932
author Simon Sapin <simon.sapin@octobus.net>
date Thu, 16 Dec 2021 17:34:51 +0100
parents 97ac588b6d9e
children 37bc3edef76f
line wrap: on
line source

use crate::error::CommandError;
use format_bytes::format_bytes;
use hg::errors::{IoErrorContext, IoResultExt};
use hg::utils::files::get_bytes_from_path;

pub const HELP_TEXT: &str = "
Print the root directory of the current repository.

Returns 0 on success.
";

pub fn args() -> clap::App<'static, 'static> {
    clap::SubCommand::with_name("root").about(HELP_TEXT)
}

pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
    let repo = invocation.repo?;
    let working_directory = repo.working_directory_path();
    let working_directory = std::fs::canonicalize(working_directory)
        .with_context(|| {
            IoErrorContext::CanonicalizingPath(working_directory.to_owned())
        })?;
    let bytes = get_bytes_from_path(&working_directory);
    invocation
        .ui
        .write_stdout(&format_bytes!(b"{}\n", bytes.as_slice()))?;
    Ok(())
}