Debian Codegraph @

rust/rhg/src/commands/root.rs

raw
1 use clap::Arg;
2 use format_bytes::format_bytes;
3 use hg::errors::IoErrorContext;
4 use hg::errors::IoResultExt;
5 use hg::utils::files::get_bytes_from_path;
6
7 use crate::error::CommandError;
8
9 pub const HELP_TEXT: &str = "
10 Print the root directory of the current repository.
11
12 Returns 0 on success.
13 ";
14
15 pub fn args() -> clap::Command {
16 clap::command!("root")
17 .args_override_self(true)
18 .arg(
19 Arg::new("share-source")
20 .long("share-source")
21 .action(clap::ArgAction::SetTrue)
22 .help("print the share source root instead"),
23 )
24 .about(HELP_TEXT)
25 }
26
27 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
28 let share_source = invocation.subcommand_args.get_flag("share-source");
29 let repo = invocation.repo?;
30 let shared_repo;
31 let working_directory = (if share_source {
32 let store_path = repo.store_path();
33 let shared_path = crate::Repo::find_repo_root_from(store_path)?;
34 shared_repo = crate::Repo::find(repo.config(), Some(shared_path))?;
35 &shared_repo
36 } else {
37 repo
38 })
39 .working_directory_path();
40 let working_directory = std::fs::canonicalize(working_directory)
41 .with_context(|| {
42 IoErrorContext::CanonicalizingPath(working_directory.to_owned())
43 })?;
44 let bytes = get_bytes_from_path(working_directory);
45 invocation.ui.write_stdout(&format_bytes!(b"{}\n", bytes.as_slice()))?;
46 Ok(())
47 }