comparison rust/hg-core/src/revlog/mod.rs @ 52294:a3fa37bdb7ec

rust: normalize `_for_unchecked_rev` naming among revlogs and the index This normalizes the naming scheme between the `Revlog`, `Changelog`, etc. which is less suprising, though no real bugs could stem from this because of the type signature mismatch. The very high-level `Repo` object still uses an `UncheckedRevision` parameter for its methods because that's what most callers will want.
author Raphaël Gomès <rgomes@octobus.net>
date Tue, 29 Oct 2024 11:00:04 +0100
parents bd8081e9fd62
children 645d247d4c75
comparison
equal deleted inserted replaced
52293:77b38c86915d 52294:a3fa37bdb7ec
369 /// Returns whether the given revision exists in this revlog. 369 /// Returns whether the given revision exists in this revlog.
370 pub fn has_rev(&self, rev: UncheckedRevision) -> bool { 370 pub fn has_rev(&self, rev: UncheckedRevision) -> bool {
371 self.index().check_revision(rev).is_some() 371 self.index().check_revision(rev).is_some()
372 } 372 }
373 373
374 pub fn get_entry_for_checked_rev( 374 pub fn get_entry(
375 &self, 375 &self,
376 rev: Revision, 376 rev: Revision,
377 ) -> Result<RevlogEntry, RevlogError> { 377 ) -> Result<RevlogEntry, RevlogError> {
378 self.inner.get_entry_for_checked_rev(rev) 378 self.inner.get_entry(rev)
379 } 379 }
380 380
381 pub fn get_entry( 381 pub fn get_entry_for_unchecked_rev(
382 &self, 382 &self,
383 rev: UncheckedRevision, 383 rev: UncheckedRevision,
384 ) -> Result<RevlogEntry, RevlogError> { 384 ) -> Result<RevlogEntry, RevlogError> {
385 self.inner.get_entry(rev) 385 self.inner.get_entry_for_unchecked_rev(rev)
386 } 386 }
387 387
388 /// Return the full data associated to a revision. 388 /// Return the full data associated to a revision.
389 /// 389 ///
390 /// All entries required to build the final data out of deltas will be 390 /// All entries required to build the final data out of deltas will be
391 /// retrieved as needed, and the deltas will be applied to the inital 391 /// retrieved as needed, and the deltas will be applied to the initial
392 /// snapshot to rebuild the final data. 392 /// snapshot to rebuild the final data.
393 pub fn get_rev_data( 393 pub fn get_data_for_unchecked_rev(
394 &self, 394 &self,
395 rev: UncheckedRevision, 395 rev: UncheckedRevision,
396 ) -> Result<Cow<[u8]>, RevlogError> { 396 ) -> Result<Cow<[u8]>, RevlogError> {
397 if rev == NULL_REVISION.into() { 397 if rev == NULL_REVISION.into() {
398 return Ok(Cow::Borrowed(&[])); 398 return Ok(Cow::Borrowed(&[]));
399 }; 399 };
400 self.get_entry(rev)?.data() 400 self.get_entry_for_unchecked_rev(rev)?.data()
401 } 401 }
402 402
403 /// [`Self::get_rev_data`] for checked revisions. 403 /// [`Self::get_data_for_unchecked_rev`] for a checked [`Revision`].
404 pub fn get_rev_data_for_checked_rev( 404 pub fn get_data(&self, rev: Revision) -> Result<Cow<[u8]>, RevlogError> {
405 &self,
406 rev: Revision,
407 ) -> Result<Cow<[u8]>, RevlogError> {
408 if rev == NULL_REVISION { 405 if rev == NULL_REVISION {
409 return Ok(Cow::Borrowed(&[])); 406 return Ok(Cow::Borrowed(&[]));
410 }; 407 };
411 self.get_entry_for_checked_rev(rev)?.data() 408 self.get_entry(rev)?.data()
412 } 409 }
413 410
414 /// Check the hash of some given data against the recorded hash. 411 /// Check the hash of some given data against the recorded hash.
415 pub fn check_hash( 412 pub fn check_hash(
416 &self, 413 &self,
588 &self, 585 &self,
589 ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> { 586 ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> {
590 if self.p1 == NULL_REVISION { 587 if self.p1 == NULL_REVISION {
591 Ok(None) 588 Ok(None)
592 } else { 589 } else {
593 Ok(Some(self.revlog.get_entry_for_checked_rev(self.p1)?)) 590 Ok(Some(self.revlog.get_entry(self.p1)?))
594 } 591 }
595 } 592 }
596 593
597 pub fn p2_entry( 594 pub fn p2_entry(
598 &self, 595 &self,
599 ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> { 596 ) -> Result<Option<RevlogEntry<'revlog>>, RevlogError> {
600 if self.p2 == NULL_REVISION { 597 if self.p2 == NULL_REVISION {
601 Ok(None) 598 Ok(None)
602 } else { 599 } else {
603 Ok(Some(self.revlog.get_entry_for_checked_rev(self.p2)?)) 600 Ok(Some(self.revlog.get_entry(self.p2)?))
604 } 601 }
605 } 602 }
606 603
607 pub fn p1(&self) -> Option<Revision> { 604 pub fn p1(&self) -> Option<Revision> {
608 if self.p1 == NULL_REVISION { 605 if self.p1 == NULL_REVISION {
735 let revlog = 732 let revlog =
736 Revlog::open(&vfs, "foo.i", None, RevlogOpenOptions::default()) 733 Revlog::open(&vfs, "foo.i", None, RevlogOpenOptions::default())
737 .unwrap(); 734 .unwrap();
738 assert!(revlog.is_empty()); 735 assert!(revlog.is_empty());
739 assert_eq!(revlog.len(), 0); 736 assert_eq!(revlog.len(), 0);
740 assert!(revlog.get_entry(0.into()).is_err()); 737 assert!(revlog.get_entry_for_unchecked_rev(0.into()).is_err());
741 assert!(!revlog.has_rev(0.into())); 738 assert!(!revlog.has_rev(0.into()));
742 assert_eq!( 739 assert_eq!(
743 revlog.rev_from_node(NULL_NODE.into()).unwrap(), 740 revlog.rev_from_node(NULL_NODE.into()).unwrap(),
744 NULL_REVISION 741 NULL_REVISION
745 ); 742 );
746 let null_entry = revlog.get_entry(NULL_REVISION.into()).ok().unwrap(); 743 let null_entry = revlog
744 .get_entry_for_unchecked_rev(NULL_REVISION.into())
745 .ok()
746 .unwrap();
747 assert_eq!(null_entry.revision(), NULL_REVISION); 747 assert_eq!(null_entry.revision(), NULL_REVISION);
748 assert!(null_entry.data().unwrap().is_empty()); 748 assert!(null_entry.data().unwrap().is_empty());
749 } 749 }
750 750
751 #[test] 751 #[test]
777 std::fs::write(temp.path().join("foo.i"), contents).unwrap(); 777 std::fs::write(temp.path().join("foo.i"), contents).unwrap();
778 let revlog = 778 let revlog =
779 Revlog::open(&vfs, "foo.i", None, RevlogOpenOptions::default()) 779 Revlog::open(&vfs, "foo.i", None, RevlogOpenOptions::default())
780 .unwrap(); 780 .unwrap();
781 781
782 let entry0 = revlog.get_entry(0.into()).ok().unwrap(); 782 let entry0 =
783 revlog.get_entry_for_unchecked_rev(0.into()).ok().unwrap();
783 assert_eq!(entry0.revision(), Revision(0)); 784 assert_eq!(entry0.revision(), Revision(0));
784 assert_eq!(*entry0.node(), node0); 785 assert_eq!(*entry0.node(), node0);
785 assert!(!entry0.has_p1()); 786 assert!(!entry0.has_p1());
786 assert_eq!(entry0.p1(), None); 787 assert_eq!(entry0.p1(), None);
787 assert_eq!(entry0.p2(), None); 788 assert_eq!(entry0.p2(), None);
788 let p1_entry = entry0.p1_entry().unwrap(); 789 let p1_entry = entry0.p1_entry().unwrap();
789 assert!(p1_entry.is_none()); 790 assert!(p1_entry.is_none());
790 let p2_entry = entry0.p2_entry().unwrap(); 791 let p2_entry = entry0.p2_entry().unwrap();
791 assert!(p2_entry.is_none()); 792 assert!(p2_entry.is_none());
792 793
793 let entry1 = revlog.get_entry(1.into()).ok().unwrap(); 794 let entry1 =
795 revlog.get_entry_for_unchecked_rev(1.into()).ok().unwrap();
794 assert_eq!(entry1.revision(), Revision(1)); 796 assert_eq!(entry1.revision(), Revision(1));
795 assert_eq!(*entry1.node(), node1); 797 assert_eq!(*entry1.node(), node1);
796 assert!(!entry1.has_p1()); 798 assert!(!entry1.has_p1());
797 assert_eq!(entry1.p1(), None); 799 assert_eq!(entry1.p1(), None);
798 assert_eq!(entry1.p2(), None); 800 assert_eq!(entry1.p2(), None);
799 let p1_entry = entry1.p1_entry().unwrap(); 801 let p1_entry = entry1.p1_entry().unwrap();
800 assert!(p1_entry.is_none()); 802 assert!(p1_entry.is_none());
801 let p2_entry = entry1.p2_entry().unwrap(); 803 let p2_entry = entry1.p2_entry().unwrap();
802 assert!(p2_entry.is_none()); 804 assert!(p2_entry.is_none());
803 805
804 let entry2 = revlog.get_entry(2.into()).ok().unwrap(); 806 let entry2 =
807 revlog.get_entry_for_unchecked_rev(2.into()).ok().unwrap();
805 assert_eq!(entry2.revision(), Revision(2)); 808 assert_eq!(entry2.revision(), Revision(2));
806 assert_eq!(*entry2.node(), node2); 809 assert_eq!(*entry2.node(), node2);
807 assert!(entry2.has_p1()); 810 assert!(entry2.has_p1());
808 assert_eq!(entry2.p1(), Some(Revision(0))); 811 assert_eq!(entry2.p1(), Some(Revision(0)));
809 assert_eq!(entry2.p2(), Some(Revision(1))); 812 assert_eq!(entry2.p2(), Some(Revision(1)));
852 Some(idx.nt), 855 Some(idx.nt),
853 ) 856 )
854 .unwrap(); 857 .unwrap();
855 858
856 // accessing the data shows the corruption 859 // accessing the data shows the corruption
857 revlog.get_entry(0.into()).unwrap().data().unwrap_err(); 860 revlog
861 .get_entry_for_unchecked_rev(0.into())
862 .unwrap()
863 .data()
864 .unwrap_err();
858 865
859 assert_eq!( 866 assert_eq!(
860 revlog.rev_from_node(NULL_NODE.into()).unwrap(), 867 revlog.rev_from_node(NULL_NODE.into()).unwrap(),
861 Revision(-1) 868 Revision(-1)
862 ); 869 );