mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-11-29 14:25:45 +03:00
Merge pull request #2082 from gitbutlerapp/fix-session-exists-check
Fix session exists check
This commit is contained in:
commit
6bb4842cfe
@ -481,7 +481,7 @@ impl Repository {
|
||||
match sessions::Session::try_from(reader) {
|
||||
Ok(session) => Ok(Some(session)),
|
||||
Err(sessions::SessionError::NoSession) => Ok(None),
|
||||
Err(sessions::SessionError::Err(err)) => Err(err),
|
||||
Err(sessions::SessionError::Other(err)) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,50 +35,66 @@ pub enum SessionError {
|
||||
#[error("session does not exist")]
|
||||
NoSession,
|
||||
#[error("{0}")]
|
||||
Err(anyhow::Error),
|
||||
Other(anyhow::Error),
|
||||
}
|
||||
|
||||
impl TryFrom<&dyn reader::Reader> for Session {
|
||||
type Error = SessionError;
|
||||
|
||||
fn try_from(reader: &dyn reader::Reader) -> Result<Self, Self::Error> {
|
||||
if !reader.exists(path::Path::new("session/meta")) {
|
||||
return Err(SessionError::NoSession);
|
||||
}
|
||||
|
||||
let id: String = reader
|
||||
.read(path::Path::new("session/meta/id"))
|
||||
.context("failed to read session id")
|
||||
.map_err(SessionError::Err)?
|
||||
.map_err(|error| match error {
|
||||
reader::Error::NotFound => SessionError::NoSession,
|
||||
_ => SessionError::Other(error.into()),
|
||||
})?
|
||||
.try_into()
|
||||
.context("failed to parse session id")
|
||||
.map_err(SessionError::Err)?;
|
||||
.context("failed to parse session id as string")
|
||||
.map_err(SessionError::Other)?;
|
||||
|
||||
let id: SessionId = id
|
||||
.parse()
|
||||
.context("failed to parse session id")
|
||||
.map_err(SessionError::Err)?;
|
||||
.context("failed to parse session id as id")
|
||||
.map_err(SessionError::Other)?;
|
||||
|
||||
let start_timestamp_ms = reader
|
||||
.read(path::Path::new("session/meta/start"))
|
||||
.context("failed to read session start timestamp")
|
||||
.map_err(SessionError::Err)?
|
||||
.map_err(|error| match error {
|
||||
reader::Error::NotFound => SessionError::NoSession,
|
||||
_ => SessionError::Other(error.into()),
|
||||
})?
|
||||
.try_into()
|
||||
.context("failed to parse session start timestamp")
|
||||
.map_err(SessionError::Err)?;
|
||||
.context("failed to parse session start timestamp as number")
|
||||
.map_err(SessionError::Other)?;
|
||||
|
||||
let last_timestamp_ms = reader
|
||||
.read(path::Path::new("session/meta/last"))
|
||||
.context("failed to read session last timestamp")
|
||||
.map_err(SessionError::Err)?
|
||||
.map_err(|error| match error {
|
||||
reader::Error::NotFound => SessionError::NoSession,
|
||||
_ => SessionError::Other(error.into()),
|
||||
})?
|
||||
.try_into()
|
||||
.context("failed to parse session last timestamp")
|
||||
.map_err(SessionError::Err)?;
|
||||
.context("failed to parse session last timestamp as number")
|
||||
.map_err(SessionError::Other)?;
|
||||
|
||||
let branch = match reader.read(path::Path::new("session/meta/branch")) {
|
||||
Ok(reader::Content::UTF8(branch)) => Some(branch.clone()),
|
||||
_ => None,
|
||||
Err(reader::Error::NotFound) => None,
|
||||
_ => {
|
||||
return Err(SessionError::Other(anyhow::anyhow!(
|
||||
"failed to read branch"
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
let commit = match reader.read(path::Path::new("session/meta/commit")) {
|
||||
Ok(reader::Content::UTF8(commit)) => Some(commit.clone()),
|
||||
_ => None,
|
||||
Err(reader::Error::NotFound) => None,
|
||||
_ => {
|
||||
return Err(SessionError::Other(anyhow::anyhow!(
|
||||
"failed to read commit"
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
|
Loading…
Reference in New Issue
Block a user