Skip to content

Commit

Permalink
Split repo methods into their own modules
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Jul 23, 2022
1 parent c220112 commit f74037c
Show file tree
Hide file tree
Showing 23 changed files with 712 additions and 566 deletions.
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use anyhow::Context;
use std::{
io::Write,
path::{Path, PathBuf},
};

use anyhow::Context;

#[derive(Copy, Clone)]
pub struct Paths<'a> {
statics_in_dir: &'a Path,
Expand Down
7 changes: 7 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
edition = "2021"
## not yet supported on stable
#imports_granularity = "Crate"
newline_style = "Unix"
## not yet supported on stable
#group_imports = "StdExternalCrate"
use_field_init_shorthand = true
7 changes: 5 additions & 2 deletions src/database/indexer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{
collections::HashSet,
path::{Path, PathBuf},
};

use git2::Sort;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use time::OffsetDateTime;
use tracing::{info, info_span};

Expand Down
7 changes: 4 additions & 3 deletions src/database/schema/commit.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::database::schema::Yoked;
use std::{borrow::Cow, ops::Deref};

use git2::{Oid, Signature};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sled::IVec;
use std::borrow::Cow;
use std::ops::Deref;
use time::OffsetDateTime;
use yoke::{Yoke, Yokeable};

use crate::database::schema::Yoked;

#[derive(Serialize, Deserialize, Debug, Yokeable)]
pub struct Commit<'a> {
#[serde(borrow)]
Expand Down
3 changes: 2 additions & 1 deletion src/database/schema/prefixes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::database::schema::repository::RepositoryId;
use std::path::Path;

use crate::database::schema::repository::RepositoryId;

#[repr(u8)]
pub enum TreePrefix {
Repository = 0,
Expand Down
12 changes: 4 additions & 8 deletions src/database/schema/repository.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use crate::database::schema::commit::CommitTree;
use crate::database::schema::prefixes::TreePrefix;
use crate::database::schema::tag::TagTree;
use crate::database::schema::Yoked;
use std::{borrow::Cow, collections::BTreeMap, ops::Deref, path::Path};

use anyhow::{Context, Result};
use nom::AsBytes;
use serde::{Deserialize, Serialize};
use sled::IVec;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::ops::Deref;
use std::path::Path;
use time::OffsetDateTime;
use yoke::{Yoke, Yokeable};

use crate::database::schema::{commit::CommitTree, prefixes::TreePrefix, tag::TagTree, Yoked};

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Yokeable)]
pub struct Repository<'a> {
/// The ID of the repository, as stored in `sled`
Expand Down
8 changes: 4 additions & 4 deletions src/database/schema/tag.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::database::schema::commit::Author;
use crate::database::schema::Yoked;
use std::{collections::HashSet, ops::Deref};

use git2::Signature;
use serde::{Deserialize, Serialize};
use sled::IVec;
use std::collections::HashSet;
use std::ops::Deref;
use yoke::{Yoke, Yokeable};

use crate::database::schema::{commit::Author, Yoked};

#[derive(Serialize, Deserialize, Debug, Yokeable)]
pub struct Tag<'a> {
#[serde(borrow)]
Expand Down
39 changes: 21 additions & 18 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::ffi::OsStr;
use std::path::Path;
use std::{borrow::Cow, fmt::Write, path::PathBuf, sync::Arc, time::Duration};
use std::{
borrow::Cow,
ffi::OsStr,
fmt::Write,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};

use crate::syntax_highlight::ComrakSyntectAdapter;
use anyhow::{Context, Result};
use bytes::{Bytes, BytesMut};
use comrak::{ComrakOptions, ComrakPlugins};
Expand All @@ -11,12 +15,16 @@ use git2::{
};
use moka::future::Cache;
use parking_lot::Mutex;
use syntect::html::{ClassStyle, ClassedHTMLGenerator};
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;
use syntect::{
html::{ClassStyle, ClassedHTMLGenerator},
parsing::SyntaxSet,
util::LinesWithEndings,
};
use time::OffsetDateTime;
use tracing::instrument;

use crate::syntax_highlight::ComrakSyntectAdapter;

