Patchwork D9264: rhg: strip copied files metadata from `cat` output

login
register
mail settings
Submitter phabricator
Date Oct. 30, 2020, 9:01 a.m.
Message ID <differential-rev-PHID-DREV-6nauwvel4t2civmacexm-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47535/
State Superseded
Headers show

Comments

phabricator - Oct. 30, 2020, 9:01 a.m.
acezar created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/hg-core/src/operations/cat.rs
  tests/test-rhg.t

CHANGE DETAILS




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

Patch

diff --git a/tests/test-rhg.t b/tests/test-rhg.t
--- a/tests/test-rhg.t
+++ b/tests/test-rhg.t
@@ -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
diff --git a/rust/hg-core/src/operations/cat.rs b/rust/hg-core/src/operations/cat.rs
--- a/rust/hg-core/src/operations/cat.rs
+++ b/rust/hg-core/src/operations/cat.rs
@@ -17,6 +17,8 @@ 
 use crate::utils::files::get_path_from_bytes;
 use crate::utils::hg_path::{HgPath, HgPathBuf};
 
+const METADATA_DELIMITER: [u8; 2] = [b'\x01', b'\n'];
+
 /// Kind of error encountered by `CatRev`
 #[derive(Debug)]
 pub enum CatRevErrorKind {
@@ -131,7 +133,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);
+                        }
                     }
                 }
             }