async_gitlib/
repo_init.rs

1use async_std::task::spawn_blocking;
2use git2::{Error, Repository, RepositoryInitOptions};
3use std::path::PathBuf;
4
5pub struct RepoInit {
6    bare: bool,
7    no_reinit: bool,
8    no_dotgit_dir: bool,
9    mkpath: bool,
10    workdir_path: PathBuf,
11    description: String,
12    initial_head: String,
13}
14
15impl RepoInit {
16
17    pub fn bare(&self) -> bool {
18        self.bare
19    }
20
21    pub fn set_bare(&mut self, enable: bool) {
22        self.bare = enable;
23    }
24
25    pub fn no_reinit(&self) -> bool {
26        self.no_reinit
27    }
28
29    pub fn set_no_reinit(&mut self, enable: bool) {
30        self.no_reinit = enable;
31    }
32
33    pub fn no_dotgit_dir(&self) -> bool {
34        self.no_dotgit_dir
35    }
36
37    pub fn set_no_dotgit_dir(&mut self, enable: bool) {
38        self.no_dotgit_dir = enable;
39    }
40
41    pub fn mkpath(&self) -> bool {
42        self.mkpath
43    }
44
45    pub fn set_mkpath(&mut self, enable: bool) {
46        self.mkpath = enable;
47    }
48
49    pub fn workdir_path(&self) -> &PathBuf {
50        &self.workdir_path
51    }
52
53    pub fn set_workdir_path<P>(&mut self, path: P)
54        where
55        P: Into<PathBuf>,
56    {
57        self.workdir_path = path.into();
58    }
59
60    pub fn description(&self) -> &str {
61        &self.description
62    }
63
64    pub fn set_description<D>(&mut self, desc: D)
65        where
66        D: Into<String>,
67    {
68        self.description = desc.into();
69    }
70
71    pub fn initial_head(&self) -> &str {
72        &self.initial_head
73    }
74
75    pub fn set_initial_head<H>(&mut self, head: H)
76        where
77        H: Into<String>,
78    {
79        self.initial_head = head.into();
80    }
81
82    pub async fn init<T>(&self, target: T) -> Result<(), Error>
83        where
84        T: Into<PathBuf>,
85    {
86        let target = target.into();
87
88        let mut opts = RepositoryInitOptions::new();
89        opts.bare(self.bare);
90        opts.no_reinit(self.no_reinit);
91        opts.no_dotgit_dir(self.no_dotgit_dir);
92        opts.mkpath(self.mkpath);
93        opts.workdir_path(&self.workdir_path);
94        opts.description(&self.description);
95        opts.initial_head(&self.initial_head);
96
97        spawn_blocking(move || {
98            Repository::init_opts(target, &opts)?;
99            Ok(())
100        }).await
101    }
102}
103
104impl Default for RepoInit {
105
106    fn default() -> Self {
107        Self {
108            bare: false,
109            no_reinit: false,
110            no_dotgit_dir: false,
111            mkpath: true,
112            workdir_path: PathBuf::from("."),
113            description: String::from("Unnamed repository"),
114            initial_head: String::from("master"),
115        }
116    }
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122    use tempfile::TempDir;
123
124    #[async_std::test]
125    async fn initializes() {
126        let target = TempDir::new().unwrap().path().to_owned();
127        let init = RepoInit::default();
128        init.init(&target).await.unwrap();
129        assert!(target.join(".git").exists());
130    }
131}