Skip to content

Commit

Permalink
feat: Do not add explicit default ports to origin (#4396)
Browse files Browse the repository at this point in the history
* feat: Do not add explicit default ports to origin

* format
  • Loading branch information
pstreef authored Aug 8, 2024
1 parent 9689f1a commit daa151b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 8 additions & 1 deletion rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private String concat() {
if (host != null) {
builder.append(host);
}
if (port > 0) {
if (!isDefaultPort()) {
builder.append(':').append(port);
}
if (!path.isEmpty()) {
Expand All @@ -173,6 +173,13 @@ private String concat() {
return builder.toString();
}

private boolean isDefaultPort() {
return port < 1 ||
("https".equals(scheme) && port == 443) ||
("http".equals(scheme) && port == 80) ||
("ssh".equals(scheme) && port == 22);
}

private String repositoryPath(@Nullable String origin) {
if (origin == null) {
origin = "";
Expand Down
12 changes: 9 additions & 3 deletions rewrite-core/src/test/java/org/openrewrite/GitRemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class GitRemoteTest {
https://gitlab.com/group/repo.git, gitlab.com, group/repo, group, repo
https://gitlab.com/group/subgroup/subergroup/subestgroup/repo.git, gitlab.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
[email protected]:group/subgroup/subergroup/subestgroup/repo.git, gitlab.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://[email protected]:22/group/subgroup/subergroup/subestgroup/repo.git, gitlab.com:22, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://[email protected]:22/group/subgroup/subergroup/subestgroup/repo.git, gitlab.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://bitbucket.org/PRJ/repo, bitbucket.org, PRJ/repo, PRJ, repo
[email protected]:PRJ/repo.git, bitbucket.org, PRJ/repo, PRJ, repo
Expand Down Expand Up @@ -72,18 +72,24 @@ void parseUnknownRemote(String cloneUrl, String expectedOrigin, String expectedP
@ParameterizedTest
@CsvSource(textBlock = """
https://scm.company.com/stash/scm/org/repo.git, scm.company.com/stash, Bitbucket, org/repo, org, repo
http://scm.company.com:80/stash/scm/org/repo.git, scm.company.com/stash, Bitbucket, org/repo, org, repo
http://scm.company.com:8080/stash/scm/org/repo.git, scm.company.com:8080/stash, Bitbucket, org/repo, org, repo
https://scm.company.com:443/stash/scm/org/repo.git, scm.company.com/stash, Bitbucket, org/repo, org, repo
https://scm.company.com:1234/stash/scm/org/repo.git, scm.company.com:1234/stash, Bitbucket, org/repo, org, repo
[email protected]:stash/org/repo.git, scm.company.com/stash, Bitbucket, org/repo, org, repo
ssh://scm.company.com/stash/org/repo, scm.company.com/stash, Bitbucket, org/repo, org, repo
ssh://scm.company.com:22/stash/org/repo, scm.company.com/stash, Bitbucket, org/repo, org, repo
ssh://scm.company.com:7999/stash/org/repo, scm.company.com:7999/stash, Bitbucket, org/repo, org, repo
https://scm.company.com/very/long/context/path/org/repo.git, scm.company.com/very/long/context/path, Bitbucket, org/repo, org, repo
https://scm.company.com:1234/very/long/context/path/org/repo.git, scm.company.com:1234/very/long/context/path, Bitbucket, org/repo, org, repo
[email protected]:very/long/context/path/org/repo.git, scm.company.com/very/long/context/path, Bitbucket, org/repo, org, repo
https://scm.company.com/group/subgroup/subergroup/subestgroup/repo, scm.company.com, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://scm.company.com:1234/group/subgroup/subergroup/subestgroup/repo, scm.company.com:1234, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
[email protected]:group/subgroup/subergroup/subestgroup/repo.git, scm.company.com, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://scm.company.com:22/group/subgroup/subergroup/subestgroup/repo.git, scm.company.com:22, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://scm.company.com:22/group/subgroup/subergroup/subestgroup/repo.git, scm.company.com, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
ssh://scm.company.com:222/group/subgroup/subergroup/subestgroup/repo.git, scm.company.com:222, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
https://scm.company.com/very/long/context/path/group/subgroup/subergroup/subestgroup/repo, scm.company.com/very/long/context/path, GitLab, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, repo
""")
Expand Down

0 comments on commit daa151b

Please sign in to comment.