Mercurial > public > mercurial-scm > hg-stable
annotate rust/rhg/src/commands/files.rs @ 50036:95ffa065204e
rhg-files: reuse centralized dirstate logic
The `files` logic predates the centralized dirstate logic. It was duplicated,
an didn't receive bugfixes along the way.
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Wed, 11 Jan 2023 16:29:29 +0100 |
parents | e43f91244de2 |
children | 795b5b01cbd2 |
rev | line source |
---|---|
46502
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; |
45384
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
2 use crate::ui::Ui; |
48492
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48457
diff
changeset
|
3 use crate::utils::path_utils::RelativizePaths; |
46553
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
4 use clap::Arg; |
48391
10c32e1b892a
rhg: Propogate manifest parse errors instead of panicking
Simon Sapin <simon.sapin@octobus.net>
parents:
48186
diff
changeset
|
5 use hg::errors::HgError; |
46504
252d1bdba33d
rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents:
46502
diff
changeset
|
6 use hg::operations::list_rev_tracked_files; |
46167
8a4914397d02
rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents:
46136
diff
changeset
|
7 use hg::repo::Repo; |
50036
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
8 use hg::utils::filter_map_results; |
48186
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
46925
diff
changeset
|
9 use hg::utils::hg_path::HgPath; |
50036
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
10 use rayon::prelude::*; |
45384
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
11 |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
12 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
|
13 List tracked files. |
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 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
|
16 "; |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
17 |
49758
37bc3edef76f
rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48492
diff
changeset
|
18 pub fn args() -> clap::Command { |
37bc3edef76f
rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48492
diff
changeset
|
19 clap::command!("files") |
46553
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
20 .arg( |
49758
37bc3edef76f
rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48492
diff
changeset
|
21 Arg::new("rev") |
46553
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
22 .help("search the repository as it is in REV") |
49758
37bc3edef76f
rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48492
diff
changeset
|
23 .short('r') |
37bc3edef76f
rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48492
diff
changeset
|
24 .long("revision") |
37bc3edef76f
rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48492
diff
changeset
|
25 .value_name("REV"), |
46553
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
26 ) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
27 .about(HELP_TEXT) |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
28 } |
1ecaf09d9964
rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents:
46552
diff
changeset
|
29 |
46631
80840b651721
rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46555
diff
changeset
|
30 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> { |
46752
c184b490da37
rhg: Fall back to Python if ui.relative-paths is configured
Simon Sapin <simon.sapin@octobus.net>
parents:
46632
diff
changeset
|
31 let relative = invocation.config.get(b"ui", b"relative-paths"); |
c184b490da37
rhg: Fall back to Python if ui.relative-paths is configured
Simon Sapin <simon.sapin@octobus.net>
parents:
46632
diff
changeset
|
32 if relative.is_some() { |
c184b490da37
rhg: Fall back to Python if ui.relative-paths is configured
Simon Sapin <simon.sapin@octobus.net>
parents:
46632
diff
changeset
|
33 return Err(CommandError::unsupported( |
c184b490da37
rhg: Fall back to Python if ui.relative-paths is configured
Simon Sapin <simon.sapin@octobus.net>
parents:
46632
diff
changeset
|
34 "non-default ui.relative-paths", |
c184b490da37
rhg: Fall back to Python if ui.relative-paths is configured
Simon Sapin <simon.sapin@octobus.net>
parents:
46632
diff
changeset
|
35 )); |
c184b490da37
rhg: Fall back to Python if ui.relative-paths is configured
Simon Sapin <simon.sapin@octobus.net>
parents:
46632
diff
changeset
|
36 } |
c184b490da37
rhg: Fall back to Python if ui.relative-paths is configured
Simon Sapin <simon.sapin@octobus.net>
parents:
46632
diff
changeset
|
37 |
49758
37bc3edef76f
rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48492
diff
changeset
|
38 let rev = invocation.subcommand_args.get_one::<String>("rev"); |
45384
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
39 |
46632
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
40 let repo = invocation.repo?; |
48457
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
41 |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
42 // It seems better if this check is removed: this would correspond to |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
43 // automatically enabling the extension if the repo requires it. |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
44 // However we need this check to be in sync with vanilla hg so hg tests |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
45 // pass. |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
46 if repo.has_sparse() |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
47 && invocation.config.get(b"extensions", b"sparse").is_none() |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
48 { |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
49 return Err(CommandError::unsupported( |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
50 "repo is using sparse, but sparse extension is not enabled", |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
51 )); |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
52 } |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
53 |
46552
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
54 if let Some(rev) = rev { |
48457
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
55 if repo.has_narrow() { |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
56 return Err(CommandError::unsupported( |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
57 "rhg files -r <rev> is not supported in narrow clones", |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
58 )); |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
59 } |
49758
37bc3edef76f
rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48492
diff
changeset
|
60 let files = list_rev_tracked_files(repo, rev) |
37bc3edef76f
rhg: upgrade `clap` dependency
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48492
diff
changeset
|
61 .map_err(|e| (e, rev.as_ref()))?; |
46632
5ce2aa7c2ad5
rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents:
46631
diff
changeset
|
62 display_files(invocation.ui, repo, files.iter()) |
46552
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
63 } else { |
48457
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
64 // The dirstate always reflects the sparse narrowspec, so if |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
65 // we only have sparse without narrow all is fine. |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
66 // If we have narrow, then [hg files] needs to check if |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
67 // the store narrowspec is in sync with the one of the dirstate, |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
68 // so we can't support that without explicit code. |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
69 if repo.has_narrow() { |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
70 return Err(CommandError::unsupported( |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
71 "rhg files is not supported in narrow clones", |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
72 )); |
005ae1a343f8
rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents:
48391
diff
changeset
|
73 } |
50036
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
74 let dirstate = repo.dirstate_map()?; |
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
75 let files_res: Result<Vec<_>, _> = |
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
76 filter_map_results(dirstate.iter(), |(path, entry)| { |
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
77 Ok(if entry.tracked() { Some(path) } else { None }) |
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
78 }) |
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
79 .collect(); |
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
80 |
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
81 let mut files = files_res?; |
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
82 files.par_sort_unstable(); |
95ffa065204e
rhg-files: reuse centralized dirstate logic
Rapha?l Gom?s <rgomes@octobus.net>
parents:
50034
diff
changeset
|
83 |
48391
10c32e1b892a
rhg: Propogate manifest parse errors instead of panicking
Simon Sapin <simon.sapin@octobus.net>
parents:
48186
diff
changeset
|
84 display_files(invocation.ui, repo, files.into_iter().map(Ok)) |
45384
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
85 } |
5fe25f8ef5d9
rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
86 } |
45541
72b7d58d6e35
hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45449
diff
changeset
|
87 |
46552
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
88 fn display_files<'a>( |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
89 ui: &Ui, |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
90 repo: &Repo, |
48391
10c32e1b892a
rhg: Propogate manifest parse errors instead of panicking
Simon Sapin <simon.sapin@octobus.net>
parents:
48186
diff
changeset
|
91 files: impl IntoIterator<Item = Result<&'a HgPath, HgError>>, |
46552
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
92 ) -> Result<(), CommandError> { |
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
93 let mut stdout = ui.stdout_buffer(); |
48186
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
46925
diff
changeset
|
94 let mut any = false; |
46925
b5e8bf10436e
rhg: Make `files` work on repo-relative paths when possible
Simon Sapin <simon.sapin@octobus.net>
parents:
46758
diff
changeset
|
95 |
48492
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48457
diff
changeset
|
96 let relativize = RelativizePaths::new(repo)?; |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48457
diff
changeset
|
97 for result in files { |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48457
diff
changeset
|
98 let path = result?; |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48457
diff
changeset
|
99 stdout.write_all(&relativize.relativize(path))?; |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48457
diff
changeset
|
100 stdout.write_all(b"\n")?; |
48186
9ecf802b06e0
rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents:
46925
diff
changeset
|
101 any = true; |
48492
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48457
diff
changeset
|
102 } |
9b0e1f64656f
rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents:
48457
diff
changeset
|
103 |
46552
184e46550dc8
rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents:
46543
diff
changeset
|
104 stdout.flush()?; |
46758
63bfcddddac1
rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents:
46753
diff
changeset
|
105 if any { |
63bfcddddac1
rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents:
46753
diff
changeset
|
106 Ok(()) |
63bfcddddac1
rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents:
46753
diff
changeset
|
107 } else { |
63bfcddddac1
rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents:
46753
diff
changeset
|
108 Err(CommandError::Unsuccessful) |
63bfcddddac1
rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents:
46753
diff
changeset
|
109 } |
45543
2f8227a12592
rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45541
diff
changeset
|
110 } |