annotate rust/hg-core/src/revlog/mod.rs @ 49937:750409505286

rust-clippy: merge "revlog" module definition and struct implementation Module inception is confusing when looking for code and almost never the right choice.
author Raphaël Gomès <rgomes@octobus.net>
date Tue, 10 Jan 2023 11:20:54 +0100
parents rust/hg-core/src/revlog.rs@4d2a5ca060e3
children 7ef51fff2c4f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49937
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
1 // Copyright 2018-2023 Georges Racinet <georges.racinet@octobus.net>
44005
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
2 // and Mercurial contributors
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
3 //
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
4 // This software may be used and distributed according to the terms of the
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
5 // GNU General Public License version 2 or any later version.
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
6 //! Mercurial concepts for handling revision history
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
7
44143
7f86426fdd2c rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents: 44142
diff changeset
8 pub mod node;
44142
63db6657d280 rust-nodemap: building blocks for nodetree structures
Georges Racinet <georges.racinet@octobus.net>
parents: 44088
diff changeset
9 pub mod nodemap;
46090
9eb07ab3f2d4 rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents: 45539
diff changeset
10 mod nodemap_docket;
45539
aebc976fd7d5 hg-core: add path_encode
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45533
diff changeset
11 pub mod path_encode;
46431
645ee7225fab rust: Make NodePrefix allocation-free and Copy, remove NodePrefixRef
Simon Sapin <simon.sapin@octobus.net>
parents: 46428
diff changeset
12 pub use node::{FromHexError, Node, NodePrefix};
45532
c2317b7624fd hg-core: add `Changlog` a specialized `Revlog`
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45526
diff changeset
13 pub mod changelog;
47961
4d2a5ca060e3 rust: Add a Filelog struct that wraps Revlog
Simon Sapin <simon.sapin@octobus.net>
parents: 46821
diff changeset
14 pub mod filelog;
45526
26c53ee51c68 hg-core: Add a limited read only `revlog` implementation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
15 pub mod index;
45533
89ac95bd4993 hg-core: add `Manifest` a specialized `Revlog`
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45532
diff changeset
16 pub mod manifest;
45526
26c53ee51c68 hg-core: Add a limited read only `revlog` implementation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44973
diff changeset
17 pub mod patch;
49937
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
18
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
19 use std::borrow::Cow;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
20 use std::io::Read;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
21 use std::ops::Deref;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
22 use std::path::Path;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
23
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
24 use flate2::read::ZlibDecoder;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
25 use sha1::{Digest, Sha1};
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
26 use zstd;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
27
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
28 use self::node::{NODE_BYTES_LENGTH, NULL_NODE};
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
29 use self::nodemap_docket::NodeMapDocket;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
30 use super::index::Index;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
31 use super::nodemap::{NodeMap, NodeMapError};
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
32 use crate::errors::HgError;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
33 use crate::vfs::Vfs;
44142
63db6657d280 rust-nodemap: building blocks for nodetree structures
Georges Racinet <georges.racinet@octobus.net>
parents: 44088
diff changeset
34
44005
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
35 /// Mercurial revision numbers
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
36 ///
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
37 /// As noted in revlog.c, revision numbers are actually encoded in
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
38 /// 4 bytes, and are liberally converted to ints, whence the i32
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
39 pub type Revision = i32;
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
40
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
41 /// Marker expressing the absence of a parent
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
42 ///
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
43 /// Independently of the actual representation, `NULL_REVISION` is guaranteed
44088
b3ec1ea95ee6 rust-core: fix typo in comment
Aay Jay Chan <aayjaychan@itopia.com.hk>
parents: 44005
diff changeset
44 /// to be smaller than all existing revisions.
44005
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
45 pub const NULL_REVISION: Revision = -1;
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
46
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
47 /// Same as `mercurial.node.wdirrev`
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
48 ///
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
49 /// This is also equal to `i32::max_value()`, but it's better to spell
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
50 /// it out explicitely, same as in `mercurial.node`
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44182
diff changeset
51 #[allow(clippy::unreadable_literal)]
44005
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
52 pub const WORKING_DIRECTORY_REVISION: Revision = 0x7fffffff;
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
53
46821
e8ae91b1a63d rhg: raise wdir specific error for `hg debugdata`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46431
diff changeset
54 pub const WORKING_DIRECTORY_HEX: &str =
e8ae91b1a63d rhg: raise wdir specific error for `hg debugdata`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46431
diff changeset
55 "ffffffffffffffffffffffffffffffffffffffff";
e8ae91b1a63d rhg: raise wdir specific error for `hg debugdata`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46431
diff changeset
56
44005
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
57 /// The simplest expression of what we need of Mercurial DAGs.
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
58 pub trait Graph {
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
59 /// Return the two parents of the given `Revision`.
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
60 ///
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
61 /// Each of the parents can be independently `NULL_REVISION`
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
62 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError>;
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
63 }
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
64
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
65 #[derive(Clone, Debug, PartialEq)]
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
66 pub enum GraphError {
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
67 ParentOutOfRange(Revision),
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
68 WorkingDirectoryUnsupported,
6b332c1fc7fe rust-core: extracted a revlog submodule
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
69 }
44181
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
70
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
71 /// The Mercurial Revlog Index
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
72 ///
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
73 /// This is currently limited to the minimal interface that is needed for
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
74 /// the [`nodemap`](nodemap/index.html) module
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
75 pub trait RevlogIndex {
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
76 /// Total number of Revisions referenced in this index
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
77 fn len(&self) -> usize;
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
78
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44182
diff changeset
79 fn is_empty(&self) -> bool {
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44182
diff changeset
80 self.len() == 0
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44182
diff changeset
81 }
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44182
diff changeset
82
44181
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
83 /// Return a reference to the Node or `None` if rev is out of bounds
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
84 ///
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
85 /// `NULL_REVISION` is not considered to be out of bounds.
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
86 fn node(&self, rev: Revision) -> Option<&Node>;
3fb39dc2e356 rust-revlog: a trait for the revlog index
Georges Racinet <georges.racinet@octobus.net>
parents: 44143
diff changeset
87 }
49937
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
88
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
89 const REVISION_FLAG_CENSORED: u16 = 1 << 15;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
90 const REVISION_FLAG_ELLIPSIS: u16 = 1 << 14;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
91 const REVISION_FLAG_EXTSTORED: u16 = 1 << 13;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
92 const REVISION_FLAG_HASCOPIESINFO: u16 = 1 << 12;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
93
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
94 // Keep this in sync with REVIDX_KNOWN_FLAGS in
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
95 // mercurial/revlogutils/flagutil.py
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
96 const REVIDX_KNOWN_FLAGS: u16 = REVISION_FLAG_CENSORED
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
97 | REVISION_FLAG_ELLIPSIS
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
98 | REVISION_FLAG_EXTSTORED
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
99 | REVISION_FLAG_HASCOPIESINFO;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
100
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
101 const NULL_REVLOG_ENTRY_FLAGS: u16 = 0;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
102
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
103 #[derive(Debug, derive_more::From)]
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
104 pub enum RevlogError {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
105 InvalidRevision,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
106 /// Working directory is not supported
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
107 WDirUnsupported,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
108 /// Found more than one entry whose ID match the requested prefix
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
109 AmbiguousPrefix,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
110 #[from]
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
111 Other(HgError),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
112 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
113
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
114 impl From<NodeMapError> for RevlogError {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
115 fn from(error: NodeMapError) -> Self {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
116 match error {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
117 NodeMapError::MultipleResults => RevlogError::AmbiguousPrefix,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
118 NodeMapError::RevisionNotInIndex(rev) => RevlogError::corrupted(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
119 format!("nodemap point to revision {} not in index", rev),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
120 ),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
121 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
122 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
123 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
124
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
125 fn corrupted<S: AsRef<str>>(context: S) -> HgError {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
126 HgError::corrupted(format!("corrupted revlog, {}", context.as_ref()))
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
127 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
128
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
129 impl RevlogError {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
130 fn corrupted<S: AsRef<str>>(context: S) -> Self {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
131 RevlogError::Other(corrupted(context))
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
132 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
133 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
134
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
135 /// Read only implementation of revlog.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
136 pub struct Revlog {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
137 /// When index and data are not interleaved: bytes of the revlog index.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
138 /// When index and data are interleaved: bytes of the revlog index and
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
139 /// data.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
140 index: Index,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
141 /// When index and data are not interleaved: bytes of the revlog data
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
142 data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>>,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
143 /// When present on disk: the persistent nodemap for this revlog
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
144 nodemap: Option<nodemap::NodeTree>,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
145 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
146
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
147 impl Revlog {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
148 /// Open a revlog index file.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
149 ///
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
150 /// It will also open the associated data file if index and data are not
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
151 /// interleaved.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
152 pub fn open(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
153 store_vfs: &Vfs,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
154 index_path: impl AsRef<Path>,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
155 data_path: Option<&Path>,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
156 use_nodemap: bool,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
157 ) -> Result<Self, HgError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
158 let index_path = index_path.as_ref();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
159 let index = {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
160 match store_vfs.mmap_open_opt(&index_path)? {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
161 None => Index::new(Box::new(vec![])),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
162 Some(index_mmap) => {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
163 let index = Index::new(Box::new(index_mmap))?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
164 Ok(index)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
165 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
166 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
167 }?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
168
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
169 let default_data_path = index_path.with_extension("d");
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
170
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
171 // type annotation required
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
172 // won't recognize Mmap as Deref<Target = [u8]>
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
173 let data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>> =
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
174 if index.is_inline() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
175 None
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
176 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
177 let data_path = data_path.unwrap_or(&default_data_path);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
178 let data_mmap = store_vfs.mmap_open(data_path)?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
179 Some(Box::new(data_mmap))
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
180 };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
181
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
182 let nodemap = if index.is_inline() || !use_nodemap {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
183 None
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
184 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
185 NodeMapDocket::read_from_file(store_vfs, index_path)?.map(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
186 |(docket, data)| {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
187 nodemap::NodeTree::load_bytes(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
188 Box::new(data),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
189 docket.data_length,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
190 )
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
191 },
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
192 )
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
193 };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
194
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
195 Ok(Revlog {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
196 index,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
197 data_bytes,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
198 nodemap,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
199 })
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
200 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
201
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
202 /// Return number of entries of the `Revlog`.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
203 pub fn len(&self) -> usize {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
204 self.index.len()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
205 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
206
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
207 /// Returns `true` if the `Revlog` has zero `entries`.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
208 pub fn is_empty(&self) -> bool {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
209 self.index.is_empty()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
210 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
211
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
212 /// Returns the node ID for the given revision number, if it exists in this
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
213 /// revlog
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
214 pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
215 if rev == NULL_REVISION {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
216 return Some(&NULL_NODE);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
217 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
218 Some(self.index.get_entry(rev)?.hash())
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
219 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
220
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
221 /// Return the revision number for the given node ID, if it exists in this
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
222 /// revlog
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
223 pub fn rev_from_node(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
224 &self,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
225 node: NodePrefix,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
226 ) -> Result<Revision, RevlogError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
227 if node.is_prefix_of(&NULL_NODE) {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
228 return Ok(NULL_REVISION);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
229 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
230
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
231 if let Some(nodemap) = &self.nodemap {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
232 return nodemap
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
233 .find_bin(&self.index, node)?
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
234 .ok_or(RevlogError::InvalidRevision);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
235 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
236
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
237 // Fallback to linear scan when a persistent nodemap is not present.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
238 // This happens when the persistent-nodemap experimental feature is not
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
239 // enabled, or for small revlogs.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
240 //
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
241 // TODO: consider building a non-persistent nodemap in memory to
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
242 // optimize these cases.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
243 let mut found_by_prefix = None;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
244 for rev in (0..self.len() as Revision).rev() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
245 let index_entry = self.index.get_entry(rev).ok_or_else(|| {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
246 HgError::corrupted(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
247 "revlog references a revision not in the index",
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
248 )
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
249 })?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
250 if node == *index_entry.hash() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
251 return Ok(rev);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
252 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
253 if node.is_prefix_of(index_entry.hash()) {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
254 if found_by_prefix.is_some() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
255 return Err(RevlogError::AmbiguousPrefix);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
256 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
257 found_by_prefix = Some(rev)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
258 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
259 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
260 found_by_prefix.ok_or(RevlogError::InvalidRevision)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
261 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
262
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
263 /// Returns whether the given revision exists in this revlog.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
264 pub fn has_rev(&self, rev: Revision) -> bool {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
265 self.index.get_entry(rev).is_some()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
266 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
267
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
268 /// Return the full data associated to a revision.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
269 ///
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
270 /// All entries required to build the final data out of deltas will be
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
271 /// retrieved as needed, and the deltas will be applied to the inital
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
272 /// snapshot to rebuild the final data.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
273 pub fn get_rev_data(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
274 &self,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
275 rev: Revision,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
276 ) -> Result<Cow<[u8]>, RevlogError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
277 if rev == NULL_REVISION {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
278 return Ok(Cow::Borrowed(&[]));
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
279 };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
280 Ok(self.get_entry(rev)?.data()?)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
281 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
282
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
283 /// Check the hash of some given data against the recorded hash.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
284 pub fn check_hash(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
285 &self,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
286 p1: Revision,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
287 p2: Revision,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
288 expected: &[u8],
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
289 data: &[u8],
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
290 ) -> bool {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
291 let e1 = self.index.get_entry(p1);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
292 let h1 = match e1 {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
293 Some(ref entry) => entry.hash(),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
294 None => &NULL_NODE,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
295 };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
296 let e2 = self.index.get_entry(p2);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
297 let h2 = match e2 {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
298 Some(ref entry) => entry.hash(),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
299 None => &NULL_NODE,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
300 };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
301
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
302 hash(data, h1.as_bytes(), h2.as_bytes()) == expected
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
303 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
304
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
305 /// Build the full data of a revision out its snapshot
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
306 /// and its deltas.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
307 fn build_data_from_deltas(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
308 snapshot: RevlogEntry,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
309 deltas: &[RevlogEntry],
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
310 ) -> Result<Vec<u8>, HgError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
311 let snapshot = snapshot.data_chunk()?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
312 let deltas = deltas
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
313 .iter()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
314 .rev()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
315 .map(RevlogEntry::data_chunk)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
316 .collect::<Result<Vec<_>, _>>()?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
317 let patches: Vec<_> =
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
318 deltas.iter().map(|d| patch::PatchList::new(d)).collect();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
319 let patch = patch::fold_patch_lists(&patches);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
320 Ok(patch.apply(&snapshot))
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
321 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
322
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
323 /// Return the revlog data.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
324 fn data(&self) -> &[u8] {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
325 match &self.data_bytes {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
326 Some(data_bytes) => data_bytes,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
327 None => panic!(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
328 "forgot to load the data or trying to access inline data"
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
329 ),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
330 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
331 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
332
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
333 pub fn make_null_entry(&self) -> RevlogEntry {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
334 RevlogEntry {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
335 revlog: self,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
336 rev: NULL_REVISION,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
337 bytes: b"",
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
338 compressed_len: 0,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
339 uncompressed_len: 0,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
340 base_rev_or_base_of_delta_chain: None,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
341 p1: NULL_REVISION,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
342 p2: NULL_REVISION,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
343 flags: NULL_REVLOG_ENTRY_FLAGS,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
344 hash: NULL_NODE,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
345 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
346 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
347
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
348 /// Get an entry of the revlog.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
349 pub fn get_entry(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
350 &self,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
351 rev: Revision,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
352 ) -> Result<RevlogEntry, RevlogError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
353 if rev == NULL_REVISION {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
354 return Ok(self.make_null_entry());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
355 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
356 let index_entry = self
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
357 .index
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
358 .get_entry(rev)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
359 .ok_or(RevlogError::InvalidRevision)?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
360 let start = index_entry.offset();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
361 let end = start + index_entry.compressed_len() as usize;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
362 let data = if self.index.is_inline() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
363 self.index.data(start, end)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
364 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
365 &self.data()[start..end]
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
366 };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
367 let entry = RevlogEntry {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
368 revlog: self,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
369 rev,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
370 bytes: data,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
371 compressed_len: index_entry.compressed_len(),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
372 uncompressed_len: index_entry.uncompressed_len(),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
373 base_rev_or_base_of_delta_chain: if index_entry
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
374 .base_revision_or_base_of_delta_chain()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
375 == rev
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
376 {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
377 None
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
378 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
379 Some(index_entry.base_revision_or_base_of_delta_chain())
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
380 },
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
381 p1: index_entry.p1(),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
382 p2: index_entry.p2(),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
383 flags: index_entry.flags(),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
384 hash: *index_entry.hash(),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
385 };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
386 Ok(entry)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
387 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
388
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
389 /// when resolving internal references within revlog, any errors
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
390 /// should be reported as corruption, instead of e.g. "invalid revision"
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
391 fn get_entry_internal(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
392 &self,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
393 rev: Revision,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
394 ) -> Result<RevlogEntry, HgError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
395 self.get_entry(rev)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
396 .map_err(|_| corrupted(format!("revision {} out of range", rev)))
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
397 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
398 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
399
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
400 /// The revlog entry's bytes and the necessary informations to extract
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
401 /// the entry's data.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
402 #[derive(Clone)]
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
403 pub struct RevlogEntry<'a> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
404 revlog: &'a Revlog,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
405 rev: Revision,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
406 bytes: &'a [u8],
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
407 compressed_len: u32,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
408 uncompressed_len: i32,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
409 base_rev_or_base_of_delta_chain: Option<Revision>,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
410 p1: Revision,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
411 p2: Revision,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
412 flags: u16,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
413 hash: Node,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
414 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
415
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
416 impl<'a> RevlogEntry<'a> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
417 pub fn revision(&self) -> Revision {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
418 self.rev
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
419 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
420
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
421 pub fn node(&self) -> &Node {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
422 &self.hash
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
423 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
424
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
425 pub fn uncompressed_len(&self) -> Option<u32> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
426 u32::try_from(self.uncompressed_len).ok()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
427 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
428
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
429 pub fn has_p1(&self) -> bool {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
430 self.p1 != NULL_REVISION
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
431 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
432
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
433 pub fn p1_entry(&self) -> Result<Option<RevlogEntry>, RevlogError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
434 if self.p1 == NULL_REVISION {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
435 Ok(None)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
436 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
437 Ok(Some(self.revlog.get_entry(self.p1)?))
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
438 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
439 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
440
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
441 pub fn p2_entry(&self) -> Result<Option<RevlogEntry>, RevlogError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
442 if self.p2 == NULL_REVISION {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
443 Ok(None)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
444 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
445 Ok(Some(self.revlog.get_entry(self.p2)?))
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
446 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
447 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
448
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
449 pub fn p1(&self) -> Option<Revision> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
450 if self.p1 == NULL_REVISION {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
451 None
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
452 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
453 Some(self.p1)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
454 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
455 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
456
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
457 pub fn p2(&self) -> Option<Revision> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
458 if self.p2 == NULL_REVISION {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
459 None
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
460 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
461 Some(self.p2)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
462 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
463 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
464
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
465 pub fn is_censored(&self) -> bool {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
466 (self.flags & REVISION_FLAG_CENSORED) != 0
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
467 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
468
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
469 pub fn has_length_affecting_flag_processor(&self) -> bool {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
470 // Relevant Python code: revlog.size()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
471 // note: ELLIPSIS is known to not change the content
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
472 (self.flags & (REVIDX_KNOWN_FLAGS ^ REVISION_FLAG_ELLIPSIS)) != 0
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
473 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
474
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
475 /// The data for this entry, after resolving deltas if any.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
476 pub fn rawdata(&self) -> Result<Cow<'a, [u8]>, HgError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
477 let mut entry = self.clone();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
478 let mut delta_chain = vec![];
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
479
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
480 // The meaning of `base_rev_or_base_of_delta_chain` depends on
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
481 // generaldelta. See the doc on `ENTRY_DELTA_BASE` in
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
482 // `mercurial/revlogutils/constants.py` and the code in
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
483 // [_chaininfo] and in [index_deltachain].
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
484 let uses_generaldelta = self.revlog.index.uses_generaldelta();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
485 while let Some(base_rev) = entry.base_rev_or_base_of_delta_chain {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
486 let base_rev = if uses_generaldelta {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
487 base_rev
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
488 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
489 entry.rev - 1
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
490 };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
491 delta_chain.push(entry);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
492 entry = self.revlog.get_entry_internal(base_rev)?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
493 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
494
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
495 let data = if delta_chain.is_empty() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
496 entry.data_chunk()?
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
497 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
498 Revlog::build_data_from_deltas(entry, &delta_chain)?.into()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
499 };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
500
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
501 Ok(data)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
502 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
503
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
504 fn check_data(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
505 &self,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
506 data: Cow<'a, [u8]>,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
507 ) -> Result<Cow<'a, [u8]>, HgError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
508 if self.revlog.check_hash(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
509 self.p1,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
510 self.p2,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
511 self.hash.as_bytes(),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
512 &data,
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
513 ) {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
514 Ok(data)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
515 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
516 if (self.flags & REVISION_FLAG_ELLIPSIS) != 0 {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
517 return Err(HgError::unsupported(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
518 "ellipsis revisions are not supported by rhg",
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
519 ));
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
520 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
521 Err(corrupted(format!(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
522 "hash check failed for revision {}",
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
523 self.rev
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
524 )))
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
525 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
526 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
527
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
528 pub fn data(&self) -> Result<Cow<'a, [u8]>, HgError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
529 let data = self.rawdata()?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
530 if self.is_censored() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
531 return Err(HgError::CensoredNodeError);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
532 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
533 self.check_data(data)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
534 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
535
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
536 /// Extract the data contained in the entry.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
537 /// This may be a delta. (See `is_delta`.)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
538 fn data_chunk(&self) -> Result<Cow<'a, [u8]>, HgError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
539 if self.bytes.is_empty() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
540 return Ok(Cow::Borrowed(&[]));
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
541 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
542 match self.bytes[0] {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
543 // Revision data is the entirety of the entry, including this
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
544 // header.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
545 b'\0' => Ok(Cow::Borrowed(self.bytes)),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
546 // Raw revision data follows.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
547 b'u' => Ok(Cow::Borrowed(&self.bytes[1..])),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
548 // zlib (RFC 1950) data.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
549 b'x' => Ok(Cow::Owned(self.uncompressed_zlib_data()?)),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
550 // zstd data.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
551 b'\x28' => Ok(Cow::Owned(self.uncompressed_zstd_data()?)),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
552 // A proper new format should have had a repo/store requirement.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
553 format_type => Err(corrupted(format!(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
554 "unknown compression header '{}'",
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
555 format_type
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
556 ))),
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
557 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
558 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
559
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
560 fn uncompressed_zlib_data(&self) -> Result<Vec<u8>, HgError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
561 let mut decoder = ZlibDecoder::new(self.bytes);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
562 if self.is_delta() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
563 let mut buf = Vec::with_capacity(self.compressed_len as usize);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
564 decoder
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
565 .read_to_end(&mut buf)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
566 .map_err(|e| corrupted(e.to_string()))?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
567 Ok(buf)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
568 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
569 let cap = self.uncompressed_len.max(0) as usize;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
570 let mut buf = vec![0; cap];
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
571 decoder
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
572 .read_exact(&mut buf)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
573 .map_err(|e| corrupted(e.to_string()))?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
574 Ok(buf)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
575 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
576 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
577
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
578 fn uncompressed_zstd_data(&self) -> Result<Vec<u8>, HgError> {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
579 if self.is_delta() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
580 let mut buf = Vec::with_capacity(self.compressed_len as usize);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
581 zstd::stream::copy_decode(self.bytes, &mut buf)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
582 .map_err(|e| corrupted(e.to_string()))?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
583 Ok(buf)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
584 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
585 let cap = self.uncompressed_len.max(0) as usize;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
586 let mut buf = vec![0; cap];
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
587 let len = zstd::bulk::decompress_to_buffer(self.bytes, &mut buf)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
588 .map_err(|e| corrupted(e.to_string()))?;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
589 if len != self.uncompressed_len as usize {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
590 Err(corrupted("uncompressed length does not match"))
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
591 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
592 Ok(buf)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
593 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
594 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
595 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
596
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
597 /// Tell if the entry is a snapshot or a delta
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
598 /// (influences on decompression).
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
599 fn is_delta(&self) -> bool {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
600 self.base_rev_or_base_of_delta_chain.is_some()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
601 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
602 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
603
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
604 /// Calculate the hash of a revision given its data and its parents.
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
605 fn hash(
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
606 data: &[u8],
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
607 p1_hash: &[u8],
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
608 p2_hash: &[u8],
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
609 ) -> [u8; NODE_BYTES_LENGTH] {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
610 let mut hasher = Sha1::new();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
611 let (a, b) = (p1_hash, p2_hash);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
612 if a > b {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
613 hasher.update(b);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
614 hasher.update(a);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
615 } else {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
616 hasher.update(a);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
617 hasher.update(b);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
618 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
619 hasher.update(data);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
620 *hasher.finalize().as_ref()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
621 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
622
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
623 #[cfg(test)]
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
624 mod tests {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
625 use super::*;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
626 use crate::index::{IndexEntryBuilder, INDEX_ENTRY_SIZE};
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
627 use itertools::Itertools;
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
628
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
629 #[test]
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
630 fn test_empty() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
631 let temp = tempfile::tempdir().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
632 let vfs = Vfs { base: temp.path() };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
633 std::fs::write(temp.path().join("foo.i"), b"").unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
634 let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
635 assert!(revlog.is_empty());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
636 assert_eq!(revlog.len(), 0);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
637 assert!(revlog.get_entry(0).is_err());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
638 assert!(!revlog.has_rev(0));
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
639 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
640
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
641 #[test]
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
642 fn test_inline() {
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
643 let temp = tempfile::tempdir().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
644 let vfs = Vfs { base: temp.path() };
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
645 let node0 = Node::from_hex("2ed2a3912a0b24502043eae84ee4b279c18b90dd")
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
646 .unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
647 let node1 = Node::from_hex("b004912a8510032a0350a74daa2803dadfb00e12")
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
648 .unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
649 let node2 = Node::from_hex("dd6ad206e907be60927b5a3117b97dffb2590582")
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
650 .unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
651 let entry0_bytes = IndexEntryBuilder::new()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
652 .is_first(true)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
653 .with_version(1)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
654 .with_inline(true)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
655 .with_offset(INDEX_ENTRY_SIZE)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
656 .with_node(node0)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
657 .build();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
658 let entry1_bytes = IndexEntryBuilder::new()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
659 .with_offset(INDEX_ENTRY_SIZE)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
660 .with_node(node1)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
661 .build();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
662 let entry2_bytes = IndexEntryBuilder::new()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
663 .with_offset(INDEX_ENTRY_SIZE)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
664 .with_p1(0)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
665 .with_p2(1)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
666 .with_node(node2)
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
667 .build();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
668 let contents = vec![entry0_bytes, entry1_bytes, entry2_bytes]
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
669 .into_iter()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
670 .flatten()
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
671 .collect_vec();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
672 std::fs::write(temp.path().join("foo.i"), contents).unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
673 let revlog = Revlog::open(&vfs, "foo.i", None, false).unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
674
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
675 let entry0 = revlog.get_entry(0).ok().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
676 assert_eq!(entry0.revision(), 0);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
677 assert_eq!(*entry0.node(), node0);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
678 assert!(!entry0.has_p1());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
679 assert_eq!(entry0.p1(), None);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
680 assert_eq!(entry0.p2(), None);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
681 let p1_entry = entry0.p1_entry().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
682 assert!(p1_entry.is_none());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
683 let p2_entry = entry0.p2_entry().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
684 assert!(p2_entry.is_none());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
685
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
686 let entry1 = revlog.get_entry(1).ok().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
687 assert_eq!(entry1.revision(), 1);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
688 assert_eq!(*entry1.node(), node1);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
689 assert!(!entry1.has_p1());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
690 assert_eq!(entry1.p1(), None);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
691 assert_eq!(entry1.p2(), None);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
692 let p1_entry = entry1.p1_entry().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
693 assert!(p1_entry.is_none());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
694 let p2_entry = entry1.p2_entry().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
695 assert!(p2_entry.is_none());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
696
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
697 let entry2 = revlog.get_entry(2).ok().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
698 assert_eq!(entry2.revision(), 2);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
699 assert_eq!(*entry2.node(), node2);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
700 assert!(entry2.has_p1());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
701 assert_eq!(entry2.p1(), Some(0));
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
702 assert_eq!(entry2.p2(), Some(1));
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
703 let p1_entry = entry2.p1_entry().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
704 assert!(p1_entry.is_some());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
705 assert_eq!(p1_entry.unwrap().revision(), 0);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
706 let p2_entry = entry2.p2_entry().unwrap();
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
707 assert!(p2_entry.is_some());
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
708 assert_eq!(p2_entry.unwrap().revision(), 1);
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
709 }
750409505286 rust-clippy: merge "revlog" module definition and struct implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 47961
diff changeset
710 }