diff options
author | Dimitar Gyurov | 2023-03-10 22:42:42 +0000 |
---|---|---|
committer | GitHub | 2023-03-10 22:42:42 +0000 |
commit | 1661e4b5e1d8ebfef28f798fcb86ba2656373ba0 (patch) | |
tree | 1c3d36d81a8fe5fafd66b6b61bc5253883ed1516 /helix-vcs/src/git.rs | |
parent | 98415f288ffa043520b0c85bc4464dc44b85f948 (diff) |
Add a version-control statusline element (#5682)
Diffstat (limited to 'helix-vcs/src/git.rs')
-rw-r--r-- | helix-vcs/src/git.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/helix-vcs/src/git.rs b/helix-vcs/src/git.rs index 2a540c8d..1732bdd0 100644 --- a/helix-vcs/src/git.rs +++ b/helix-vcs/src/git.rs @@ -1,4 +1,6 @@ +use arc_swap::ArcSwap; use std::path::Path; +use std::sync::Arc; use gix::objs::tree::EntryMode; use gix::sec::trust::DefaultForLevel; @@ -87,6 +89,21 @@ impl DiffProvider for Git { } Some(data) } + + fn get_current_head_name(&self, file: &Path) -> Option<Arc<ArcSwap<Box<str>>>> { + debug_assert!(!file.exists() || file.is_file()); + debug_assert!(file.is_absolute()); + let repo = Git::open_repo(file.parent()?, None)?.to_thread_local(); + let head_ref = repo.head_ref().ok()?; + let head_commit = repo.head_commit().ok()?; + + let name = match head_ref { + Some(reference) => reference.name().shorten().to_string(), + None => head_commit.id.to_hex_with_len(8).to_string(), + }; + + Some(Arc::new(ArcSwap::from_pointee(name.into_boxed_str()))) + } } /// Finds the object that contains the contents of a file at a specific commit. |