repos/vcs/
git.rs

1use std::path::Path;
2use std::process::Command;
3
4use super::super::metadata::Proxy;
5use super::util::gen_proxy_env_vars;
6use super::Vcs;
7
8pub struct Git;
9
10impl Git {
11    /// Create a new `Git`.
12    pub fn new() -> Self {
13        Git
14    }
15}
16
17impl Vcs for Git {
18    fn clone(&self, url: &str, path: &Path, bare: bool, proxy: Option<&Proxy>) {
19        // Build command arguments.
20        let mut args = Vec::new();
21        args.push("clone");
22        if bare {
23            args.push("--bare");
24        }
25        args.push(url);
26        args.push(path.to_str().unwrap());
27
28        let proxy_env_vars = gen_proxy_env_vars(proxy);
29        match Command::new("git")
30            .args(&args)
31            .envs(&proxy_env_vars)
32            .spawn()
33        {
34            Ok(mut child) => match child.wait() {
35                Ok(_status) => {}
36                Err(err) => println!("Failed to clone repository because of: {}", err.to_string()),
37            },
38            Err(err) => println!(
39                "Failed to execute git clone because of: {}",
40                err.to_string()
41            ),
42        }
43    }
44
45    fn update(&self, path: &Path, bare: bool, proxy: Option<&Proxy>) {
46        // Build command arguments.
47        let mut args = Vec::new();
48        if bare {
49            args.push("fetch");
50        } else {
51            args.push("pull");
52            args.push("--rebase");
53        }
54
55        let proxy_env_vars = gen_proxy_env_vars(proxy);
56        match Command::new("git")
57            .args(&args)
58            .current_dir(path)
59            .envs(&proxy_env_vars)
60            .spawn()
61        {
62            Ok(mut child) => match child.wait() {
63                Ok(_status) => {}
64                Err(err) => println!(
65                    "Failed to update repository because of: {}",
66                    err.to_string()
67                ),
68            },
69            Err(err) => {
70                if bare {
71                    println!(
72                        "Failed to execute git fetch because of: {}",
73                        err.to_string()
74                    );
75                } else {
76                    println!("Failed to execute git pull because of: {}", err.to_string());
77                }
78            }
79        }
80    }
81}