Mercurial > public > mercurial-scm > hg
annotate rust/rhg/src/commands/files.rs @ 45535:72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Use directly `ListDirstateTrackedFiles` rather than having an operation builder.
Differential Revision: https://phab.mercurial-scm.org/D9013
author | Antoine Cezar <antoine.cezar@octobus.net> |
---|---|
date | Wed, 09 Sep 2020 12:12:11 +0200 |
parents | ed95ccc94333 |
children | 2f8227a12592 |
rev | line source |
---|---|
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
1 use crate::commands::Command; |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
2 use crate::error::{CommandError, CommandErrorKind}; |
45535
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
3 use crate::ui::utf8_to_local; |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
4 use crate::ui::Ui; |
45535
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
5 use hg::operations::FindRoot; |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
6 use hg::operations::{ |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
7 ListDirstateTrackedFiles, ListDirstateTrackedFilesError, |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
8 ListDirstateTrackedFilesErrorKind, |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
9 }; |
45436
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
10 use hg::utils::files::{get_bytes_from_path, relativize_path}; |
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
11 use hg::utils::hg_path::HgPathBuf; |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
12 |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
13 pub const HELP_TEXT: &str = " |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
14 List tracked files. |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
15 |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
16 Returns 0 on success. |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
17 "; |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
18 |
45438
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45436
diff
changeset
|
19 pub struct FilesCommand {} |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
20 |
45438
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45436
diff
changeset
|
21 impl FilesCommand { |
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45436
diff
changeset
|
22 pub fn new() -> Self { |
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45436
diff
changeset
|
23 FilesCommand {} |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
24 } |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
25 } |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
26 |
45438
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45436
diff
changeset
|
27 impl Command for FilesCommand { |
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45436
diff
changeset
|
28 fn run(&self, ui: &Ui) -> Result<(), CommandError> { |
45535
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
29 let root = FindRoot::new().run()?; |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
30 let mut operation = ListDirstateTrackedFiles::new(&root) |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
31 .map_err(map_dirstate_error)?; |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
32 let files = operation.run().map_err(map_dirstate_error)?; |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
33 |
45436
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
34 let cwd = std::env::current_dir() |
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
35 .or_else(|e| Err(CommandErrorKind::CurrentDirNotFound(e)))?; |
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
36 let rooted_cwd = cwd |
45535
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
37 .strip_prefix(&root) |
45436
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
38 .expect("cwd was already checked within the repository"); |
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
39 let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd)); |
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
40 |
45438
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45436
diff
changeset
|
41 let mut stdout = ui.stdout_buffer(); |
45436
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
42 |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
43 for file in files { |
45436
1b3197047f5c
rhg: make output of `files` relative to the current directory and the root
Rapha?l Gom?s <rgomes@octobus.net>
parents:
45364
diff
changeset
|
44 stdout.write_all(relativize_path(file, &rooted_cwd).as_ref())?; |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
45 stdout.write_all(b"\n")?; |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
46 } |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
47 stdout.flush()?; |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
48 Ok(()) |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
49 } |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
50 } |
45535
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
51 |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
52 /// Convert operation errors to command errors |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
53 fn map_dirstate_error(err: ListDirstateTrackedFilesError) -> CommandError { |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
54 CommandError { |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
55 kind: match err.kind { |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
56 ListDirstateTrackedFilesErrorKind::IoError(err) => { |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
57 CommandErrorKind::Abort(Some( |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
58 utf8_to_local(&format!("abort: {}\n", err)).into(), |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
59 )) |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
60 } |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
61 ListDirstateTrackedFilesErrorKind::ParseError(_) => { |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
62 CommandErrorKind::Abort(Some( |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
63 // TODO find a better error message |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
64 b"abort: parse error\n".to_vec(), |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
65 )) |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
66 } |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
67 }, |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
68 } |
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
69 } |