Show More
@@ -0,0 +1,48 b'' | |||
|
1 | // path utils module | |
|
2 | // | |
|
3 | // This software may be used and distributed according to the terms of the | |
|
4 | // GNU General Public License version 2 or any later version. | |
|
5 | ||
|
6 | use crate::error::CommandError; | |
|
7 | use crate::ui::UiError; | |
|
8 | use hg::repo::Repo; | |
|
9 | use hg::utils::current_dir; | |
|
10 | use hg::utils::files::{get_bytes_from_path, relativize_path}; | |
|
11 | use hg::utils::hg_path::HgPath; | |
|
12 | use hg::utils::hg_path::HgPathBuf; | |
|
13 | use std::borrow::Cow; | |
|
14 | ||
|
15 | pub fn relativize_paths( | |
|
16 | repo: &Repo, | |
|
17 | paths: impl IntoIterator<Item = impl AsRef<HgPath>>, | |
|
18 | mut callback: impl FnMut(Cow<[u8]>) -> Result<(), UiError>, | |
|
19 | ) -> Result<(), CommandError> { | |
|
20 | let cwd = current_dir()?; | |
|
21 | let repo_root = repo.working_directory_path(); | |
|
22 | let repo_root = cwd.join(repo_root); // Make it absolute | |
|
23 | let repo_root_hgpath = | |
|
24 | HgPathBuf::from(get_bytes_from_path(repo_root.to_owned())); | |
|
25 | let outside_repo: bool; | |
|
26 | let cwd_hgpath: HgPathBuf; | |
|
27 | ||
|
28 | if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&repo_root) { | |
|
29 | // The current directory is inside the repo, so we can work with | |
|
30 | // relative paths | |
|
31 | outside_repo = false; | |
|
32 | cwd_hgpath = | |
|
33 | HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo)); | |
|
34 | } else { | |
|
35 | outside_repo = true; | |
|
36 | cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd)); | |
|
37 | } | |
|
38 | ||
|
39 | for file in paths { | |
|
40 | if outside_repo { | |
|
41 | let file = repo_root_hgpath.join(file.as_ref()); | |
|
42 | callback(relativize_path(&file, &cwd_hgpath))?; | |
|
43 | } else { | |
|
44 | callback(relativize_path(file.as_ref(), &cwd_hgpath))?; | |
|
45 | } | |
|
46 | } | |
|
47 | Ok(()) | |
|
48 | } |
@@ -1,12 +1,13 b'' | |||
|
1 | 1 | use crate::error::CommandError; |
|
2 | 2 | use crate::ui::Ui; |
|
3 | use crate::ui::UiError; | |
|
4 | use crate::utils::path_utils::relativize_paths; | |
|
3 | 5 | use clap::Arg; |
|
4 | 6 | use hg::operations::list_rev_tracked_files; |
|
5 | 7 | use hg::operations::Dirstate; |
|
6 | 8 | use hg::repo::Repo; |
|
7 | use hg::utils::current_dir; | |
|
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; | |
|
10 | use std::borrow::Cow; | |
|
10 | 11 | |
|
11 | 12 | pub const HELP_TEXT: &str = " |
|
12 | 13 | List tracked files. |
@@ -54,34 +55,13 b" fn display_files<'a>(" | |||
|
54 | 55 | files: impl IntoIterator<Item = &'a HgPath>, |
|
55 | 56 | ) -> Result<(), CommandError> { |
|
56 | 57 | let mut stdout = ui.stdout_buffer(); |
|
57 | ||
|
58 | let cwd = current_dir()?; | |
|
59 | let working_directory = repo.working_directory_path(); | |
|
60 | let working_directory = cwd.join(working_directory); // Make it absolute | |
|
58 | let mut any = false; | |
|
61 | 59 | |
|
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 { | |
|
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 | ||
|
60 | relativize_paths(repo, files, |path: Cow<[u8]>| -> Result<(), UiError> { | |
|
61 | any = true; | |
|
62 | stdout.write_all(path.as_ref())?; | |
|
63 | stdout.write_all(b"\n") | |
|
64 | })?; | |
|
85 | 65 | stdout.flush()?; |
|
86 | 66 | if any { |
|
87 | 67 | Ok(()) |
General Comments 0
You need to be logged in to leave comments.
Login now