From 153700c11fda7a51b0597e5f14876c6c98fcb2d9 Mon Sep 17 00:00:00 2001
From: Peter Streef <peter@moderne.io>
Date: Wed, 4 Sep 2024 21:08:46 +0200
Subject: [PATCH] Ignore case on GitRemote equals (#4468)

* Ignore case on GitRemote equals

* Add tests
---
 .../main/java/org/openrewrite/GitRemote.java  | 28 +++++++++++++++++++
 .../java/org/openrewrite/GitRemoteTest.java   | 12 ++++++++
 2 files changed, 40 insertions(+)

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"));
+    }
 }