~/selfhut

76d4fa6d683dd47a527d44c570e85b62b12ff751 — Arthur Melton beb58dd0 2 years ago
log

This is kinda a test but this adds the log tab and others
M src/git/commits.rs => src/git/commits.rs +21 -33
@@ 3,6 3,7 @@ use chrono::{Duration, Utc};

use chrono_humanize::HumanTime;
use serde_derive::Serialize;
use std::borrow::Cow;
use crate::git::diffs::diffs;

pub fn get_commits(
    repo_name: String,

@@ 31,33 32,18 @@ pub fn get_commits(

                let mut allowed = true;
                if path.is_some() {
                    let path_value = path.as_ref().unwrap();
                    match repo_commit.tree() {
                        Ok(tree) => match repo_commit.parent(0) {
                            Ok(parent) => match parent.tree() {
                                Ok(parent_tree) => {
                                    match repo.diff_tree_to_tree(
                                        Some(&tree),
                                        Some(&parent_tree),
                                        None,
                                    ) {
                                        Ok(diff) => {
                                            let files = diff
                                                .deltas()
                                                .map(|i| i.new_file().path())
                                                .filter(|i| i.is_some())
                                                .map(|i| i.unwrap().to_string_lossy())
                                                .filter(|i| i.starts_with(path_value))
                                                .count();
                                            allowed = files > 0;
                                        }
                                        Err(_) => {}
                                    }
                                }
                                Err(_) => {}
                            },
                            Err(_) => {}
                        },
                        Err(_) => {}
                    match diffs(repo_commit.clone(), &repo) {
                        Some(diff) => {
                            let files = diff
                                .deltas()
                                .map(|i| i.new_file().path())
                                .filter(|i| i.is_some())
                                .map(|i| i.unwrap().to_string_lossy())
                                .filter(|i| i.starts_with(path_value))
                                .count();
                            allowed = files > 0;
                        }
                        None => {}
                    }
                }
                if allowed {

@@ 84,6 70,7 @@ pub fn get_commits(

                            )
                        },
                        time_relitive: HumanTime::from(time).to_string(),
                        commit_body: repo_commit.body().unwrap_or("").to_string()
                    });
                    i += 1;
                }

@@ 97,10 84,11 @@ pub fn get_commits(


#[derive(Serialize, Clone)]
pub struct Commits {
    commit: String,
    commit_hash: String,
    commit_hash_short: String,
    commitie: String,
    time_utc: String,
    time_relitive: String,
    pub commit: String,
    pub commit_hash: String,
    pub commit_hash_short: String,
    pub commitie: String,
    pub time_utc: String,
    pub time_relitive: String,
    pub commit_body: String,
}

A src/git/diffs.rs +23 -0
@@ 0,0 1,23 @@

pub fn diffs<'a>(commit: git2::Commit<'a>, repo: &'a git2::Repository) -> Option<git2::Diff<'a>> {    
    match commit.tree() {
        Ok(tree) => match commit.parent(0) {
            Ok(parent) => match parent.tree() {
                Ok(parent_tree) => {
                    match repo.diff_tree_to_tree(
                        Some(&tree),
                        Some(&parent_tree),
                        None,
                    ) {
                        Ok(diff) => {
                            Some(diff)
                        }
                        Err(_) => None
                    }
                }
                Err(_) => None
            },
            Err(_) => None
        },
        Err(_) => None
    }
}

M src/git/mod.rs => src/git/mod.rs +1 -0
@@ 3,3 3,4 @@ pub mod file;

pub mod main_branch;
pub mod repos;
pub mod tag;
pub mod diffs;

M src/main.rs => src/main.rs +4 -1
@@ 11,6 11,7 @@ use utils::config;

use rocket::fs::relative;
use std::path::Path;

use crate::repository::log;
use crate::repository::raw;
use crate::repository::summary;
use crate::repository::tree;

@@ 30,7 31,9 @@ fn rocket() -> _ {

                tree::tree_main,
                tree::tree,
                clone::clone,
                raw::raw
                raw::raw,
                log::log_main,
                log::log,
            ],
        )
        .attach(Template::fairing())

M src/repository/log.rs => src/repository/log.rs +65 -22
@@ 13,9 13,30 @@ use syntect::util::LinesWithEndings;

use syntect::html::highlighted_html_for_string;
use std::path::Path;

