author | Simon Sapin <simon.sapin@octobus.net> |
Mon, 08 Feb 2021 21:37:30 +0100 | |
changeset 46503 | d8730ff51d5a |
parent 46501 | 1ecaf09d9964 |
child 46592 | 80840b651721 |
permissions | -rw-r--r-- |
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; |
45542
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
2 |
use crate::ui::Ui; |
46501
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
3 |
use clap::Arg; |
46500
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
4 |
use clap::ArgMatches; |
46484
a6e4e4650bac
rhg: Parse system and user configuration at program start
Simon Sapin <simon.sapin@octobus.net>
parents:
46446
diff
changeset
|
5 |
use hg::config::Config; |
46436
252d1bdba33d
rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents:
46434
diff
changeset
|
6 |
use hg::operations::cat; |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
7 |
use hg::repo::Repo; |
45542
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
8 |
use hg::utils::hg_path::HgPathBuf; |
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
9 |
use micro_timer::timed; |
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
10 |
use std::convert::TryFrom; |
46503
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
11 |
use std::path::Path; |
45542
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
12 |
|
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
13 |
pub const HELP_TEXT: &str = " |
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
14 |
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
|
15 |
"; |
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
16 |
|
46501
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
17 |
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
|
18 |
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
|
19 |
.arg( |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
20 |
Arg::with_name("rev") |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
21 |
.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
|
22 |
.short("-r") |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
23 |
.long("--revision") |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
24 |
.value_name("REV") |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
25 |
.takes_value(true), |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
26 |
) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
27 |
.arg( |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
28 |
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
|
29 |
.required(true) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
30 |
.multiple(true) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
31 |
.empty_values(false) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
32 |
.value_name("FILE") |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
33 |
.help("Activity to start: activity@category"), |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
34 |
) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
35 |
.about(HELP_TEXT) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
36 |
} |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46500
diff
changeset
|
37 |
|
46500
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
38 |
#[timed] |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
39 |
pub fn run( |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
40 |
ui: &Ui, |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
41 |
config: &Config, |
46503
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
42 |
repo_path: Option<&Path>, |
46500
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
43 |
args: &ArgMatches, |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
44 |
) -> Result<(), CommandError> { |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
45 |
let rev = args.value_of("rev"); |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
46 |
let file_args = match args.values_of("files") { |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
47 |
Some(files) => files.collect(), |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
48 |
None => vec![], |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
49 |
}; |
45542
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
50 |
|
46503
d8730ff51d5a
rhg: Add support for -R and --repository command-line arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
46501
diff
changeset
|
51 |
let repo = Repo::find(config, repo_path)?; |
46500
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
52 |
let cwd = hg::utils::current_dir()?; |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
53 |
|
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
54 |
let mut files = vec![]; |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
55 |
for file in file_args.iter() { |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
56 |
// TODO: actually normalize `..` path segments etc? |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
57 |
let normalized = cwd.join(&file); |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
58 |
let stripped = normalized |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
59 |
.strip_prefix(&repo.working_directory_path()) |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
60 |
// 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
|
61 |
.map_err(|_| CommandError::abort(""))?; |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
62 |
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
|
63 |
.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
|
64 |
files.push(hg_file); |
45542
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
65 |
} |
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
66 |
|
46500
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
67 |
match rev { |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
68 |
Some(rev) => { |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
69 |
let data = cat(&repo, rev, &files).map_err(|e| (e, rev))?; |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
70 |
ui.write_stdout(&data)?; |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
71 |
Ok(()) |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
72 |
} |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46484
diff
changeset
|
73 |
None => Err(CommandError::Unimplemented.into()), |
45542
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
74 |
} |
33ded2d3f4fc
rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
75 |
} |