1use std::sync::Arc;
2
3use askama::Template;
4use axum::{
5 extract::Query,
6 http::HeaderValue,
7 response::{IntoResponse, Response},
8 Extension,
9};
10
11use crate::{
12 git::Commit,
13 http, into_response,
14 methods::repo::{commit::UriQuery, Repository, RepositoryPath, Result},
15 Git,
16};
17
18#[derive(Template)]
19#[template(path = "repo/diff.html")]
20pub struct View {
21 pub repo: Repository,
22 pub commit: Arc<Commit>,
23}
24
25pub async fn handle(
26 Extension(repo): Extension<Repository>,
27 Extension(RepositoryPath(repository_path)): Extension<RepositoryPath>,
28 Extension(git): Extension<Arc<Git>>,
29 Query(query): Query<UriQuery>,
30) -> Result<Response> {
31 let open_repo = git.repo(repository_path).await?;
32 let commit = if let Some(commit) = query.id {
33 open_repo.commit(&commit).await?
34 } else {
35 Arc::new(open_repo.latest_commit().await?)
36 };
37
38 Ok(into_response(&View { repo, commit }))
39}
40
41pub async fn handle_plain(
42 Extension(RepositoryPath(repository_path)): Extension<RepositoryPath>,
43 Extension(git): Extension<Arc<Git>>,
44 Query(query): Query<UriQuery>,
45) -> Result<Response> {
46 let open_repo = git.repo(repository_path).await?;
47 let commit = if let Some(commit) = query.id {
48 open_repo.commit(&commit).await?
49 } else {
50 Arc::new(open_repo.latest_commit().await?)
51 };
52
53 let headers = [(
54 http::header::CONTENT_TYPE,
55 HeaderValue::from_static("text/plain"),
56 )];
57
58 Ok((headers, commit.diff_plain.clone()).into_response())
59}