changeset 52058:b55f653a0b34

rust-utils: move the `filesystem_now` function to a util This is going to be useful for an upcoming `hg update` fastpath.
author Raphaël Gomès <rgomes@octobus.net>
date Mon, 30 Sep 2024 17:45:10 +0200
parents 88aa21d654e5
children babfa9ddca0e
files rust/hg-core/src/dirstate_tree/status.rs rust/hg-core/src/utils/files.rs
diffstat 2 files changed, 25 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/dirstate_tree/status.rs	Mon Sep 30 17:43:51 2024 +0200
+++ b/rust/hg-core/src/dirstate_tree/status.rs	Mon Sep 30 17:45:10 2024 +0200
@@ -9,6 +9,7 @@
 use crate::dirstate_tree::on_disk::DirstateV2ParseError;
 use crate::matchers::get_ignore_function;
 use crate::matchers::{Matcher, VisitChildrenSet};
+use crate::utils::files::filesystem_now;
 use crate::utils::files::get_bytes_from_os_string;
 use crate::utils::files::get_bytes_from_path;
 use crate::utils::files::get_path_from_bytes;
@@ -30,7 +31,6 @@
 use std::path::Path;
 use std::path::PathBuf;
 use std::sync::Mutex;
-use std::time::SystemTime;
 
 /// Returns the status of the working directory compared to its parent
 /// changeset.
@@ -1034,22 +1034,3 @@
         }
     }
 }
-
-/// Return the `mtime` of a temporary file newly-created in the `.hg` directory
-/// of the give repository.
-///
-/// This is similar to `SystemTime::now()`, with the result truncated to the
-/// same time resolution as other files’ modification times. Using `.hg`
-/// instead of the system’s default temporary directory (such as `/tmp`) makes
-/// it more likely the temporary file is in the same disk partition as contents
-/// of the working directory, which can matter since different filesystems may
-/// store timestamps with different resolutions.
-///
-/// This may fail, typically if we lack write permissions. In that case we
-/// should continue the `status()` algoritm anyway and consider the current
-/// date/time to be unknown.
-fn filesystem_now(repo_root: &Path) -> Result<SystemTime, io::Error> {
-    tempfile::tempfile_in(repo_root.join(".hg"))?
-        .metadata()?
-        .modified()
-}
--- a/rust/hg-core/src/utils/files.rs	Mon Sep 30 17:43:51 2024 +0200
+++ b/rust/hg-core/src/utils/files.rs	Mon Sep 30 17:45:10 2024 +0200
@@ -16,11 +16,15 @@
 };
 use lazy_static::lazy_static;
 use same_file::is_same_file;
-use std::borrow::{Cow, ToOwned};
 use std::ffi::{OsStr, OsString};
 use std::iter::FusedIterator;
 use std::ops::Deref;
 use std::path::{Path, PathBuf};
+use std::{
+    borrow::{Cow, ToOwned},
+    io,
+    time::SystemTime,
+};
 
 pub fn get_os_str_from_bytes(bytes: &[u8]) -> &OsStr {
     let os_str;
@@ -306,6 +310,25 @@
     }
 }
 
+/// Return the `mtime` of a temporary file newly-created in the `.hg` directory
+/// of the give repository.
+///
+/// This is similar to `SystemTime::now()`, with the result truncated to the
+/// same time resolution as other files’ modification times. Using `.hg`
+/// instead of the system’s default temporary directory (such as `/tmp`) makes
+/// it more likely the temporary file is in the same disk partition as contents
+/// of the working directory, which can matter since different filesystems may
+/// store timestamps with different resolutions.
+///
+/// This may fail, typically if we lack write permissions. In that case we
+/// should continue the `status()` algoritm anyway and consider the current
+/// date/time to be unknown.
+pub fn filesystem_now(repo_root: &Path) -> Result<SystemTime, io::Error> {
+    tempfile::tempfile_in(repo_root.join(".hg"))?
+        .metadata()?
+        .modified()
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;