diff --git a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java index 1da3335207d..b56b0997ed2 100644 --- a/rewrite-core/src/main/java/org/openrewrite/GitRemote.java +++ b/rewrite-core/src/main/java/org/openrewrite/GitRemote.java @@ -16,6 +16,7 @@ package org.openrewrite; import lombok.Value; +import org.apache.commons.lang3.StringUtils; import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.openrewrite.jgit.transport.URIish; @@ -38,6 +39,33 @@ public class GitRemote { String repositoryName; + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + GitRemote gitRemote = (GitRemote) o; + return service == gitRemote.service && + StringUtils.equalsIgnoreCase(url, gitRemote.url) && + StringUtils.equalsIgnoreCase(origin, gitRemote.origin) && + StringUtils.equalsIgnoreCase(path, gitRemote.path) && + StringUtils.equalsIgnoreCase(organization, gitRemote.organization) && + StringUtils.equalsIgnoreCase(repositoryName, gitRemote.repositoryName); + } + + @Override + public int hashCode() { + return Objects.hash(service, + url == null ? null : url.toLowerCase(Locale.ENGLISH), + origin == null ? null : origin.toLowerCase(Locale.ENGLISH), + path == null ? null : path.toLowerCase(Locale.ENGLISH), + organization == null ? null : organization.toLowerCase(Locale.ENGLISH), + repositoryName == null ? null : repositoryName.toLowerCase(Locale.ENGLISH)); + } + public enum Service { GitHub, GitLab, diff --git a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java index c6d86ea15af..d765cd8aa1d 100644 --- a/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java @@ -274,4 +274,16 @@ void parseOriginCaseInsensitive(String cloneUrl, String expectedOrigin, String e void findServiceForName(String name, GitRemote.Service service){ assertThat(GitRemote.Service.forName(name)).isEqualTo(service); } + + @Test + void equalsIgnoresCase() { + assertThat(new GitRemote(GitRemote.Service.GitHub, "https://github.com/org/repo", "github.com", "org/repo", "org", "repo")) + .isEqualTo(new GitRemote(GitRemote.Service.GitHub, "https://GITHUB.COM/ORG/REPO", "GITHUB.COM", "ORG/REPO", "ORG", "REPO")); + } + + @Test + void hashCodeIgnoresCase() { + assertThat(new GitRemote(GitRemote.Service.GitHub, "https://github.com/org/repo", "github.com", "org/repo", "org", "repo")) + .hasSameHashCodeAs(new GitRemote(GitRemote.Service.GitHub, "https://GITHUB.COM/ORG/REPO", "GITHUB.COM", "ORG/REPO", "ORG", "REPO")); + } }