From 6b875e775d56d815718c7d105680e8b5d59c4a83 Mon Sep 17 00:00:00 2001 From: sciwhiz12 Date: Sun, 21 Apr 2024 00:35:17 +0800 Subject: [PATCH] Use URIish for Git remote instead of URL The URL class checks whether there is a stream factory for the given protocol of the URL; in other words, it checks if it recognizes the URL protocol. Unfortunately, the `ssh` protocol, commonly used in Git remotes, is not recognized. This means trying to construct an SSH URL fails with an exception. To fix this, we use URIish, which implements no such checks. URIish belongs to JGit, and is used instead of URI as jopt-simple is able to handle the URIish constructor, thus avoiding manually converting a URI to a URIish for use by JGit. --- src/main/java/net/neoforged/snowblower/Generator.java | 9 ++++----- src/main/java/net/neoforged/snowblower/Main.java | 5 +++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/neoforged/snowblower/Generator.java b/src/main/java/net/neoforged/snowblower/Generator.java index a2365f5..b0af79f 100644 --- a/src/main/java/net/neoforged/snowblower/Generator.java +++ b/src/main/java/net/neoforged/snowblower/Generator.java @@ -97,7 +97,7 @@ public Generator(Path output, Path cache, Path extraMappings, DependencyHashCach this.excludes = excludes; } - public Generator setup(String branchName, @Nullable URL remoteUrl, boolean checkout, boolean push, Config cfg, BranchSpec cliBranch, + public Generator setup(String branchName, @Nullable URIish remoteUrl, boolean checkout, boolean push, Config cfg, BranchSpec cliBranch, boolean fresh, boolean freshIfRequired, boolean partialCache) throws IOException, GitAPIException { try { this.git = Git.open(this.output.toFile()); @@ -183,11 +183,10 @@ private boolean deleteBranch(String branchName, String currentBranch) throws Git return deleteTemp; } - private void setupRemote(@Nullable URL remoteUrl) throws GitAPIException { + private void setupRemote(@Nullable URIish remoteUrl) throws GitAPIException { if (remoteUrl == null) return; - URIish remoteFakeUri = new URIish(remoteUrl); String foundRemote = null; Set remoteNames = new HashSet<>(); @@ -200,7 +199,7 @@ private void setupRemote(@Nullable URL remoteUrl) throws GitAPIException { continue; for (URIish fakeUri : remoteConfig.getURIs()) { - if (fakeUri.equals(remoteFakeUri)) { + if (fakeUri.equals(remoteUrl)) { foundRemote = currRemoteName; break; } @@ -215,7 +214,7 @@ private void setupRemote(@Nullable URL remoteUrl) throws GitAPIException { foundRemote = "origin" + i; } - this.git.remoteAdd().setName(foundRemote).setUri(remoteFakeUri).call(); + this.git.remoteAdd().setName(foundRemote).setUri(remoteUrl).call(); this.removeRemote = true; } diff --git a/src/main/java/net/neoforged/snowblower/Main.java b/src/main/java/net/neoforged/snowblower/Main.java index b888260..4fa4445 100644 --- a/src/main/java/net/neoforged/snowblower/Main.java +++ b/src/main/java/net/neoforged/snowblower/Main.java @@ -14,6 +14,7 @@ import net.neoforged.srgutils.MinecraftVersion; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.transport.CredentialsProvider; +import org.eclipse.jgit.transport.URIish; import java.io.File; import java.net.MalformedURLException; @@ -39,7 +40,7 @@ public static void main(String[] args) throws Exception { var startOverO = parser.accepts("start-over", "Whether to start over by deleting the target branch"); var startOverIfRequiredO = parser.accepts("start-over-if-required", "Whether to start over by deleting the target branch, only if it is necessary to do so").availableUnless("start-over"); var configO = parser.accepts("cfg", "Config file for SnowBlower").withRequiredArg().ofType(URI.class); - var remoteO = parser.accepts("remote", "The URL of the Git remote to use").withRequiredArg().ofType(URL.class); + var remoteO = parser.accepts("remote", "The URL of the Git remote to use").withRequiredArg().ofType(URIish.class); var checkoutO = parser.accepts("checkout", "Whether to checkout the remote branch (if it exists) before generating").availableIf("remote"); var pushO = parser.accepts("push", "Whether to push the branch to the remote once finished").availableIf("remote"); var committerO = parser.accepts("committer", "The name and email of the user to use as the committer, separated by a space. If omitted, defaults to snowforge").withRequiredArg(); @@ -82,7 +83,7 @@ public static void main(String[] args) throws Exception { boolean startOver = options.has(startOverO); boolean startOverIfRequired = !startOver && options.has(startOverIfRequiredO); boolean partialCachce = options.has(partialCache); - URL remote = options.has(remoteO) ? options.valueOf(remoteO) : null; + URIish remote = options.has(remoteO) ? options.valueOf(remoteO) : null; boolean checkout = options.has(checkoutO); boolean push = options.has(pushO); List includes = options.valuesOf(includeO);