Is there a method to kill a clone operation mid-stream? I will just use the cloning to validate the repository? Is there any other way to test if the remote url/repository is valid?
To check whether or not you have git installed, simply open a terminal window and type "git --version". If you've already followed the video Installing Git for Windows on a Windows Machine you'll see a message like "git version 1.9. 5. msysgit.
JGit is a lightweight, pure Java library implementation of the Git version control system – including repository access routines, network protocols, and core version control algorithms. JGit is a relatively full-featured implementation of Git written in Java and is widely used in the Java community.
If you want to use Git from within a Java program, there is a fully featured Git library called JGit. JGit is a relatively full-featured implementation of Git written natively in Java, and is widely used in the Java community.
you can invoke 'git ls-remote ' using JGIT. see at here
Sample code as below:
final LsRemoteCommand lsCmd = new LsRemoteCommand(null);
final List<String> repos = Arrays.asList(
"https://github.com/MuchContact/java.git",
"[email protected]:MuchContact/java.git");
for (String gitRepo: repos){
lsCmd.setRemote(gitRepo);
System.out.println(lsCmd.call().toString());
}
I'm using the following heuristic (to be improved further):
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;
}
This works well with bare and non-bare repositories.
Please note that there seems to be a problem with the URIish.isRemote() method. When you create an URIish from a file-URL, the host is not null but an empty string! URIish.isRemote() however returns true, if the host field is not null...
EDIT: Added ssh support to the isValidRemoteRepository() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With