gitscope
← rgit / 0.1.3 / src/methods/repo/diff.rs
https://dev.kronoz.win/r/rgit.git
src/methods/repo/diff.rs · Rust
Blame Raw
 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::{
15        filters,
16        repo::{commit::UriQuery, Repository, RepositoryPath, Result},
17    },
18    Git,
19};
20
21#[derive(Template)]
22#[template(path = "repo/diff.html")]
23pub struct View {
24    pub repo: Repository,
25    pub commit: Arc<Commit>,
26    pub branch: Option<Arc<str>>,
27}
28
29pub async fn handle(
30    Extension(repo): Extension<Repository>,
31    Extension(RepositoryPath(repository_path)): Extension<RepositoryPath>,
32    Extension(git): Extension<Arc<Git>>,
33    Query(query): Query<UriQuery>,
34) -> Result<impl IntoResponse> {
35    let open_repo = git.repo(repository_path, query.branch.clone()).await?;
36    let commit = if let Some(commit) = query.id {
37        open_repo.commit(&commit).await?
38    } else {
39        Arc::new(open_repo.latest_commit().await?)
40    };
41
42    Ok(into_response(View {
43        repo,
44        commit,
45        branch: query.branch,
46    }))
47}
48
49pub async fn handle_plain(
50    Extension(RepositoryPath(repository_path)): Extension<RepositoryPath>,
51    Extension(git): Extension<Arc<Git>>,
52    Query(query): Query<UriQuery>,
53) -> Result<Response> {
54    let open_repo = git.repo(repository_path, query.branch).await?;
55    let commit = if let Some(commit) = query.id {
56        open_repo.commit(&commit).await?
57    } else {
58        Arc::new(open_repo.latest_commit().await?)
59    };
60
61    let headers = [(
62        http::header::CONTENT_TYPE,
63        HeaderValue::from_static("text/plain"),
64    )];
65
66    Ok((headers, commit.diff_plain.clone()).into_response())
67}