| 1 |
use std::ffi::OsString; |
| 2 |
|
| 3 |
use clap::Arg; |
| 4 |
use format_bytes::format_bytes; |
| 5 |
use hg::operations::cat; |
| 6 |
|
| 7 |
use crate::error::CommandError; |
| 8 |
use crate::utils::path_utils::resolve_file_args; |
| 9 |
|
| 10 |
pub const HELP_TEXT: &str = " |
| 11 |
Output the current or given revision of files |
| 12 |
"; |
| 13 |
|
| 14 |
pub fn args() -> clap::Command { |
| 15 |
clap::command!("cat") |
| 16 |
.args_override_self(true) |
| 17 |
.arg( |
| 18 |
Arg::new("rev") |
| 19 |
.help("search the repository as it is in REV") |
| 20 |
.short('r') |
| 21 |
.long("rev") |
| 22 |
.value_name("REV"), |
| 23 |
) |
| 24 |
.arg( |
| 25 |
clap::Arg::new("files") |
| 26 |
.required(true) |
| 27 |
.num_args(1..) |
| 28 |
.value_name("FILE") |
| 29 |
.value_parser(clap::value_parser!(std::ffi::OsString)) |
| 30 |
.help("Files to output"), |
| 31 |
) |
| 32 |
.about(HELP_TEXT) |
| 33 |
} |
| 34 |
|
| 35 |
#[tracing::instrument(level = "debug", skip_all, name = "rhg cat")] |
| 36 |
pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> { |
| 37 |
let cat_enabled = invocation.config.get_bool(b"rhg", b"cat")?; |
| 38 |
if !cat_enabled { |
| 39 |
return Err(CommandError::unsupported( |
| 40 |
"cat is disabled in rhg (enable it with 'rhg.cat = true' \ |
| 41 |
or enable fallback with 'rhg.on-unsupported = fallback')", |
| 42 |
)); |
| 43 |
} |
| 44 |
|
| 45 |
let repo = invocation.repo?; |
| 46 |
|
| 47 |
let rev = invocation.subcommand_args.get_one::<String>("rev"); |
| 48 |
let files = match invocation.subcommand_args.get_many::<OsString>("files") { |
| 49 |
None => vec![], |
| 50 |
Some(files) => resolve_file_args(repo, files)?, |
| 51 |
}; |
| 52 |
|
| 53 |
let files = files.iter().map(|file| file.as_ref()).collect(); |
| 54 |
// TODO probably move this to a util function like `repo.default_rev` or |
| 55 |
// something when it's used somewhere else |
| 56 |
let rev = match rev { |
| 57 |
Some(r) => r.to_string(), |
| 58 |
None => format!("{:x}", repo.dirstate_parents()?.p1), |
| 59 |
}; |
| 60 |
|
| 61 |
let output = cat(repo, &rev, files)?; |
| 62 |
for (_file, contents) in output.results { |
| 63 |
invocation.ui.write_stdout(&contents)?; |
| 64 |
} |
| 65 |
if !output.missing.is_empty() { |
| 66 |
let short = format!("{:x}", output.node.short()).into_bytes(); |
| 67 |
for path in &output.missing { |
| 68 |
invocation.ui.write_stderr(&format_bytes!( |
| 69 |
b"{}: no such file in rev {}\n", |
| 70 |
path.as_bytes(), |
| 71 |
short |
| 72 |
))?; |
| 73 |
} |
| 74 |
} |
| 75 |
if output.found_any { |
| 76 |
Ok(()) |
| 77 |
} else { |
| 78 |
Err(CommandError::Unsuccessful) |
| 79 |
} |
| 80 |
} |