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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use error;
use project::{Project, SnippetLocation};
use std::ffi::OsStr;
use std::io::Error;
use std::process::Output;
use std::process::{Command, Stdio};

/// Runt git command for this snippet location
fn run_git_command_for<I, S>(location: &SnippetLocation, commands: I) -> Result<Output, Error>
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    Command::new("git")
        .stdout(Stdio::piped())
        .current_dir(&location.local)
        .args(commands)
        .spawn()?
        .wait_with_output()
}

/// Determine the git support for a project, modifies the git status
pub fn determine_git_status(project: &mut Project) -> bool {
    for location in &mut project.locations {
        if location.git == None {
            let support = determine_git_support(location)
                .expect("Cannot determine git support for project location");
            if support {
                location.git = Some(true);
            } else {
                location.git = Some(false);
            }
        }
    }
    false
}

/// Determine the git support for a given snippet location
pub fn determine_git_support(location: &mut SnippetLocation) -> Result<bool, error::Error> {
    let output = run_git_command_for(location, &["rev-parse", "--is-inside-work-tree"]);
    let output_str_result = String::from_utf8(output?.stdout);
    match output_str_result {
        Ok(s) => {
            if s.eq_ignore_ascii_case("true\n") {
                return Ok(true);
            } else {
                return Ok(false);
            }
        }
        Err(_) => Ok(false),
    }
}

/// Struct that gives the git status of the project
pub enum GitStatus {
    Clean,
    Modified,
}

/// Determine the git file status for the snippet location
pub fn determine_git_modified_status(
    location: &SnippetLocation,
) -> Result<GitStatus, error::Error> {
    let output = run_git_command_for(location, &["status", "--porcelain"]);
    let output_str_result = String::from_utf8(output?.stdout);

    output_str_result.map(|s| {
        if s.eq_ignore_ascii_case("") {
            Ok(GitStatus::Clean)
        } else {
            Ok(GitStatus::Modified)
        }
    })?
}

/// Sync/pull git location with upstream repo
pub fn git_pull(location: &SnippetLocation) -> Result<(), error::Error> {
    let output = run_git_command_for(location, &["pull"]);

    // Return if success
    if output?.status.success() {
        Ok(())
    } else {
        Err(error::Error::InternalError(
            "Failed to execute pull command".to_string(),
        ))
    }
}

/// Git push from snippet location
pub fn git_push(location: &SnippetLocation) -> Result<(), error::Error> {
    let output = run_git_command_for(location, &["push"]);

    // Return if success
    if output?.status.success() {
        Ok(())
    } else {
        Err(error::Error::InternalError(
            "Failed to execute push command".to_string(),
        ))
    }
}

/// Git add from snippet location
pub fn git_add(location: &SnippetLocation) -> Result<(), error::Error> {
    let output = run_git_command_for(location, &["add", "-A"]);

    // Return if success
    if output?.status.success() {
        Ok(())
    } else {
        Err(error::Error::InternalError(
            "Failed to execute `add -A` command".to_string(),
        ))
    }
}

/// Git commit from snippet location
pub fn git_commit(location: &SnippetLocation, msg: String) -> Result<(), error::Error> {
    let output = run_git_command_for(location, &["commit", "-am", &format!("\"{}\"", msg)]);

    // Return if success
    if output?.status.success() {
        Ok(())
    } else {
        Err(error::Error::InternalError(
            "Failed to execute `commit -a` command".to_string(),
        ))
    }
}