Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: stop looking at git history if commit tagged #813

Merged
merged 6 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions crates/git_cmd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,41 @@ impl Repo {
self.git(&["log", "-1", "--pretty=format:%B"])
}

pub fn current_commit_hash(&self) -> anyhow::Result<String> {
self.git(&["log", "-1", "--pretty=format:%H"])
}

/// Create a git tag
pub fn tag(&self, name: &str) -> anyhow::Result<String> {
self.git(&["tag", name])
}

/// Get the commit hash of the given tag
pub fn get_tag_commit(&self, tag: &str) -> Option<String> {
self.git(&["rev-list", "-n", "1", tag]).ok()
}

/// Check if a commit comes before another one.
///
/// ## Example
///
/// For this git log:
/// ```txt
/// commit d6ec399b80d44bf9c4391e4a9ead8482faa9bffd
/// commit e880d8786cb16aa9a3f258e7503932445d708df7
/// ```
///
/// `git.is_ancestor("e880d8786cb16aa9a3f258e7503932445d708df7", "d6ec399b80d44bf9c4391e4a9ead8482faa9bffd")` returns true.
pub fn is_ancestor(&self, maybe_ancestor_commit: &str, descendant_commit: &str) -> bool {
self.git(&[
"merge-base",
"--is-ancestor",
maybe_ancestor_commit,
descendant_commit,
])
.is_ok()
}

/// Url of the remote when the [`Repo`] was created.
pub fn original_remote_url(&self) -> anyhow::Result<String> {
let param = format!("remote.{}.url", self.original_remote);
Expand Down
29 changes: 26 additions & 3 deletions crates/release_plz_core/src/next_ver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{
path::{Path, PathBuf},
};
use tempfile::{tempdir, TempDir};
use tracing::{debug, info, instrument};
use tracing::{debug, info, instrument, warn};

#[derive(Debug, Clone)]
pub struct UpdateRequest {
Expand Down Expand Up @@ -704,17 +704,25 @@ fn get_diff(
.map(|p| get_package_path(p, repository, &project.root))
.collect();
let ignored_dirs = ignored_dirs?;

let tag_commit = {
let git_tag = project.git_tag(&package.name, &package.version.to_string());
repository.get_tag_commit(&git_tag)
};
loop {
let current_commit_message = repository.current_commit_message()?;
let current_commit_hash = repository.current_commit_hash()?;
if let Some(registry_package) = registry_package {
debug!("package {} found in cargo registry", registry_package.name);
let registry_package_path = registry_package.package_path()?;
let are_packages_equal =
are_packages_equal(&package_path, registry_package_path, ignored_dirs.clone())
.context("cannot compare packages")?;
if are_packages_equal {
if are_packages_equal
|| is_commit_too_old(repository, tag_commit.as_deref(), &current_commit_hash)
{
debug!(
"next version calculated starting from commit after `{current_commit_message}`"
"next version calculated starting from commits after `{current_commit_hash}`"
);
if diff.commits.is_empty() {
let are_dependencies_updated = are_toml_dependencies_updated(
Expand Down Expand Up @@ -759,6 +767,21 @@ fn get_diff(
Ok(diff)
}

/// Check if commit belongs to a previous version of the package.
fn is_commit_too_old(
repository: &Repo,
tag_commit: Option<&str>,
current_commit_hash: &str,
) -> bool {
if let Some(tag_commit) = tag_commit.as_ref() {
if repository.is_ancestor(current_commit_hash, tag_commit) {
debug!("stopping looking at git history because the current commit ({}) is an ancestor of the commit ({}) tagged with the previous version.", current_commit_hash, tag_commit);
return true;
}
}
false
}

/// Check if release-plz should check the semver compatibility of the package.
/// - `run_semver_check` is true if the user wants to run the semver check.
fn should_check_semver(package: &Package, run_semver_check: bool) -> bool {
Expand Down