| 1 |
use clap::Arg; |
| 2 |
use hg::narrow::shape::ShardTreeNode; |
| 3 |
use hg::narrow::store_patterns; |
| 4 |
use hg::utils::strings::SliceExt; |
| 5 |
use hg::warnings::HgWarningContext; |
| 6 |
|
| 7 |
use crate::error::CommandError; |
| 8 |
use crate::ui::print_warnings; |
| 9 |
|
| 10 |
pub const HELP_TEXT: &str = " |
| 11 |
Narrow-related administration utils (EXPERIMENTAL) |
| 12 |
"; |
| 13 |
|
| 14 |
pub fn args() -> clap::Command { |
| 15 |
clap::command!("admin::narrow-client") |
| 16 |
.arg( |
| 17 |
Arg::new("store-fingerprint") |
| 18 |
.long("store-fingerprint") |
| 19 |
.help("get the fingerprint for this repo's store narrospec") |
| 20 |
.action(clap::ArgAction::SetTrue), |
| 21 |
) |
| 22 |
.about(HELP_TEXT) |
| 23 |
} |
| 24 |
|
| 25 |
#[tracing::instrument( |
| 26 |
level = "debug", |
| 27 |
skip_all, |
| 28 |
name = "rhg admin::narrow-client" |
| 29 |
)] |
| 30 |
pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> { |
| 31 |
let args = invocation.subcommand_args; |
| 32 |
let store_fingerprint = args.get_one::<bool>("store-fingerprint"); |
| 33 |
if store_fingerprint.is_none() { |
| 34 |
return Err(CommandError::unsupported( |
| 35 |
"rhg only supports --store-fingerprint", |
| 36 |
)); |
| 37 |
} |
| 38 |
|
| 39 |
let repo = invocation.repo?; |
| 40 |
if !repo.has_narrow() { |
| 41 |
return Err(CommandError::abort( |
| 42 |
"abort: this command only makes sense in a narrow clone", |
| 43 |
)); |
| 44 |
} |
| 45 |
|
| 46 |
let warning_context = HgWarningContext::new(); |
| 47 |
let patterns = store_patterns(warning_context.sender(), repo)?; |
| 48 |
let (includes, excludes) = patterns.unwrap_or((vec![], vec![])); |
| 49 |
let tree = ShardTreeNode::from_patterns(&includes, &excludes)?; |
| 50 |
|
| 51 |
print_warnings( |
| 52 |
invocation.ui, |
| 53 |
warning_context, |
| 54 |
repo.working_directory_path(), |
| 55 |
); |
| 56 |
|
| 57 |
let mut stdout = invocation.ui.stdout_buffer(); |
| 58 |
stdout.write_all(&tree.fingerprint().to_hex_bytes())?; |
| 59 |
stdout.write_all(b"\n")?; |
| 60 |
stdout.flush()?; |
| 61 |
|
| 62 |
Ok(()) |
| 63 |
} |