diff rust/rhg/src/commands/cat.rs @ 48072:d919b0ca8449

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
author Rapha?l Gom?s <rgomes@octobus.net>
date Wed, 01 Sep 2021 16:13:25 +0200
parents b1f2c2b336ec
children 1e00834491a5
line wrap: on
line diff
--- 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)
     }
 }