| 1 |
/// Retrieves the git-tag or hash describing the exact version and a boolean |
| 2 |
/// that indicates if the repository currently has dirty/staged files. |
| 3 |
/// |
| 4 |
/// If a valid git-repo can't be discovered at or above the given path, |
| 5 |
/// `Ok(None)` is returned instead of an `Err`-value. |
| 6 |
/// |
| 7 |
/// # Errors |
| 8 |
/// Errors from `git2` are returned if the repository does exists at all. |
| 9 |
#[cfg(feature = "git2")] |
| 10 |
pub fn get_repo_description(root: &std::path::Path) -> Result<Option<(String, bool)>, git2::Error> { |
| 11 |
match git2::Repository::discover(root) { |
| 12 |
Ok(repo) => { |
| 13 |
let mut desc_opt = git2::DescribeOptions::new(); |
| 14 |
desc_opt.describe_tags().show_commit_oid_as_fallback(true); |
| 15 |
let tag = repo |
| 16 |
.describe(&desc_opt) |
| 17 |
.and_then(|desc| desc.format(None))?; |
| 18 |
let mut st_opt = git2::StatusOptions::new(); |
| 19 |
st_opt.include_ignored(false); |
| 20 |
st_opt.include_untracked(false); |
| 21 |
let dirty = repo |
| 22 |
.statuses(Some(&mut st_opt))? |
| 23 |
.iter() |
| 24 |
.any(|status| !matches!(status.status(), git2::Status::CURRENT)); |
| 25 |
Ok(Some((tag, dirty))) |
| 26 |
} |
| 27 |
Err(ref e) |
| 28 |
if e.class() == git2::ErrorClass::Repository |
| 29 |
&& e.code() == git2::ErrorCode::NotFound => |
| 30 |
{ |
| 31 |
Ok(None) |
| 32 |
} |
| 33 |
Err(e) => Err(e), |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/// Retrieves the branch name and hash of HEAD. |
| 38 |
/// |
| 39 |
/// The returned value is a tuple of head's reference-name, long-hash and short-hash. The |
| 40 |
/// branch name will be `None` if the head is detached, or it's not valid UTF-8. |
| 41 |
/// |
| 42 |
/// If a valid git-repo can't be discovered at or above the given path, |
| 43 |
/// `Ok(None)` is returned instead of an `Err`-value. |
| 44 |
/// |
| 45 |
/// # Errors |
| 46 |
/// Errors from `git2` are returned if the repository does exists at all. |
| 47 |
#[cfg(feature = "git2")] |
| 48 |
pub fn get_repo_head( |
| 49 |
root: &std::path::Path, |
| 50 |
) -> Result<Option<(Option<String>, String, String)>, git2::Error> { |
| 51 |
match git2::Repository::discover(root) { |
| 52 |
Ok(repo) => { |
| 53 |
// Supposed to be the reference pointed to by HEAD, but it's HEAD |
| 54 |
// itself, if detached |
| 55 |
let head_ref = repo.head()?; |
| 56 |
let branch = { |
| 57 |
// Check whether `head` is really the pointed to reference and |
| 58 |
// not HEAD itself. |
| 59 |
if repo.head_detached()? { |
| 60 |
None |
| 61 |
} else { |
| 62 |
head_ref.name().ok() |
| 63 |
} |
| 64 |
}; |
| 65 |
let head = head_ref.peel_to_commit()?; |
| 66 |
let commit = head.id(); |
| 67 |
let commit_short = head.into_object().short_id()?; |
| 68 |
Ok(Some(( |
| 69 |
branch.map(ToString::to_string), |
| 70 |
format!("{commit}"), |
| 71 |
commit_short.as_str().unwrap_or_default().to_string(), |
| 72 |
))) |
| 73 |
} |
| 74 |
Err(ref e) |
| 75 |
if e.class() == git2::ErrorClass::Repository |
| 76 |
&& e.code() == git2::ErrorCode::NotFound => |
| 77 |
{ |
| 78 |
Ok(None) |
| 79 |
} |
| 80 |
Err(e) => Err(e), |
| 81 |
} |
| 82 |
} |