pub struct Git {
commits: Cache<Oid, Arc<Commit>>,
readme_cache: Cache<PathBuf, Option<(ReadmeFormat, Arc<str>)>>,
Expand Down Expand Up @@ -109,8 +117,7 @@ impl OpenRepository {
let extension = path
.extension()
.or_else(|| path.file_name())
.map(|v| v.to_string_lossy())
.unwrap_or_else(|| Cow::Borrowed(""));
.map_or_else(|| Cow::Borrowed(""), OsStr::to_string_lossy);
let content = format_file(blob.content(), &extension, &self.git.syntax_set)?;

return Ok(PathDestination::File(FileWithContent {
Expand Down Expand Up @@ -187,8 +194,7 @@ impl OpenRepository {
tagger: tag.tagger().map(TryInto::try_into).transpose()?,
message: tag
.message_bytes()
.map(String::from_utf8_lossy)
.unwrap_or_else(|| Cow::Borrowed(""))
.map_or_else(|| Cow::Borrowed(""), String::from_utf8_lossy)
.into_owned(),
tagged_object,
})
Expand Down Expand Up @@ -440,13 +446,11 @@ impl TryFrom<git2::Commit<'_>> for Commit {
parents: commit.parent_ids().map(|v| v.to_string()).collect(),
summary: commit
.summary_bytes()
.map(String::from_utf8_lossy)
.unwrap_or_else(|| Cow::Borrowed(""))
.map_or_else(|| Cow::Borrowed(""), String::from_utf8_lossy)
.into_owned(),
body: commit
.body_bytes()
.map(String::from_utf8_lossy)
.unwrap_or_else(|| Cow::Borrowed(""))
.map_or_else(|| Cow::Borrowed(""), String::from_utf8_lossy)
.into_owned(),
diff_stats: String::with_capacity(0),
diff: String::with_capacity(0),
Expand Down Expand Up @@ -559,8 +563,7 @@ fn format_diff(diff: &git2::Diff<'_>, syntax_set: &SyntaxSet) -> Result<String>
if let Some(path) = delta.new_file().path() {
path.extension()
.or_else(|| path.file_name())
.map(|v| v.to_string_lossy())
.unwrap_or_else(|| Cow::Borrowed(""))
.map_or_else(|| Cow::Borrowed(""), OsStr::to_string_lossy)
} else {
Cow::Borrowed("")
}
Expand All @@ -572,7 +575,7 @@ fn format_diff(diff: &git2::Diff<'_>, syntax_set: &SyntaxSet) -> Result<String>
.unwrap_or_else(|| syntax_set.find_syntax_plain_text());
let mut html_generator =
ClassedHTMLGenerator::new_with_class_style(syntax, syntax_set, ClassStyle::Spaced);
let _ = html_generator.parse_html_for_line_which_includes_newline(&line);
let _res = html_generator.parse_html_for_line_which_includes_newline(&line);
if let Some(class) = class {
let _ = write!(diff_output, r#"<span class="diff-{class}">"#);
}
Expand Down
12 changes: 7 additions & 5 deletions src/git_cgi.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::str::FromStr;

use anyhow::{bail, Context, Result};
use axum::body::{boxed, Body};
use axum::http::header::HeaderName;
use axum::http::HeaderValue;
use axum::response::Response;
use axum::{
body::{boxed, Body},
http::{header::HeaderName, HeaderValue},
response::Response,
};
use httparse::Status;
use std::str::FromStr;

// https://en.wikipedia.org/wiki/Common_Gateway_Interface
pub fn cgi_to_response(buffer: &[u8]) -> Result<Response> {
Expand Down
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#![deny(clippy::pedantic)]

use std::{sync::Arc, time::Duration};

use askama::Template;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{
body::Body, handler::Handler, http, http::HeaderValue, response::Response, routing::get,
body::Body,
handler::Handler,
http,
http::{HeaderValue, StatusCode},
response::{IntoResponse, Response},
routing::get,
Extension, Router,
};
use bat::assets::HighlightingAssets;
use std::sync::Arc;
use std::time::Duration;
use syntect::html::ClassStyle;
use tower_layer::layer_fn;
use tracing::{info, instrument};
Expand Down
8 changes: 3 additions & 5 deletions src/methods/index.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use anyhow::Context;
use std::collections::BTreeMap;

use anyhow::Context;
use askama::Template;
use axum::response::Response;
use axum::Extension;
use axum::{response::Response, Extension};

use super::filters;
use crate::database::schema::repository::Repository;
use crate::into_response;
use crate::{database::schema::repository::Repository, into_response};

#[derive(Template)]
#[template(path = "index.html")]
Expand Down
Loading

0 comments on commit f74037c

Please sign in to comment.