rhg: strip copied files metadata from `cat` output
authorAntoine cezar<acezar@chwitlabs.fr>
Thu, 29 Oct 2020 19:25:33 +0100
changeset 45804 6991f3014311
parent 45803 84eb4c833c41
child 45805 49535e56fab8
rhg: strip copied files metadata from `cat` output Differential Revision: https://phab.mercurial-scm.org/D9264
rust/hg-core/src/operations/cat.rs
tests/test-rhg.t
--- a/rust/hg-core/src/operations/cat.rs	Mon Nov 02 14:39:43 2020 -0500
+++ b/rust/hg-core/src/operations/cat.rs	Thu Oct 29 19:25:33 2020 +0100
@@ -16,6 +16,8 @@
 use crate::revlog::Revision;
 use crate::utils::hg_path::HgPathBuf;
 
+const METADATA_DELIMITER: [u8; 2] = [b'\x01', b'\n'];
+
 /// Kind of error encountered by `CatRev`
 #[derive(Debug)]
 pub enum CatRevErrorKind {
@@ -132,7 +134,18 @@
                             .map_err(|_| CatRevErrorKind::CorruptedRevlog)?;
                         let file_rev = file_log.get_node_rev(&file_node)?;
                         let data = file_log.get_rev_data(file_rev)?;
-                        bytes.extend(data);
+                        if data.starts_with(&METADATA_DELIMITER) {
+                            let end_delimiter_position = data
+                                [METADATA_DELIMITER.len()..]
+                                .windows(METADATA_DELIMITER.len())
+                                .position(|bytes| bytes == METADATA_DELIMITER);
+                            if let Some(position) = end_delimiter_position {
+                                let offset = METADATA_DELIMITER.len() * 2;
+                                bytes.extend(data[position + offset..].iter());
+                            }
+                        } else {
+                            bytes.extend(data);
+                        }
                     }
                 }
             }
--- a/tests/test-rhg.t	Mon Nov 02 14:39:43 2020 -0500
+++ b/tests/test-rhg.t	Thu Oct 29 19:25:33 2020 +0100
@@ -90,3 +90,19 @@
   file1\x00b8e02f6433738021a065f94175c7cd23db5f05be (esc)
   file2\x005d9299349fc01ddd25d0070d149b124d8f10411e (esc)
   file3\x002661d26c649684b482d10f91960cc3db683c38b4 (esc)
+
+Cat files
+  $ cd $TESTTMP
+  $ rm -rf repository
+  $ hg init repository
+  $ cd repository
+  $ echo "original content" > original
+  $ hg add original
+  $ hg commit -m "add original" original
+  $ 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"
+  $ rhg cat -r 1 copy_of_original
+  original content