summaryrefslogtreecommitdiffstats
path: root/src/model/repo.rs
blob: a656bac019749bc20db4a3a69804b36ab65b29a9 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::path::Path;
use std::sync::Mutex;

use anyhow::Result;
use anyhow::anyhow;

use crate::model::backend::worker::BackendWorker;
use crate::model::backend::fassade::BackendFassade;

#[derive(getset::Getters)]
pub struct Repo {
    #[getset(get = "pub")]
    name: String,

    #[getset(get = "pub")]
    repo: git2::Repository,

    backend: BackendFassade,
}

impl Repo {
    pub fn open_repo<P: AsRef<Path>>(path: P) -> Result<Self> {
        let name = path.as_ref()
            .file_name()
            .and_then(std::ffi::OsStr::to_str)
            .map(String::from)
            .ok_or_else(|| anyhow!("Not valid UTF-8, cannot process: {}", path.as_ref().display()))?;

        log::info!("Opening {} = {}", name, path.as_ref().display());
        let repo = git2::Repository::open(&path)?;
        let repo2 = git2::Repository::open(&path)?; // TODO: Remove duplicated open()

        let (sender, receiver) = crossbeam::channel::unbounded();
        let worker = BackendWorker::new(name.clone(), Mutex::new(repo2), receiver);
        let backend = BackendFassade::new(sender);

        std::thread::spawn(move || {
            worker.run()
        });

        Ok(Repo { name, repo, backend })
    }

    pub async fn stat(&self) -> Result<cached::Return<RepositoryStat>> {
        let (branches, tags) = futures::try_join!(self.branch_names(), self.tag_names())?;

        Ok(cached::Return::new(RepositoryStat {
            branches: branches,
            tags: tags,
        }))
    }

    pub async fn branch_names(&self) -> Result<Vec<String>> {
        self.backend.get_branch_list().await
    }

    pub async fn tag_names(&self) -> Result<Vec<String>> {
        self.backend.get_tag_list().await
    }
}


#[derive(Clone)]
pub struct RepositoryStat {
    pub branches: Vec<String>,
    pub tags: Vec<String>,
}