diff options
author | qkzk <qu3nt1n@gmail.com> | 2022-11-07 14:02:47 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2022-11-07 14:02:47 +0100 |
commit | 5d18139b22f20f37eb4a3dc7f7c3fd87e00882de (patch) | |
tree | 9fb17d110543bfda710bc342128ccf94e13dc9d0 /src/git.rs | |
parent | 32e39d4fddee2959ffc02185833fdbc4aef5e4aa (diff) |
git integration in first line of normal mode
Diffstat (limited to 'src/git.rs')
-rw-r--r-- | src/git.rs | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/git.rs b/src/git.rs new file mode 100644 index 0000000..8dbbe74 --- /dev/null +++ b/src/git.rs @@ -0,0 +1,143 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +use std::error::Error; +use std::fmt::Write as _; +use std::path::PathBuf; +use std::process; + +struct GitStatus { + branch: Option<String>, + ahead: i64, + behind: i64, + + staged: i64, + modified: i64, + deleted: i64, + unmerged: i64, + untracked: i64, +} + +fn parse_porcelain2(data: String) -> Option<GitStatus> { + let mut status = GitStatus { + branch: None, + ahead: 0, + behind: 0, + + staged: 0, + modified: 0, + deleted: 0, + unmerged: 0, + untracked: 0, + }; + // Simple parser for the porcelain v2 format + for entry in data.split('\0') { + let mut entry = entry.split(' '); + match entry.next() { + // Header lines + Some("#") => match entry.next()? { + "branch.head" => { + let head = entry.next()?; + if head != "(detached)" { + status.branch = Some(String::from(head)); + } + } + "branch.ab" => { + let a = entry.next()?; + let b = entry.next()?; + status.ahead = a.parse::<i64>().ok()?.abs(); + status.behind = b.parse::<i64>().ok()?.abs(); + } + _ => {} + }, + // File entries + Some("1") | Some("2") => { + let mut xy = entry.next()?.chars(); + let x = xy.next()?; + let y = xy.next()?; + if x != '.' { + status.staged += 1; + } + match y { + 'M' => status.modified += 1, + 'D' => status.deleted += 1, + _ => {} + } + } + Some("u") => status.unmerged += 1, + Some("?") => status.untracked += 1, + _ => {} + } + } + Some(status) +} + +pub fn git(path: PathBuf) -> Result<String, Box<dyn Error>> { + if std::env::set_current_dir(&path).is_err() { + // The path may not exist. It should never happen. + return Ok("".to_owned()); + } + let output = process::Command::new("git") + .args(&[ + "status", + "--porcelain=v2", + "-z", + "--branch", + "--untracked-files=all", + ]) + .stdin(process::Stdio::null()) + .stderr(process::Stdio::null()) + .output()?; + if !output.status.success() { + // We're most likely not in a Git repo + return Ok("".to_owned()); + } + let status = String::from_utf8(output.stdout) + .ok() + .ok_or("Invalid UTF-8 while decoding Git output")?; + + let status = parse_porcelain2(status).ok_or("Error while parsing Git output")?; + + let mut git_string = String::new(); + + git_string.push('('); + + if let Some(branch) = status.branch { + git_string.push_str(&branch); + } else { + // Detached head + git_string.push_str(":HEAD"); + } + + // Divergence with remote branch + if status.ahead != 0 { + write!(git_string, "↑{}", status.ahead)?; + } + if status.behind != 0 { + write!(git_string, "↓{}", status.ahead)?; + } + + if status.untracked + status.modified + status.deleted + status.unmerged + status.staged > 0 { + git_string.push('|'); + } + if status.untracked != 0 { + write!(git_string, "+{}", status.untracked)?; + } + if status.modified != 0 { + write!(git_string, "~{}", status.modified)?; + } + if status.deleted != 0 { + write!(git_string, "-{}", status.deleted)?; + } + if status.unmerged != 0 { + write!(git_string, "x{}", status.unmerged)?; + } + if status.staged != 0 { + write!(git_string, "•{}", status.staged)?; + } + + git_string.push(')'); + + Ok(git_string) +} |