comparison rust/hg-core/src/dirstate.rs @ 48018:08efe5945d2b

rust: Move DirstateEntry to its own module … and RawEntry to the dirstate::parsers module, the only one that uses it. Differential Revision: https://phab.mercurial-scm.org/D11457
author Simon Sapin <simon.sapin@octobus.net>
date Fri, 17 Sep 2021 12:42:24 +0200
parents 81aedf1fc897
children bf8837e3d7ce
comparison
equal deleted inserted replaced
48016:5caec48d9a01 48018:08efe5945d2b
4 // 4 //
5 // This software may be used and distributed according to the terms of the 5 // This software may be used and distributed according to the terms of the
6 // GNU General Public License version 2 or any later version. 6 // GNU General Public License version 2 or any later version.
7 7
8 use crate::dirstate_tree::on_disk::DirstateV2ParseError; 8 use crate::dirstate_tree::on_disk::DirstateV2ParseError;
9 use crate::errors::HgError;
10 use crate::revlog::node::NULL_NODE; 9 use crate::revlog::node::NULL_NODE;
11 use crate::revlog::Node; 10 use crate::revlog::Node;
12 use crate::utils::hg_path::{HgPath, HgPathBuf}; 11 use crate::utils::hg_path::{HgPath, HgPathBuf};
13 use crate::FastHashMap; 12 use crate::FastHashMap;
14 use bytes_cast::{unaligned, BytesCast}; 13 use bytes_cast::BytesCast;
15 use std::convert::TryFrom;
16 14
17 pub mod dirs_multiset; 15 pub mod dirs_multiset;
18 pub mod dirstate_map; 16 pub mod dirstate_map;
17 pub mod entry;
19 pub mod parsers; 18 pub mod parsers;
20 pub mod status; 19 pub mod status;
20
21 pub use self::entry::*;
21 22
22 #[derive(Debug, PartialEq, Copy, Clone, BytesCast)] 23 #[derive(Debug, PartialEq, Copy, Clone, BytesCast)]
23 #[repr(C)] 24 #[repr(C)]
24 pub struct DirstateParents { 25 pub struct DirstateParents {
25 pub p1: Node, 26 pub p1: Node,
31 p1: NULL_NODE, 32 p1: NULL_NODE,
32 p2: NULL_NODE, 33 p2: NULL_NODE,
33 }; 34 };
34 } 35 }
35 36
36 /// The C implementation uses all signed types. This will be an issue
37 /// either when 4GB+ source files are commonplace or in 2038, whichever
38 /// comes first.
39 #[derive(Debug, PartialEq, Copy, Clone)]
40 pub struct DirstateEntry {
41 pub state: EntryState,
42 pub mode: i32,
43 pub mtime: i32,
44 pub size: i32,
45 }
46
47 impl DirstateEntry {
48 pub fn is_non_normal(&self) -> bool {
49 self.state != EntryState::Normal || self.mtime == MTIME_UNSET
50 }
51
52 pub fn is_from_other_parent(&self) -> bool {
53 self.state == EntryState::Normal && self.size == SIZE_FROM_OTHER_PARENT
54 }
55
56 // TODO: other platforms
57 #[cfg(unix)]
58 pub fn mode_changed(
59 &self,
60 filesystem_metadata: &std::fs::Metadata,
61 ) -> bool {
62 use std::os::unix::fs::MetadataExt;
63 const EXEC_BIT_MASK: u32 = 0o100;
64 let dirstate_exec_bit = (self.mode as u32) & EXEC_BIT_MASK;
65 let fs_exec_bit = filesystem_metadata.mode() & EXEC_BIT_MASK;
66 dirstate_exec_bit != fs_exec_bit
67 }
68
69 /// Returns a `(state, mode, size, mtime)` tuple as for
70 /// `DirstateMapMethods::debug_iter`.
71 pub fn debug_tuple(&self) -> (u8, i32, i32, i32) {
72 (self.state.into(), self.mode, self.size, self.mtime)
73 }
74 }
75
76 #[derive(BytesCast)]
77 #[repr(C)]
78 struct RawEntry {
79 state: u8,
80 mode: unaligned::I32Be,
81 size: unaligned::I32Be,
82 mtime: unaligned::I32Be,
83 length: unaligned::I32Be,
84 }
85
86 pub const V1_RANGEMASK: i32 = 0x7FFFFFFF;
87
88 pub const MTIME_UNSET: i32 = -1;
89
90 /// A `DirstateEntry` with a size of `-2` means that it was merged from the
91 /// other parent. This allows revert to pick the right status back during a
92 /// merge.
93 pub const SIZE_FROM_OTHER_PARENT: i32 = -2;
94 /// A special value used for internal representation of special case in
95 /// dirstate v1 format.
96 pub const SIZE_NON_NORMAL: i32 = -1;
97
98 pub type StateMap = FastHashMap<HgPathBuf, DirstateEntry>; 37 pub type StateMap = FastHashMap<HgPathBuf, DirstateEntry>;
99 pub type StateMapIter<'a> = Box< 38 pub type StateMapIter<'a> = Box<
100 dyn Iterator< 39 dyn Iterator<
101 Item = Result<(&'a HgPath, DirstateEntry), DirstateV2ParseError>, 40 Item = Result<(&'a HgPath, DirstateEntry), DirstateV2ParseError>,
102 > + Send 41 > + Send
107 pub type CopyMapIter<'a> = Box< 46 pub type CopyMapIter<'a> = Box<
108 dyn Iterator<Item = Result<(&'a HgPath, &'a HgPath), DirstateV2ParseError>> 47 dyn Iterator<Item = Result<(&'a HgPath, &'a HgPath), DirstateV2ParseError>>
109 + Send 48 + Send
110 + 'a, 49 + 'a,
111 >; 50 >;
112
113 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
114 pub enum EntryState {
115 Normal,
116 Added,
117 Removed,
118 Merged,
119 Unknown,
120 }
121
122 impl EntryState {
123 pub fn is_tracked(self) -> bool {
124 use EntryState::*;
125 match self {
126 Normal | Added | Merged => true,
127 Removed | Unknown => false,
128 }
129 }
130 }
131
132 impl TryFrom<u8> for EntryState {
133 type Error = HgError;
134
135 fn try_from(value: u8) -> Result<Self, Self::Error> {
136 match value {
137 b'n' => Ok(EntryState::Normal),
138 b'a' => Ok(EntryState::Added),
139 b'r' => Ok(EntryState::Removed),
140 b'm' => Ok(EntryState::Merged),
141 b'?' => Ok(EntryState::Unknown),
142 _ => Err(HgError::CorruptedRepository(format!(
143 "Incorrect dirstate entry state {}",
144 value
145 ))),
146 }
147 }
148 }
149
150 impl Into<u8> for EntryState {
151 fn into(self) -> u8 {
152 match self {
153 EntryState::Normal => b'n',
154 EntryState::Added => b'a',
155 EntryState::Removed => b'r',
156 EntryState::Merged => b'm',
157 EntryState::Unknown => b'?',
158 }
159 }
160 }