~/selfhut

selfhut/src/git/blame.rs -rw-r--r-- 938 B
1268e21e — Arthur Melton blame working 2 years ago
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use crate::config::CONFIG;
use std::path::Path;
use crate::git::commits::{Commits, get_commits};
use serde_derive::Serialize;
use std::ops::Range;

pub fn blame<'a>(repo: String, branch: String, path: String) -> Option<Vec<Blame>> {
    if branch.contains(":") {
        return None;
    }
    let mut repo_path = CONFIG.git_location.clone();
    repo_path.push(format!("{}.git", repo));
    let repo_clone = repo.clone();
    let repo = git2::Repository::open(repo_path).ok()?;
    let blame = repo.blame_file(Path::new(&path), None).ok()?;
    let mut blames = Vec::new();
    for i in blame.iter() {
        blames.push( Blame {
            commit: (*get_commits(repo_clone.clone(), 1, Some(i.final_commit_id().to_string()), None)?.first()?).clone(),
            lines: (0..i.lines_in_hunk()-1).collect()
        });
    }
    Some(blames)
}

#[derive(Serialize, Clone)]
pub struct Blame {
    commit: Commits,
    lines: Vec<usize>
}