有没有办法在克隆操作进行中停止它? 我只是想使用克隆来验证存储库? 还有其他方法可以测试远程URL /存储库是否有效吗?
有没有办法在克隆操作进行中停止它? 我只是想使用克隆来验证存储库? 还有其他方法可以测试远程URL /存储库是否有效吗?
您可以使用JGIT调用“git ls-remote”命令。详见此处
示例代码如下:
final LsRemoteCommand lsCmd = new LsRemoteCommand(null);
final List<String> repos = Arrays.asList(
"https://github.com/MuchContact/java.git",
"git@github.com:MuchContact/java.git");
for (String gitRepo: repos){
lsCmd.setRemote(gitRepo);
System.out.println(lsCmd.call().toString());
}
我正在使用以下启发式方法(需要进一步改进):
private final static String INFO_REFS_PATH = "info/refs";
public static boolean isValidRepository(URIish repoUri) {
if (repoUri.isRemote()) {
return isValidRemoteRepository(repoUri);
} else {
return isValidLocalRepository(repoUri);
}
}
private static boolean isValidLocalRepository(URIish repoUri) {
boolean result;
try {
result = new FileRepository(repoUri.getPath()).getObjectDatabase().exists();
} catch (IOException e) {
result = false;
}
return result;
}
private static boolean isValidRemoteRepository(URIish repoUri) {
boolean result;
if (repoUri.getScheme().toLowerCase().startsWith("http") ) {
String path = repoUri.getPath();
String newPath = path.endsWith("/")? path + INFO_REFS_PATH : path + "/" + INFO_REFS_PATH;
URIish checkUri = repoUri.setPath(newPath);
InputStream ins = null;
try {
URLConnection conn = new URL(checkUri.toString()).openConnection();
conn.setReadTimeout(NETWORK_TIMEOUT_MSEC);
ins = conn.getInputStream();
result = true;
} catch (Exception e) {
result = false;
} finally {
try { ins.close(); } catch (Exception e) { /* ignore */ }
}
} else if (repoUri.getScheme().toLowerCase().startsWith("ssh") ) {
RemoteSession ssh = null;
Process exec = null;
try {
ssh = SshSessionFactory.getInstance().getSession(repoUri, null, FS.detect(), 5000);
exec = ssh.exec("cd " + repoUri.getPath() +"; git rev-parse --git-dir", 5000);
Integer exitValue = null;
do {
try {
exitValue = exec.exitValue();
} catch (Exception e) {
try{Thread.sleep(1000);}catch(Exception ee){}
}
} while (exitValue == null);
result = exitValue == 0;
} catch (Exception e) {
result = false;
} finally {
try { exec.destroy(); } catch (Exception e) { /* ignore */ }
try { ssh.disconnect(); } catch (Exception e) { /* ignore */ }
}
} else {
// TODO need to implement tests for other schemas
result = true;
}
return result;
}
true
: new URIish(new File("/tmp").toURI().toURL()).isRemote()
,因此至少应该执行 repoUri.isRemote() && !repoUri.getScheme().equals("file")
。 - Daniel C. Sobral我查看了JGit的源代码,似乎没有现成的方法来检查远程仓库的有效性。
这是CloneCommand
的call
方法:
public Git call() throws JGitInternalException {
try {
URIish u = new URIish(uri);
Repository repository = init(u);
FetchResult result = fetch(repository, u);
if (!noCheckout)
checkout(repository, result);
return new Git(repository);
} catch (IOException ioe) {
throw new JGitInternalException(ioe.getMessage(), ioe);
} catch (InvalidRemoteException e) {
throw new JGitInternalException(e.getMessage(), e);
} catch (URISyntaxException e) {
throw new JGitInternalException(e.getMessage(), e);
}
}
JGitInternalException
e
时,您可以使用e.getCause()
获取实际原因,查找InvalidRemoteException
或者URISyntaxException
,但正如您所指出的,如果它实际上是有效的,您最终会克隆;该库不允许您中断操作。TransportLocal
类具有open(URIsh,Repository,String)
方法,您可以使用该方法检查是否抛出InvalidRemoteException
,但其构造函数不是公共的。不幸的是,需要自己动手解决。您可以从我提到的TransportLocal.open
方法的内容开始。验证远程代码库的API
public boolean validateRepository(String repositoryURL, String username, String password) throws Exception {
boolean result = false;
Repository db = FileRepositoryBuilder.create(new File("/tmp"));
Git git = Git.wrap(db);
final LsRemoteCommand lsCmd = git.lsRemote();
lsCmd.setRemote(repositoryURL);
if (username != null && password != null) {
lsCmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
}
if (null != lsCmd.call()){
result = true;
}
return result;
}
git fsck
命令,但据我所知,这还没有在mvnrepository.com上发布。@Test
public void testHealthyRepo() throws Exception {
RevCommit commit0 = git.commit().message("0").create();
RevCommit commit1 = git.commit().message("1").parent(commit0).create();
git.update("master", commit1);
DfsFsck fsck = new DfsFsck(repo);
FsckError errors = fsck.check(null);
assertEquals(errors.getCorruptObjects().size(), 0);
assertEquals(errors.getMissingObjects().size(), 0);
assertEquals(errors.getCorruptIndices().size(), 0);
}
git fsck
。public static bool IsValidRemoteRepository(URIish repoUri, CredentialsProvider credentialsProvider = null)
{
var repoPath = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
Directory.CreateDirectory(repoPath);
var git = Git.Init().SetBare(true).SetDirectory(repoPath).Call();
var config = git.GetRepository().GetConfig();
config.SetString("remote", "origin", "url", repoUri.ToString());
config.Save();
try
{
var cmd = git.LsRemote();
if (credentialsProvider != null)
{
cmd.SetCredentialsProvider(credentialsProvider);
}
cmd.SetRemote("origin").Call();
}
catch (TransportException e)
{
LastException = e;
return false;
}
return true;
}