##// END OF EJS Templates
rhg: Use binary search in manifest lookup...
Simon Sapin -
r49324:e293ff80 default
parent child Browse files
Show More
@@ -1,119 +1,193 b''
1 1 use crate::errors::HgError;
2 2 use crate::repo::Repo;
3 3 use crate::revlog::revlog::{Revlog, RevlogError};
4 4 use crate::revlog::Revision;
5 5 use crate::revlog::{Node, NodePrefix};
6 6 use crate::utils::hg_path::HgPath;
7 7 use crate::utils::SliceExt;
8 8
9 9 /// A specialized `Revlog` to work with `manifest` data format.
10 10 pub struct Manifestlog {
11 11 /// The generic `revlog` format.
12 12 revlog: Revlog,
13 13 }
14 14
15 15 impl Manifestlog {
16 16 /// Open the `manifest` of a repository given by its root.
17 17 pub fn open(repo: &Repo) -> Result<Self, HgError> {
18 18 let revlog = Revlog::open(repo, "00manifest.i", None)?;
19 19 Ok(Self { revlog })
20 20 }
21 21
22 22 /// Return the `Manifest` for the given node ID.
23 23 ///
24 24 /// Note: this is a node ID in the manifestlog, typically found through
25 25 /// `ChangelogEntry::manifest_node`. It is *not* the node ID of any
26 26 /// changeset.
27 27 ///
28 28 /// See also `Repo::manifest_for_node`
29 29 pub fn data_for_node(
30 30 &self,
31 31 node: NodePrefix,
32 32 ) -> Result<Manifest, RevlogError> {
33 33 let rev = self.revlog.rev_from_node(node)?;
34 34 self.data_for_rev(rev)
35 35 }
36 36
37 37 /// Return the `Manifest` of a given revision number.
38 38 ///
39 39 /// Note: this is a revision number in the manifestlog, *not* of any
40 40 /// changeset.
41 41 ///
42 42 /// See also `Repo::manifest_for_rev`
43 43 pub fn data_for_rev(
44 44 &self,
45 45 rev: Revision,
46 46 ) -> Result<Manifest, RevlogError> {
47 47 let bytes = self.revlog.get_rev_data(rev)?;
48 48 Ok(Manifest { bytes })
49 49 }
50 50 }
51 51
52 52 /// `Manifestlog` entry which knows how to interpret the `manifest` data bytes.
53 53 #[derive(Debug)]
54 54 pub struct Manifest {
55 /// Format for a manifest: flat sequence of variable-size entries,
56 /// sorted by path, each as:
57 ///
58 /// ```text
59 /// <path> \0 <hex_node_id> <flags> \n
60 /// ```
61 ///
62 /// The last entry is also terminated by a newline character.
63 /// Flags is one of `b""` (the empty string), `b"x"`, `b"l"`, or `b"t"`.
55 64 bytes: Vec<u8>,
56 65 }
57 66
58 67 impl Manifest {
59 68 pub fn iter(
60 69 &self,
61 70 ) -> impl Iterator<Item = Result<ManifestEntry, HgError>> {
62 71 self.bytes
63 72 .split(|b| b == &b'\n')
64 73 .filter(|line| !line.is_empty())
65 .map(|line| {
66 let (path, rest) = line.split_2(b'\0').ok_or_else(|| {
67 HgError::corrupted("manifest line should contain \\0")
68 })?;
69 let path = HgPath::new(path);
70 let (hex_node_id, flags) = match rest.split_last() {
71 Some((&b'x', rest)) => (rest, Some(b'x')),
72 Some((&b'l', rest)) => (rest, Some(b'l')),
73 Some((&b't', rest)) => (rest, Some(b't')),
74 _ => (rest, None),
75 };
76 Ok(ManifestEntry {
77 path,
78 hex_node_id,
79 flags,
80 })
81 })
74 .map(ManifestEntry::from_raw)
82 75 }
83 76
84 77 /// If the given path is in this manifest, return its filelog node ID
85 pub fn find_file(
78 pub fn find_by_path(
86 79 &self,
87 80 path: &HgPath,
88 81 ) -> Result<Option<ManifestEntry>, HgError> {
89 // TODO: use binary search instead of linear scan. This may involve
90 // building (and caching) an index of the byte indicex of each manifest
91 // line.
82 use std::cmp::Ordering::*;
83 let path = path.as_bytes();
84 // Both boundaries of this `&[u8]` slice are always at the boundary of
85 // an entry
86 let mut bytes = &*self.bytes;
92 87
93 // TODO: use try_find when available (if still using linear scan)
94 // https://github.com/rust-lang/rust/issues/63178
95 for entry in self.iter() {
96 let entry = entry?;
97 if entry.path == path {
98 return Ok(Some(entry));
88 // Binary search algorithm derived from `[T]::binary_search_by`
89 // <https://github.com/rust-lang/rust/blob/1.57.0/library/core/src/slice/mod.rs#L2221>
90 // except we don’t have a slice of entries. Instead we jump to the
91 // middle of the byte slice and look around for entry delimiters
92 // (newlines).
93 while let Some(entry_range) = Self::find_entry_near_middle_of(bytes)? {
94 let (entry_path, rest) =
95 ManifestEntry::split_path(&bytes[entry_range.clone()])?;
96 let cmp = entry_path.cmp(path);
97 if cmp == Less {
98 let after_newline = entry_range.end + 1;
99 bytes = &bytes[after_newline..];
100 } else if cmp == Greater {
101 bytes = &bytes[..entry_range.start];
102 } else {
103 return Ok(Some(ManifestEntry::from_path_and_rest(
104 entry_path, rest,
105 )));
99 106 }
100 107 }
101 108 Ok(None)
102 109 }
110
111 /// If there is at least one, return the byte range of an entry *excluding*
112 /// the final newline.
113 fn find_entry_near_middle_of(
114 bytes: &[u8],
115 ) -> Result<Option<std::ops::Range<usize>>, HgError> {
116 let len = bytes.len();
117 if len > 0 {
118 let middle = bytes.len() / 2;
119 // Integer division rounds down, so `middle < len`.
120 let (before, after) = bytes.split_at(middle);
121 let is_newline = |&byte: &u8| byte == b'\n';
122 let entry_start = match before.iter().rposition(is_newline) {
123 Some(i) => i + 1,
124 None => 0, // We choose the first entry in `bytes`
125 };
126 let entry_end = match after.iter().position(is_newline) {
127 Some(i) => {
128 // No `+ 1` here to exclude this newline from the range
129 middle + i
130 }
131 None => {
132 // In a well-formed manifest:
133 //
134 // * Since `len > 0`, `bytes` contains at least one entry
135 // * Every entry ends with a newline
136 // * Since `middle < len`, `after` contains at least the
137 // newline at the end of the last entry of `bytes`.
138 //
139 // We didn’t find a newline, so this manifest is not
140 // well-formed.
141 return Err(HgError::corrupted(
142 "manifest entry without \\n delimiter",
143 ));
144 }
145 };
146 Ok(Some(entry_start..entry_end))
147 } else {
148 // len == 0
149 Ok(None)
150 }
151 }
103 152 }
104 153
105 154 /// `Manifestlog` entry which knows how to interpret the `manifest` data bytes.
106 155 #[derive(Debug)]
107 156 pub struct ManifestEntry<'manifest> {
108 157 pub path: &'manifest HgPath,
109 158 pub hex_node_id: &'manifest [u8],
110 159
111 160 /// `Some` values are b'x', b'l', or 't'
112 161 pub flags: Option<u8>,
113 162 }
114 163
115 impl ManifestEntry<'_> {
164 impl<'a> ManifestEntry<'a> {
165 fn split_path(bytes: &[u8]) -> Result<(&[u8], &[u8]), HgError> {
166 bytes.split_2(b'\0').ok_or_else(|| {
167 HgError::corrupted("manifest entry without \\0 delimiter")
168 })
169 }
170
171 fn from_path_and_rest(path: &'a [u8], rest: &'a [u8]) -> Self {
172 let (hex_node_id, flags) = match rest.split_last() {
173 Some((&b'x', rest)) => (rest, Some(b'x')),
174 Some((&b'l', rest)) => (rest, Some(b'l')),
175 Some((&b't', rest)) => (rest, Some(b't')),
176 _ => (rest, None),
177 };
178 Self {
179 path: HgPath::new(path),
180 hex_node_id,
181 flags,
182 }
183 }
184
185 fn from_raw(bytes: &'a [u8]) -> Result<Self, HgError> {
186 let (path, rest) = Self::split_path(bytes)?;
187 Ok(Self::from_path_and_rest(path, rest))
188 }
189
116 190 pub fn node_id(&self) -> Result<Node, HgError> {
117 191 Node::from_hex_for_repo(self.hex_node_id)
118 192 }
119 193 }
@@ -1,506 +1,506 b''
1 1 // status.rs
2 2 //
3 3 // Copyright 2020, Georges Racinet <georges.racinets@octobus.net>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 use crate::error::CommandError;
9 9 use crate::ui::Ui;
10 10 use crate::utils::path_utils::RelativizePaths;
11 11 use clap::{Arg, SubCommand};
12 12 use format_bytes::format_bytes;
13 13 use hg;
14 14 use hg::config::Config;
15 15 use hg::dirstate::has_exec_bit;
16 16 use hg::dirstate::status::StatusPath;
17 17 use hg::dirstate::TruncatedTimestamp;
18 18 use hg::dirstate::RANGE_MASK_31BIT;
19 19 use hg::errors::{HgError, IoResultExt};
20 20 use hg::lock::LockError;
21 21 use hg::manifest::Manifest;
22 22 use hg::matchers::AlwaysMatcher;
23 23 use hg::repo::Repo;
24 24 use hg::utils::files::get_bytes_from_os_string;
25 25 use hg::utils::files::get_path_from_bytes;
26 26 use hg::utils::hg_path::{hg_path_to_path_buf, HgPath};
27 27 use hg::StatusOptions;
28 28 use log::{info, warn};
29 29 use std::io;
30 30 use std::path::PathBuf;
31 31
32 32 pub const HELP_TEXT: &str = "
33 33 Show changed files in the working directory
34 34
35 35 This is a pure Rust version of `hg status`.
36 36
37 37 Some options might be missing, check the list below.
38 38 ";
39 39
40 40 pub fn args() -> clap::App<'static, 'static> {
41 41 SubCommand::with_name("status")
42 42 .alias("st")
43 43 .about(HELP_TEXT)
44 44 .arg(
45 45 Arg::with_name("all")
46 46 .help("show status of all files")
47 47 .short("-A")
48 48 .long("--all"),
49 49 )
50 50 .arg(
51 51 Arg::with_name("modified")
52 52 .help("show only modified files")
53 53 .short("-m")
54 54 .long("--modified"),
55 55 )
56 56 .arg(
57 57 Arg::with_name("added")
58 58 .help("show only added files")
59 59 .short("-a")
60 60 .long("--added"),
61 61 )
62 62 .arg(
63 63 Arg::with_name("removed")
64 64 .help("show only removed files")
65 65 .short("-r")
66 66 .long("--removed"),
67 67 )
68 68 .arg(
69 69 Arg::with_name("clean")
70 70 .help("show only clean files")
71 71 .short("-c")
72 72 .long("--clean"),
73 73 )
74 74 .arg(
75 75 Arg::with_name("deleted")
76 76 .help("show only deleted files")
77 77 .short("-d")
78 78 .long("--deleted"),
79 79 )
80 80 .arg(
81 81 Arg::with_name("unknown")
82 82 .help("show only unknown (not tracked) files")
83 83 .short("-u")
84 84 .long("--unknown"),
85 85 )
86 86 .arg(
87 87 Arg::with_name("ignored")
88 88 .help("show only ignored files")
89 89 .short("-i")
90 90 .long("--ignored"),
91 91 )
92 92 .arg(
93 93 Arg::with_name("copies")
94 94 .help("show source of copied files (DEFAULT: ui.statuscopies)")
95 95 .short("-C")
96 96 .long("--copies"),
97 97 )
98 98 .arg(
99 99 Arg::with_name("no-status")
100 100 .help("hide status prefix")
101 101 .short("-n")
102 102 .long("--no-status"),
103 103 )
104 104 }
105 105
106 106 /// Pure data type allowing the caller to specify file states to display
107 107 #[derive(Copy, Clone, Debug)]
108 108 pub struct DisplayStates {
109 109 pub modified: bool,
110 110 pub added: bool,
111 111 pub removed: bool,
112 112 pub clean: bool,
113 113 pub deleted: bool,
114 114 pub unknown: bool,
115 115 pub ignored: bool,
116 116 }
117 117
118 118 pub const DEFAULT_DISPLAY_STATES: DisplayStates = DisplayStates {
119 119 modified: true,
120 120 added: true,
121 121 removed: true,
122 122 clean: false,
123 123 deleted: true,
124 124 unknown: true,
125 125 ignored: false,
126 126 };
127 127
128 128 pub const ALL_DISPLAY_STATES: DisplayStates = DisplayStates {
129 129 modified: true,
130 130 added: true,
131 131 removed: true,
132 132 clean: true,
133 133 deleted: true,
134 134 unknown: true,
135 135 ignored: true,
136 136 };
137 137
138 138 impl DisplayStates {
139 139 pub fn is_empty(&self) -> bool {
140 140 !(self.modified
141 141 || self.added
142 142 || self.removed
143 143 || self.clean
144 144 || self.deleted
145 145 || self.unknown
146 146 || self.ignored)
147 147 }
148 148 }
149 149
150 150 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
151 151 let status_enabled_default = false;
152 152 let status_enabled = invocation.config.get_option(b"rhg", b"status")?;
153 153 if !status_enabled.unwrap_or(status_enabled_default) {
154 154 return Err(CommandError::unsupported(
155 155 "status is experimental in rhg (enable it with 'rhg.status = true' \
156 156 or enable fallback with 'rhg.on-unsupported = fallback')"
157 157 ));
158 158 }
159 159
160 160 // TODO: lift these limitations
161 161 if invocation.config.get_bool(b"ui", b"tweakdefaults")? {
162 162 return Err(CommandError::unsupported(
163 163 "ui.tweakdefaults is not yet supported with rhg status",
164 164 ));
165 165 }
166 166 if invocation.config.get_bool(b"ui", b"statuscopies")? {
167 167 return Err(CommandError::unsupported(
168 168 "ui.statuscopies is not yet supported with rhg status",
169 169 ));
170 170 }
171 171 if invocation
172 172 .config
173 173 .get(b"commands", b"status.terse")
174 174 .is_some()
175 175 {
176 176 return Err(CommandError::unsupported(
177 177 "status.terse is not yet supported with rhg status",
178 178 ));
179 179 }
180 180
181 181 let ui = invocation.ui;
182 182 let config = invocation.config;
183 183 let args = invocation.subcommand_args;
184 184 let all = args.is_present("all");
185 185 let display_states = if all {
186 186 // TODO when implementing `--quiet`: it excludes clean files
187 187 // from `--all`
188 188 ALL_DISPLAY_STATES
189 189 } else {
190 190 let requested = DisplayStates {
191 191 modified: args.is_present("modified"),
192 192 added: args.is_present("added"),
193 193 removed: args.is_present("removed"),
194 194 clean: args.is_present("clean"),
195 195 deleted: args.is_present("deleted"),
196 196 unknown: args.is_present("unknown"),
197 197 ignored: args.is_present("ignored"),
198 198 };
199 199 if requested.is_empty() {
200 200 DEFAULT_DISPLAY_STATES
201 201 } else {
202 202 requested
203 203 }
204 204 };
205 205 let no_status = args.is_present("no-status");
206 206 let list_copies = all
207 207 || args.is_present("copies")
208 208 || config.get_bool(b"ui", b"statuscopies")?;
209 209
210 210 let repo = invocation.repo?;
211 211
212 212 if repo.has_sparse() || repo.has_narrow() {
213 213 return Err(CommandError::unsupported(
214 214 "rhg status is not supported for sparse checkouts or narrow clones yet"
215 215 ));
216 216 }
217 217
218 218 let mut dmap = repo.dirstate_map_mut()?;
219 219
220 220 let options = StatusOptions {
221 221 // we're currently supporting file systems with exec flags only
222 222 // anyway
223 223 check_exec: true,
224 224 list_clean: display_states.clean,
225 225 list_unknown: display_states.unknown,
226 226 list_ignored: display_states.ignored,
227 227 list_copies,
228 228 collect_traversed_dirs: false,
229 229 };
230 230 let (mut ds_status, pattern_warnings) = dmap.status(
231 231 &AlwaysMatcher,
232 232 repo.working_directory_path().to_owned(),
233 233 ignore_files(repo, config),
234 234 options,
235 235 )?;
236 236 if !pattern_warnings.is_empty() {
237 237 warn!("Pattern warnings: {:?}", &pattern_warnings);
238 238 }
239 239
240 240 for (path, error) in ds_status.bad {
241 241 let error = match error {
242 242 hg::BadMatch::OsError(code) => {
243 243 std::io::Error::from_raw_os_error(code).to_string()
244 244 }
245 245 hg::BadMatch::BadType(ty) => {
246 246 format!("unsupported file type (type is {})", ty)
247 247 }
248 248 };
249 249 ui.write_stderr(&format_bytes!(
250 250 b"{}: {}\n",
251 251 path.as_bytes(),
252 252 error.as_bytes()
253 253 ))?
254 254 }
255 255 if !ds_status.unsure.is_empty() {
256 256 info!(
257 257 "Files to be rechecked by retrieval from filelog: {:?}",
258 258 ds_status.unsure.iter().map(|s| &s.path).collect::<Vec<_>>()
259 259 );
260 260 }
261 261 let mut fixup = Vec::new();
262 262 if !ds_status.unsure.is_empty()
263 263 && (display_states.modified || display_states.clean)
264 264 {
265 265 let p1 = repo.dirstate_parents()?.p1;
266 266 let manifest = repo.manifest_for_node(p1).map_err(|e| {
267 267 CommandError::from((e, &*format!("{:x}", p1.short())))
268 268 })?;
269 269 for to_check in ds_status.unsure {
270 270 if unsure_is_modified(repo, &manifest, &to_check.path)? {
271 271 if display_states.modified {
272 272 ds_status.modified.push(to_check);
273 273 }
274 274 } else {
275 275 if display_states.clean {
276 276 ds_status.clean.push(to_check.clone());
277 277 }
278 278 fixup.push(to_check.path.into_owned())
279 279 }
280 280 }
281 281 }
282 282 let relative_paths = (!ui.plain())
283 283 && config
284 284 .get_option(b"commands", b"status.relative")?
285 285 .unwrap_or(config.get_bool(b"ui", b"relative-paths")?);
286 286 let output = DisplayStatusPaths {
287 287 ui,
288 288 no_status,
289 289 relativize: if relative_paths {
290 290 Some(RelativizePaths::new(repo)?)
291 291 } else {
292 292 None
293 293 },
294 294 };
295 295 if display_states.modified {
296 296 output.display(b"M", ds_status.modified)?;
297 297 }
298 298 if display_states.added {
299 299 output.display(b"A", ds_status.added)?;
300 300 }
301 301 if display_states.removed {
302 302 output.display(b"R", ds_status.removed)?;
303 303 }
304 304 if display_states.deleted {
305 305 output.display(b"!", ds_status.deleted)?;
306 306 }
307 307 if display_states.unknown {
308 308 output.display(b"?", ds_status.unknown)?;
309 309 }
310 310 if display_states.ignored {
311 311 output.display(b"I", ds_status.ignored)?;
312 312 }
313 313 if display_states.clean {
314 314 output.display(b"C", ds_status.clean)?;
315 315 }
316 316
317 317 let mut dirstate_write_needed = ds_status.dirty;
318 318 let filesystem_time_at_status_start = ds_status
319 319 .filesystem_time_at_status_start
320 320 .map(TruncatedTimestamp::from);
321 321
322 322 if (fixup.is_empty() || filesystem_time_at_status_start.is_none())
323 323 && !dirstate_write_needed
324 324 {
325 325 // Nothing to update
326 326 return Ok(());
327 327 }
328 328
329 329 // Update the dirstate on disk if we can
330 330 let with_lock_result =
331 331 repo.try_with_wlock_no_wait(|| -> Result<(), CommandError> {
332 332 if let Some(mtime_boundary) = filesystem_time_at_status_start {
333 333 for hg_path in fixup {
334 334 use std::os::unix::fs::MetadataExt;
335 335 let fs_path = hg_path_to_path_buf(&hg_path)
336 336 .expect("HgPath conversion");
337 337 // Specifically do not reuse `fs_metadata` from
338 338 // `unsure_is_clean` which was needed before reading
339 339 // contents. Here we access metadata again after reading
340 340 // content, in case it changed in the meantime.
341 341 let fs_metadata = repo
342 342 .working_directory_vfs()
343 343 .symlink_metadata(&fs_path)?;
344 344 if let Some(mtime) =
345 345 TruncatedTimestamp::for_reliable_mtime_of(
346 346 &fs_metadata,
347 347 &mtime_boundary,
348 348 )
349 349 .when_reading_file(&fs_path)?
350 350 {
351 351 let mode = fs_metadata.mode();
352 352 let size = fs_metadata.len() as u32 & RANGE_MASK_31BIT;
353 353 let mut entry = dmap
354 354 .get(&hg_path)?
355 355 .expect("ambiguous file not in dirstate");
356 356 entry.set_clean(mode, size, mtime);
357 357 dmap.add_file(&hg_path, entry)?;
358 358 dirstate_write_needed = true
359 359 }
360 360 }
361 361 }
362 362 drop(dmap); // Avoid "already mutably borrowed" RefCell panics
363 363 if dirstate_write_needed {
364 364 repo.write_dirstate()?
365 365 }
366 366 Ok(())
367 367 });
368 368 match with_lock_result {
369 369 Ok(closure_result) => closure_result?,
370 370 Err(LockError::AlreadyHeld) => {
371 371 // Not updating the dirstate is not ideal but not critical:
372 372 // don’t keep our caller waiting until some other Mercurial
373 373 // process releases the lock.
374 374 }
375 375 Err(LockError::Other(HgError::IoError { error, .. }))
376 376 if error.kind() == io::ErrorKind::PermissionDenied =>
377 377 {
378 378 // `hg status` on a read-only repository is fine
379 379 }
380 380 Err(LockError::Other(error)) => {
381 381 // Report other I/O errors
382 382 Err(error)?
383 383 }
384 384 }
385 385 Ok(())
386 386 }
387 387
388 388 fn ignore_files(repo: &Repo, config: &Config) -> Vec<PathBuf> {
389 389 let mut ignore_files = Vec::new();
390 390 let repo_ignore = repo.working_directory_vfs().join(".hgignore");
391 391 if repo_ignore.exists() {
392 392 ignore_files.push(repo_ignore)
393 393 }
394 394 for (key, value) in config.iter_section(b"ui") {
395 395 if key == b"ignore" || key.starts_with(b"ignore.") {
396 396 let path = get_path_from_bytes(value);
397 397 // TODO: expand "~/" and environment variable here, like Python
398 398 // does with `os.path.expanduser` and `os.path.expandvars`
399 399
400 400 let joined = repo.working_directory_path().join(path);
401 401 ignore_files.push(joined);
402 402 }
403 403 }
404 404 ignore_files
405 405 }
406 406
407 407 struct DisplayStatusPaths<'a> {
408 408 ui: &'a Ui,
409 409 no_status: bool,
410 410 relativize: Option<RelativizePaths>,
411 411 }
412 412
413 413 impl DisplayStatusPaths<'_> {
414 414 // Probably more elegant to use a Deref or Borrow trait rather than
415 415 // harcode HgPathBuf, but probably not really useful at this point
416 416 fn display(
417 417 &self,
418 418 status_prefix: &[u8],
419 419 mut paths: Vec<StatusPath<'_>>,
420 420 ) -> Result<(), CommandError> {
421 421 paths.sort_unstable();
422 422 for StatusPath { path, copy_source } in paths {
423 423 let relative;
424 424 let path = if let Some(relativize) = &self.relativize {
425 425 relative = relativize.relativize(&path);
426 426 &*relative
427 427 } else {
428 428 path.as_bytes()
429 429 };
430 430 // TODO optim, probably lots of unneeded copies here, especially
431 431 // if out stream is buffered
432 432 if self.no_status {
433 433 self.ui.write_stdout(&format_bytes!(b"{}\n", path))?
434 434 } else {
435 435 self.ui.write_stdout(&format_bytes!(
436 436 b"{} {}\n",
437 437 status_prefix,
438 438 path
439 439 ))?
440 440 }
441 441 if let Some(source) = copy_source {
442 442 self.ui.write_stdout(&format_bytes!(
443 443 b" {}\n",
444 444 source.as_bytes()
445 445 ))?
446 446 }
447 447 }
448 448 Ok(())
449 449 }
450 450 }
451 451
452 452 /// Check if a file is modified by comparing actual repo store and file system.
453 453 ///
454 454 /// This meant to be used for those that the dirstate cannot resolve, due
455 455 /// to time resolution limits.
456 456 fn unsure_is_modified(
457 457 repo: &Repo,
458 458 manifest: &Manifest,
459 459 hg_path: &HgPath,
460 460 ) -> Result<bool, HgError> {
461 461 let vfs = repo.working_directory_vfs();
462 462 let fs_path = hg_path_to_path_buf(hg_path).expect("HgPath conversion");
463 463 let fs_metadata = vfs.symlink_metadata(&fs_path)?;
464 464 let is_symlink = fs_metadata.file_type().is_symlink();
465 465 // TODO: Also account for `FALLBACK_SYMLINK` and `FALLBACK_EXEC` from the
466 466 // dirstate
467 467 let fs_flags = if is_symlink {
468 468 Some(b'l')
469 469 } else if has_exec_bit(&fs_metadata) {
470 470 Some(b'x')
471 471 } else {
472 472 None
473 473 };
474 474
475 475 let entry = manifest
476 .find_file(hg_path)?
476 .find_by_path(hg_path)?
477 477 .expect("ambgious file not in p1");
478 478 if entry.flags != fs_flags {
479 479 return Ok(true);
480 480 }
481 481 let filelog = repo.filelog(hg_path)?;
482 482 let fs_len = fs_metadata.len();
483 483 // TODO: check `fs_len` here like below, but based on
484 484 // `RevlogEntry::uncompressed_len` without decompressing the full filelog
485 485 // contents where possible. This is only valid if the revlog data does not
486 486 // contain metadata. See how Python’s `revlog.rawsize` calls
487 487 // `storageutil.filerevisioncopied`.
488 488 // (Maybe also check for content-modifying flags? See `revlog.size`.)
489 489 let filelog_entry =
490 490 filelog.data_for_node(entry.node_id()?).map_err(|_| {
491 491 HgError::corrupted("filelog missing node from manifest")
492 492 })?;
493 493 let contents_in_p1 = filelog_entry.data()?;
494 494 if contents_in_p1.len() as u64 != fs_len {
495 495 // No need to read the file contents:
496 496 // it cannot be equal if it has a different length.
497 497 return Ok(true);
498 498 }
499 499
500 500 let fs_contents = if is_symlink {
501 501 get_bytes_from_os_string(vfs.read_link(fs_path)?.into_os_string())
502 502 } else {
503 503 vfs.read(fs_path)?
504 504 };
505 505 Ok(contents_in_p1 != &*fs_contents)
506 506 }
General Comments 0
You need to be logged in to leave comments. Login now