| 1 |
use crate::{environment, fmt_option_str, write_variable}; |
| 2 |
use std::{fs, io, path}; |
| 3 |
|
| 4 |
/// Length of the short commit hash (8 characters by default) |
| 5 |
const SHORT_HASH_LENGTH: usize = 8; |
| 6 |
|
| 7 |
/// Fully resolved repository information, that may incorporate overrides. |
| 8 |
#[derive(Debug, Default, PartialEq)] |
| 9 |
pub(crate) struct RepoInfo { |
| 10 |
pub branch: Option<String>, |
| 11 |
pub tag: Option<String>, |
| 12 |
pub dirty: Option<bool>, |
| 13 |
pub commit_id: Option<String>, |
| 14 |
pub commit_id_short: Option<String>, |
| 15 |
} |
| 16 |
|
| 17 |
impl RepoInfo { |
| 18 |
pub(crate) fn from_overrides(envmap: &environment::EnvironmentMap) -> Self { |
| 19 |
RepoInfo { |
| 20 |
branch: envmap.get_override_var("GIT_HEAD_REF"), |
| 21 |
tag: envmap.get_override_var("GIT_VERSION"), |
| 22 |
dirty: envmap.get_override_var("GIT_DIRTY"), |
| 23 |
commit_id: envmap.get_override_var("GIT_COMMIT_HASH"), |
| 24 |
commit_id_short: envmap.get_override_var("GIT_COMMIT_HASH_SHORT"), |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
pub(crate) fn write_git_version( |
| 30 |
manifest_location: &path::Path, |
| 31 |
envmap: &environment::EnvironmentMap, |
| 32 |
w: &fs::File, |
| 33 |
) -> io::Result<()> { |
| 34 |
#[cfg(feature = "git2")] |
| 35 |
use crate::git_impl::{get_repo_description, get_repo_head}; |
| 36 |
// `git2` takes precedence |
| 37 |
#[cfg(all(feature = "gix", not(feature = "git2")))] |
| 38 |
use crate::gix_impl::{get_repo_description, get_repo_head}; |
| 39 |
|
| 40 |
let RepoInfo { |
| 41 |
mut branch, |
| 42 |
mut tag, |
| 43 |
mut dirty, |
| 44 |
mut commit_id, |
| 45 |
mut commit_id_short, |
| 46 |
} = RepoInfo::from_overrides(envmap); |
| 47 |
|
| 48 |
if branch.is_none() || commit_id.is_none() || commit_id_short.is_none() { |
| 49 |
if let Ok(Some((git_branch, git_commit_id, git_commit_short_id))) = |
| 50 |
get_repo_head(manifest_location) |
| 51 |
{ |
| 52 |
branch = branch.or(git_branch); |
| 53 |
commit_id = commit_id.or(Some(git_commit_id)); |
| 54 |
commit_id_short = commit_id_short.or(Some(git_commit_short_id)) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// This is an expensive call, avoid it if it's all overridden. |
| 59 |
// TODO(performance): could be split into dirty + describe, and re-use the opened Repository. |
| 60 |
if tag.is_none() || dirty.is_none() { |
| 61 |
if let Ok(Some((git_tag, git_dirty))) = get_repo_description(manifest_location) { |
| 62 |
tag = tag.or(Some(git_tag)); |
| 63 |
dirty = dirty.or(Some(git_dirty)); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
write_variables( |
| 68 |
w, |
| 69 |
RepoInfo { |
| 70 |
branch, |
| 71 |
tag, |
| 72 |
dirty, |
| 73 |
commit_id, |
| 74 |
commit_id_short, |
| 75 |
}, |
| 76 |
) |
| 77 |
} |
| 78 |
|
| 79 |
pub(crate) fn write_variables( |
| 80 |
mut w: &fs::File, |
| 81 |
RepoInfo { |
| 82 |
branch, |
| 83 |
tag, |
| 84 |
dirty, |
| 85 |
commit_id, |
| 86 |
mut commit_id_short, |
| 87 |
}: RepoInfo, |
| 88 |
) -> io::Result<()> { |
| 89 |
use io::Write; |
| 90 |
|
| 91 |
// If we have a commit hash but no short hash, generate the short hash from the full hash |
| 92 |
if let (Some(h), None) = (&commit_id, &commit_id_short) { |
| 93 |
commit_id_short = Some(h.chars().take(SHORT_HASH_LENGTH).collect()) |
| 94 |
} |
| 95 |
|
| 96 |
write_variable!( |
| 97 |
w, |
| 98 |
"GIT_VERSION", |
| 99 |
"Option<&str>", |
| 100 |
fmt_option_str(tag), |
| 101 |
"If the crate was compiled from within a git-repository, \ |
| 102 |
`GIT_VERSION` contains HEAD's tag. The short commit id is used if HEAD is not tagged." |
| 103 |
); |
| 104 |
write_variable!( |
| 105 |
w, |
| 106 |
"GIT_DIRTY", |
| 107 |
"Option<bool>", |
| 108 |
match dirty { |
| 109 |
Some(true) => "Some(true)", |
| 110 |
Some(false) => "Some(false)", |
| 111 |
None => "None", |
| 112 |
}, |
| 113 |
"If the repository had dirty/staged files." |
| 114 |
); |
| 115 |
|
| 116 |
let doc = "If the crate was compiled from within a git-repository, `GIT_HEAD_REF` \ |
| 117 |
contains full name to the reference pointed to by HEAD \ |
| 118 |
(e.g.: `refs/heads/master`). If HEAD is detached or the branch name is not \ |
| 119 |
valid UTF-8 `None` will be stored.\n"; |
| 120 |
write_variable!( |
| 121 |
w, |
| 122 |
"GIT_HEAD_REF", |
| 123 |
"Option<&str>", |
| 124 |
fmt_option_str(branch), |
| 125 |
doc |
| 126 |
); |
| 127 |
|
| 128 |
write_variable!( |
| 129 |
w, |
| 130 |
"GIT_COMMIT_HASH", |
| 131 |
"Option<&str>", |
| 132 |
fmt_option_str(commit_id), |
| 133 |
"If the crate was compiled from within a git-repository, `GIT_COMMIT_HASH` \ |
| 134 |
contains HEAD's full commit SHA-1 hash." |
| 135 |
); |
| 136 |
|
| 137 |
write_variable!( |
| 138 |
w, |
| 139 |
"GIT_COMMIT_HASH_SHORT", |
| 140 |
"Option<&str>", |
| 141 |
fmt_option_str(commit_id_short), |
| 142 |
"If the crate was compiled from within a git-repository, `GIT_COMMIT_HASH_SHORT` \ |
| 143 |
contains HEAD's short commit SHA-1 hash." |
| 144 |
); |
| 145 |
|
| 146 |
Ok(()) |
| 147 |
} |
| 148 |
|
| 149 |
// Tests need git2 for initial setup right now. |
| 150 |
#[cfg(feature = "git2")] |
| 151 |
#[cfg(test)] |
| 152 |
mod tests { |
| 153 |
#[cfg(not(feature = "gix"))] |
| 154 |
use crate::git_impl::{get_repo_description, get_repo_head}; |
| 155 |
// When testing, `gix` must take precedence, or it's not tested. |
| 156 |
// Using `gix-testtools` would allow to setup fixtures without such dependencies. |
| 157 |
#[cfg(feature = "gix")] |
| 158 |
use crate::gix_impl::{get_repo_description, get_repo_head}; |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn parse_git_repo() { |
| 162 |
use std::fs; |
| 163 |
use std::path; |
| 164 |
|
| 165 |
let repo_root = tempfile::tempdir().unwrap(); |
| 166 |
assert_eq!(get_repo_description(repo_root.as_ref()).unwrap(), None); |
| 167 |
|
| 168 |
let repo = git2::Repository::init_opts( |
| 169 |
&repo_root, |
| 170 |
git2::RepositoryInitOptions::new() |
| 171 |
.external_template(false) |
| 172 |
.mkdir(false) |
| 173 |
.no_reinit(true) |
| 174 |
.mkpath(false), |
| 175 |
) |
| 176 |
.unwrap(); |
| 177 |
|
| 178 |
let cruft_file = repo_root.path().join("cruftfile"); |
| 179 |
std::fs::write(&cruft_file, "Who? Me?").unwrap(); |
| 180 |
|
| 181 |
let project_root = repo_root.path().join("project_root"); |
| 182 |
fs::create_dir(&project_root).unwrap(); |
| 183 |
|
| 184 |
let sig = git2::Signature::now("foo", "bar").unwrap(); |
| 185 |
let mut idx = repo.index().unwrap(); |
| 186 |
idx.add_path(path::Path::new("cruftfile")).unwrap(); |
| 187 |
idx.write().unwrap(); |
| 188 |
let commit_oid = repo |
| 189 |
.commit( |
| 190 |
Some("HEAD"), |
| 191 |
&sig, |
| 192 |
&sig, |
| 193 |
"Testing testing 1 2 3", |
| 194 |
&repo.find_tree(idx.write_tree().unwrap()).unwrap(), |
| 195 |
&[], |
| 196 |
) |
| 197 |
.unwrap(); |
| 198 |
|
| 199 |
let binding = repo |
| 200 |
.find_commit(commit_oid) |
| 201 |
.unwrap() |
| 202 |
.into_object() |
| 203 |
.short_id() |
| 204 |
.unwrap(); |
| 205 |
|
| 206 |
let commit_oid_short = binding.as_str().unwrap(); |
| 207 |
|
| 208 |
let commit_hash = commit_oid.to_string(); |
| 209 |
let commit_hash_short = commit_oid_short.to_string(); |
| 210 |
|
| 211 |
assert!(commit_hash.starts_with(&commit_hash_short)); |
| 212 |
|
| 213 |
// The commit, the commit-id is something and the repo is not dirty |
| 214 |
let (tag, dirty) = get_repo_description(&project_root).unwrap().unwrap(); |
| 215 |
assert!(!tag.is_empty()); |
| 216 |
assert!(!dirty); |
| 217 |
|
| 218 |
// Tag the commit, it should be retrieved |
| 219 |
repo.tag( |
| 220 |
"foobar", |
| 221 |
&repo |
| 222 |
.find_object(commit_oid, Some(git2::ObjectType::Commit)) |
| 223 |
.unwrap(), |
| 224 |
&sig, |
| 225 |
"Tagged foobar", |
| 226 |
false, |
| 227 |
) |
| 228 |
.unwrap(); |
| 229 |
|
| 230 |
let (tag, dirty) = get_repo_description(&project_root).unwrap().unwrap(); |
| 231 |
assert_eq!(tag, "foobar"); |
| 232 |
assert!(!dirty); |
| 233 |
|
| 234 |
// Make some dirt |
| 235 |
std::fs::write(cruft_file, "now dirty").unwrap(); |
| 236 |
let (tag, dirty) = get_repo_description(&project_root).unwrap().unwrap(); |
| 237 |
assert_eq!(tag, "foobar"); |
| 238 |
assert!(dirty); |
| 239 |
|
| 240 |
let branch_short_name = "baz"; |
| 241 |
let branch_name = "refs/heads/baz"; |
| 242 |
let commit = repo.find_commit(commit_oid).unwrap(); |
| 243 |
repo.branch(branch_short_name, &commit, true).unwrap(); |
| 244 |
repo.set_head(branch_name).unwrap(); |
| 245 |
|
| 246 |
assert_eq!( |
| 247 |
get_repo_head(&project_root).unwrap(), |
| 248 |
Some((Some(branch_name.to_owned()), commit_hash, commit_hash_short)) |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn detached_head_repo() { |
| 254 |
let repo_root = tempfile::tempdir().unwrap(); |
| 255 |
let repo = git2::Repository::init_opts( |
| 256 |
&repo_root, |
| 257 |
git2::RepositoryInitOptions::new() |
| 258 |
.external_template(false) |
| 259 |
.mkdir(false) |
| 260 |
.no_reinit(true) |
| 261 |
.mkpath(false), |
| 262 |
) |
| 263 |
.unwrap(); |
| 264 |
let sig = git2::Signature::now("foo", "bar").unwrap(); |
| 265 |
let commit_oid = repo |
| 266 |
.commit( |
| 267 |
Some("HEAD"), |
| 268 |
&sig, |
| 269 |
&sig, |
| 270 |
"Testing", |
| 271 |
&repo |
| 272 |
.find_tree(repo.index().unwrap().write_tree().unwrap()) |
| 273 |
.unwrap(), |
| 274 |
&[], |
| 275 |
) |
| 276 |
.unwrap(); |
| 277 |
|
| 278 |
let binding = repo |
| 279 |
.find_commit(commit_oid) |
| 280 |
.unwrap() |
| 281 |
.into_object() |
| 282 |
.short_id() |
| 283 |
.unwrap(); |
| 284 |
|
| 285 |
let commit_oid_short = binding.as_str().unwrap(); |
| 286 |
|
| 287 |
let commit_hash = commit_oid.to_string(); |
| 288 |
let commit_hash_short = commit_oid_short.to_string(); |
| 289 |
|
| 290 |
assert!(commit_hash.starts_with(&commit_hash_short)); |
| 291 |
|
| 292 |
repo.set_head_detached(commit_oid).unwrap(); |
| 293 |
assert_eq!( |
| 294 |
get_repo_head(repo_root.as_ref()).unwrap(), |
| 295 |
Some((None, commit_hash, commit_hash_short)) |
| 296 |
); |
| 297 |
} |
| 298 |
} |