##// END OF EJS Templates
rhg: add `--revision` argument to `rhg files`...
Antoine Cezar -
r46108:2f8227a1 default
parent child Browse files
Show More
@@ -7,8 +7,13 b' use hg::operations::{'
7 ListDirstateTrackedFiles, ListDirstateTrackedFilesError,
7 ListDirstateTrackedFiles, ListDirstateTrackedFilesError,
8 ListDirstateTrackedFilesErrorKind,
8 ListDirstateTrackedFilesErrorKind,
9 };
9 };
10 use hg::operations::{
11 ListRevTrackedFiles, ListRevTrackedFilesError,
12 ListRevTrackedFilesErrorKind,
13 };
10 use hg::utils::files::{get_bytes_from_path, relativize_path};
14 use hg::utils::files::{get_bytes_from_path, relativize_path};
11 use hg::utils::hg_path::HgPathBuf;
15 use hg::utils::hg_path::{HgPath, HgPathBuf};
16 use std::path::PathBuf;
12
17
13 pub const HELP_TEXT: &str = "
18 pub const HELP_TEXT: &str = "
14 List tracked files.
19 List tracked files.
@@ -16,21 +21,21 b' List tracked files.'
16 Returns 0 on success.
21 Returns 0 on success.
17 ";
22 ";
18
23
19 pub struct FilesCommand {}
24 pub struct FilesCommand<'a> {
20
25 rev: Option<&'a str>,
21 impl FilesCommand {
22 pub fn new() -> Self {
23 FilesCommand {}
24 }
25 }
26 }
26
27
27 impl Command for FilesCommand {
28 impl<'a> FilesCommand<'a> {
28 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
29 pub fn new(rev: Option<&'a str>) -> Self {
29 let root = FindRoot::new().run()?;
30 FilesCommand { rev }
30 let mut operation = ListDirstateTrackedFiles::new(&root)
31 }
31 .map_err(map_dirstate_error)?;
32 let files = operation.run().map_err(map_dirstate_error)?;
33
32
33 fn display_files(
34 &self,
35 ui: &Ui,
36 root: &PathBuf,
37 files: impl IntoIterator<Item = &'a HgPath>,
38 ) -> Result<(), CommandError> {
34 let cwd = std::env::current_dir()
39 let cwd = std::env::current_dir()
35 .or_else(|e| Err(CommandErrorKind::CurrentDirNotFound(e)))?;
40 .or_else(|e| Err(CommandErrorKind::CurrentDirNotFound(e)))?;
36 let rooted_cwd = cwd
41 let rooted_cwd = cwd
@@ -49,7 +54,69 b' impl Command for FilesCommand {'
49 }
54 }
50 }
55 }
51
56
52 /// Convert operation errors to command errors
57 impl<'a> Command for FilesCommand<'a> {
58 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
59 let root = FindRoot::new().run()?;
60 if let Some(rev) = self.rev {
61 let mut operation = ListRevTrackedFiles::new(&root, rev)
62 .map_err(|e| map_rev_error(rev, e))?;
63 let files = operation.run().map_err(|e| map_rev_error(rev, e))?;
64 self.display_files(ui, &root, files)
65 } else {
66 let mut operation = ListDirstateTrackedFiles::new(&root)
67 .map_err(map_dirstate_error)?;
68 let files = operation.run().map_err(map_dirstate_error)?;
69 self.display_files(ui, &root, files)
70 }
71 }
72 }
73
74 /// Convert `ListRevTrackedFilesErrorKind` to `CommandError`
75 fn map_rev_error(rev: &str, err: ListRevTrackedFilesError) -> CommandError {
76 CommandError {
77 kind: match err.kind {
78 ListRevTrackedFilesErrorKind::IoError(err) => {
79 CommandErrorKind::Abort(Some(
80 utf8_to_local(&format!("abort: {}\n", err)).into(),
81 ))
82 }
83 ListRevTrackedFilesErrorKind::InvalidRevision => {
84 CommandErrorKind::Abort(Some(
85 utf8_to_local(&format!(
86 "abort: invalid revision identifier{}\n",
87 rev
88 ))
89 .into(),
90 ))
91 }
92 ListRevTrackedFilesErrorKind::UnsuportedRevlogVersion(version) => {
93 CommandErrorKind::Abort(Some(
94 utf8_to_local(&format!(
95 "abort: unsupported revlog version {}\n",
96 version
97 ))
98 .into(),
99 ))
100 }
101 ListRevTrackedFilesErrorKind::CorruptedRevlog => {
102 CommandErrorKind::Abort(Some(
103 "abort: corrupted revlog\n".into(),
104 ))
105 }
106 ListRevTrackedFilesErrorKind::UnknowRevlogDataFormat(format) => {
107 CommandErrorKind::Abort(Some(
108 utf8_to_local(&format!(
109 "abort: unknow revlog dataformat {:?}\n",
110 format
111 ))
112 .into(),
113 ))
114 }
115 },
116 }
117 }
118
119 /// Convert `ListDirstateTrackedFilesError` to `CommandError`
53 fn map_dirstate_error(err: ListDirstateTrackedFilesError) -> CommandError {
120 fn map_dirstate_error(err: ListDirstateTrackedFilesError) -> CommandError {
54 CommandError {
121 CommandError {
55 kind: match err.kind {
122 kind: match err.kind {
@@ -26,7 +26,16 b' fn main() {'
26 SubCommand::with_name("root").about(commands::root::HELP_TEXT),
26 SubCommand::with_name("root").about(commands::root::HELP_TEXT),
27 )
27 )
28 .subcommand(
28 .subcommand(
29 SubCommand::with_name("files").about(commands::files::HELP_TEXT),
29 SubCommand::with_name("files")
30 .arg(
31 Arg::with_name("rev")
32 .help("search the repository as it is in REV")
33 .short("-r")
34 .long("--revision")
35 .value_name("REV")
36 .takes_value(true),
37 )
38 .about(commands::files::HELP_TEXT),
30 )
39 )
31 .subcommand(
40 .subcommand(
32 SubCommand::with_name("debugdata")
41 SubCommand::with_name("debugdata")
@@ -86,7 +95,9 b' fn match_subcommand('
86 ) -> Result<(), CommandError> {
95 ) -> Result<(), CommandError> {
87 match matches.subcommand() {
96 match matches.subcommand() {
88 ("root", _) => commands::root::RootCommand::new().run(&ui),
97 ("root", _) => commands::root::RootCommand::new().run(&ui),
89 ("files", _) => commands::files::FilesCommand::new().run(&ui),
98 ("files", Some(matches)) => {
99 commands::files::FilesCommand::try_from(matches)?.run(&ui)
100 }
90 ("debugdata", Some(matches)) => {
101 ("debugdata", Some(matches)) => {
91 commands::debugdata::DebugDataCommand::try_from(matches)?.run(&ui)
102 commands::debugdata::DebugDataCommand::try_from(matches)?.run(&ui)
92 }
103 }
@@ -94,6 +105,15 b' fn match_subcommand('
94 }
105 }
95 }
106 }
96
107
108 impl<'a> TryFrom<&'a ArgMatches<'_>> for commands::files::FilesCommand<'a> {
109 type Error = CommandError;
110
111 fn try_from(args: &'a ArgMatches) -> Result<Self, Self::Error> {
112 let rev = args.value_of("rev");
113 Ok(commands::files::FilesCommand::new(rev))
114 }
115 }
116
97 impl<'a> TryFrom<&'a ArgMatches<'_>>
117 impl<'a> TryFrom<&'a ArgMatches<'_>>
98 for commands::debugdata::DebugDataCommand<'a>
118 for commands::debugdata::DebugDataCommand<'a>
99 {
119 {
General Comments 0
You need to be logged in to leave comments. Login now