annotate rust/rhg/src/commands/cat.rs @ 48237:027ebad952ac

rhg: internally, return a structured representation from hg cat The purpose of this change is to make it possible to support limited templating in `hg cat`, so we could print separators between files etc. The templating itself is not implemented yet, so this functionality is unused in `rhg cat`. However, in our fork of hg we're implementing a slightly different command `hg jscat` which makes use of this. So accepting this change will let us minimize the size of the patch we're maintaining on our side. Differential Revision: https://phab.mercurial-scm.org/D11679
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Fri, 15 Oct 2021 14:05:20 +0100
parents 6b5773f89183
children 698b70b9e8ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
1 use crate::error::CommandError;
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
2 use clap::Arg;
46744
b1f2c2b336ec rhg: `cat` command: print error messages for missing files
Simon Sapin <simon.sapin@octobus.net>
parents: 46740
diff changeset
3 use format_bytes::format_bytes;
46436
252d1bdba33d rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents: 46434
diff changeset
4 use hg::operations::cat;
45542
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
5 use hg::utils::hg_path::HgPathBuf;
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
6 use micro_timer::timed;
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
7 use std::convert::TryFrom;
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
8
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
9 pub const HELP_TEXT: &str = "
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
10 Output the current or given revision of files
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
11 ";
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
12
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
13 pub fn args() -> clap::App<'static, 'static> {
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
14 clap::SubCommand::with_name("cat")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
15 .arg(
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
16 Arg::with_name("rev")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
17 .help("search the repository as it is in REV")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
18 .short("-r")
48172
08c8cd2527bc rhg: in rhg cat cli, fix the long name of the --rev flag
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48074
diff changeset
19 .long("--rev")
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
20 .value_name("REV")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
21 .takes_value(true),
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
22 )
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
23 .arg(
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
24 clap::Arg::with_name("files")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
25 .required(true)
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
26 .multiple(true)
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
27 .empty_values(false)
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
28 .value_name("FILE")
48172
08c8cd2527bc rhg: in rhg cat cli, fix the long name of the --rev flag
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48074
diff changeset
29 .help("Files to output"),
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
30 )
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
31 .about(HELP_TEXT)
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
32 }
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
33
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
34 #[timed]
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
35 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
36 let rev = invocation.subcommand_args.value_of("rev");
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
37 let file_args = match invocation.subcommand_args.values_of("files") {
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
38 Some(files) => files.collect(),
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
39 None => vec![],
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
40 };
45542
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
41
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
42 let repo = invocation.repo?;
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
43 let cwd = hg::utils::current_dir()?;
46740
97ac588b6d9e rhg: Don’t make repository path absolute too early
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
44 let working_directory = repo.working_directory_path();
97ac588b6d9e rhg: Don’t make repository path absolute too early
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
45 let working_directory = cwd.join(working_directory); // Make it absolute
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
46
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
47 let mut files = vec![];
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
48 for file in file_args.iter() {
48074
4a6fa6b6f079 rhg-cat: fallback in presence of a fileset
Raphaël Gomès <rgomes@octobus.net>
parents: 48073
diff changeset
49 if file.starts_with("set:") {
4a6fa6b6f079 rhg-cat: fallback in presence of a fileset
Raphaël Gomès <rgomes@octobus.net>
parents: 48073
diff changeset
50 let message = "fileset";
4a6fa6b6f079 rhg-cat: fallback in presence of a fileset
Raphaël Gomès <rgomes@octobus.net>
parents: 48073
diff changeset
51 return Err(CommandError::unsupported(message));
4a6fa6b6f079 rhg-cat: fallback in presence of a fileset
Raphaël Gomès <rgomes@octobus.net>
parents: 48073
diff changeset
52 }
4a6fa6b6f079 rhg-cat: fallback in presence of a fileset
Raphaël Gomès <rgomes@octobus.net>
parents: 48073
diff changeset
53
48073
1e00834491a5 rhg-cat: fallback when detecting `.` or `..` path segments
Raphaël Gomès <rgomes@octobus.net>
parents: 48072
diff changeset
54 let normalized = cwd.join(&file);
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
55 // TODO: actually normalize `..` path segments etc?
48073
1e00834491a5 rhg-cat: fallback when detecting `.` or `..` path segments
Raphaël Gomès <rgomes@octobus.net>
parents: 48072
diff changeset
56 let dotted = normalized.components().any(|c| c.as_os_str() == "..");
1e00834491a5 rhg-cat: fallback when detecting `.` or `..` path segments
Raphaël Gomès <rgomes@octobus.net>
parents: 48072
diff changeset
57 if file == &"." || dotted {
1e00834491a5 rhg-cat: fallback when detecting `.` or `..` path segments
Raphaël Gomès <rgomes@octobus.net>
parents: 48072
diff changeset
58 let message = "`..` or `.` path segment";
1e00834491a5 rhg-cat: fallback when detecting `.` or `..` path segments
Raphaël Gomès <rgomes@octobus.net>
parents: 48072
diff changeset
59 return Err(CommandError::unsupported(message));
1e00834491a5 rhg-cat: fallback when detecting `.` or `..` path segments
Raphaël Gomès <rgomes@octobus.net>
parents: 48072
diff changeset
60 }
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
61 let stripped = normalized
46740
97ac588b6d9e rhg: Don’t make repository path absolute too early
Simon Sapin <simon.sapin@octobus.net>
parents: 46666
diff changeset
62 .strip_prefix(&working_directory)
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
63 // TODO: error message for path arguments outside of the repo
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
64 .map_err(|_| CommandError::abort(""))?;
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
65 let hg_file = HgPathBuf::try_from(stripped.to_path_buf())
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
66 .map_err(|e| CommandError::abort(e.to_string()))?;
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
67 files.push(hg_file);
45542
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
68 }
48237
027ebad952ac rhg: internally, return a structured representation from hg cat
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48224
diff changeset
69 let files = files.iter().map(|file| file.as_ref()).collect();
48072
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
70 // TODO probably move this to a util function like `repo.default_rev` or
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
71 // something when it's used somewhere else
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
72 let rev = match rev {
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
73 Some(r) => r.to_string(),
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
74 None => format!("{:x}", repo.dirstate_parents()?.p1),
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
75 };
45542
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
76
48224
6b5773f89183 rhg: faster hg cat when many files are requested
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48172
diff changeset
77 let output = cat(&repo, &rev, files).map_err(|e| (e, rev.as_str()))?;
48237
027ebad952ac rhg: internally, return a structured representation from hg cat
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48224
diff changeset
78 for (_file, contents) in output.results {
027ebad952ac rhg: internally, return a structured representation from hg cat
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48224
diff changeset
79 invocation.ui.write_stdout(&contents)?;
027ebad952ac rhg: internally, return a structured representation from hg cat
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48224
diff changeset
80 }
48072
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
81 if !output.missing.is_empty() {
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
82 let short = format!("{:x}", output.node.short()).into_bytes();
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
83 for path in &output.missing {
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
84 invocation.ui.write_stderr(&format_bytes!(
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
85 b"{}: no such file in rev {}\n",
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
86 path.as_bytes(),
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
87 short
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
88 ))?;
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
89 }
48072
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
90 }
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
91 if output.found_any {
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
92 Ok(())
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
93 } else {
d919b0ca8449 rhg: add support for calling `rhg cat` without a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 46744
diff changeset
94 Err(CommandError::Unsuccessful)
45542
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
95 }
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
96 }