Skip to content

Commit

Permalink
Ignore case on GitRemote equals (#4468)
Browse files Browse the repository at this point in the history
* Ignore case on GitRemote equals

* Add tests
  • Loading branch information
pstreef authored Sep 4, 2024
1 parent bc70b59 commit 153700c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
28 changes: 28 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 153700c

Please sign in to comment.