added 'GitUrl'; stopped assuming first remote
This commit is contained in:
parent
a076eb5986
commit
a58e369ada
@ -1,11 +1,15 @@
|
|||||||
package com.inteligr8.git;
|
package com.inteligr8.git;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.jgit.api.CloneCommand;
|
import org.eclipse.jgit.api.CloneCommand;
|
||||||
import org.eclipse.jgit.api.Git;
|
import org.eclipse.jgit.api.Git;
|
||||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||||
import org.eclipse.jgit.api.errors.InvalidRemoteException;
|
import org.eclipse.jgit.api.errors.InvalidRemoteException;
|
||||||
import org.eclipse.jgit.api.errors.TransportException;
|
import org.eclipse.jgit.api.errors.TransportException;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
|
import org.eclipse.jgit.transport.RemoteConfig;
|
||||||
|
import org.eclipse.jgit.transport.URIish;
|
||||||
|
|
||||||
public class CachedGit extends Git {
|
public class CachedGit extends Git {
|
||||||
|
|
||||||
@ -21,8 +25,12 @@ public class CachedGit extends Git {
|
|||||||
super(repo);
|
super(repo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFirstRemoteUrl() throws GitAPIException {
|
public URIish getRemoteUrl(String remoteName) throws GitAPIException {
|
||||||
return this.remoteList().call().iterator().next().getURIs().iterator().next().toString();
|
List<RemoteConfig> configs = this.remoteList().call();
|
||||||
|
for (RemoteConfig config : configs)
|
||||||
|
if (config.getName().equals(remoteName))
|
||||||
|
return config.getURIs().iterator().next();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
44
src/main/java/com/inteligr8/git/GitUrl.java
Normal file
44
src/main/java/com/inteligr8/git/GitUrl.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -114,7 +114,7 @@ public class LocalRepositoryCache {
|
|||||||
if (git == null) {
|
if (git == null) {
|
||||||
String directoryBaseName;
|
String directoryBaseName;
|
||||||
if (this.simultaneousThreadsPerGitRepo == 1)
|
if (this.simultaneousThreadsPerGitRepo == 1)
|
||||||
directoryBaseName = ExtendedGit.getRepositoryFullyQualifiedName(url).replace('/', '_');
|
directoryBaseName = new GitUrl(url).getFullyQualifiedRepositoryName().replace('/', '_');
|
||||||
else directoryBaseName = UUID.randomUUID().toString();
|
else directoryBaseName = UUID.randomUUID().toString();
|
||||||
|
|
||||||
gitRepoDirectory = new File(this.cacheDirectory, directoryBaseName + ".git");
|
gitRepoDirectory = new File(this.cacheDirectory, directoryBaseName + ".git");
|
||||||
@ -139,7 +139,7 @@ public class LocalRepositoryCache {
|
|||||||
this.logger.debug("Cloning Git repository: " + url);
|
this.logger.debug("Cloning Git repository: " + url);
|
||||||
git = creds != null ? new CredentialedGit(clone, creds) : new ExtendedGit(clone);
|
git = creds != null ? new CredentialedGit(clone, creds) : new ExtendedGit(clone);
|
||||||
if (this.logger.isInfoEnabled())
|
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 {
|
} else {
|
||||||
if (this.logger.isDebugEnabled())
|
if (this.logger.isDebugEnabled())
|
||||||
this.logger.debug("resetting Git");
|
this.logger.debug("resetting Git");
|
||||||
@ -163,7 +163,7 @@ public class LocalRepositoryCache {
|
|||||||
return git;
|
return git;
|
||||||
} catch (URISyntaxException use) {
|
} catch (URISyntaxException use) {
|
||||||
semaphore.release();
|
semaphore.release();
|
||||||
throw new DeveloperException(this.logger, use);
|
throw new IllegalArgumentException("The Git Repository URL is not properly formatted", use);
|
||||||
} catch (IOException ie) {
|
} catch (IOException ie) {
|
||||||
semaphore.release();
|
semaphore.release();
|
||||||
throw new TransportException("A I/O issue occurred", ie);
|
throw new TransportException("A I/O issue occurred", ie);
|
||||||
@ -178,7 +178,7 @@ public class LocalRepositoryCache {
|
|||||||
this.logger.trace("release('" + git.getRepository().getIdentifier() + "')");
|
this.logger.trace("release('" + git.getRepository().getIdentifier() + "')");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String url = git.getFirstRemoteUrl();
|
String url = git.getRemoteUrl("origin").toString();
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
Semaphore semaphore = this.gitUrlSemaphores.get(url);
|
Semaphore semaphore = this.gitUrlSemaphores.get(url);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user