wip render repo
This commit is contained in:
parent
44c310ac11
commit
145e3c592e
6 changed files with 190 additions and 50 deletions
|
|
@ -1,6 +1,9 @@
|
|||
use std::{collections::HashSet, io::Write, path::PathBuf};
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::{
|
||||
config::Config,
|
||||
repo::{RepoMetadata, WriteRepoMetadataError},
|
||||
};
|
||||
|
||||
use rand::Rng;
|
||||
use serde::Deserialize;
|
||||
|
|
@ -108,6 +111,7 @@ pub enum FetchRepoError {
|
|||
TooManyEntries,
|
||||
Client(ClientError),
|
||||
UrlParsing,
|
||||
WriteRepoMetadata(WriteRepoMetadataError),
|
||||
}
|
||||
|
||||
impl From<ClientError> for FetchRepoError {
|
||||
|
|
@ -140,14 +144,20 @@ impl From<url::ParseError> for FetchRepoError {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<WriteRepoMetadataError> for FetchRepoError {
|
||||
fn from(value: WriteRepoMetadataError) -> Self {
|
||||
Self::WriteRepoMetadata(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RepoIndex {
|
||||
files: Vec<RepoIndexFile>,
|
||||
pub files: Vec<RepoIndexFile>,
|
||||
}
|
||||
|
||||
pub struct RepoIndexFile {
|
||||
path: String,
|
||||
url: String,
|
||||
pub path: String,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
pub async fn fetch_repo_tree_index_at_commit(
|
||||
|
|
@ -155,10 +165,11 @@ pub async fn fetch_repo_tree_index_at_commit(
|
|||
repo_url: &str,
|
||||
commit_hash: &str,
|
||||
token: Option<&str>,
|
||||
) -> Result<RepoIndex, FetchRepoError> {
|
||||
) -> Result<(RepoIndex, RepoMetadata), FetchRepoError> {
|
||||
let parsed = url::Url::parse(repo_url)?;
|
||||
let mut base = parsed.clone();
|
||||
base.set_fragment(None);
|
||||
base.set_path("");
|
||||
let base_url = base.as_str();
|
||||
// Is the URL always /owner/repo?
|
||||
let mut segments = parsed.path_segments().ok_or(FetchRepoError::UrlParsing)?;
|
||||
|
|
@ -196,7 +207,15 @@ pub async fn fetch_repo_tree_index_at_commit(
|
|||
}
|
||||
}
|
||||
|
||||
Ok(repo_index)
|
||||
Ok((
|
||||
repo_index,
|
||||
RepoMetadata {
|
||||
date: std::time::UNIX_EPOCH.elapsed().unwrap().as_secs(),
|
||||
repo_url: repo_url.to_string(),
|
||||
commit_hash: commit_hash.to_string(),
|
||||
content: Vec::new(),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
@ -208,6 +227,7 @@ pub async fn fetch_repo_files(
|
|||
config: &Config,
|
||||
client: &mut Client,
|
||||
repo_index: &RepoIndex,
|
||||
repo_metadata: &mut RepoMetadata,
|
||||
token: Option<&str>,
|
||||
) -> Result<(), FetchRepoError> {
|
||||
let mut hasher = sha2::Sha256::default();
|
||||
|
|
@ -246,6 +266,15 @@ pub async fn fetch_repo_files(
|
|||
base64_turbo::URL_SAFE
|
||||
.encode_into(file_name, &mut file_name_str)
|
||||
.expect("unreachable");
|
||||
|
||||
repo_metadata
|
||||
.content
|
||||
.extend_from_slice(&(file.path.len() as u32).to_be_bytes());
|
||||
repo_metadata
|
||||
.content
|
||||
.extend_from_slice(file.path.as_bytes());
|
||||
repo_metadata.content.extend_from_slice(&file_name_str);
|
||||
|
||||
let file_name_str = str::from_utf8(&file_name_str).expect("unreachable");
|
||||
if let Ok(mut file) = std::fs::OpenOptions::new()
|
||||
.create_new(true)
|
||||
|
|
@ -258,5 +287,7 @@ pub async fn fetch_repo_files(
|
|||
}
|
||||
}
|
||||
|
||||
repo_metadata.write_to_file(config, &repo_dir)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue