rhg: add support for calling `rhg cat` without a revision
authorRaphaël Gomès <rgomes@octobus.net>
Wed, 01 Sep 2021 16:13:25 +0200
changeset 48072 d919b0ca8449
parent 48071 adb367f0e9a2
child 48073 1e00834491a5
rhg: add support for calling `rhg cat` without a revision Turns out the necessary pieces were there already. Like the Python implementation, we default to the first parent of the dirstate. Differential Revision: https://phab.mercurial-scm.org/D11377
rust/rhg/src/commands/cat.rs
tests/test-rhg.t
--- a/rust/rhg/src/commands/cat.rs	Thu Sep 30 17:34:28 2021 +0200
+++ b/rust/rhg/src/commands/cat.rs	Wed Sep 01 16:13:25 2021 +0200
@@ -56,29 +56,28 @@
             .map_err(|e| CommandError::abort(e.to_string()))?;
         files.push(hg_file);
     }
+    // TODO probably move this to a util function like `repo.default_rev` or
+    // something when it's used somewhere else
+    let rev = match rev {
+        Some(r) => r.to_string(),
+        None => format!("{:x}", repo.dirstate_parents()?.p1),
+    };
 
-    match rev {
-        Some(rev) => {
-            let output = cat(&repo, rev, &files).map_err(|e| (e, rev))?;
-            invocation.ui.write_stdout(&output.concatenated)?;
-            if !output.missing.is_empty() {
-                let short = format!("{:x}", output.node.short()).into_bytes();
-                for path in &output.missing {
-                    invocation.ui.write_stderr(&format_bytes!(
-                        b"{}: no such file in rev {}\n",
-                        path.as_bytes(),
-                        short
-                    ))?;
-                }
-            }
-            if output.found_any {
-                Ok(())
-            } else {
-                Err(CommandError::Unsuccessful)
-            }
+    let output = cat(&repo, &rev, &files).map_err(|e| (e, rev.as_str()))?;
+    invocation.ui.write_stdout(&output.concatenated)?;
+    if !output.missing.is_empty() {
+        let short = format!("{:x}", output.node.short()).into_bytes();
+        for path in &output.missing {
+            invocation.ui.write_stderr(&format_bytes!(
+                b"{}: no such file in rev {}\n",
+                path.as_bytes(),
+                short
+            ))?;
         }
-        None => Err(CommandError::unsupported(
-            "`rhg cat` without `--rev` / `-r`",
-        )),
+    }
+    if output.found_any {
+        Ok(())
+    } else {
+        Err(CommandError::Unsuccessful)
     }
 }
--- a/tests/test-rhg.t	Thu Sep 30 17:34:28 2021 +0200
+++ b/tests/test-rhg.t	Wed Sep 01 16:13:25 2021 +0200
@@ -138,40 +138,65 @@
   $ echo "original content" > original
   $ hg add original
   $ hg commit -m "add original" original
+Without `--rev`
+  $ $NO_FALLBACK rhg cat original
+  original content
+With `--rev`
   $ $NO_FALLBACK rhg cat -r 0 original
   original content
 Cat copied file should not display copy metadata
   $ hg copy original copy_of_original
   $ hg commit -m "add copy of original"
+  $ $NO_FALLBACK rhg cat original
+  original content
   $ $NO_FALLBACK rhg cat -r 1 copy_of_original
   original content
 
+
 Fallback to Python
-  $ $NO_FALLBACK rhg cat original
-  unsupported feature: `rhg cat` without `--rev` / `-r`
+  $ $NO_FALLBACK rhg cat original --exclude="*.rs"
+  unsupported feature: error: Found argument '--exclude' which wasn't expected, or isn't valid in this context
+  
+  USAGE:
+      rhg cat [OPTIONS] <FILE>...
+  
+  For more information try --help
+  
   [252]
-  $ rhg cat original
+  $ rhg cat original --exclude="*.rs"
   original content
 
   $ FALLBACK_EXE="$RHG_FALLBACK_EXECUTABLE"
   $ unset RHG_FALLBACK_EXECUTABLE
-  $ rhg cat original
+  $ rhg cat original --exclude="*.rs"
   abort: 'rhg.on-unsupported=fallback' without 'rhg.fallback-executable' set.
   [255]
   $ RHG_FALLBACK_EXECUTABLE="$FALLBACK_EXE"
   $ export RHG_FALLBACK_EXECUTABLE
 
-  $ rhg cat original --config rhg.fallback-executable=false
+  $ rhg cat original --exclude="*.rs" --config rhg.fallback-executable=false
   [1]
 
-  $ rhg cat original --config rhg.fallback-executable=hg-non-existent
+  $ rhg cat original --exclude="*.rs" --config rhg.fallback-executable=hg-non-existent
   tried to fall back to a 'hg-non-existent' sub-process but got error $ENOENT$
-  unsupported feature: `rhg cat` without `--rev` / `-r`
+  unsupported feature: error: Found argument '--exclude' which wasn't expected, or isn't valid in this context
+  
+  USAGE:
+      rhg cat [OPTIONS] <FILE>...
+  
+  For more information try --help
+  
   [252]
 
-  $ rhg cat original --config rhg.fallback-executable=rhg
+  $ rhg cat original --exclude="*.rs" --config rhg.fallback-executable=rhg
   Blocking recursive fallback. The 'rhg.fallback-executable = rhg' config points to `rhg` itself.
-  unsupported feature: `rhg cat` without `--rev` / `-r`
+  unsupported feature: error: Found argument '--exclude' which wasn't expected, or isn't valid in this context
+  
+  USAGE:
+      rhg cat [OPTIONS] <FILE>...
+  
+  For more information try --help
+  
   [252]
 
 Requirements