| 1 |
use crate::{environment, fmt_option_str, write_variable}; |
| 2 |
use std::{fs, io, path}; |
| 3 |
|
| 4 |
pub fn write_git_version( |
| 5 |
manifest_location: &path::Path, |
| 6 |
envmap: &environment::EnvironmentMap, |
| 7 |
mut w: &fs::File, |
| 8 |
) -> io::Result<()> { |
| 9 |
use io::Write; |
| 10 |
|
| 11 |
// CIs will do shallow clones of repositories, causing libgit2 to error |
| 12 |
// out. We try to detect if we are running on a CI and ignore the |
| 13 |
// error. |
| 14 |
let (mut tag, mut dirty) = ( |
| 15 |
envmap.get_override_var("GIT_VERSION"), |
| 16 |
envmap.get_override_var("GIT_DIRTY"), |
| 17 |
); |
| 18 |
if tag.is_none() || dirty.is_none() { |
| 19 |
if let Some((git_tag, git_dirty)) = get_repo_description(manifest_location).ok().flatten() { |
| 20 |
if tag.is_none() { |
| 21 |
tag = Some(git_tag); |
| 22 |
} |
| 23 |
if dirty.is_none() { |
| 24 |
dirty = Some(git_dirty); |
| 25 |
} |
| 26 |
}; |
| 27 |
} |
| 28 |
write_variable!( |
| 29 |
w, |
| 30 |
"GIT_VERSION", |
| 31 |
"Option<&str>", |
| 32 |
fmt_option_str(tag), |
| 33 |
"If the crate was compiled from within a git-repository, \ |
| 34 |
`GIT_VERSION` contains HEAD's tag. The short commit id is used if HEAD is not tagged." |
| 35 |
); |
| 36 |
write_variable!( |
| 37 |
w, |
| 38 |
"GIT_DIRTY", |
| 39 |
"Option<bool>", |
| 40 |
match dirty { |
| 41 |
Some(true) => "Some(true)", |
| 42 |
Some(false) => "Some(false)", |
| 43 |
None => "None", |
| 44 |
}, |
| 45 |
"If the repository had dirty/staged files." |
| 46 |
); |
| 47 |
|
| 48 |
let (mut branch, mut commit, mut commit_short) = ( |
| 49 |
envmap.get_override_var("GIT_HEAD_REF"), |
| 50 |
envmap.get_override_var::<String>("GIT_COMMIT_HASH"), |
| 51 |
envmap.get_override_var("GIT_COMMIT_HASH_SHORT"), |
| 52 |
); |
| 53 |
if branch.is_none() || commit.is_none() || commit_short.is_none() { |
| 54 |
if let Some((git_branch, git_commit, git_commit_short)) = |
| 55 |
get_repo_head(manifest_location).ok().flatten() |
| 56 |
{ |
| 57 |
if branch.is_none() { |
| 58 |
branch = git_branch; |
| 59 |
} |
| 60 |
if commit.is_none() { |
| 61 |
commit = Some(git_commit); |
| 62 |
} |
| 63 |
if commit_short.is_none() { |
| 64 |
commit_short = Some(git_commit_short); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
if let (Some(h), None) = (&commit, &commit_short) { |
| 69 |
commit_short = Some(h.chars().take(8).collect()) |
| 70 |
} |
| 71 |
|
| 72 |
let doc = "If the crate was compiled from within a git-repository, `GIT_HEAD_REF` \ |
| 73 |
contains full name to the reference pointed to by HEAD \ |
| 74 |
(e.g.: `refs/heads/master`). If HEAD is detached or the branch name is not \ |
| 75 |
valid UTF-8 `None` will be stored.\n"; |
| 76 |
write_variable!( |
| 77 |
w, |
| 78 |
"GIT_HEAD_REF", |
| 79 |
"Option<&str>", |
| 80 |
fmt_option_str(branch), |
| 81 |
doc |
| 82 |
); |
| 83 |
|
| 84 |
write_variable!( |
| 85 |
w, |
| 86 |
"GIT_COMMIT_HASH", |
| 87 |
"Option<&str>", |
| 88 |
fmt_option_str(commit), |
| 89 |
"If the crate was compiled from within a git-repository, `GIT_COMMIT_HASH` \ |
| 90 |
contains HEAD's full commit SHA-1 hash." |
| 91 |
); |
| 92 |
|
| 93 |
write_variable!( |
| 94 |
w, |
| 95 |
"GIT_COMMIT_HASH_SHORT", |
| 96 |
"Option<&str>", |
| 97 |
fmt_option_str(commit_short), |
| 98 |
"If the crate was compiled from within a git-repository, `GIT_COMMIT_HASH_SHORT` \ |
| 99 |
contains HEAD's short commit SHA-1 hash." |
| 100 |
); |
| 101 |
|
| 102 |
Ok(()) |
| 103 |
} |
| 104 |
|
| 105 |
/// Retrieves the git-tag or hash describing the exact version and a boolean |
| 106 |
/// that indicates if the repository currently has dirty/staged files. |
| 107 |
/// |
| 108 |
/// If a valid git-repo can't be discovered at or above the given path, |
| 109 |
/// `Ok(None)` is returned instead of an `Err`-value. |
| 110 |
/// |
| 111 |
/// # Errors |
| 112 |
/// Errors from `git2` are returned if the repository does exists at all. |
| 113 |
#[cfg(feature = "git2")] |
| 114 |
pub fn get_repo_description(root: &std::path::Path) -> Result<Option<(String, bool)>, git2::Error> { |
| 115 |
match git2::Repository::discover(root) { |
| 116 |
Ok(repo) => { |
| 117 |
let mut desc_opt = git2::DescribeOptions::new(); |
| 118 |
desc_opt.describe_tags().show_commit_oid_as_fallback(true); |
| 119 |
let tag = repo |
| 120 |
.describe(&desc_opt) |
| 121 |
.and_then(|desc| desc.format(None))?; |
| 122 |
let mut st_opt = git2::StatusOptions::new(); |
| 123 |
st_opt.include_ignored(false); |
| 124 |
st_opt.include_untracked(false); |
| 125 |
let dirty = repo |
| 126 |
.statuses(Some(&mut st_opt))? |
| 127 |
.iter() |
| 128 |
.any(|status| !matches!(status.status(), git2::Status::CURRENT)); |
| 129 |
Ok(Some((tag, dirty))) |
| 130 |
} |
| 131 |
Err(ref e) |
| 132 |
if e.class() == git2::ErrorClass::Repository |
| 133 |
&& e.code() == git2::ErrorCode::NotFound => |
| 134 |
{ |
| 135 |
Ok(None) |
| 136 |
} |
| 137 |
Err(e) => Err(e), |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/// Retrieves the branch name and hash of HEAD. |
| 142 |
/// |
| 143 |
/// The returned value is a tuple of head's reference-name, long-hash and short-hash. The |
| 144 |
/// branch name will be `None` if the head is detached, or it's not valid UTF-8. |
| 145 |
/// |
| 146 |
/// If a valid git-repo can't be discovered at or above the given path, |
| 147 |
/// `Ok(None)` is returned instead of an `Err`-value. |
| 148 |
/// |
| 149 |
/// # Errors |
| 150 |
/// Errors from `git2` are returned if the repository does exists at all. |
| 151 |
#[cfg(feature = "git2")] |
| 152 |
pub fn get_repo_head( |
| 153 |
root: &std::path::Path, |
| 154 |
) -> Result<Option<(Option<String>, String, String)>, git2::Error> { |
| 155 |
match git2::Repository::discover(root) { |
| 156 |
Ok(repo) => { |
| 157 |
// Supposed to be the reference pointed to by HEAD, but it's HEAD |
| 158 |
// itself, if detached |
| 159 |
let head_ref = repo.head()?; |
| 160 |
let branch = { |
| 161 |
// Check whether `head` is really the pointed to reference and |
| 162 |
// not HEAD itself. |
| 163 |
if repo.head_detached()? { |
| 164 |
None |
| 165 |
} else { |
| 166 |
head_ref.name() |
| 167 |
} |
| 168 |
}; |
| 169 |
let head = head_ref.peel_to_commit()?; |
| 170 |
let commit = head.id(); |
| 171 |
let commit_short = head.into_object().short_id()?; |
| 172 |
Ok(Some(( |
| 173 |
branch.map(ToString::to_string), |
| 174 |
format!("{commit}"), |
| 175 |
commit_short.as_str().unwrap_or_default().to_string(), |
| 176 |
))) |
| 177 |
} |
| 178 |
Err(ref e) |
| 179 |
if e.class() == git2::ErrorClass::Repository |
| 180 |
&& e.code() == git2::ErrorCode::NotFound => |
| 181 |
{ |
| 182 |
Ok(None) |
| 183 |
} |
| 184 |
Err(e) => Err(e), |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
#[cfg(test)] |
| 189 |
mod tests { |
| 190 |
#[test] |
| 191 |
fn parse_git_repo() { |
| 192 |
use std::fs; |
| 193 |
use std::path; |
| 194 |
|
| 195 |
let repo_root = tempfile::tempdir().unwrap(); |
| 196 |
assert_eq!(super::get_repo_description(repo_root.as_ref()), Ok(None)); |
| 197 |
|
| 198 |
let repo = git2::Repository::init_opts( |
| 199 |
&repo_root, |
| 200 |
git2::RepositoryInitOptions::new() |
| 201 |
.external_template(false) |
| 202 |
.mkdir(false) |
| 203 |
.no_reinit(true) |
| 204 |
.mkpath(false), |
| 205 |
) |
| 206 |
.unwrap(); |
| 207 |
|
| 208 |
let cruft_file = repo_root.path().join("cruftfile"); |
| 209 |
std::fs::write(&cruft_file, "Who? Me?").unwrap(); |
| 210 |
|
| 211 |
let project_root = repo_root.path().join("project_root"); |
| 212 |
fs::create_dir(&project_root).unwrap(); |
| 213 |
|
| 214 |
let sig = git2::Signature::now("foo", "bar").unwrap(); |
| 215 |
let mut idx = repo.index().unwrap(); |
| 216 |
idx.add_path(path::Path::new("cruftfile")).unwrap(); |
| 217 |
idx.write().unwrap(); |
| 218 |
let commit_oid = repo |
| 219 |
.commit( |
| 220 |
Some("HEAD"), |
| 221 |
&sig, |
| 222 |
&sig, |
| 223 |
"Testing testing 1 2 3", |
| 224 |
&repo.find_tree(idx.write_tree().unwrap()).unwrap(), |
| 225 |
&[], |
| 226 |
) |
| 227 |
.unwrap(); |
| 228 |
|
| 229 |
let binding = repo |
| 230 |
.find_commit(commit_oid) |
| 231 |
.unwrap() |
| 232 |
.into_object() |
| 233 |
.short_id() |
| 234 |
.unwrap(); |
| 235 |
|
| 236 |
let commit_oid_short = binding.as_str().unwrap(); |
| 237 |
|
| 238 |
let commit_hash = commit_oid.to_string(); |
| 239 |
let commit_hash_short = commit_oid_short.to_string(); |
| 240 |
|
| 241 |
assert!(commit_hash.starts_with(&commit_hash_short)); |
| 242 |
|
| 243 |
// The commit, the commit-id is something and the repo is not dirty |
| 244 |
let (tag, dirty) = super::get_repo_description(&project_root).unwrap().unwrap(); |
| 245 |
assert!(!tag.is_empty()); |
| 246 |
assert!(!dirty); |
| 247 |
|
| 248 |
// Tag the commit, it should be retrieved |
| 249 |
repo.tag( |
| 250 |
"foobar", |
| 251 |
&repo |
| 252 |
.find_object(commit_oid, Some(git2::ObjectType::Commit)) |
| 253 |
.unwrap(), |
| 254 |
&sig, |
| 255 |
"Tagged foobar", |
| 256 |
false, |
| 257 |
) |
| 258 |
.unwrap(); |
| 259 |
|
| 260 |
let (tag, dirty) = super::get_repo_description(&project_root).unwrap().unwrap(); |
| 261 |
assert_eq!(tag, "foobar"); |
| 262 |
assert!(!dirty); |
| 263 |
|
| 264 |
// Make some dirt |
| 265 |
std::fs::write(cruft_file, "now dirty").unwrap(); |
| 266 |
let (tag, dirty) = super::get_repo_description(&project_root).unwrap().unwrap(); |
| 267 |
assert_eq!(tag, "foobar"); |
| 268 |
assert!(dirty); |
| 269 |
|
| 270 |
let branch_short_name = "baz"; |
| 271 |
let branch_name = "refs/heads/baz"; |
| 272 |
let commit = repo.find_commit(commit_oid).unwrap(); |
| 273 |
repo.branch(branch_short_name, &commit, true).unwrap(); |
| 274 |
repo.set_head(branch_name).unwrap(); |
| 275 |
|
| 276 |
assert_eq!( |
| 277 |
super::get_repo_head(&project_root), |
| 278 |
Ok(Some(( |
| 279 |
Some(branch_name.to_owned()), |
| 280 |
commit_hash, |
| 281 |
commit_hash_short |
| 282 |
))) |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn detached_head_repo() { |
| 288 |
let repo_root = tempfile::tempdir().unwrap(); |
| 289 |
let repo = git2::Repository::init_opts( |
| 290 |
&repo_root, |
| 291 |
git2::RepositoryInitOptions::new() |
| 292 |
.external_template(false) |
| 293 |
.mkdir(false) |
| 294 |
.no_reinit(true) |
| 295 |
.mkpath(false), |
| 296 |
) |
| 297 |
.unwrap(); |
| 298 |
let sig = git2::Signature::now("foo", "bar").unwrap(); |
| 299 |
let commit_oid = repo |
| 300 |
.commit( |
| 301 |
Some("HEAD"), |
| 302 |
&sig, |
| 303 |
&sig, |
| 304 |
"Testing", |
| 305 |
&repo |
| 306 |
.find_tree(repo.index().unwrap().write_tree().unwrap()) |
| 307 |
.unwrap(), |
| 308 |
&[], |
| 309 |
) |
| 310 |
.unwrap(); |
| 311 |
|
| 312 |
let binding = repo |
| 313 |
.find_commit(commit_oid) |
| 314 |
.unwrap() |
| 315 |
.into_object() |
| 316 |
.short_id() |
| 317 |
.unwrap(); |
| 318 |
|
| 319 |
let commit_oid_short = binding.as_str().unwrap(); |
| 320 |
|
| 321 |
let commit_hash = commit_oid.to_string(); |
| 322 |
let commit_hash_short = commit_oid_short.to_string(); |
| 323 |
|
| 324 |
assert!(commit_hash.starts_with(&commit_hash_short)); |
| 325 |
|
| 326 |
repo.set_head_detached(commit_oid).unwrap(); |
| 327 |
assert_eq!( |
| 328 |
super::get_repo_head(repo_root.as_ref()), |
| 329 |
Ok(Some((None, commit_hash, commit_hash_short))) |
| 330 |
); |
| 331 |
} |
| 332 |
} |