Skip to content

Commit

Permalink
Updated let else as the feature is unstable with
Browse files Browse the repository at this point in the history
stable rust
  • Loading branch information
CraZySacX committed Nov 29, 2022
1 parent 1fa8357 commit 8f28441
Showing 1 changed file with 125 additions and 123 deletions.
248 changes: 125 additions & 123 deletions src/feature/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,151 +240,153 @@ pub(crate) fn configure_git<T>(
where
T: AsRef<Path>,
{
let Some(repo_path) = repo_path_opt else { return Ok(()) };
if let Some(repo_path) = repo_path_opt {
let git_config = instructions.git();

let git_config = instructions.git();
let add_entries = || {
let repo = Repository::discover(repo_path)?;
let ref_head = repo.find_reference("HEAD")?;
let repo_path = repo.path().to_path_buf();

let add_entries = || {
let repo = Repository::discover(repo_path)?;
let ref_head = repo.find_reference("HEAD")?;
let repo_path = repo.path().to_path_buf();

if *git_config.branch() {
add_branch_name(&repo, config)?;
}
if *git_config.branch() {
add_branch_name(&repo, config)?;
}

if *git_config.commit_timestamp()
|| *git_config.sha()
|| *git_config.commit_author()
|| *git_config.commit_message()
{
let commit = ref_head.peel_to_commit()?;

if *git_config.commit_timestamp() {
let commit_time = OffsetDateTime::from_unix_timestamp(commit.time().seconds())?;

match git_config.commit_timestamp_timezone() {
crate::TimeZone::Utc => {
add_config_entries(
config,
git_config,
&commit_time.to_offset(UtcOffset::UTC),
)?;
}
#[cfg(feature = "local_offset")]
crate::TimeZone::Local => {
add_config_entries(
config,
git_config,
&commit_time.to_offset(UtcOffset::current_local_offset()?),
)?;
if *git_config.commit_timestamp()
|| *git_config.sha()
|| *git_config.commit_author()
|| *git_config.commit_message()
{
let commit = ref_head.peel_to_commit()?;

if *git_config.commit_timestamp() {
let commit_time = OffsetDateTime::from_unix_timestamp(commit.time().seconds())?;

match git_config.commit_timestamp_timezone() {
crate::TimeZone::Utc => {
add_config_entries(
config,
git_config,
&commit_time.to_offset(UtcOffset::UTC),
)?;
}
#[cfg(feature = "local_offset")]
crate::TimeZone::Local => {
add_config_entries(
config,
git_config,
&commit_time.to_offset(UtcOffset::current_local_offset()?),
)?;
}
}
}
}

if *git_config.sha() {
match git_config.sha_kind() {
crate::ShaKind::Normal => {
add_entry(
config.cfg_map_mut(),
VergenKey::Sha,
Some(commit.id().to_string()),
);
}
crate::ShaKind::Short => {
let obj = repo.revparse_single("HEAD")?;
add_entry(
config.cfg_map_mut(),
VergenKey::ShortSha,
obj.short_id()?.as_str().map(str::to_string),
);
}
crate::ShaKind::Both => {
add_entry(
config.cfg_map_mut(),
VergenKey::Sha,
Some(commit.id().to_string()),
);

let obj = repo.revparse_single("HEAD")?;
add_entry(
config.cfg_map_mut(),
VergenKey::ShortSha,
obj.short_id()?.as_str().map(str::to_string),
);
if *git_config.sha() {
match git_config.sha_kind() {
crate::ShaKind::Normal => {
add_entry(
config.cfg_map_mut(),
VergenKey::Sha,
Some(commit.id().to_string()),
);
}
crate::ShaKind::Short => {
let obj = repo.revparse_single("HEAD")?;
add_entry(
config.cfg_map_mut(),
VergenKey::ShortSha,
obj.short_id()?.as_str().map(str::to_string),
);
}
crate::ShaKind::Both => {
add_entry(
config.cfg_map_mut(),
VergenKey::Sha,
Some(commit.id().to_string()),
);

let obj = repo.revparse_single("HEAD")?;
add_entry(
config.cfg_map_mut(),
VergenKey::ShortSha,
obj.short_id()?.as_str().map(str::to_string),
);
}
}
}
}

if *git_config.commit_author() {
add_entry(
config.cfg_map_mut(),
VergenKey::CommitAuthorName,
commit.author().name().map(str::to_string),
);
add_entry(
config.cfg_map_mut(),
VergenKey::CommitAuthorEmail,
commit.author().email().map(str::to_string),
);
}
if *git_config.commit_author() {
add_entry(
config.cfg_map_mut(),
VergenKey::CommitAuthorName,
commit.author().name().map(str::to_string),
);
add_entry(
config.cfg_map_mut(),
VergenKey::CommitAuthorEmail,
commit.author().email().map(str::to_string),
);
}

if *git_config.commit_message() {
add_entry(
config.cfg_map_mut(),
VergenKey::CommitMessage,
commit.message().map(str::to_string),
);
if *git_config.commit_message() {
add_entry(
config.cfg_map_mut(),
VergenKey::CommitMessage,
commit.message().map(str::to_string),
);
}
}
}

if *git_config.semver() {
let dirty = git_config.semver_dirty();
match *git_config.semver_kind() {
crate::SemverKind::Normal => {
add_semver(&repo, &DescribeOptions::new(), false, dirty, config);
}
crate::SemverKind::Lightweight => {
let mut opts = DescribeOptions::new();
let _ = opts.describe_tags();
if *git_config.semver() {
let dirty = git_config.semver_dirty();
match *git_config.semver_kind() {
crate::SemverKind::Normal => {
add_semver(&repo, &DescribeOptions::new(), false, dirty, config);
}
crate::SemverKind::Lightweight => {
let mut opts = DescribeOptions::new();
let _ = opts.describe_tags();

add_semver(&repo, &opts, true, dirty, config);
add_semver(&repo, &opts, true, dirty, config);
}
}
}
}

if *git_config.commit_count() {
add_commit_count(&repo, config);
}
if *git_config.commit_count() {
add_commit_count(&repo, config);
}

if let Ok(resolved) = ref_head.resolve() {
if let Some(name) = resolved.name() {
let path = repo_path.join(name);
// Check whether the path exists in the filesystem before emitting it
if path.exists() {
*config.ref_path_mut() = Some(path);
if let Ok(resolved) = ref_head.resolve() {
if let Some(name) = resolved.name() {
let path = repo_path.join(name);
// Check whether the path exists in the filesystem before emitting it
if path.exists() {
*config.ref_path_mut() = Some(path);
}
}
}
}
*config.head_path_mut() = Some(repo_path.join("HEAD"));
Ok(())
};
*config.head_path_mut() = Some(repo_path.join("HEAD"));
Ok(())
};

if git_config.has_enabled() {
if git_config.skip_if_error {
// hide errors, but emit a warning
let result = add_entries();
if result.is_err() {
let warning = format!(
"An Error occurred during processing of {}. \
VERGEN_{}_* may be incomplete.",
"Git", "GIT"
);
config.warnings_mut().push(warning);
if git_config.has_enabled() {
if git_config.skip_if_error {
// hide errors, but emit a warning
let result = add_entries();
if result.is_err() {
let warning = format!(
"An Error occurred during processing of {}. \
VERGEN_{}_* may be incomplete.",
"Git", "GIT"
);
config.warnings_mut().push(warning);
}
Ok(())
} else {
add_entries()
}
Ok(())
} else {
add_entries()
Ok(())
}
} else {
Ok(())
Expand Down

0 comments on commit 8f28441

Please sign in to comment.