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