rust/rhg/src/commands/files.rs
changeset 50540 9db197c73138
parent 50539 74e4dbb0fcd5
child 50867 788113f056d4
equal deleted inserted replaced
50539:74e4dbb0fcd5 50540:9db197c73138
     1 use crate::error::CommandError;
     1 use crate::error::CommandError;
     2 use crate::ui::{print_narrow_sparse_warnings, Ui, RelativePaths, relative_paths};
     2 use crate::ui::{
       
     3     print_narrow_sparse_warnings, relative_paths, RelativePaths, Ui,
       
     4 };
     3 use crate::utils::path_utils::RelativizePaths;
     5 use crate::utils::path_utils::RelativizePaths;
     4 use clap::Arg;
     6 use clap::Arg;
     5 use hg::narrow;
     7 use hg::narrow;
     6 use hg::operations::list_rev_tracked_files;
     8 use hg::operations::list_rev_tracked_files;
     7 use hg::repo::Repo;
     9 use hg::repo::Repo;
    26         )
    28         )
    27         .about(HELP_TEXT)
    29         .about(HELP_TEXT)
    28 }
    30 }
    29 
    31 
    30 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
    32 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
    31     match relative_paths(invocation.config)? {
    33     let relative_paths = match relative_paths(invocation.config)? {
    32       RelativePaths::Legacy | RelativePaths::Bool(true) => (),
    34         RelativePaths::Legacy => true,
    33       RelativePaths::Bool(false) => {
    35         RelativePaths::Bool(v) => v,
    34         return Err(CommandError::unsupported(
    36     };
    35             "non-default ui.relative-paths",
       
    36         ));
       
    37       }
       
    38     }
       
    39 
    37 
    40     let rev = invocation.subcommand_args.get_one::<String>("rev");
    38     let rev = invocation.subcommand_args.get_one::<String>("rev");
    41 
    39 
    42     let repo = invocation.repo?;
    40     let repo = invocation.repo?;
    43 
    41 
    57     print_narrow_sparse_warnings(&narrow_warnings, &[], invocation.ui, repo)?;
    55     print_narrow_sparse_warnings(&narrow_warnings, &[], invocation.ui, repo)?;
    58 
    56 
    59     if let Some(rev) = rev {
    57     if let Some(rev) = rev {
    60         let files = list_rev_tracked_files(repo, rev, narrow_matcher)
    58         let files = list_rev_tracked_files(repo, rev, narrow_matcher)
    61             .map_err(|e| (e, rev.as_ref()))?;
    59             .map_err(|e| (e, rev.as_ref()))?;
    62         display_files(invocation.ui, repo, files.iter())
    60         display_files(invocation.ui, repo, relative_paths, files.iter())
    63     } else {
    61     } else {
    64         // The dirstate always reflects the sparse narrowspec.
    62         // The dirstate always reflects the sparse narrowspec.
    65         let dirstate = repo.dirstate_map()?;
    63         let dirstate = repo.dirstate_map()?;
    66         let files_res: Result<Vec<_>, _> =
    64         let files_res: Result<Vec<_>, _> =
    67             filter_map_results(dirstate.iter(), |(path, entry)| {
    65             filter_map_results(dirstate.iter(), |(path, entry)| {
    77         files.par_sort_unstable();
    75         files.par_sort_unstable();
    78 
    76 
    79         display_files(
    77         display_files(
    80             invocation.ui,
    78             invocation.ui,
    81             repo,
    79             repo,
       
    80             relative_paths,
    82             files.into_iter().map::<Result<_, CommandError>, _>(Ok),
    81             files.into_iter().map::<Result<_, CommandError>, _>(Ok),
    83         )
    82         )
    84     }
    83     }
    85 }
    84 }
    86 
    85 
    87 fn display_files<'a, E>(
    86 fn display_files<'a, E>(
    88     ui: &Ui,
    87     ui: &Ui,
    89     repo: &Repo,
    88     repo: &Repo,
       
    89     relative_paths: bool,
    90     files: impl IntoIterator<Item = Result<&'a HgPath, E>>,
    90     files: impl IntoIterator<Item = Result<&'a HgPath, E>>,
    91 ) -> Result<(), CommandError>
    91 ) -> Result<(), CommandError>
    92 where
    92 where
    93     CommandError: From<E>,
    93     CommandError: From<E>,
    94 {
    94 {
    96     let mut any = false;
    96     let mut any = false;
    97 
    97 
    98     let relativize = RelativizePaths::new(repo)?;
    98     let relativize = RelativizePaths::new(repo)?;
    99     for result in files {
    99     for result in files {
   100         let path = result?;
   100         let path = result?;
   101         stdout.write_all(&relativize.relativize(path))?;
   101         if relative_paths {
       
   102             stdout.write_all(&relativize.relativize(path))?;
       
   103         } else {
       
   104             stdout.write_all(path.as_bytes())?;
       
   105         }
   102         stdout.write_all(b"\n")?;
   106         stdout.write_all(b"\n")?;
   103         any = true;
   107         any = true;
   104     }
   108     }
   105 
   109 
   106     stdout.flush()?;
   110     stdout.flush()?;