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