mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2025-01-07 10:26:45 +03:00
feat: add mark_active_session function to update session metadata
The mark_active_session function has been added to the Repository struct. This function updates the last_timestamp_ms field of the current session's metadata to the current system time. This is done to keep track of the last time the session was active. This function is called in several places where the current session is required to be updated, such as when writing to targets, branches, deltas, bookmarks, and handling git file changes. By updating the session metadata, we ensure that the session remains active and up-to-date. This change improves the accuracy of session tracking and helps in managing active sessions effectively.
This commit is contained in:
parent
62d2b7c6fa
commit
53ed4425f6
@ -1,4 +1,4 @@
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::gb_repository;
|
||||
|
||||
@ -10,13 +10,12 @@ pub struct BookmarksWriter<'writer> {
|
||||
|
||||
impl<'writer> BookmarksWriter<'writer> {
|
||||
pub fn new(repository: &'writer gb_repository::Repository) -> Result<Self> {
|
||||
repository
|
||||
.get_or_create_current_session()
|
||||
.context("failed to create session")?;
|
||||
Ok(Self { repository })
|
||||
}
|
||||
|
||||
pub fn write(&self, bookmark: &Bookmark) -> Result<()> {
|
||||
self.repository.mark_active_session()?;
|
||||
|
||||
let _lock = self.repository.lock();
|
||||
|
||||
serde_jsonlines::append_json_lines(
|
||||
|
@ -1,4 +1,4 @@
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::{
|
||||
gb_repository,
|
||||
@ -19,9 +19,7 @@ impl<'writer> DeltasWriter<'writer> {
|
||||
}
|
||||
|
||||
pub fn write<P: AsRef<std::path::Path>>(&self, path: P, deltas: &Vec<Delta>) -> Result<()> {
|
||||
self.repository
|
||||
.get_or_create_current_session()
|
||||
.context("failed to create session")?;
|
||||
self.repository.mark_active_session()?;
|
||||
|
||||
let _lock = self.repository.lock();
|
||||
|
||||
@ -41,9 +39,7 @@ impl<'writer> DeltasWriter<'writer> {
|
||||
}
|
||||
|
||||
pub fn write_wd_file<P: AsRef<std::path::Path>>(&self, path: P, contents: &str) -> Result<()> {
|
||||
self.repository
|
||||
.get_or_create_current_session()
|
||||
.context("failed to create session")?;
|
||||
self.repository.mark_active_session()?;
|
||||
|
||||
let _lock = self.repository.lock();
|
||||
|
||||
|
@ -354,6 +354,29 @@ impl Repository {
|
||||
lock::FileLock::lock(&self.lock_file)
|
||||
}
|
||||
|
||||
pub fn mark_active_session(&self) -> Result<()> {
|
||||
let current_session = self
|
||||
.get_or_create_current_session()
|
||||
.context("failed to get current session")?;
|
||||
|
||||
let updated_session = sessions::Session {
|
||||
meta: sessions::Meta {
|
||||
last_timestamp_ms: time::SystemTime::now()
|
||||
.duration_since(time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis(),
|
||||
..current_session.meta
|
||||
},
|
||||
..current_session
|
||||
};
|
||||
|
||||
sessions::Writer::new(self)
|
||||
.write(&updated_session)
|
||||
.context("failed to write session")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_or_create_current_session(&self) -> Result<sessions::Session> {
|
||||
let _lock = self.lock();
|
||||
|
||||
|
@ -21,18 +21,15 @@ impl<'writer> BranchWriter<'writer> {
|
||||
}
|
||||
|
||||
pub fn delete(&self, branch: &Branch) -> Result<()> {
|
||||
self.repository
|
||||
.get_or_create_current_session()
|
||||
.context("Failed to get or create current session")?;
|
||||
self.repository.mark_active_session()?;
|
||||
|
||||
let _lock = self.repository.lock();
|
||||
self.writer.remove(&format!("branches/{}", branch.id))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write(&self, branch: &Branch) -> Result<()> {
|
||||
self.repository
|
||||
.get_or_create_current_session()
|
||||
.context("Failed to get or create current session")?;
|
||||
self.repository.mark_active_session()?;
|
||||
|
||||
let _lock = self.repository.lock();
|
||||
|
||||
|
@ -21,9 +21,7 @@ impl<'writer> TargetWriter<'writer> {
|
||||
}
|
||||
|
||||
pub fn write_default(&self, target: &Target) -> Result<()> {
|
||||
self.repository
|
||||
.get_or_create_current_session()
|
||||
.context("Failed to get or create current session")?;
|
||||
self.repository.mark_active_session()?;
|
||||
|
||||
let _lock = self.repository.lock();
|
||||
|
||||
@ -47,7 +45,7 @@ impl<'writer> TargetWriter<'writer> {
|
||||
|
||||
pub fn write(&self, id: &str, target: &Target) -> Result<()> {
|
||||
self.repository
|
||||
.get_or_create_current_session()
|
||||
.mark_active_session()
|
||||
.context("Failed to get or create current session")?;
|
||||
|
||||
let _lock = self.repository.lock();
|
||||
|
@ -37,7 +37,6 @@ impl Dispatcher {
|
||||
let (notify_tx, mut notify_rx) = channel(1);
|
||||
let mut watcher = RecommendedWatcher::new(
|
||||
{
|
||||
let project_id = project_id.to_string();
|
||||
move |res: notify::Result<notify::Event>| match res {
|
||||
Ok(event) => {
|
||||
if !is_interesting_kind(&event.kind) {
|
||||
@ -49,11 +48,6 @@ impl Dispatcher {
|
||||
.filter(|file| is_interesting_file(&repo, file))
|
||||
{
|
||||
block_on(async {
|
||||
tracing::info!(
|
||||
project_id,
|
||||
path = %path.display(),
|
||||
"file change detected"
|
||||
);
|
||||
if let Err(error) = notify_tx.send(path).await {
|
||||
tracing::error!(?error, "failed to send file change event",);
|
||||
}
|
||||
@ -85,6 +79,11 @@ impl Dispatcher {
|
||||
match file_path.strip_prefix(&path) {
|
||||
Ok(relative_file_path) => {
|
||||
let event = if relative_file_path.starts_with(".git") {
|
||||
tracing::info!(
|
||||
project_id,
|
||||
file_path = %relative_file_path.display(),
|
||||
"git file change",
|
||||
);
|
||||
events::Event::GitFileChange(
|
||||
project_id.to_string(),
|
||||
relative_file_path
|
||||
@ -93,6 +92,11 @@ impl Dispatcher {
|
||||
.to_path_buf(),
|
||||
)
|
||||
} else {
|
||||
tracing::info!(
|
||||
project_id,
|
||||
file_path = %relative_file_path.display(),
|
||||
"project file change",
|
||||
);
|
||||
events::Event::ProjectFileChange(
|
||||
project_id.to_string(),
|
||||
relative_file_path.to_path_buf(),
|
||||
|
Loading…
Reference in New Issue
Block a user