--- a/rust/rhg/src/commands/cat.rs Mon Nov 14 17:18:56 2022 +0100
+++ b/rust/rhg/src/commands/cat.rs Tue Nov 15 00:02:43 2022 +0100
@@ -4,27 +4,28 @@
use hg::operations::cat;
use hg::utils::hg_path::HgPathBuf;
use micro_timer::timed;
+use std::ffi::OsString;
+use std::os::unix::prelude::OsStrExt;
pub const HELP_TEXT: &str = "
Output the current or given revision of files
";
-pub fn args() -> clap::App<'static, 'static> {
- clap::SubCommand::with_name("cat")
+pub fn args() -> clap::Command {
+ clap::command!("cat")
.arg(
- Arg::with_name("rev")
+ Arg::new("rev")
.help("search the repository as it is in REV")
- .short("-r")
- .long("--rev")
- .value_name("REV")
- .takes_value(true),
+ .short('r')
+ .long("rev")
+ .value_name("REV"),
)
.arg(
- clap::Arg::with_name("files")
+ clap::Arg::new("files")
.required(true)
- .multiple(true)
- .empty_values(false)
+ .num_args(1..)
.value_name("FILE")
+ .value_parser(clap::value_parser!(std::ffi::OsString))
.help("Files to output"),
)
.about(HELP_TEXT)
@@ -41,11 +42,15 @@
));
}
- let rev = invocation.subcommand_args.value_of("rev");
- let file_args = match invocation.subcommand_args.values_of("files") {
- Some(files) => files.collect(),
- None => vec![],
- };
+ let rev = invocation.subcommand_args.get_one::<String>("rev");
+ let file_args =
+ match invocation.subcommand_args.get_many::<OsString>("files") {
+ Some(files) => files
+ .filter(|s| !s.is_empty())
+ .map(|s| s.as_os_str())
+ .collect(),
+ None => vec![],
+ };
let repo = invocation.repo?;
let cwd = hg::utils::current_dir()?;
@@ -53,8 +58,8 @@
let working_directory = cwd.join(working_directory); // Make it absolute
let mut files = vec![];
- for file in file_args.iter() {
- if file.starts_with("set:") {
+ for file in file_args {
+ if file.as_bytes().starts_with(b"set:") {
let message = "fileset";
return Err(CommandError::unsupported(message));
}
@@ -62,7 +67,7 @@
let normalized = cwd.join(&file);
// TODO: actually normalize `..` path segments etc?
let dotted = normalized.components().any(|c| c.as_os_str() == "..");
- if file == &"." || dotted {
+ if file.as_bytes() == b"." || dotted {
let message = "`..` or `.` path segment";
return Err(CommandError::unsupported(message));
}
@@ -74,7 +79,7 @@
.map_err(|_| {
CommandError::abort(format!(
"abort: {} not under root '{}'\n(consider using '--cwd {}')",
- file,
+ String::from_utf8_lossy(file.as_bytes()),
working_directory.display(),
relative_path.display(),
))