Mercurial > hg
comparison rust/hg-core/src/dirstate/entry.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 | |
children | f2a9db29cb2d |
comparison
equal
deleted
inserted
replaced
48016:5caec48d9a01 | 48018:08efe5945d2b |
---|---|
1 use crate::errors::HgError; | |
2 use std::convert::TryFrom; | |
3 | |
4 #[derive(Copy, Clone, Debug, Eq, PartialEq)] | |
5 pub enum EntryState { | |
6 Normal, | |
7 Added, | |
8 Removed, | |
9 Merged, | |
10 Unknown, | |
11 } | |
12 | |
13 /// The C implementation uses all signed types. This will be an issue | |
14 /// either when 4GB+ source files are commonplace or in 2038, whichever | |
15 /// comes first. | |
16 #[derive(Debug, PartialEq, Copy, Clone)] | |
17 pub struct DirstateEntry { | |
18 pub state: EntryState, | |
19 pub mode: i32, | |
20 pub mtime: i32, | |
21 pub size: i32, | |
22 } | |
23 | |
24 pub const V1_RANGEMASK: i32 = 0x7FFFFFFF; | |
25 | |
26 pub const MTIME_UNSET: i32 = -1; | |
27 | |
28 /// A `DirstateEntry` with a size of `-2` means that it was merged from the | |
29 /// other parent. This allows revert to pick the right status back during a | |
30 /// merge. | |
31 pub const SIZE_FROM_OTHER_PARENT: i32 = -2; | |
32 /// A special value used for internal representation of special case in | |
33 /// dirstate v1 format. | |
34 pub const SIZE_NON_NORMAL: i32 = -1; | |
35 | |
36 impl DirstateEntry { | |
37 pub fn is_non_normal(&self) -> bool { | |
38 self.state != EntryState::Normal || self.mtime == MTIME_UNSET | |
39 } | |
40 | |
41 pub fn is_from_other_parent(&self) -> bool { | |
42 self.state == EntryState::Normal && self.size == SIZE_FROM_OTHER_PARENT | |
43 } | |
44 | |
45 // TODO: other platforms | |
46 #[cfg(unix)] | |
47 pub fn mode_changed( | |
48 &self, | |
49 filesystem_metadata: &std::fs::Metadata, | |
50 ) -> bool { | |
51 use std::os::unix::fs::MetadataExt; | |
52 const EXEC_BIT_MASK: u32 = 0o100; | |
53 let dirstate_exec_bit = (self.mode as u32) & EXEC_BIT_MASK; | |
54 let fs_exec_bit = filesystem_metadata.mode() & EXEC_BIT_MASK; | |
55 dirstate_exec_bit != fs_exec_bit | |
56 } | |
57 | |
58 /// Returns a `(state, mode, size, mtime)` tuple as for | |
59 /// `DirstateMapMethods::debug_iter`. | |
60 pub fn debug_tuple(&self) -> (u8, i32, i32, i32) { | |
61 (self.state.into(), self.mode, self.size, self.mtime) | |
62 } | |
63 | |
64 pub fn mtime_is_ambiguous(&self, now: i32) -> bool { | |
65 self.state == EntryState::Normal && self.mtime == now | |
66 } | |
67 | |
68 pub fn clear_ambiguous_mtime(&mut self, now: i32) -> bool { | |
69 let ambiguous = self.mtime_is_ambiguous(now); | |
70 if ambiguous { | |
71 // The file was last modified "simultaneously" with the current | |
72 // write to dirstate (i.e. within the same second for file- | |
73 // systems with a granularity of 1 sec). This commonly happens | |
74 // for at least a couple of files on 'update'. | |
75 // The user could change the file without changing its size | |
76 // within the same second. Invalidate the file's mtime in | |
77 // dirstate, forcing future 'status' calls to compare the | |
78 // contents of the file if the size is the same. This prevents | |
79 // mistakenly treating such files as clean. | |
80 self.clear_mtime() | |
81 } | |
82 ambiguous | |
83 } | |
84 | |
85 pub fn clear_mtime(&mut self) { | |
86 self.mtime = -1; | |
87 } | |
88 } | |
89 | |
90 impl EntryState { | |
91 pub fn is_tracked(self) -> bool { | |
92 use EntryState::*; | |
93 match self { | |
94 Normal | Added | Merged => true, | |
95 Removed | Unknown => false, | |
96 } | |
97 } | |
98 } | |
99 | |
100 impl TryFrom<u8> for EntryState { | |
101 type Error = HgError; | |
102 | |
103 fn try_from(value: u8) -> Result<Self, Self::Error> { | |
104 match value { | |
105 b'n' => Ok(EntryState::Normal), | |
106 b'a' => Ok(EntryState::Added), | |
107 b'r' => Ok(EntryState::Removed), | |
108 b'm' => Ok(EntryState::Merged), | |
109 b'?' => Ok(EntryState::Unknown), | |
110 _ => Err(HgError::CorruptedRepository(format!( | |
111 "Incorrect dirstate entry state {}", | |
112 value | |
113 ))), | |
114 } | |
115 } | |
116 } | |
117 | |
118 impl Into<u8> for EntryState { | |
119 fn into(self) -> u8 { | |
120 match self { | |
121 EntryState::Normal => b'n', | |
122 EntryState::Added => b'a', | |
123 EntryState::Removed => b'r', | |
124 EntryState::Merged => b'm', | |
125 EntryState::Unknown => b'?', | |
126 } | |
127 } | |
128 } |