annotate rust/rhg/src/commands/files.rs @ 50540:9db197c73138

rhg: support `rhg files` with `ui.relative-paths=false`
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Mon, 29 May 2023 17:04:14 +0100
parents 74e4dbb0fcd5
children 788113f056d4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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;
50540
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
2 use crate::ui::{
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
3 print_narrow_sparse_warnings, relative_paths, RelativePaths, Ui,
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
4 };
48453
9b0e1f64656f rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents: 48409
diff changeset
5 use crate::utils::path_utils::RelativizePaths;
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
6 use clap::Arg;
49984
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
7 use hg::narrow;
46436
252d1bdba33d rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents: 46434
diff changeset
8 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: 46135
diff changeset
9 use hg::repo::Repo;
49980
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
10 use hg::utils::filter_map_results;
48174
9ecf802b06e0 rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46925
diff changeset
11 use hg::utils::hg_path::HgPath;
49980
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
12 use rayon::prelude::*;
45364
5fe25f8ef5d9 rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
13
5fe25f8ef5d9 rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
14 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
15 List tracked files.
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 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
18 ";
5fe25f8ef5d9 rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
19
49640
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 48453
diff changeset
20 pub fn args() -> clap::Command {
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 48453
diff changeset
21 clap::command!("files")
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
22 .arg(
49640
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 48453
diff changeset
23 Arg::new("rev")
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
24 .help("search the repository as it is in REV")
49640
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 48453
diff changeset
25 .short('r')
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 48453
diff changeset
26 .long("revision")
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 48453
diff changeset
27 .value_name("REV"),
46501
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
28 )
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
29 .about(HELP_TEXT)
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
30 }
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46500
diff changeset
31
46592
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46503
diff changeset
32 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
50540
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
33 let relative_paths = match relative_paths(invocation.config)? {
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
34 RelativePaths::Legacy => true,
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
35 RelativePaths::Bool(v) => v,
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
36 };
46739
c184b490da37 rhg: Fall back to Python if ui.relative-paths is configured
Simon Sapin <simon.sapin@octobus.net>
parents: 46593
diff changeset
37
49640
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 48453
diff changeset
38 let rev = invocation.subcommand_args.get_one::<String>("rev");
45364
5fe25f8ef5d9 rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
39
46593
5ce2aa7c2ad5 rhg: Move `Repo` object creation into `main()`
Simon Sapin <simon.sapin@octobus.net>
parents: 46592
diff changeset
40 let repo = invocation.repo?;
48409
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48342
diff changeset
41
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48342
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: 48342
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: 48342
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: 48342
diff changeset
45 // pass.
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48342
diff changeset
46 if repo.has_sparse()
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48342
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: 48342
diff changeset
48 {
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48342
diff changeset
49 return Err(CommandError::unsupported(
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48342
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: 48342
diff changeset
51 ));
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48342
diff changeset
52 }
005ae1a343f8 rhg: add support for narrow clones and sparse checkouts
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48342
diff changeset
53
49985
e57f76c28f7b rhg-files: add support for narrow when specifying a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 49984
diff changeset
54 let (narrow_matcher, narrow_warnings) = narrow::matcher(repo)?;
e57f76c28f7b rhg-files: add support for narrow when specifying a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 49984
diff changeset
55 print_narrow_sparse_warnings(&narrow_warnings, &[], invocation.ui, repo)?;
e57f76c28f7b rhg-files: add support for narrow when specifying a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 49984
diff changeset
56
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
57 if let Some(rev) = rev {
49985
e57f76c28f7b rhg-files: add support for narrow when specifying a revision
Raphaël Gomès <rgomes@octobus.net>
parents: 49984
diff changeset
58 let files = list_rev_tracked_files(repo, rev, narrow_matcher)
49640
37bc3edef76f rhg: upgrade `clap` dependency
Raphaël Gomès <rgomes@octobus.net>
parents: 48453
diff changeset
59 .map_err(|e| (e, rev.as_ref()))?;
50540
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
60 display_files(invocation.ui, repo, relative_paths, files.iter())
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
61 } else {
49984
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
62 // The dirstate always reflects the sparse narrowspec.
49980
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
63 let dirstate = repo.dirstate_map()?;
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
64 let files_res: Result<Vec<_>, _> =
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
65 filter_map_results(dirstate.iter(), |(path, entry)| {
49984
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
66 Ok(if entry.tracked() && narrow_matcher.matches(path) {
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
67 Some(path)
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
68 } else {
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
69 None
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
70 })
49980
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
71 })
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
72 .collect();
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
73
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
74 let mut files = files_res?;
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
75 files.par_sort_unstable();
95ffa065204e rhg-files: reuse centralized dirstate logic
Raphaël Gomès <rgomes@octobus.net>
parents: 49978
diff changeset
76
49984
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
77 display_files(
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
78 invocation.ui,
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
79 repo,
50540
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
80 relative_paths,
49984
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
81 files.into_iter().map::<Result<_, CommandError>, _>(Ok),
df9eabc9837b rust-narrow: enable narrow support for plain `rhg files`
Raphaël Gomès <rgomes@octobus.net>
parents: 49983
diff changeset
82 )
45364
5fe25f8ef5d9 rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
83 }
5fe25f8ef5d9 rhg: add a `Files` `Command` to prepare the `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
84 }
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45438
diff changeset
85
49983
795b5b01cbd2 rhg-files: make signature of `display_files` more flexible
Raphaël Gomès <rgomes@octobus.net>
parents: 49980
diff changeset
86 fn display_files<'a, E>(
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
87 ui: &Ui,
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
88 repo: &Repo,
50540
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
89 relative_paths: bool,
49983
795b5b01cbd2 rhg-files: make signature of `display_files` more flexible
Raphaël Gomès <rgomes@octobus.net>
parents: 49980
diff changeset
90 files: impl IntoIterator<Item = Result<&'a HgPath, E>>,
795b5b01cbd2 rhg-files: make signature of `display_files` more flexible
Raphaël Gomès <rgomes@octobus.net>
parents: 49980
diff changeset
91 ) -> Result<(), CommandError>
795b5b01cbd2 rhg-files: make signature of `display_files` more flexible
Raphaël Gomès <rgomes@octobus.net>
parents: 49980
diff changeset
92 where
795b5b01cbd2 rhg-files: make signature of `display_files` more flexible
Raphaël Gomès <rgomes@octobus.net>
parents: 49980
diff changeset
93 CommandError: From<E>,
795b5b01cbd2 rhg-files: make signature of `display_files` more flexible
Raphaël Gomès <rgomes@octobus.net>
parents: 49980
diff changeset
94 {
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
95 let mut stdout = ui.stdout_buffer();
48174
9ecf802b06e0 rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46925
diff changeset
96 let mut any = false;
46925
b5e8bf10436e rhg: Make `files` work on repo-relative paths when possible
Simon Sapin <simon.sapin@octobus.net>
parents: 46745
diff changeset
97
48453
9b0e1f64656f rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents: 48409
diff changeset
98 let relativize = RelativizePaths::new(repo)?;
9b0e1f64656f rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents: 48409
diff changeset
99 for result in files {
9b0e1f64656f rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents: 48409
diff changeset
100 let path = result?;
50540
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
101 if relative_paths {
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
102 stdout.write_all(&relativize.relativize(path))?;
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
103 } else {
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
104 stdout.write_all(path.as_bytes())?;
9db197c73138 rhg: support `rhg files` with `ui.relative-paths=false`
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 50539
diff changeset
105 }
48453
9b0e1f64656f rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents: 48409
diff changeset
106 stdout.write_all(b"\n")?;
48174
9ecf802b06e0 rhg: refactor function to relativize paths in utils
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46925
diff changeset
107 any = true;
48453
9b0e1f64656f rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents: 48409
diff changeset
108 }
9b0e1f64656f rhg: refactor relativize_path into a struct + method
Simon Sapin <simon.sapin@octobus.net>
parents: 48409
diff changeset
109
46500
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46484
diff changeset
110 stdout.flush()?;
46745
63bfcddddac1 rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents: 46740
diff changeset
111 if any {
63bfcddddac1 rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents: 46740
diff changeset
112 Ok(())
63bfcddddac1 rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents: 46740
diff changeset
113 } else {
63bfcddddac1 rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents: 46740
diff changeset
114 Err(CommandError::Unsuccessful)
63bfcddddac1 rhg: Exit with an error code if `files` finds nothing
Simon Sapin <simon.sapin@octobus.net>
parents: 46740
diff changeset
115 }
45537
2f8227a12592 rhg: add `--revision` argument to `rhg files`
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
116 }