~/selfhut

selfhut/src/git/tag.rs -rw-r--r-- 1.67 kB
b1605a54 — Arthur Melton refs 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::config::CONFIG;
use crate::git::commits::{get_commits, Commits};
use serde_derive::Serialize;

pub fn get_tag(repo_name: String, amount: usize, after: usize, name: Option<String>) -> Option<(Vec<Tags>, usize)> {
    let mut repo_path = CONFIG.git_location.clone();
    repo_path.push(format!("{}.git", repo_name));
    let repo = git2::Repository::open(repo_path).ok()?;
    let mut tags = Vec::new();
    let total = repo.tag_names(None).ok()?.len();
    let mut i = total-1;
    repo.tag_foreach(|x,y| {
        if (name.is_some() && name.as_ref().unwrap().as_bytes() == &y[10..]) || name.is_none() {
            if i >= after && (total < amount + after ||  i < amount - after) {
                match get_commits(repo_name.clone(), 1, Some(x.to_string()), None) {
                    Some(z) => match z.first() {
                        Some(z) => tags.push(Tags {
                            commit: z.clone(),
                            body: match repo.find_tag(x) {
                                Ok(x) => x.message().unwrap_or("").to_string(),
                                Err(_) => "".to_string()
                            },
                            name: std::str::from_utf8(&y[10..]).unwrap_or("").to_string()
                        }),
                        None => {}
                    },
                    None => {}
                }
            }
            if i < after+1 {
                return false;
            }
            i-=1;
        }
        true
    });
    tags.reverse();
    Some((tags, total))
}

#[derive(Serialize, Clone)]
pub struct Tags {
    commit: Commits,
    name: String,
    body: String,
}