Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm using the following heuristic (to be improved further):</p> <pre><code>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; } </code></pre> <p>This works well with bare and non-bare repositories.</p> <p>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...</p> <p>EDIT: Added ssh support to the isValidRemoteRepository() method. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload