~/selfhut

selfhut/src/git/branches.rs -rw-r--r-- 1.28 kB
e4d868e2 — Arthur Melton cargo fmt 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
use crate::config::CONFIG;
use crate::git::commits::{get_commits, Commits};
use serde_derive::Serialize;

pub fn branches(repo_name: String) -> Option<Vec<Branch>> {
    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 branches = Vec::new();
    for i in repo.branches(Some(git2::BranchType::Local)).ok()? {
        match i {
            Ok(x) => match x.0.name() {
                Ok(name) => match name {
                    Some(name) => branches.push(Branch {
                        branch: name.to_string(),
                        commit: match get_commits(
                            repo_name.clone(),
                            1,
                            Some(name.to_string()),
                            None,
                        ) {
                            Some(x) => x.first().cloned(),
                            None => None,
                        },
                    }),
                    None => {}
                },
                Err(_) => {}
            },
            Err(_) => {}
        }
    }
    Some(branches)
}

#[derive(Serialize, Clone)]
pub struct Branch {
    branch: String,
    commit: Option<Commits>,
}