Skip to content

Commit

Permalink
Use URIish for Git remote instead of URL
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sciwhiz12 committed Apr 20, 2024
1 parent d63fd68 commit 6b875e7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/main/java/net/neoforged/snowblower/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<String> remoteNames = new HashSet<>();
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/neoforged/snowblower/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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<String> includes = options.valuesOf(includeO);
Expand Down

0 comments on commit 6b875e7

Please sign in to comment.