rust/rhg/src/commands/files.rs
changeset 48174 9ecf802b06e0
parent 46925 b5e8bf10436e
child 48342 10c32e1b892a
equal deleted inserted replaced
48173:1d70fb83ff4a 48174:9ecf802b06e0
     1 use crate::error::CommandError;
     1 use crate::error::CommandError;
     2 use crate::ui::Ui;
     2 use crate::ui::Ui;
       
     3 use crate::ui::UiError;
       
     4 use crate::utils::path_utils::relativize_paths;
     3 use clap::Arg;
     5 use clap::Arg;
     4 use hg::operations::list_rev_tracked_files;
     6 use hg::operations::list_rev_tracked_files;
     5 use hg::operations::Dirstate;
     7 use hg::operations::Dirstate;
     6 use hg::repo::Repo;
     8 use hg::repo::Repo;
     7 use hg::utils::current_dir;
     9 use hg::utils::hg_path::HgPath;
     8 use hg::utils::files::{get_bytes_from_path, relativize_path};
    10 use std::borrow::Cow;
     9 use hg::utils::hg_path::{HgPath, HgPathBuf};
       
    10 
    11 
    11 pub const HELP_TEXT: &str = "
    12 pub const HELP_TEXT: &str = "
    12 List tracked files.
    13 List tracked files.
    13 
    14 
    14 Returns 0 on success.
    15 Returns 0 on success.
    52     ui: &Ui,
    53     ui: &Ui,
    53     repo: &Repo,
    54     repo: &Repo,
    54     files: impl IntoIterator<Item = &'a HgPath>,
    55     files: impl IntoIterator<Item = &'a HgPath>,
    55 ) -> Result<(), CommandError> {
    56 ) -> Result<(), CommandError> {
    56     let mut stdout = ui.stdout_buffer();
    57     let mut stdout = ui.stdout_buffer();
       
    58     let mut any = false;
    57 
    59 
    58     let cwd = current_dir()?;
    60     relativize_paths(repo, files, |path: Cow<[u8]>| -> Result<(), UiError> {
    59     let working_directory = repo.working_directory_path();
    61         any = true;
    60     let working_directory = cwd.join(working_directory); // Make it absolute
    62         stdout.write_all(path.as_ref())?;
    61 
    63         stdout.write_all(b"\n")
    62     let mut any = false;
    64     })?;
    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 {
       
    73         let working_directory =
       
    74             HgPathBuf::from(get_bytes_from_path(working_directory));
       
    75         let cwd = HgPathBuf::from(get_bytes_from_path(cwd));
       
    76         for file in files {
       
    77             any = true;
       
    78             // Absolute path in the filesystem
       
    79             let file = working_directory.join(file);
       
    80             stdout.write_all(relativize_path(&file, &cwd).as_ref())?;
       
    81             stdout.write_all(b"\n")?;
       
    82         }
       
    83     }
       
    84 
       
    85     stdout.flush()?;
    65     stdout.flush()?;
    86     if any {
    66     if any {
    87         Ok(())
    67         Ok(())
    88     } else {
    68     } else {
    89         Err(CommandError::Unsuccessful)
    69         Err(CommandError::Unsuccessful)