[Updated] D11613: rhg: refactor function to relativize paths in utils
pulkit (Pulkit Goyal)
phabricator at mercurial-scm.org
Mon Oct 11 18:32:06 UTC 2021
Closed by commit rHG9ecf802b06e0: rhg: refactor function to relativize paths in utils (authored by pulkit).
This revision was automatically updated to reflect the committed changes.
REPOSITORY
rHG Mercurial
CHANGES SINCE LAST UPDATE
https://phab.mercurial-scm.org/D11613?vs=30674&id=30701
CHANGES SINCE LAST ACTION
https://phab.mercurial-scm.org/D11613/new/
REVISION DETAIL
https://phab.mercurial-scm.org/D11613
AFFECTED FILES
rust/rhg/src/commands/files.rs
rust/rhg/src/main.rs
rust/rhg/src/utils/path_utils.rs
CHANGE DETAILS
diff --git a/rust/rhg/src/utils/path_utils.rs b/rust/rhg/src/utils/path_utils.rs
new file mode 100644
--- /dev/null
+++ b/rust/rhg/src/utils/path_utils.rs
@@ -0,0 +1,48 @@
+// path utils module
+//
+// This software may be used and distributed according to the terms of the
+// GNU General Public License version 2 or any later version.
+
+use crate::error::CommandError;
+use crate::ui::UiError;
+use hg::repo::Repo;
+use hg::utils::current_dir;
+use hg::utils::files::{get_bytes_from_path, relativize_path};
+use hg::utils::hg_path::HgPath;
+use hg::utils::hg_path::HgPathBuf;
+use std::borrow::Cow;
+
+pub fn relativize_paths(
+ repo: &Repo,
+ paths: impl IntoIterator<Item = impl AsRef<HgPath>>,
+ mut callback: impl FnMut(Cow<[u8]>) -> Result<(), UiError>,
+) -> Result<(), CommandError> {
+ let cwd = current_dir()?;
+ let repo_root = repo.working_directory_path();
+ let repo_root = cwd.join(repo_root); // Make it absolute
+ let repo_root_hgpath =
+ HgPathBuf::from(get_bytes_from_path(repo_root.to_owned()));
+ let outside_repo: bool;
+ let cwd_hgpath: HgPathBuf;
+
+ if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&repo_root) {
+ // The current directory is inside the repo, so we can work with
+ // relative paths
+ outside_repo = false;
+ cwd_hgpath =
+ HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo));
+ } else {
+ outside_repo = true;
+ cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd));
+ }
+
+ for file in paths {
+ if outside_repo {
+ let file = repo_root_hgpath.join(file.as_ref());
+ callback(relativize_path(&file, &cwd_hgpath))?;
+ } else {
+ callback(relativize_path(file.as_ref(), &cwd_hgpath))?;
+ }
+ }
+ Ok(())
+}
diff --git a/rust/rhg/src/main.rs b/rust/rhg/src/main.rs
--- a/rust/rhg/src/main.rs
+++ b/rust/rhg/src/main.rs
@@ -17,6 +17,9 @@
mod blackbox;
mod error;
mod ui;
+pub mod utils {
+ pub mod path_utils;
+}
use error::CommandError;
fn main_with_result(
diff --git a/rust/rhg/src/commands/files.rs b/rust/rhg/src/commands/files.rs
--- a/rust/rhg/src/commands/files.rs
+++ b/rust/rhg/src/commands/files.rs
@@ -1,12 +1,13 @@
use crate::error::CommandError;
use crate::ui::Ui;
+use crate::ui::UiError;
+use crate::utils::path_utils::relativize_paths;
use clap::Arg;
use hg::operations::list_rev_tracked_files;
use hg::operations::Dirstate;
use hg::repo::Repo;
-use hg::utils::current_dir;
-use hg::utils::files::{get_bytes_from_path, relativize_path};
-use hg::utils::hg_path::{HgPath, HgPathBuf};
+use hg::utils::hg_path::HgPath;
+use std::borrow::Cow;
pub const HELP_TEXT: &str = "
List tracked files.
@@ -54,34 +55,13 @@
files: impl IntoIterator<Item = &'a HgPath>,
) -> Result<(), CommandError> {
let mut stdout = ui.stdout_buffer();
-
- let cwd = current_dir()?;
- let working_directory = repo.working_directory_path();
- let working_directory = cwd.join(working_directory); // Make it absolute
+ let mut any = false;
- let mut any = false;
- if let Ok(cwd_relative_to_repo) = cwd.strip_prefix(&working_directory) {
- // The current directory is inside the repo, so we can work with
- // relative paths
- let cwd = HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo));
- for file in files {
- any = true;
- stdout.write_all(relativize_path(&file, &cwd).as_ref())?;
- stdout.write_all(b"\n")?;
- }
- } else {
- let working_directory =
- HgPathBuf::from(get_bytes_from_path(working_directory));
- let cwd = HgPathBuf::from(get_bytes_from_path(cwd));
- for file in files {
- any = true;
- // Absolute path in the filesystem
- let file = working_directory.join(file);
- stdout.write_all(relativize_path(&file, &cwd).as_ref())?;
- stdout.write_all(b"\n")?;
- }
- }
-
+ relativize_paths(repo, files, |path: Cow<[u8]>| -> Result<(), UiError> {
+ any = true;
+ stdout.write_all(path.as_ref())?;
+ stdout.write_all(b"\n")
+ })?;
stdout.flush()?;
if any {
Ok(())
To: pulkit, #hg-reviewers, martinvonz
Cc: martinvonz, mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial-patches/attachments/20211011/1eff3d12/attachment-0002.html>
More information about the Mercurial-patches
mailing list