Patchwork D10910: rhg: refactor code a little around printing of relative paths

login
register
mail settings
Submitter phabricator
Date June 25, 2021, 9:34 a.m.
Message ID <differential-rev-PHID-DREV-z2lvmqtc4rlad7sk3pqj-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49224/
State Superseded
Headers show

Comments

phabricator - June 25, 2021, 9:34 a.m.
pulkit created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  It's easier to understand when setup to compute relative path is decoupled from
  actual iteration over list of files.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/rhg/src/commands/files.rs

CHANGE DETAILS




To: pulkit, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

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
@@ -58,28 +58,32 @@ 
     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 mut any = false;
+    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
-        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")?;
-        }
+        outside_repo = false;
+        cwd_hgpath =
+            HgPathBuf::from(get_bytes_from_path(cwd_relative_to_repo));
     } 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")?;
+        outside_repo = true;
+        cwd_hgpath = HgPathBuf::from(get_bytes_from_path(cwd));
+    }
+
+    let mut any = false;
+    for file in files {
+        any = true;
+        if outside_repo {
+            let file = working_directory_hgpath.join(file);
+            stdout.write_all(relativize_path(&file, &cwd_hgpath).as_ref())?;
+        } else {
+            stdout.write_all(relativize_path(&file, &cwd_hgpath).as_ref())?;
         }
+        stdout.write_all(b"\n")?;
     }
 
     stdout.flush()?;