Skip to content

Commit

Permalink
Delay handle opening of repositories in pmrrepo
Browse files Browse the repository at this point in the history
- Goal is to allow the possibility to allow a repository to be non-
  existent and still have the option to give that feedback.
- Also allow pathinfo to return empty repository data.
- All related structs have been modified to include optional fields.
  • Loading branch information
metatoaster committed Nov 7, 2024
1 parent 350e69c commit f1359b5
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 186 deletions.
6 changes: 3 additions & 3 deletions pmrapp/src/server/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub async fn raw_workspace_download(
// Ok(blob)

match &result.target() {
GitResultTarget::Object(object) => {
Some(GitResultTarget::Object(object)) => {
let info: PathObjectInfo = object.into();
match info {
PathObjectInfo::FileInfo(info) => {
Expand All @@ -69,12 +69,12 @@ pub async fn raw_workspace_download(
}
}
}
GitResultTarget::RemoteInfo(RemoteInfo { location, commit, subpath, .. }) => {
// XXX this should be a redirect
Some(GitResultTarget::RemoteInfo(RemoteInfo { location, commit, subpath, .. })) => {
Ok(Redirect::temporary(
&format!("{}/raw/{}/{}", location, commit, subpath)
).into_response())
},
None => Err(AppError::NotFound),
}
},
Err(_) => {
Expand Down
33 changes: 22 additions & 11 deletions pmrapp/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,12 @@ pub fn WorkspaceSynchronize() -> impl IntoView {
#[component]
fn WorkspaceListingView(repo_result: RepoResult) -> impl IntoView {
let workspace_id = repo_result.workspace.id;
let commit_id = repo_result.commit.commit_id.clone();
let path = repo_result.path.clone();
let pardir = repo_result.path != "";
let commit_id = repo_result.commit
.clone()
.map(|commit| commit.commit_id)
.unwrap_or_else(|| "<none>".to_string());
let path = repo_result.path.clone().unwrap_or_else(|| String::new());
let pardir = path != "";
let pardir = move || { pardir.then(|| view! {
<WorkspaceTreeInfoRow
workspace_id=workspace_id
Expand All @@ -260,8 +263,11 @@ fn WorkspaceListingView(repo_result: RepoResult) -> impl IntoView {
name=".."/>
})};

let commit_id = repo_result.commit.commit_id.clone();
let path = repo_result.path.clone();
let path = repo_result.path.clone().unwrap_or_else(|| String::new());
let commit_id = repo_result.commit
.clone()
.map(|commit| commit.commit_id)
.unwrap_or_else(|| "<none>".to_string());
view! {
<table class="file-listing">
<thead>
Expand All @@ -275,7 +281,7 @@ fn WorkspaceListingView(repo_result: RepoResult) -> impl IntoView {
{pardir}
{
match repo_result.target {
PathObjectInfo::TreeInfo(tree_info) =>
Some(PathObjectInfo::TreeInfo(tree_info)) =>
Some(view! {
<WorkspaceTreeInfoRows
workspace_id=workspace_id
Expand All @@ -294,13 +300,18 @@ fn WorkspaceListingView(repo_result: RepoResult) -> impl IntoView {

#[component]
fn WorkspaceFileInfoView(repo_result: RepoResult) -> impl IntoView {
let path = repo_result.path.clone().unwrap_or_else(|| String::new());
let commit_id = repo_result.commit
.map(|commit| commit.commit_id)
.unwrap_or_else(|| "<none>".to_string())
.clone();
match repo_result.target {
PathObjectInfo::FileInfo(ref file_info) => {
Some(PathObjectInfo::FileInfo(ref file_info)) => {
let href = format!(
"/workspace/{}/rawfile/{}/{}",
&repo_result.workspace.id,
&repo_result.commit.commit_id,
&repo_result.path,
&commit_id,
&path,
);
let info = format!("{:?}", file_info);
Some(view! {
Expand Down Expand Up @@ -329,9 +340,9 @@ fn WorkspaceFileInfoView(repo_result: RepoResult) -> impl IntoView {
#[component]
fn WorkspaceRepoResultView(repo_result: RepoResult) -> impl IntoView {
match repo_result.target {
PathObjectInfo::TreeInfo(_) =>
Some(PathObjectInfo::TreeInfo(_)) =>
Some(view! { <div><WorkspaceListingView repo_result/></div> }.into_any()),
PathObjectInfo::FileInfo(_) =>
Some(PathObjectInfo::FileInfo(_)) =>
Some(view! { <div><WorkspaceFileInfoView repo_result/></div> }.into_any()),
_ => None,
}
Expand Down
22 changes: 16 additions & 6 deletions pmrapp/src/workspace/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ pub async fn get_workspace_info(
) -> Result<RepoResult, ServerFnError<AppError>> {
enforcer(format!("/workspace/{id}/"), "").await?;
let platform = platform().await?;
Ok(platform.repo_backend()
let handle = platform.repo_backend()
.git_handle(id).await
.map_err(|_| AppError::InternalServerError)?
.pathinfo(commit, path)
.map_err(|_| AppError::InternalServerError)?
.into()
)
.map_err(|_| AppError::InternalServerError)?;
match (commit.as_ref(), path.as_ref(), handle.repo()) {
(None, None, Err(_)) => Ok(RepoResult {
workspace: handle.workspace().clone_inner(),
commit: None,
path: None,
target: None,
}),
(_, _, Err(_)) => Err(AppError::InternalServerError)?,
(_, _, Ok(_)) => Ok(handle
.pathinfo(commit, path)
.map_err(|_| AppError::InternalServerError)?
.into()
),
}
}

#[server]
Expand Down
6 changes: 3 additions & 3 deletions pmrcore/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ pub struct RepoResult {
/// The workspace that this result was derived from.
pub workspace: Workspace,
/// The commit id that this result was derived from.
pub commit: CommitInfo,
pub commit: Option<CommitInfo>,
/// The path to the target.
pub path: String,
pub path: Option<String>,
/// The target is the resource identified at the path.
pub target: PathObjectInfo,
pub target: Option<PathObjectInfo>,
}

/*
Expand Down
12 changes: 6 additions & 6 deletions pmrctrl/tests/pmrctrl_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ async fn test_platform_exposure_ctrl_attach_file() -> anyhow::Result<()> {

let efctrl = exposure.ctrl_file(ef_ref)?;
let pathinfo = efctrl.pathinfo();
assert_eq!(pathinfo.path(), "if1");
assert_eq!(pathinfo.path(), Some("if1"));

let efctrl = exposure.ctrl_path("if1").await?;
let pathinfo = efctrl.pathinfo();
assert_eq!(pathinfo.path(), "if1");
assert_eq!(pathinfo.path(), Some("if1"));

Ok(())
}
Expand Down Expand Up @@ -223,28 +223,28 @@ async fn test_platform_exposure_ctrl_resolve_file() -> anyhow::Result<()> {
.resolve_file_viewstr("dir1/nested/file_c")
.await
.expect("expected valid path not found");
assert_eq!(efctrl.pathinfo().path(), "dir1/nested/file_c");
assert_eq!(efctrl.pathinfo().path(), Some("dir1/nested/file_c"));
assert_eq!(viewstr, None);

let (efctrl, viewstr) = exposure
.resolve_file_viewstr("dir1/nested/file_c/")
.await
.expect("expected valid path not found");
assert_eq!(efctrl.pathinfo().path(), "dir1/nested/file_c");
assert_eq!(efctrl.pathinfo().path(), Some("dir1/nested/file_c"));
assert_eq!(viewstr, Some(""));

let (efctrl, viewstr) = exposure
.resolve_file_viewstr("dir1/nested/file_c/view")
.await
.expect("expected valid path not found");
assert_eq!(efctrl.pathinfo().path(), "dir1/nested/file_c");
assert_eq!(efctrl.pathinfo().path(), Some("dir1/nested/file_c"));
assert_eq!(viewstr, Some("view"));

let (efctrl, viewstr) = exposure
.resolve_file_viewstr("dir1/nested/file_c/view/subpath/target")
.await
.expect("expected valid path not found");
assert_eq!(efctrl.pathinfo().path(), "dir1/nested/file_c");
assert_eq!(efctrl.pathinfo().path(), Some("dir1/nested/file_c"));
assert_eq!(viewstr, Some("view/subpath/target"));

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion pmrrepo/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Backend {

pub async fn git_handle<'a>(&'a self, workspace_id: i64) -> Result<GitHandle<'a>, PmrRepoError> {
let workspace = self.db_platform.get_workspace(workspace_id).await?;
Ok(GitHandle::new(&self, self.repo_root.clone(), workspace)?)
Ok(GitHandle::new(&self, self.repo_root.clone(), workspace))
}

pub fn platform(&self) -> &(dyn MCPlatform + Send + Sync) {
Expand Down
8 changes: 4 additions & 4 deletions pmrrepo/src/bin/pmrrepo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ fn stream_git_result_default<'a>(
have path_object_info {:?}
\n",
item.repo().path(),
item.commit(&repo).id(),
item.commit(&repo).map(|c| c.id()),
item.commit(&repo),
item.path(),
&item.target(),
<PathObjectInfo>::from(item),
<Option<PathObjectInfo>>::from(item),
).as_bytes())
}

fn stream_git_result_as_json(
writer: impl Write,
item: &GitHandleResult,
) -> Result<(), serde_json::Error> {
serde_json::to_writer(writer, &<PathObjectInfo>::from(item))
serde_json::to_writer(writer, &<Option<PathObjectInfo>>::from(item))
}

fn fetch_envvar(key: &str) -> anyhow::Result<String> {
Expand Down Expand Up @@ -220,7 +220,7 @@ async fn main(args: Args) -> anyhow::Result<()> {
}
Some(Command::Blob { workspace_id, obj_id }) => {
let handle = backend.git_handle(workspace_id).await?;
let repo = handle.repo();
let repo = handle.repo()?;
let obj = repo.rev_parse_single(obj_id.deref())?.object()?;
log::info!("Found object {} {}", obj.kind, obj.id);
// info!("{:?}", object_to_info(&obj));
Expand Down
4 changes: 4 additions & 0 deletions pmrrepo/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@ pub enum PathError {
oid: String,
path: String,
},
#[error("couldn't open repository for workspace `{workspace_id}`")]
Repository {
workspace_id: i64,
},
}
12 changes: 9 additions & 3 deletions pmrrepo/src/handle/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ use gix::{
ObjectDetached,
ThreadSafeRepository,
};
use std::{
path::PathBuf,
sync::OnceLock,
};
use crate::error::GixError;

pub struct GitHandle<'repo> {
pub(super) backend: &'repo Backend,
pub(super) workspace: WorkspaceRef<'repo>,
pub(super) repo: ThreadSafeRepository,
pub(super) repo_dir: PathBuf,
pub(super) repo: OnceLock<Result<ThreadSafeRepository, GixError>>,
}

#[derive(Debug)]
Expand All @@ -23,8 +29,8 @@ pub enum GitResultTarget {
pub struct GitHandleResult<'repo> {
pub(super) backend: &'repo Backend,
pub(super) repo: &'repo ThreadSafeRepository,
pub(super) commit: ObjectDetached,
pub(super) target: GitResultTarget,
pub(super) commit: Option<ObjectDetached>,
pub(super) target: Option<GitResultTarget>,
pub(super) workspace: &'repo WorkspaceRef<'repo>,
}

Expand Down
Loading

0 comments on commit f1359b5

Please sign in to comment.