##// END OF EJS Templates
rhg: Make `files` work on repo-relative paths when possible...
Simon Sapin -
r47687:b5e8bf10 default
parent child Browse files
Show More
@@ -1,77 +1,91 b''
1 use crate::error::CommandError;
1 use crate::error::CommandError;
2 use crate::ui::Ui;
2 use crate::ui::Ui;
3 use clap::Arg;
3 use clap::Arg;
4 use hg::operations::list_rev_tracked_files;
4 use hg::operations::list_rev_tracked_files;
5 use hg::operations::Dirstate;
5 use hg::operations::Dirstate;
6 use hg::repo::Repo;
6 use hg::repo::Repo;
7 use hg::utils::current_dir;
7 use hg::utils::current_dir;
8 use hg::utils::files::{get_bytes_from_path, relativize_path};
8 use hg::utils::files::{get_bytes_from_path, relativize_path};
9 use hg::utils::hg_path::{HgPath, HgPathBuf};
9 use hg::utils::hg_path::{HgPath, HgPathBuf};
10
10
11 pub const HELP_TEXT: &str = "
11 pub const HELP_TEXT: &str = "
12 List tracked files.
12 List tracked files.
13
13
14 Returns 0 on success.
14 Returns 0 on success.
15 ";
15 ";
16
16
17 pub fn args() -> clap::App<'static, 'static> {
17 pub fn args() -> clap::App<'static, 'static> {
18 clap::SubCommand::with_name("files")
18 clap::SubCommand::with_name("files")
19 .arg(
19 .arg(
20 Arg::with_name("rev")
20 Arg::with_name("rev")
21 .help("search the repository as it is in REV")
21 .help("search the repository as it is in REV")
22 .short("-r")
22 .short("-r")
23 .long("--revision")
23 .long("--revision")
24 .value_name("REV")
24 .value_name("REV")
25 .takes_value(true),
25 .takes_value(true),
26 )
26 )
27 .about(HELP_TEXT)
27 .about(HELP_TEXT)
28 }
28 }
29
29
30 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
30 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
31 let relative = invocation.config.get(b"ui", b"relative-paths");
31 let relative = invocation.config.get(b"ui", b"relative-paths");
32 if relative.is_some() {
32 if relative.is_some() {
33 return Err(CommandError::unsupported(
33 return Err(CommandError::unsupported(
34 "non-default ui.relative-paths",
34 "non-default ui.relative-paths",
35 ));
35 ));
36 }
36 }
37
37
38 let rev = invocation.subcommand_args.value_of("rev");
38 let rev = invocation.subcommand_args.value_of("rev");
39
39
40 let repo = invocation.repo?;
40 let repo = invocation.repo?;
41 if let Some(rev) = rev {
41 if let Some(rev) = rev {
42 let files = list_rev_tracked_files(repo, rev).map_err(|e| (e, rev))?;
42 let files = list_rev_tracked_files(repo, rev).map_err(|e| (e, rev))?;
43 display_files(invocation.ui, repo, files.iter())
43 display_files(invocation.ui, repo, files.iter())
44 } else {
44 } else {
45 let distate = Dirstate::new(repo)?;
45 let distate = Dirstate::new(repo)?;
46 let files = distate.tracked_files()?;
46 let files = distate.tracked_files()?;
47 display_files(invocation.ui, repo, files)
47 display_files(invocation.ui, repo, files)
48 }
48 }
49 }
49 }
50
50
51 fn display_files<'a>(
51 fn display_files<'a>(
52 ui: &Ui,
52 ui: &Ui,
53 repo: &Repo,
53 repo: &Repo,
54 files: impl IntoIterator<Item = &'a HgPath>,
54 files: impl IntoIterator<Item = &'a HgPath>,
55 ) -> Result<(), CommandError> {
55 ) -> Result<(), CommandError> {
56 let cwd = HgPathBuf::from(get_bytes_from_path(hg::utils::current_dir()?));
56 let mut stdout = ui.stdout_buffer();
57
58 let cwd = current_dir()?;
57 let working_directory = repo.working_directory_path();
59 let working_directory = repo.working_directory_path();
58 let working_directory = current_dir()?.join(working_directory); // Make it absolute
60 let working_directory = cwd.join(working_directory); // Make it absolute
61
62 let mut any = false;
63 if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&working_directory) {
64 // The current directory is inside the repo, so we can work with
65 // relative paths
66 let cwd = HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo));
67 for file in files {
68 any = true;
69 stdout.write_all(relativize_path(&file, &cwd).as_ref())?;
70 stdout.write_all(b"\n")?;
71 }
72 } else {
59 let working_directory =
73 let working_directory =
60 HgPathBuf::from(get_bytes_from_path(working_directory));
74 HgPathBuf::from(get_bytes_from_path(working_directory));
61
75 let cwd = HgPathBuf::from(get_bytes_from_path(cwd));
62 let mut stdout = ui.stdout_buffer();
63
64 let mut any = false;
65 for file in files {
76 for file in files {
66 any = true;
77 any = true;
78 // Absolute path in the filesystem
67 let file = working_directory.join(file);
79 let file = working_directory.join(file);
68 stdout.write_all(relativize_path(&file, &cwd).as_ref())?;
80 stdout.write_all(relativize_path(&file, &cwd).as_ref())?;
69 stdout.write_all(b"\n")?;
81 stdout.write_all(b"\n")?;
70 }
82 }
83 }
84
71 stdout.flush()?;
85 stdout.flush()?;
72 if any {
86 if any {
73 Ok(())
87 Ok(())
74 } else {
88 } else {
75 Err(CommandError::Unsuccessful)
89 Err(CommandError::Unsuccessful)
76 }
90 }
77 }
91 }
General Comments 0
You need to be logged in to leave comments. Login now