diff options
author | Kyohei Uto <im@kyoheiu.dev> | 2023-09-09 10:45:18 +0900 |
---|---|---|
committer | Kyohei Uto <im@kyoheiu.dev> | 2023-09-09 10:45:18 +0900 |
commit | b65772178aafc29673abd0d3faa4a8313c5868f1 (patch) | |
tree | 8b25891a9680d3d06f36f3156edd284c0f771646 | |
parent | 621bd26fba3ee78b735396eecb6d71409f6afb1f (diff) |
Get branch name by git2
-rw-r--r-- | src/state.rs | 10 |
1 files changed, 3 insertions, 7 deletions
diff --git a/src/state.rs b/src/state.rs index 2a8ddea..13e5431 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1131,18 +1131,14 @@ impl State { } //If .git directory exists, get the branch information and print it. - let git = self.current_dir.join(".git"); - if git.exists() { - let head = git.join("HEAD"); - if let Ok(head) = std::fs::read(head) { - let branch: Vec<u8> = head.into_iter().skip(16).collect(); - if let Ok(branch) = std::str::from_utf8(&branch) { + if let Ok(repo) = git2::Repository::open(&self.current_dir) { + if let Ok(head) = repo.head() { + if let Some(branch) = head.shorthand() { if branch.len() + 4 <= header_space { print!(" on ",); set_color(&TermColor::ForeGround(&Colorname::LightMagenta)); print!("{}", branch.trim().bold()); reset_color(); - } else { } } } |