Merge pull request #3666 from gitbutlerapp/unix-timestamp

eliminate redundancy in unix timestamp retrieval
This commit is contained in:
Josh Junon 2024-05-02 12:56:48 +02:00 committed by GitHub
commit f5ef57e74d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 33 additions and 59 deletions

View File

@ -1,7 +1,4 @@
use std::{
fmt::{Display, Formatter},
time::SystemTime,
};
use std::fmt::{Display, Formatter};
use anyhow::Result;
@ -50,29 +47,20 @@ impl Document {
};
let operations = operations::get_delta_operations(&self.to_string(), new_text);
let delta = if operations.is_empty() {
if operations.is_empty() {
if let Some(reader::Content::UTF8(value)) = value {
if !value.is_empty() {
return Ok(None);
}
}
}
delta::Delta {
operations,
timestamp_ms: SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis(),
}
} else {
delta::Delta {
operations,
timestamp_ms: SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis(),
}
let delta = delta::Delta {
operations,
timestamp_ms: crate::time::now_ms(),
};
apply_deltas(&mut self.doc, &vec![delta.clone()])?;
self.deltas.push(delta.clone());
Ok(Some(delta))

View File

@ -4,7 +4,7 @@ use std::{
collections::HashSet,
fs::File,
io::{BufReader, Read},
path, time,
path,
};
use anyhow::{anyhow, bail, Context, Result};
@ -280,10 +280,7 @@ impl Repository {
&self,
project_repository: &project_repository::Repository,
) -> Result<sessions::Session> {
let now_ms = time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap()
.as_millis();
let now_ms = crate::time::now_ms();
let meta = match project_repository.get_head() {
Result::Ok(head) => sessions::Meta {
@ -336,10 +333,7 @@ impl Repository {
let updated_session = sessions::Session {
meta: sessions::Meta {
last_timestamp_ms: time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap()
.as_millis(),
last_timestamp_ms: crate::time::now_ms(),
..current_session.meta
},
..current_session

View File

@ -33,6 +33,7 @@ pub mod sessions;
pub mod snapshots;
pub mod ssh;
pub mod storage;
pub mod time;
pub mod types;
pub mod users;
pub mod virtual_branches;

View File

@ -178,17 +178,11 @@ impl Repository {
let branch = self.git_repository.find_branch(&target_branch_refname)?;
let commit_id = branch.peel_to_commit()?.id();
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or(std::time::Duration::from_secs(0))
.as_millis()
.to_string();
let branch_name = format!("test-push-{}", now);
let now = crate::time::now_ms();
let branch_name = format!("test-push-{now}");
let refname = git::RemoteRefname::from_str(&format!(
"refs/remotes/{}/{}",
remote_name, branch_name,
))?;
let refname =
git::RemoteRefname::from_str(&format!("refs/remotes/{remote_name}/{branch_name}",))?;
match self.push(
&commit_id,

View File

@ -1,5 +1,3 @@
use std::time;
use anyhow::{anyhow, Context, Result};
use super::Session;
@ -55,11 +53,7 @@ impl<'writer> SessionWriter<'writer> {
let mut batch = vec![writer::BatchTask::Write(
"session/meta/last",
time::SystemTime::now()
.duration_since(time::SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis()
.to_string(),
crate::time::now_ms().to_string(),
)];
if current_session_id.is_some()

View File

@ -0,0 +1,12 @@
use std::time::UNIX_EPOCH;
/// Gets the number of milliseconds since the Unix epoch.
///
/// # Panics
/// Panics if the system time is set before the Unix epoch.
pub fn now_ms() -> u128 {
UNIX_EPOCH
.elapsed()
.expect("system time is set before the Unix epoch")
.as_millis()
}

View File

@ -212,10 +212,7 @@ pub fn set_base_branch(
},
);
let now_ms = time::UNIX_EPOCH
.elapsed()
.context("failed to get elapsed time")?
.as_millis();
let now_ms = crate::time::now_ms();
let (upstream, upstream_head) = if let git::Refname::Local(head_name) = &head_name {
let upstream_name = target_branch_ref.with_branch(head_name.branch());

View File

@ -1118,11 +1118,6 @@ pub fn create_virtual_branch(
}
}
let now = time::UNIX_EPOCH
.elapsed()
.context("failed to get elapsed time")?
.as_millis();
let name = dedup(
&all_virtual_branches
.iter()
@ -1134,6 +1129,8 @@ pub fn create_virtual_branch(
.unwrap_or(&"Virtual branch".to_string()),
);
let now = crate::time::now_ms();
let mut branch = Branch {
id: BranchId::generate(),
name,
@ -4189,10 +4186,7 @@ pub fn create_virtual_branch_from_branch(
.any(|b| b.selected_for_changes.is_some()))
.then_some(chrono::Utc::now().timestamp_millis());
let now = time::UNIX_EPOCH
.elapsed()
.context("failed to get elapsed time")?
.as_millis();
let now = crate::time::now_ms();
// only set upstream if it's not the default target
let upstream_branch = match upstream {