~/selfhut

5ec2ce6e5e9a917ad9ffac37f9de8596e6130a12 — Arthur Melton 532ca616 2 years ago
show commits for path
3 files changed, 64 insertions(+), 28 deletions(-)

M src/git/commits.rs
M src/repository/summary.rs
M src/repository/tree.rs
M src/git/commits.rs => src/git/commits.rs +60 -24
@@ 2,11 2,13 @@ use crate::config::CONFIG;

use chrono::{Duration, Utc};
use chrono_humanize::HumanTime;
use serde_derive::Serialize;
use std::borrow::Cow;

pub fn get_commits(
    repo_name: String,
    ammount: usize,
    after: Option<String>,
    path: Option<String>,
) -> Option<Vec<Commits>> {
    let mut repo_path = CONFIG.git_location.clone();
    repo_path.push(format!("{}.git", repo_name));

@@ 26,30 28,64 @@ pub fn get_commits(

        let repo_commit = repo.find_commit(commit_rev);
        match repo_commit {
            Ok(repo_commit) => {
                let time = Duration::seconds(repo_commit.time().seconds() - Utc::now().timestamp());
                commits.push(Commits {
                    commit_hash: commit_rev.to_string(),
                    commit: repo_commit.message().unwrap_or("").to_string(),
                    commitie: repo_commit
                        .committer()
                        .name()
                        .unwrap_or("Unknown")
                        .to_string(),
                    commit_hash_short: commit_rev.to_string()[..8].to_string(),
                    time_utc: {
                        let date = chrono::naive::NaiveDateTime::from_timestamp(
                            Utc::now().timestamp() + time.num_seconds(),
                            0,
                        );
                        format!(
                            "{} {} UTC",
                            date.date().format("%Y-%m-%d"),
                            date.time().format("%H:%M:%S")
                        )
                    },
                    time_relitive: HumanTime::from(time).to_string(),
                });
                i += 1;
                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(_) => {}
                    }
                }
                if allowed {
                    let time = Duration::seconds(repo_commit.time().seconds() - Utc::now().timestamp());
                    commits.push(Commits {
                        commit_hash: commit_rev.to_string(),
                        commit: repo_commit.message().unwrap_or("").to_string(),
                        commitie: repo_commit
                            .committer()
                            .name()
                            .unwrap_or("Unknown")
                            .to_string(),
                        commit_hash_short: commit_rev.to_string()[..8].to_string(),
                        time_utc: {
                            let date = chrono::naive::NaiveDateTime::from_timestamp(
                                Utc::now().timestamp() + time.num_seconds(),
                                0,
                            );
                            format!(
                                "{} {} UTC",
                                date.date().format("%Y-%m-%d"),
                                date.time().format("%H:%M:%S")
                            )
                        },
                        time_relitive: HumanTime::from(time).to_string(),
                    });
                    i += 1;
                }
            }
            Err(_) => {}
        }

M src/repository/summary.rs => src/repository/summary.rs +1 -1
@@ 21,7 21,7 @@ pub fn repository(repo: String) -> Option<Template> {

            domain: CONFIG.domain.to_string(),
            readme: md_to_html(&file.1?),
            active: "summary",
            preview: get_commits(repo.clone(), 3, None),
            preview: get_commits(repo.clone(), 3, None, None),
            main_branch,
            tag: match get_tag(repo.clone(), 1) {
                Some(x) => match x.get(0) {

M src/repository/tree.rs => src/repository/tree.rs +3 -3
@@ 24,7 24,7 @@ pub fn tree_main(repo: String) -> Option<Template> {

            config: repo_config(repo.clone()),
            domain: CONFIG.domain.to_string(),
            active: "tree",
            commit: match get_commits(repo.clone(), 1, None) {
            commit: match get_commits(repo.clone(), 1, None, None) {
                Some(x) => match x.get(0) {
                    Some(x) => Some(x.clone()),
                    None => None

@@ 76,7 76,7 @@ pub fn tree(repo: String, branch: String, location: PathBufWithDotfiles) -> Opti

                    config: repo_config(repo.clone()),
                    domain: CONFIG.domain.to_string(),
                    active: "tree",
                    commit: match get_commits(repo.clone(), 1, None) {
                    commit: match get_commits(repo.clone(), 1, None, Some(format!("{}", location.get().display()).replace("//", "/"))) {
                        Some(x) => match x.clone().get(0) {
                            Some(x) => Some(x.clone()),
                            None => None

@@ 102,7 102,7 @@ pub fn tree(repo: String, branch: String, location: PathBufWithDotfiles) -> Opti

                config: repo_config(repo.clone()),
                domain: CONFIG.domain.to_string(),
                active: "tree",
                commit: match get_commits(repo.clone(), 1, None) {
                commit: match get_commits(repo.clone(), 1, None, Some(format!("{}", location.get().display()).replace("//", "/"))) {
                    Some(x) => match x.clone().get(0) {
                        Some(x) => Some(x.clone()),
                        None => None