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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! # gitinfo
//!
//! Loads git information.
//!

#[cfg(test)]
#[path = "./gitinfo_test.rs"]
mod gitinfo_test;

use crate::types::{GitInfo, Head};
use std::collections::HashMap;
use std::io::Error;
use std::process::{Command, ExitStatus};

/// Returns the exit code (-1 if no exit code found)
fn get_exit_code(exit_status: Result<ExitStatus, Error>) -> i32 {
    match exit_status {
        Ok(code) => {
            if !code.success() {
                match code.code() {
                    Some(value) => value,
                    None => -1,
                }
            } else {
                0
            }
        }
        _ => -1,
    }
}

fn get_command_output(command: &mut Command) -> Option<String> {
    let result = command.output();

    match result {
        Ok(output) => {
            let exit_code = get_exit_code(Ok(output.status));

            if exit_code == 0 {
                let stdout = String::from_utf8_lossy(&output.stdout);
                let line = stdout.trim();

                Some(line.to_string())
            } else {
                None
            }
        }
        Err(_) => None,
    }
}

fn load_state(info: &mut GitInfo) {
    let mut command = Command::new("git");
    command.arg("status").arg("--short");

    let result = get_command_output(&mut command);

    if let Some(output) = result {
        let dirty = output.len() > 0;

        info.dirty = Some(dirty);
    }
}

fn load_config(info: &mut GitInfo) {
    let mut command = Command::new("git");
    command.arg("config").arg("--list");

    let result = get_command_output(&mut command);

    if let Some(output) = result {
        let lines: Vec<&str> = output.split('\n').collect();

        let mut config = HashMap::new();

        for mut line in lines {
            line = line.trim();

            let mut line_split = line.splitn(2, '=');

            if let Some(key) = line_split.next() {
                if let Some(value) = line_split.next() {
                    config.insert(key.to_string(), value.to_string());
                }
            }
        }

        info.config = Some(config);
    }
}

fn load_from_config(info: &mut GitInfo) {
    match info.config {
        Some(ref config) => {
            if let Some(value) = config.get("user.name") {
                info.user_name = Some(value.to_string());
            }
            if let Some(value) = config.get("user.email") {
                info.user_email = Some(value.to_string());
            }
        }
        None => (),
    };
}

fn load_branches(info: &mut GitInfo) {
    let mut command = Command::new("git");
    command.arg("branch").arg("--list").arg("--no-color");

    let result = get_command_output(&mut command);

    if let Some(output) = result {
        let lines: Vec<&str> = output.split('\n').collect();

        let mut branches = vec![];

        for mut line in lines {
            line = line.trim();

            let mut line_split = line.splitn(2, ' ');

            let name = match line_split.next() {
                Some(marker_or_name) => {
                    if marker_or_name == "*" {
                        match line_split.next() {
                            Some(value) => {
                                info.current_branch = Some(value.to_string());
                                value
                            }
                            None => "",
                        }
                    } else {
                        marker_or_name
                    }
                }
                None => "",
            };

            if name.len() > 0 {
                branches.push(name.to_string());
            }
        }

        info.branches = Some(branches);
    }
}

fn load_head(head: &mut Head) {
    let mut command = Command::new("git");
    command.arg("rev-parse").arg("HEAD");

    let mut result = get_command_output(&mut command);

    if let Some(output) = result {
        if !output.is_empty() {
            head.last_commit_hash = Some(output);
        }
    }

    command = Command::new("git");
    command.arg("rev-parse").arg("--short").arg("HEAD");

    result = get_command_output(&mut command);

    if let Some(output) = result {
        if !output.is_empty() {
            head.last_commit_hash_short = Some(output);
        }
    }
}

pub(crate) fn get() -> GitInfo {
    let mut info = GitInfo::new();

    load_state(&mut info);
    load_config(&mut info);
    load_from_config(&mut info);
    load_branches(&mut info);
    load_head(&mut info.head);

    info
}