[Request] [+-- ] D10913: rhg: refactor relative path resolution in utility fn

pulkit (Pulkit Goyal) phabricator at mercurial-scm.org
Fri Jun 25 17:48:00 UTC 2021


pulkit created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  The same code is being used in `rhg files` and `rhg status` and I expect it to
  be used at more places in upcoming future. So let's have a utility function for
  that.
  
  `rhg files` still doesn't use this codepath but will start using in upcoming
  patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10913

AFFECTED FILES
  rust/rhg/src/commands/status.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::HgPathBuf;
+use hg::HgPathCow;
+use std::borrow::Cow;
+
+pub fn relativize_paths(
+    repo: &Repo,
+    paths: &[HgPathCow],
+    callback: &dyn Fn(Cow<[u8]>) -> Result<(), UiError>,
+) -> Result<(), CommandError> {
+    let cwd = current_dir()?;
+    let working_directory = repo.working_directory_path();
+    let working_directory = cwd.join(working_directory); // Make it absolute
+    let working_directory_hgpath =
+        HgPathBuf::from(get_bytes_from_path(working_directory.to_owned()));
+    let outside_repo: bool;
+    let cwd_hgpath: HgPathBuf;
+
+    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
+        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 = working_directory_hgpath.join(file);
+            callback(relativize_path(&file, &cwd_hgpath))?;
+        } else {
+            callback(relativize_path(file, &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/status.rs b/rust/rhg/src/commands/status.rs
--- a/rust/rhg/src/commands/status.rs
+++ b/rust/rhg/src/commands/status.rs
@@ -7,6 +7,7 @@
 
 use crate::error::CommandError;
 use crate::ui::{Ui, UiError};
+use crate::utils::path_utils::relativize_paths;
 use clap::{Arg, SubCommand};
 use hg;
 use hg::config::Config;
@@ -17,13 +18,11 @@
 use hg::operations::cat;
 use hg::repo::Repo;
 use hg::revlog::node::Node;
-use hg::utils::current_dir;
-use hg::utils::files::{get_bytes_from_path, relativize_path};
-use hg::utils::hg_path::HgPathBuf;
 use hg::utils::hg_path::{hg_path_to_os_string, HgPath};
 use hg::StatusError;
 use hg::{HgPathCow, StatusOptions};
 use log::{info, warn};
+use std::borrow::Cow;
 use std::convert::TryInto;
 use std::fs;
 use std::io::BufReader;
@@ -291,46 +290,12 @@
         .get_bool(b"commands", b"status.relative")
         .unwrap_or(relative);
     if relative && !ui.plain() {
-        let cwd = current_dir()?;
-        let working_directory = repo.working_directory_path();
-        let working_directory = cwd.join(working_directory); // Make it absolute
-        let working_directory_hgpath =
-            HgPathBuf::from(get_bytes_from_path(working_directory.to_owned()));
-        let outside_repo: bool;
-        let cwd_hgpath: HgPathBuf;
-
-        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
-            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));
-        }
-
-        let print_path = |path: &HgPath| -> Result<(), UiError> {
+        let print_path = |path: Cow<[u8]>| -> Result<(), UiError> {
             ui.write_stdout(
-                &[
-                    status_prefix,
-                    b" ",
-                    relativize_path(path, &cwd_hgpath).as_ref(),
-                    b"\n",
-                ]
-                .concat(),
+                &[status_prefix, b" ", path.as_ref(), b"\n"].concat(),
             )
         };
-
-        for file in paths {
-            if outside_repo {
-                let file = working_directory_hgpath.join(file);
-                print_path(&file)?;
-            } else {
-                print_path(file)?;
-            }
-        }
+        relativize_paths(repo, paths, &print_path)?;
     } else {
         for path in paths {
             // Same TODO as in commands::root



To: pulkit, #hg-reviewers
Cc: mercurial-patches, mercurial-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mercurial-scm.org/pipermail/mercurial-patches/attachments/20210625/821365e8/attachment-0001.html>


More information about the Mercurial-patches mailing list