author | Simon Sapin <simon.sapin@octobus.net> |
Thu, 28 Jan 2021 20:31:42 +0100 | |
changeset 46446 | 1dcd9c9975ed |
parent 46445 | ca3f73cc3cf4 |
child 46484 | a6e4e4650bac |
permissions | -rw-r--r-- |
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; |
46434
3e2d539d0d1a
rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents:
46167
diff
changeset
|
2 |
use crate::error::CommandError; |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
3 |
use crate::ui::Ui; |
46436
252d1bdba33d
rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents:
46434
diff
changeset
|
4 |
use hg::operations::list_rev_tracked_files; |
252d1bdba33d
rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents:
46434
diff
changeset
|
5 |
use hg::operations::Dirstate; |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
6 |
use hg::repo::Repo; |
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
|
7 |
use hg::utils::files::{get_bytes_from_path, relativize_path}; |
45537
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
8 |
use hg::utils::hg_path::{HgPath, HgPathBuf}; |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
9 |
|
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
10 |
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
|
11 |
List tracked files. |
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 |
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
|
14 |
"; |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
15 |
|
45537
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
16 |
pub struct FilesCommand<'a> { |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
17 |
rev: Option<&'a str>, |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
18 |
} |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
19 |
|
45537
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
20 |
impl<'a> FilesCommand<'a> { |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
21 |
pub fn new(rev: Option<&'a str>) -> Self { |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
22 |
FilesCommand { rev } |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
23 |
} |
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
24 |
|
45537
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
25 |
fn display_files( |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
26 |
&self, |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
27 |
ui: &Ui, |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
28 |
repo: &Repo, |
45537
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
29 |
files: impl IntoIterator<Item = &'a HgPath>, |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
30 |
) -> Result<(), CommandError> { |
46445
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
46436
diff
changeset
|
31 |
let cwd = hg::utils::current_dir()?; |
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
|
32 |
let rooted_cwd = cwd |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
33 |
.strip_prefix(repo.working_directory_path()) |
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 |
.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
|
35 |
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
|
36 |
|
45438
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45436
diff
changeset
|
37 |
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
|
38 |
|
45364
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
39 |
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
|
40 |
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
|
41 |
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
|
42 |
} |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
43 |
stdout.flush()?; |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
44 |
Ok(()) |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
45 |
} |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
46 |
} |
45535
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45438
diff
changeset
|
47 |
|
45537
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
48 |
impl<'a> Command for FilesCommand<'a> { |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
49 |
fn run(&self, ui: &Ui) -> Result<(), CommandError> { |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
50 |
let repo = Repo::find()?; |
45537
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
51 |
if let Some(rev) = self.rev { |
46436
252d1bdba33d
rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents:
46434
diff
changeset
|
52 |
let files = |
252d1bdba33d
rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents:
46434
diff
changeset
|
53 |
list_rev_tracked_files(&repo, rev).map_err(|e| (e, rev))?; |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
54 |
self.display_files(ui, &repo, files.iter()) |
45537
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
55 |
} else { |
46436
252d1bdba33d
rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents:
46434
diff
changeset
|
56 |
let distate = Dirstate::new(&repo)?; |
252d1bdba33d
rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents:
46434
diff
changeset
|
57 |
let files = distate.tracked_files()?; |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46135
diff
changeset
|
58 |
self.display_files(ui, &repo, files) |
45537
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
59 |
} |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
60 |
} |
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45535
diff
changeset
|
61 |
} |