added 'GitUrl'; stopped assuming first remote

This commit is contained in:
Brian Long 2021-01-11 09:50:46 -05:00
parent a076eb5986
commit a58e369ada
3 changed files with 58 additions and 6 deletions

View File

@ -1,11 +1,15 @@
package com.inteligr8.git;
import java.util.List;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
public class CachedGit extends Git {
@ -21,8 +25,12 @@ public class CachedGit extends Git {
super(repo);
}
public String getFirstRemoteUrl() throws GitAPIException {
return this.remoteList().call().iterator().next().getURIs().iterator().next().toString();
public URIish getRemoteUrl(String remoteName) throws GitAPIException {
List<RemoteConfig> configs = this.remoteList().call();
for (RemoteConfig config : configs)
if (config.getName().equals(remoteName))
return config.getURIs().iterator().next();
return null;
}
@Override

View File

@ -0,0 +1,44 @@
package com.inteligr8.git;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jgit.transport.URIish;
public class GitUrl {
private static final Pattern gitUrlPattern = Pattern.compile("(((ssh|http(s)?)://([^@]+@)?([^:/]+)(:[0-9]+)?/)|git@([^:]+):)([\\w\\.@\\:/\\-~]+)(\\.git)");
private final String url;
private final String hostname;
private final String fullyQualifiedRepositoryName;
public GitUrl(URIish url) {
this.url = url.toString();
Matcher matcher = gitUrlPattern.matcher(url.toString());
if (!matcher.find())
throw new IllegalArgumentException("The '" + url + "' URL does not match the expected pattern: " + gitUrlPattern.toString());
this.hostname = matcher.group(6) != null ? matcher.group(6) : matcher.group(8);
this.fullyQualifiedRepositoryName = matcher.group(9);
}
public GitUrl(String url) throws URISyntaxException {
this(new URIish(url));
}
public String getUrl() {
return this.url;
}
public String getHostname() {
return this.hostname;
}
public String getFullyQualifiedRepositoryName() {
return this.fullyQualifiedRepositoryName;
}
}

View File

@ -114,7 +114,7 @@ public class LocalRepositoryCache {
if (git == null) {
String directoryBaseName;
if (this.simultaneousThreadsPerGitRepo == 1)
directoryBaseName = ExtendedGit.getRepositoryFullyQualifiedName(url).replace('/', '_');
directoryBaseName = new GitUrl(url).getFullyQualifiedRepositoryName().replace('/', '_');
else directoryBaseName = UUID.randomUUID().toString();
gitRepoDirectory = new File(this.cacheDirectory, directoryBaseName + ".git");
@ -139,7 +139,7 @@ public class LocalRepositoryCache {
this.logger.debug("Cloning Git repository: " + url);
git = creds != null ? new CredentialedGit(clone, creds) : new ExtendedGit(clone);
if (this.logger.isInfoEnabled())
this.logger.info("Cloned Git Repository: " + ((ExtendedGit)git).getRepositoryFullyQualifiedName());
this.logger.info("Cloned Git Repository: " + new GitUrl(git.getRemoteUrl("origin")).getFullyQualifiedRepositoryName());
} else {
if (this.logger.isDebugEnabled())
this.logger.debug("resetting Git");
@ -163,7 +163,7 @@ public class LocalRepositoryCache {
return git;
} catch (URISyntaxException use) {
semaphore.release();
throw new DeveloperException(this.logger, use);
throw new IllegalArgumentException("The Git Repository URL is not properly formatted", use);
} catch (IOException ie) {
semaphore.release();
throw new TransportException("A I/O issue occurred", ie);
@ -178,7 +178,7 @@ public class LocalRepositoryCache {
this.logger.trace("release('" + git.getRepository().getIdentifier() + "')");
try {
String url = git.getFirstRemoteUrl();
String url = git.getRemoteUrl("origin").toString();
synchronized (this) {
Semaphore semaphore = this.gitUrlSemaphores.get(url);