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
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::path::Path;
use std::process::Command;

use super::super::metadata::Proxy;
use super::util::gen_proxy_env_vars;
use super::Vcs;

pub struct Git;

impl Git {
    /// Create a new `Git`.
    pub fn new() -> Self {
        Git
    }
}

impl Vcs for Git {
    fn clone(&self, url: &str, path: &Path, bare: bool, proxy: Option<&Proxy>) {
        // Build command arguments.
        let mut args = Vec::new();
        args.push("clone");
        if bare {
            args.push("--bare");
        }
        args.push(url);
        args.push(path.to_str().unwrap());

        let proxy_env_vars = gen_proxy_env_vars(proxy);
        match Command::new("git")
            .args(&args)
            .envs(&proxy_env_vars)
            .spawn()
        {
            Ok(mut child) => match child.wait() {
                Ok(_status) => {}
                Err(err) => println!("Failed to clone repository because of: {}", err.to_string()),
            },
            Err(err) => println!(
                "Failed to execute git clone because of: {}",
                err.to_string()
            ),
        }
    }

    fn update(&self, path: &Path, bare: bool, proxy: Option<&Proxy>) {
        // Build command arguments.
        let mut args = Vec::new();
        if bare {
            args.push("fetch");
        } else {
            args.push("pull");
            args.push("--rebase");
        }

        let proxy_env_vars = gen_proxy_env_vars(proxy);
        match Command::new("git")
            .args(&args)
            .current_dir(path)
            .envs(&proxy_env_vars)
            .spawn()
        {
            Ok(mut child) => match child.wait() {
                Ok(_status) => {}
                Err(err) => println!(
                    "Failed to update repository because of: {}",
                    err.to_string()
                ),
            },
            Err(err) => {
                if bare {
                    println!(
                        "Failed to execute git fetch because of: {}",
                        err.to_string()
                    );
                } else {
                    println!("Failed to execute git pull because of: {}", err.to_string());
                }
            }
        }
    }
}