~/selfhut

selfhut/src/git/get_all.rs -rw-r--r-- 1.18 kB
b961c5bf — Arthur Melton tarballs! 2 years ago

            
b961c5bf Arthur Melton
b961c5bf Arthur Melton
b961c5bf Arthur Melton
0016c2cc arthurmelton
b961c5bf Arthur Melton
0016c2cc arthurmelton
b961c5bf Arthur Melton
47885f67 Arthur Melton
b961c5bf Arthur Melton



























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
use crate::config::CONFIG;
use git2::ObjectType;
use humansize::{format_size, DECIMAL};
use serde_derive::Serialize;

use std::path::PathBuf;

pub fn files(repo: String, branch: String, path: String) -> Option<Vec<String>> {
    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 obj = repo.revparse_single(&format!("{}:{}", branch, path)).ok()?;
    let mut returns = Vec::new();
    match obj.kind() {
        Some(ObjectType::Tree) => {
            let tree = obj.as_tree()?;
            for entry in tree.iter() {
                if entry.kind() == Some(ObjectType::Blob) {
                    returns.push(format!("{}{}", path, entry.name()?));
                }
                else {
                    for i in files(repo_clone.clone(), branch.clone(), format!("{}{}/", path, entry.name()?))? {
                        returns.push(i);
                    }
                }
            }
        }
        _ => {
            return None;
        }
    }
    Some(returns)
}