Patchwork D11815: rhg: Add support for `rhg status -n`

login
register
mail settings
Submitter phabricator
Date Nov. 26, 2021, 3 p.m.
Message ID <differential-rev-PHID-DREV-sgma3snipvmejs7u5a7z-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50141/
State Superseded
Headers show

Comments

phabricator - Nov. 26, 2021, 3 p.m.
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  The `RHG_STATUS=1` bit added here can be removed when `unset RHG_STATUS` near
  the top of the file is removed.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/rhg/src/commands/status.rs
  tests/test-status.t

CHANGE DETAILS




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

Patch

diff --git a/tests/test-status.t b/tests/test-status.t
--- a/tests/test-status.t
+++ b/tests/test-status.t
@@ -222,6 +222,13 @@ 
   ! deleted
   ? unknown
 
+hg status -n:
+  $ env RHG_STATUS=1 RHG_ON_UNSUPPORTED=abort hg status -n
+  added
+  removed
+  deleted
+  unknown
+
 hg status modified added removed deleted unknown never-existed ignored:
 
   $ hg status modified added removed deleted unknown never-existed ignored
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
@@ -6,9 +6,10 @@ 
 // GNU General Public License version 2 or any later version.
 
 use crate::error::CommandError;
-use crate::ui::{Ui, UiError};
+use crate::ui::Ui;
 use crate::utils::path_utils::relativize_paths;
 use clap::{Arg, SubCommand};
+use format_bytes::format_bytes;
 use hg;
 use hg::config::Config;
 use hg::dirstate::{has_exec_bit, TruncatedTimestamp};
@@ -20,7 +21,6 @@ 
 use hg::utils::hg_path::{hg_path_to_os_string, HgPath};
 use hg::{HgPathCow, StatusOptions};
 use log::{info, warn};
-use std::borrow::Cow;
 
 pub const HELP_TEXT: &str = "
 Show changed files in the working directory
@@ -82,6 +82,12 @@ 
                 .short("-i")
                 .long("--ignored"),
         )
+        .arg(
+            Arg::with_name("no-status")
+                .help("hide status prefix")
+                .short("-n")
+                .long("--no-status"),
+        )
 }
 
 /// Pure data type allowing the caller to specify file states to display
@@ -182,6 +188,7 @@ 
             requested
         }
     };
+    let no_status = args.is_present("no-status");
 
     let repo = invocation.repo?;
     let mut dmap = repo.dirstate_map_mut()?;
@@ -240,25 +247,74 @@ 
         }
     }
     if display_states.modified {
-        display_status_paths(ui, repo, config, &mut ds_status.modified, b"M")?;
+        display_status_paths(
+            ui,
+            repo,
+            config,
+            no_status,
+            &mut ds_status.modified,
+            b"M",
+        )?;
     }
     if display_states.added {
-        display_status_paths(ui, repo, config, &mut ds_status.added, b"A")?;
+        display_status_paths(
+            ui,
+            repo,
+            config,
+            no_status,
+            &mut ds_status.added,
+            b"A",
+        )?;
     }
     if display_states.removed {
-        display_status_paths(ui, repo, config, &mut ds_status.removed, b"R")?;
+        display_status_paths(
+            ui,
+            repo,
+            config,
+            no_status,
+            &mut ds_status.removed,
+            b"R",
+        )?;
     }
     if display_states.deleted {
-        display_status_paths(ui, repo, config, &mut ds_status.deleted, b"!")?;
+        display_status_paths(
+            ui,
+            repo,
+            config,
+            no_status,
+            &mut ds_status.deleted,
+            b"!",
+        )?;
     }
     if display_states.unknown {
-        display_status_paths(ui, repo, config, &mut ds_status.unknown, b"?")?;
+        display_status_paths(
+            ui,
+            repo,
+            config,
+            no_status,
+            &mut ds_status.unknown,
+            b"?",
+        )?;
     }
     if display_states.ignored {
-        display_status_paths(ui, repo, config, &mut ds_status.ignored, b"I")?;
+        display_status_paths(
+            ui,
+            repo,
+            config,
+            no_status,
+            &mut ds_status.ignored,
+            b"I",
+        )?;
     }
     if display_states.clean {
-        display_status_paths(ui, repo, config, &mut ds_status.clean, b"C")?;
+        display_status_paths(
+            ui,
+            repo,
+            config,
+            no_status,
+            &mut ds_status.clean,
+            b"C",
+        )?;
     }
     Ok(())
 }
@@ -269,6 +325,7 @@ 
     ui: &Ui,
     repo: &Repo,
     config: &Config,
+    no_status: bool,
     paths: &mut [HgPathCow],
     status_prefix: &[u8],
 ) -> Result<(), CommandError> {
@@ -277,23 +334,23 @@ 
     relative = config
         .get_option(b"commands", b"status.relative")?
         .unwrap_or(relative);
+    let print_path = |path: &[u8]| {
+        // TODO optim, probably lots of unneeded copies here, especially
+        // if out stream is buffered
+        if no_status {
+            ui.write_stdout(&format_bytes!(b"{}\n", path))
+        } else {
+            ui.write_stdout(&format_bytes!(b"{} {}\n", status_prefix, path))
+        }
+    };
+
     if relative && !ui.plain() {
-        relativize_paths(
-            repo,
-            paths.iter().map(Ok),
-            |path: Cow<[u8]>| -> Result<(), UiError> {
-                ui.write_stdout(
-                    &[status_prefix, b" ", path.as_ref(), b"\n"].concat(),
-                )
-            },
-        )?;
+        relativize_paths(repo, paths.iter().map(Ok), |path| {
+            print_path(&path)
+        })?;
     } else {
         for path in paths {
-            // Same TODO as in commands::root
-            let bytes: &[u8] = path.as_bytes();
-            // TODO optim, probably lots of unneeded copies here, especially
-            // if out stream is buffered
-            ui.write_stdout(&[status_prefix, b" ", bytes, b"\n"].concat())?;
+            print_path(path.as_bytes())?
         }
     }
     Ok(())