if use_state_handle enabled get state from toml

This commit is contained in:
Kiril Videlov 2024-04-01 00:21:07 +02:00
parent a1bd1fafea
commit d94542d0c7
No known key found for this signature in database
3 changed files with 61 additions and 26 deletions

View File

@ -1,3 +1,5 @@
use anyhow::anyhow;
use super::{Branch, BranchId};
use crate::{reader, sessions, virtual_branches::VirtualBranchesHandle};
@ -21,6 +23,13 @@ impl<'r> BranchReader<'r> {
}
pub fn read(&self, id: &BranchId) -> Result<Branch, reader::Error> {
Branch::from_reader(&self.reader.sub(format!("branches/{}", id)))
if self.use_state_handle && self.state_handle.file_exists() {
self.state_handle
.get_branch(id)
.and_then(|op| op.ok_or(anyhow!("Branch not found")))
.map_err(|_| reader::Error::NotFound)
} else {
Branch::from_reader(&self.reader.sub(format!("branches/{}", id)))
}
}
}

View File

@ -19,22 +19,29 @@ impl<'i> BranchIterator<'i> {
state_handle: VirtualBranchesHandle,
use_state_handle: bool,
) -> Result<Self> {
let reader = session_reader.reader();
// TODO: If use_state_handle is true, we should read the branch ids from the state file
let ids_itarator = reader
.list_files("branches")?
.into_iter()
.map(|file_path| {
file_path
.iter()
.next()
.unwrap()
.to_string_lossy()
.to_string()
})
.filter(|file_path| file_path != "selected")
.filter(|file_path| file_path != "target");
let unique_ids: HashSet<String> = ids_itarator.collect();
let unique_ids: HashSet<String> = if use_state_handle && state_handle.file_exists() {
state_handle
.list_branches()?
.into_keys()
.map(|id| id.to_string())
.collect()
} else {
session_reader
.reader()
.list_files("branches")?
.into_iter()
.map(|file_path| {
file_path
.iter()
.next()
.unwrap()
.to_string_lossy()
.to_string()
})
.filter(|file_path| file_path != "selected")
.filter(|file_path| file_path != "target")
.collect()
};
let mut ids: Vec<BranchId> = unique_ids
.into_iter()
.map(|id| id.parse())

View File

@ -1,3 +1,5 @@
use anyhow::anyhow;
use super::Target;
use crate::{
reader, sessions,
@ -24,18 +26,35 @@ impl<'r> TargetReader<'r> {
}
pub fn read_default(&self) -> Result<Target, reader::Error> {
Target::try_from(&self.reader.sub("branches/target"))
if self.use_state_handle && self.state_handle.file_exists() {
self.state_handle
.get_default_target()
.and_then(|op| op.ok_or(anyhow!("Branch not found")))
.map_err(|_| reader::Error::NotFound)
} else {
Target::try_from(&self.reader.sub("branches/target"))
}
}
/// If the target for the specified branchid is not found, returns the default target
pub fn read(&self, id: &BranchId) -> Result<Target, reader::Error> {
if !self
.reader
.exists(format!("branches/{}/target", id))
.map_err(reader::Error::from)?
{
return self.read_default();
}
if self.use_state_handle && self.state_handle.file_exists() {
let branch_target = self.state_handle.get_branch_target(id);
match branch_target {
Ok(Some(target)) => Ok(target),
Ok(None) => self.read_default(),
Err(_) => Err(reader::Error::NotFound),
}
} else {
if !self
.reader
.exists(format!("branches/{}/target", id))
.map_err(reader::Error::from)?
{
return self.read_default();
}
Target::try_from(&self.reader.sub(format!("branches/{}/target", id)))
Target::try_from(&self.reader.sub(format!("branches/{}/target", id)))
}
}
}