Debian Codegraph @

src/gix_impl.rs

raw
1 use gix::bstr::ByteSlice;
2 use std::path;
3
4 #[derive(Debug)]
5 #[cfg_attr(all(feature = "gix", feature = "git2"), allow(dead_code))]
6 pub enum GixError {
7 Discover(Box<gix::discover::Error>),
8 Describe(Box<gix::commit::describe::Error>),
9 HeadCommit(gix::reference::head_commit::Error),
10 FindExisting(gix::reference::find::existing::Error),
11 IsDirty(Box<gix::status::is_dirty::Error>),
12 Peel(gix::head::peel::Error),
13 }
14
15 impl From<gix::discover::Error> for GixError {
16 fn from(e: gix::discover::Error) -> Self {
17 Self::Discover(Box::new(e))
18 }
19 }
20
21 impl From<gix::status::is_dirty::Error> for GixError {
22 fn from(e: gix::status::is_dirty::Error) -> Self {
23 Self::IsDirty(Box::new(e))
24 }
25 }
26
27 impl From<gix::commit::describe::Error> for GixError {
28 fn from(e: gix::commit::describe::Error) -> Self {
29 Self::Describe(Box::new(e))
30 }
31 }
32
33 impl From<gix::reference::find::existing::Error> for GixError {
34 fn from(e: gix::reference::find::existing::Error) -> Self {
35 Self::FindExisting(e)
36 }
37 }
38
39 impl From<gix::head::peel::Error> for GixError {
40 fn from(e: gix::head::peel::Error) -> Self {
41 Self::Peel(e)
42 }
43 }
44
45 impl From<gix::reference::head_commit::Error> for GixError {
46 fn from(e: gix::reference::head_commit::Error) -> Self {
47 Self::HeadCommit(e)
48 }
49 }
50
51 /// Retrieves the git-tag or hash describing the exact version and a boolean
52 /// that indicates if the repository currently has dirty/staged files.
53 ///
54 /// If a valid git-repo can't be discovered at or above the given path,
55 /// or if any operation on the repository fails, `None` is returned.
56 pub fn get_repo_description(
57 manifest_location: &path::Path,
58 ) -> Result<Option<(String, bool)>, GixError> {
59 match gix::discover(manifest_location) {
60 Ok(repo) => {
61 let commit = repo.head_commit()?;
62 let tag = commit.describe().format()?.to_string();
63 let dirty = repo.is_dirty()?;
64 Ok(Some((tag, dirty)))
65 }
66 Err(gix::discover::Error::Discover(_)) => Ok(None),
67 Err(e) => Err(e.into()),
68 }
69 }
70
71 /// Retrieves the branch name and hash of HEAD.
72 ///
73 /// The returned value is a tuple of head's reference-name, long-hash and short-hash. The
74 /// branch name will be `None` if the head is detached, or it's not valid UTF-8.
75 ///
76 /// If a valid git-repo can't be discovered at or above the given path,
77 /// or if any operation on the repository fails, `None` is returned.
78 pub fn get_repo_head(
79 manifest_location: &path::Path,
80 ) -> Result<Option<(Option<String>, String, String)>, GixError> {
81 match gix::discover(manifest_location) {
82 Ok(repo) => {
83 let mut head = repo.head()?;
84 let branch = head
85 .clone()
86 .referent_name()
87 .and_then(|rn| rn.as_bstr().to_str().ok().map(ToOwned::to_owned));
88 if let Some(commit_id) = head.try_peel_to_id()? {
89 let commit_id_short = commit_id.shorten_or_id().to_string();
90 Ok(Some((branch, commit_id.to_string(), commit_id_short)))
91 } else {
92 Ok(None)
93 }
94 }
95 Err(gix::discover::Error::Discover(_)) => Ok(None),
96 Err(e) => Err(e.into()),
97 }
98 }