#[get("/<repo>/log", rank=2)]
pub fn tree_main(repo: String) -> Option<Template> {
#[get("/<repo>/log?<from>", rank=2)]
pub fn log_main(repo: String, from: Option<String>) -> Option<Template> {
    let main_branch = main_branch(repo.clone())?;
    let commits = get_commits(repo.clone(), 21, from, None);
    let last_commit = match commits {
        Some(ref commits) => {
            if commits.len() == 21 {
                Some(commits[19].commit_hash.clone())
            }
            else {
                None
            }
        },
        None => None
    };
    let commits = match commits {
        Some(mut commits) => {
            if commits.len() == 21 {
                commits.pop();
            }
            Some(commits)
        },
        None => None
    };
    Some(Template::render(
            "repository/log",
            context! {

@@ 23,32 44,54 @@ pub fn tree_main(repo: String) -> Option<Template> {

                repo: repo.clone(),
                config: repo_config(repo.clone()),
                domain: CONFIG.domain.to_string(),
                active: "tree",
                commits: get_commits(repo.clone(), 1, None),
                active: "log",
                commits,
                branch: main_branch.clone(),
                current_dir_file: "/",
                current_dir: "/",
                payment: CONFIG.payment_link.clone()
                payment: CONFIG.payment_link.clone(),
                last_commit,
            }
        ))
}

#[get("/<repo>/log/<branch>/<location..>", rank=2)]
pub fn tree(repo: String, branch: String, location: PathBufWithDotfiles) -> Option<Template> {
        Some(Template::render(
            "repository/log",
            context! {
                title: format!("/{} :: {}", location.get().display(), repo.clone()),
                repo: repo.clone(),
                config: repo_config(repo.clone()),
                domain: CONFIG.domain.to_string(),
                active: "tree",
                commits: get_commits(repo.clone(), 1, None),
                branch: branch.clone(),
                current_dir_file: format!("/{}/", location.get().display()).replace("//", "/"),
                current_dir: format!("/{}", location.get().display()),
                payment: CONFIG.payment_link.clone()
#[get("/<repo>/log/<branch>/<location..>?<from>", rank=2)]
pub fn log(repo: String, branch: String, location: PathBufWithDotfiles, from: Option<String>) -> Option<Template> {
    let commits = get_commits(repo.clone(), 21, from, Some(format!("{}", location.get().display()).replace("//", "/")));
    let last_commit = match commits {
        Some(ref commits) => {
            if commits.len() == 21 {
                Some(commits[19].commit_hash.clone())
            }
        ))
    }
            else {
                None
            }
        },
        None => None
    };
    let commits = match commits {
        Some(mut commits) => {
            if commits.len() == 21 {
                commits.pop();
            }
            Some(commits)
        },
        None => None
    };
    Some(Template::render(
        "repository/log",
        context! {
            title: format!("/{} :: {}", location.get().display(), repo.clone()),
            repo: repo.clone(),
            config: repo_config(repo.clone()),
            domain: CONFIG.domain.to_string(),
            active: "log",
            commits,
            branch: branch.clone(),
            current_dir_file: format!("/{}/", location.get().display()).replace("//", "/"),
            current_dir: format!("/{}", location.get().display()),
            payment: CONFIG.payment_link.clone(),
            last_commit,
        }
    ))
}

M src/repository/mod.rs => src/repository/mod.rs +1 -0
@@ 1,3 1,4 @@

pub mod raw;
pub mod summary;
pub mod tree;
pub mod log;

M templates/repository/log.html.hbs => templates/repository/log.html.hbs +23 -54
@@ 1,63 1,32 @@

{{#*inline "page"}}
{{> navbar}}
<div class="header-extension">
	<div class="container tree-header">
		<div class="breadcrumb">
			{{repo}}{{current_dir}}
			<span class="text-muted" style="margin-left: 1rem">
			<span title="40000">
			d---------
			</span>
			</span>
			<div class="blob-nav" style="margin-left: 1rem">
				<ul class="nav nav-tabs">
					<li class="nav-item">
						<a class="nav-link active" href="/{{repo}}/tree/{{branch}}{{current_dir}}">Tree</a>
					</li>
					<li class="nav-item">
						<a class="nav-link" href="/{{repo}}/log/{{branch}}{{current_dir}}">Log</a>
					</li>
				</ul>
			</div>
		</div>
		{{#if commit}}
		<div class="commit-info">
			<a href="/{{repo}}/commit/{{branch}}">{{commit.commit_hash_short}}</a> —
			{{commit.commitie}}
			{{commit.commit}}
			<span class="text-muted">
			<span title="{{commit.time_utc}}">{{commit.time_relitive}}</span>
			</span>
		</div>
		{{/if}}
	</div>
</div>
<div class="container">
	<div class="row" style="margin-bottom: 1rem">
		<div class="col-md-12">
			<div class="tree-list">
				{{#each files}}	
				<div class="mode">
					<span title="{{this.properties_int}}">
					{{this.properties}}
					</span>
				</div>
				<div class="name {{this.file_type}}">
					<a href="/{{../repo}}/tree/{{../branch}}{{../current_dir_file}}{{this.name}}">
					{{this.name}}
					</a>
				</div>
				<div class="commit"></div>
				<div class="date"></div>
				<div class="size">
					<span title="{{this.size}}">
					{{this.size}}
					</span>
				</div>
				{{/each}}
			<div class="event-list">
				{{#each commits}}	
				<div class="event">
                <div>
                <a href="/{{../repo}}/commit/{{this.commit_hash}}" title="{{this.commit_hash}}">{{this.commit_hash_short}}</a> — {{this.commitie}}  
                <small class="pull-right">
                <a href="/{{../repo}}/log?from={{this.commit_hash}}#log-{{this.commit_hash}}" id="log-{{this.commit_hash}}"><span title="{{this.time_utc}}">{{this.time_relitive}}</span></a>
                </small>
                </div>
                {{#if (eq this.commit_body "")}}
                <pre class="commit">{{this.commit}}</pre>
                {{else}}
                <pre class="commit">{{this.commit}}

{{this.commit_body}}</pre>
                {{/if}}
                </div>
                {{/each}}
			</div>
		</div>
            {{#if last_commit}}
                <a class="pull-right btn btn-primary" href="/{{repo}}/log/{{branch}}{{current_dir}}?from={{last_commit}}">Next <span aria-hidden="true" class="icon icon-caret-right"><svg viewBox="0 0 192 512" xmlns="http://www.w3.org/2000/svg"><path d="M0 384.662V127.338c0-17.818 21.543-26.741 34.142-14.142l128.662 128.662c7.81 7.81 7.81 20.474 0 28.284L34.142 398.804C21.543 411.404 0 402.48 0 384.662z"></path></svg></span></a>
		    {{/if}}
        </div>
	</div>
</div>
{{/inline}}
{{> layout}}
\ No newline at end of file
{{> layout}}